1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-05 19:50:06 +00:00

chore(*): use server type from utils types

This commit is contained in:
Krishna Acondy
2021-01-24 16:02:34 +00:00
parent 301edab8ad
commit 3a9cd46e6e
5 changed files with 43 additions and 57 deletions

View File

@@ -1,10 +1,4 @@
import { import { convertToCSV, isRelativePath, isUri, isUrl } from './utils'
parseAndSubmitAuthorizeForm,
convertToCSV,
isRelativePath,
isUri,
isUrl
} from './utils'
import * as NodeFormData from 'form-data' import * as NodeFormData from 'form-data'
import { import {
Job, Job,
@@ -24,6 +18,7 @@ import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
import { Logger, LogLevel } from '@sasjs/utils/logger' import { Logger, LogLevel } from '@sasjs/utils/logger'
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
import { parseAndSubmitAuthorizeForm } from './auth'
/** /**
* A client for interfacing with the SAS Viya REST API. * A client for interfacing with the SAS Viya REST API.

View File

@@ -13,7 +13,6 @@ import {
SASjsConfig, SASjsConfig,
SASjsRequest, SASjsRequest,
SASjsWaitingRequest, SASjsWaitingRequest,
ServerType,
CsrfToken, CsrfToken,
UploadFile, UploadFile,
EditContextInput, EditContextInput,
@@ -24,13 +23,14 @@ import { SASViyaApiClient } from './SASViyaApiClient'
import { SAS9ApiClient } from './SAS9ApiClient' import { SAS9ApiClient } from './SAS9ApiClient'
import { FileUploader } from './FileUploader' import { FileUploader } from './FileUploader'
import { isLogInRequired, AuthManager } from './auth' import { isLogInRequired, AuthManager } from './auth'
import { ServerType } from '@sasjs/utils/types'
const defaultConfig: SASjsConfig = { const defaultConfig: SASjsConfig = {
serverUrl: '', serverUrl: '',
pathSAS9: '/SASStoredProcess/do', pathSAS9: '/SASStoredProcess/do',
pathSASViya: '/SASJobExecution', pathSASViya: '/SASJobExecution',
appLoc: '/Public/seedapp', appLoc: '/Public/seedapp',
serverType: ServerType.SASViya, serverType: ServerType.SasViya,
debug: false, debug: false,
contextName: 'SAS Job Execution compute context', contextName: 'SAS Job Execution compute context',
useComputeApi: false useComputeApi: false
@@ -71,7 +71,7 @@ export default class SASjs {
serverName: string, serverName: string,
repositoryName: string repositoryName: string
) { ) {
this.isMethodSupported('executeScriptSAS9', ServerType.SAS9) this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
return await this.sas9ApiClient?.executeScript( return await this.sas9ApiClient?.executeScript(
linesOfCode, linesOfCode,
@@ -85,7 +85,7 @@ export default class SASjs {
* @param accessToken - an access token for an authorized user. * @param accessToken - an access token for an authorized user.
*/ */
public async getComputeContexts(accessToken: string) { public async getComputeContexts(accessToken: string) {
this.isMethodSupported('getComputeContexts', ServerType.SASViya) this.isMethodSupported('getComputeContexts', ServerType.SasViya)
return await this.sasViyaApiClient!.getComputeContexts(accessToken) return await this.sasViyaApiClient!.getComputeContexts(accessToken)
} }
@@ -95,7 +95,7 @@ export default class SASjs {
* @param accessToken - an access token for an authorized user. * @param accessToken - an access token for an authorized user.
*/ */
public async getLauncherContexts(accessToken: string) { public async getLauncherContexts(accessToken: string) {
this.isMethodSupported('getLauncherContexts', ServerType.SASViya) this.isMethodSupported('getLauncherContexts', ServerType.SasViya)
return await this.sasViyaApiClient!.getLauncherContexts(accessToken) return await this.sasViyaApiClient!.getLauncherContexts(accessToken)
} }
@@ -104,7 +104,7 @@ export default class SASjs {
* Gets default(system) launcher contexts. * Gets default(system) launcher contexts.
*/ */
public getDefaultComputeContexts() { public getDefaultComputeContexts() {
this.isMethodSupported('getDefaultComputeContexts', ServerType.SASViya) this.isMethodSupported('getDefaultComputeContexts', ServerType.SasViya)
return this.sasViyaApiClient!.getDefaultComputeContexts() return this.sasViyaApiClient!.getDefaultComputeContexts()
} }
@@ -114,7 +114,7 @@ export default class SASjs {
* @param accessToken - an access token for an authorized user. * @param accessToken - an access token for an authorized user.
*/ */
public async getExecutableContexts(accessToken: string) { public async getExecutableContexts(accessToken: string) {
this.isMethodSupported('getExecutableContexts', ServerType.SASViya) this.isMethodSupported('getExecutableContexts', ServerType.SasViya)
return await this.sasViyaApiClient!.getExecutableContexts(accessToken) return await this.sasViyaApiClient!.getExecutableContexts(accessToken)
} }
@@ -136,7 +136,7 @@ export default class SASjs {
accessToken: string, accessToken: string,
authorizedUsers?: string[] authorizedUsers?: string[]
) { ) {
this.isMethodSupported('createComputeContext', ServerType.SASViya) this.isMethodSupported('createComputeContext', ServerType.SasViya)
return await this.sasViyaApiClient!.createComputeContext( return await this.sasViyaApiClient!.createComputeContext(
contextName, contextName,
@@ -161,7 +161,7 @@ export default class SASjs {
launchType: string, launchType: string,
accessToken: string accessToken: string
) { ) {
this.isMethodSupported('createLauncherContext', ServerType.SASViya) this.isMethodSupported('createLauncherContext', ServerType.SasViya)
return await this.sasViyaApiClient!.createLauncherContext( return await this.sasViyaApiClient!.createLauncherContext(
contextName, contextName,
@@ -182,7 +182,7 @@ export default class SASjs {
editedContext: EditContextInput, editedContext: EditContextInput,
accessToken?: string accessToken?: string
) { ) {
this.isMethodSupported('editComputeContext', ServerType.SASViya) this.isMethodSupported('editComputeContext', ServerType.SasViya)
return await this.sasViyaApiClient!.editComputeContext( return await this.sasViyaApiClient!.editComputeContext(
contextName, contextName,
@@ -197,7 +197,7 @@ export default class SASjs {
* @param accessToken - an access token for an authorized user. * @param accessToken - an access token for an authorized user.
*/ */
public async deleteComputeContext(contextName: string, accessToken?: string) { public async deleteComputeContext(contextName: string, accessToken?: string) {
this.isMethodSupported('deleteComputeContext', ServerType.SASViya) this.isMethodSupported('deleteComputeContext', ServerType.SasViya)
return await this.sasViyaApiClient!.deleteComputeContext( return await this.sasViyaApiClient!.deleteComputeContext(
contextName, contextName,
@@ -215,7 +215,7 @@ export default class SASjs {
contextName: string, contextName: string,
accessToken?: string accessToken?: string
) { ) {
this.isMethodSupported('getComputeContextByName', ServerType.SASViya) this.isMethodSupported('getComputeContextByName', ServerType.SasViya)
return await this.sasViyaApiClient!.getComputeContextByName( return await this.sasViyaApiClient!.getComputeContextByName(
contextName, contextName,
@@ -229,7 +229,7 @@ export default class SASjs {
* @param accessToken - an access token for an authorized user. * @param accessToken - an access token for an authorized user.
*/ */
public async getComputeContextById(contextId: string, accessToken?: string) { public async getComputeContextById(contextId: string, accessToken?: string) {
this.isMethodSupported('getComputeContextById', ServerType.SASViya) this.isMethodSupported('getComputeContextById', ServerType.SasViya)
return await this.sasViyaApiClient!.getComputeContextById( return await this.sasViyaApiClient!.getComputeContextById(
contextId, contextId,
@@ -238,7 +238,7 @@ export default class SASjs {
} }
public async createSession(contextName: string, accessToken: string) { public async createSession(contextName: string, accessToken: string) {
this.isMethodSupported('createSession', ServerType.SASViya) this.isMethodSupported('createSession', ServerType.SasViya)
return await this.sasViyaApiClient!.createSession(contextName, accessToken) return await this.sasViyaApiClient!.createSession(contextName, accessToken)
} }
@@ -258,7 +258,7 @@ export default class SASjs {
accessToken?: string, accessToken?: string,
debug?: boolean debug?: boolean
) { ) {
this.isMethodSupported('executeScriptSASViya', ServerType.SASViya) this.isMethodSupported('executeScriptSASViya', ServerType.SasViya)
return await this.sasViyaApiClient!.executeScript( return await this.sasViyaApiClient!.executeScript(
fileName, fileName,
@@ -309,7 +309,7 @@ export default class SASjs {
* @param accessToken - an access token for authorizing the request. * @param accessToken - an access token for authorizing the request.
*/ */
public async deleteFolder(folderPath: string, accessToken: string) { public async deleteFolder(folderPath: string, accessToken: string) {
this.isMethodSupported('deleteFolder', ServerType.SASViya) this.isMethodSupported('deleteFolder', ServerType.SasViya)
return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken) return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken)
} }
@@ -327,7 +327,7 @@ export default class SASjs {
targetFolderName: string, targetFolderName: string,
accessToken: string accessToken: string
) { ) {
this.isMethodSupported('moveFolder', ServerType.SASViya) this.isMethodSupported('moveFolder', ServerType.SasViya)
return await this.sasViyaApiClient?.moveFolder( return await this.sasViyaApiClient?.moveFolder(
sourceFolder, sourceFolder,
@@ -345,7 +345,7 @@ export default class SASjs {
accessToken?: string, accessToken?: string,
sasApiClient?: SASViyaApiClient sasApiClient?: SASViyaApiClient
) { ) {
this.isMethodSupported('createJobDefinition', ServerType.SASViya) this.isMethodSupported('createJobDefinition', ServerType.SasViya)
if (sasApiClient) if (sasApiClient)
return await sasApiClient!.createJobDefinition( return await sasApiClient!.createJobDefinition(
@@ -365,7 +365,7 @@ export default class SASjs {
} }
public async getAuthCode(clientId: string) { public async getAuthCode(clientId: string) {
this.isMethodSupported('getAuthCode', ServerType.SASViya) this.isMethodSupported('getAuthCode', ServerType.SasViya)
return await this.sasViyaApiClient!.getAuthCode(clientId) return await this.sasViyaApiClient!.getAuthCode(clientId)
} }
@@ -375,7 +375,7 @@ export default class SASjs {
clientSecret: string, clientSecret: string,
authCode: string authCode: string
) { ) {
this.isMethodSupported('getAccessToken', ServerType.SASViya) this.isMethodSupported('getAccessToken', ServerType.SasViya)
return await this.sasViyaApiClient!.getAccessToken( return await this.sasViyaApiClient!.getAccessToken(
clientId, clientId,
@@ -389,7 +389,7 @@ export default class SASjs {
clientSecret: string, clientSecret: string,
refreshToken: string refreshToken: string
) { ) {
this.isMethodSupported('refreshTokens', ServerType.SASViya) this.isMethodSupported('refreshTokens', ServerType.SasViya)
return await this.sasViyaApiClient!.refreshTokens( return await this.sasViyaApiClient!.refreshTokens(
clientId, clientId,
@@ -399,7 +399,7 @@ export default class SASjs {
} }
public async deleteClient(clientId: string, accessToken: string) { public async deleteClient(clientId: string, accessToken: string) {
this.isMethodSupported('deleteClient', ServerType.SASViya) this.isMethodSupported('deleteClient', ServerType.SasViya)
return await this.sasViyaApiClient!.deleteClient(clientId, accessToken) return await this.sasViyaApiClient!.deleteClient(clientId, accessToken)
} }
@@ -537,7 +537,7 @@ export default class SASjs {
...config ...config
} }
if (config.serverType === ServerType.SASViya && config.contextName) { if (config.serverType === ServerType.SasViya && config.contextName) {
if (config.useComputeApi) { if (config.useComputeApi) {
requestResponse = await this.executeJobViaComputeApi( requestResponse = await this.executeJobViaComputeApi(
sasJob, sasJob,
@@ -589,7 +589,7 @@ export default class SASjs {
accessToken?: string, accessToken?: string,
isForced = false isForced = false
) { ) {
this.isMethodSupported('deployServicePack', ServerType.SASViya) this.isMethodSupported('deployServicePack', ServerType.SasViya)
let sasApiClient: any = null let sasApiClient: any = null
if (serverUrl || appLoc) { if (serverUrl || appLoc) {
@@ -599,7 +599,7 @@ export default class SASjs {
if (!appLoc) { if (!appLoc) {
appLoc = this.sasjsConfig.appLoc appLoc = this.sasjsConfig.appLoc
} }
if (this.sasjsConfig.serverType === ServerType.SASViya) { if (this.sasjsConfig.serverType === ServerType.SasViya) {
sasApiClient = new SASViyaApiClient( sasApiClient = new SASViyaApiClient(
serverUrl, serverUrl,
appLoc, appLoc,
@@ -607,14 +607,14 @@ export default class SASjs {
this.setCsrfTokenApi this.setCsrfTokenApi
) )
sasApiClient.debug = this.sasjsConfig.debug sasApiClient.debug = this.sasjsConfig.debug
} else if (this.sasjsConfig.serverType === ServerType.SAS9) { } else if (this.sasjsConfig.serverType === ServerType.Sas9) {
sasApiClient = new SAS9ApiClient(serverUrl) sasApiClient = new SAS9ApiClient(serverUrl)
} }
} else { } else {
let sasClientConfig: any = null let sasClientConfig: any = null
if (this.sasjsConfig.serverType === ServerType.SASViya) { if (this.sasjsConfig.serverType === ServerType.SasViya) {
sasClientConfig = this.sasViyaApiClient!.getConfig() sasClientConfig = this.sasViyaApiClient!.getConfig()
} else if (this.sasjsConfig.serverType === ServerType.SAS9) { } else if (this.sasjsConfig.serverType === ServerType.Sas9) {
sasClientConfig = this.sas9ApiClient!.getConfig() sasClientConfig = this.sas9ApiClient!.getConfig()
} }
serverUrl = sasClientConfig.serverUrl serverUrl = sasClientConfig.serverUrl
@@ -670,7 +670,7 @@ export default class SASjs {
...config ...config
} }
this.isMethodSupported('startComputeJob', ServerType.SASViya) this.isMethodSupported('startComputeJob', ServerType.SasViya)
if (!config.contextName) { if (!config.contextName) {
throw new Error( throw new Error(
'Context name is undefined. Please set a `contextName` in your SASjs or override config.' 'Context name is undefined. Please set a `contextName` in your SASjs or override config.'
@@ -917,7 +917,7 @@ export default class SASjs {
: sasJob : sasJob
: sasJob : sasJob
const jobUri = const jobUri =
config.serverType === ServerType.SASViya config.serverType === ServerType.SasViya
? await this.getJobUri(sasJob) ? await this.getJobUri(sasJob)
: '' : ''
const apiUrl = `${config.serverUrl}${this.jobsPath}/?${ const apiUrl = `${config.serverUrl}${this.jobsPath}/?${
@@ -938,7 +938,7 @@ export default class SASjs {
if (data) { if (data) {
const stringifiedData = JSON.stringify(data) const stringifiedData = JSON.stringify(data)
if ( if (
config.serverType === ServerType.SAS9 || config.serverType === ServerType.Sas9 ||
stringifiedData.length > 500000 || stringifiedData.length > 500000 ||
stringifiedData.includes(';') stringifiedData.includes(';')
) { ) {
@@ -1030,7 +1030,7 @@ export default class SASjs {
} }
} }
if (response.redirected && config.serverType === ServerType.SAS9) { if (response.redirected && config.serverType === ServerType.Sas9) {
isRedirected = true isRedirected = true
} }
@@ -1062,7 +1062,7 @@ export default class SASjs {
sasjsWaitingRequest.config = config sasjsWaitingRequest.config = config
this.sasjsWaitingRequests.push(sasjsWaitingRequest) this.sasjsWaitingRequests.push(sasjsWaitingRequest)
} else { } else {
if (config.serverType === ServerType.SAS9 && config.debug) { if (config.serverType === ServerType.Sas9 && config.debug) {
const jsonResponseText = parseWeboutResponse(responseText) const jsonResponseText = parseWeboutResponse(responseText)
if (jsonResponseText !== '') { if (jsonResponseText !== '') {
@@ -1076,7 +1076,7 @@ export default class SASjs {
) )
} }
} else if ( } else if (
config.serverType === ServerType.SASViya && config.serverType === ServerType.SasViya &&
config.debug config.debug
) { ) {
try { try {
@@ -1259,7 +1259,7 @@ export default class SASjs {
} }
private parseLogFromResponse(response: any, program: string) { private parseLogFromResponse(response: any, program: string) {
if (this.sasjsConfig.serverType === ServerType.SAS9) { if (this.sasjsConfig.serverType === ServerType.Sas9) {
this.appendSasjsRequest(response, program, null) this.appendSasjsRequest(response, program, null)
} else { } else {
if (!this.sasjsConfig.debug) { if (!this.sasjsConfig.debug) {
@@ -1339,7 +1339,7 @@ export default class SASjs {
if (this.sasjsConfig.debug) { if (this.sasjsConfig.debug) {
let jsonResponse let jsonResponse
if (this.sasjsConfig.serverType === ServerType.SAS9) { if (this.sasjsConfig.serverType === ServerType.Sas9) {
try { try {
jsonResponse = JSON.parse(parseWeboutResponse(response)) jsonResponse = JSON.parse(parseWeboutResponse(response))
} catch (e) { } catch (e) {
@@ -1397,7 +1397,7 @@ export default class SASjs {
} }
this.jobsPath = this.jobsPath =
this.sasjsConfig.serverType === ServerType.SASViya this.sasjsConfig.serverType === ServerType.SasViya
? this.sasjsConfig.pathSASViya ? this.sasjsConfig.pathSASViya
: this.sasjsConfig.pathSAS9 : this.sasjsConfig.pathSAS9
this.authManager = new AuthManager( this.authManager = new AuthManager(
@@ -1406,7 +1406,7 @@ export default class SASjs {
this.resendWaitingRequests this.resendWaitingRequests
) )
if (this.sasjsConfig.serverType === ServerType.SASViya) { if (this.sasjsConfig.serverType === ServerType.SasViya) {
if (this.sasViyaApiClient) if (this.sasViyaApiClient)
this.sasViyaApiClient!.setConfig( this.sasViyaApiClient!.setConfig(
this.sasjsConfig.serverUrl, this.sasjsConfig.serverUrl,
@@ -1422,7 +1422,7 @@ export default class SASjs {
this.sasViyaApiClient.debug = this.sasjsConfig.debug this.sasViyaApiClient.debug = this.sasjsConfig.debug
} }
if (this.sasjsConfig.serverType === ServerType.SAS9) { if (this.sasjsConfig.serverType === ServerType.Sas9) {
if (this.sas9ApiClient) if (this.sas9ApiClient)
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl) this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl)
else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl) else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl)
@@ -1483,7 +1483,7 @@ export default class SASjs {
if (this.sasjsConfig.serverType !== serverType) { if (this.sasjsConfig.serverType !== serverType) {
throw new Error( throw new Error(
`Method '${method}' is only supported on ${ `Method '${method}' is only supported on ${
serverType === ServerType.SAS9 ? 'SAS9' : 'SAS Viya' serverType === ServerType.Sas9 ? 'SAS9' : 'SAS Viya'
} servers.` } servers.`
) )
} }

View File

@@ -1,4 +1,4 @@
import { ServerType } from './ServerType' import { ServerType } from '@sasjs/utils/types'
/** /**
* Specifies the configuration for the SASjs instance - eg where and how to * Specifies the configuration for the SASjs instance - eg where and how to

View File

@@ -1,8 +0,0 @@
/**
* Server type that can be `Viya` or `SAS9`.
*
*/
export enum ServerType {
SASViya = 'SASVIYA',
SAS9 = 'SAS9'
}

View File

@@ -9,7 +9,6 @@ export * from './Link'
export * from './SASjsConfig' export * from './SASjsConfig'
export * from './SASjsRequest' export * from './SASjsRequest'
export * from './SASjsWaitingRequest' export * from './SASjsWaitingRequest'
export * from './ServerType'
export * from './Session' export * from './Session'
export * from './UploadFile' export * from './UploadFile'
export * from './PollOptions' export * from './PollOptions'