1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

fix: handled updated sasjs response

This commit is contained in:
2022-08-19 16:10:05 +05:00
parent 92be5a2dca
commit 4a319f1aef
7 changed files with 61 additions and 75 deletions

View File

@@ -102,10 +102,14 @@ export default class SASjs {
* @param code - a string of code from the file to run.
* @param authConfig - (optional) a valid client, secret, refresh and access tokens that are authorised to execute scripts.
*/
public async executeScriptSASjs(code: string, authConfig?: AuthConfig) {
public async executeScriptSASjs(
code: string,
runTime?: string,
authConfig?: AuthConfig
) {
this.isMethodSupported('executeScriptSASJS', [ServerType.Sasjs])
return await this.sasJSApiClient?.executeScript(code, authConfig)
return await this.sasJSApiClient?.executeScript(code, runTime, authConfig)
}
/**

View File

@@ -3,7 +3,7 @@ import { ExecutionQuery } from './types'
import { RequestClient } from './request/RequestClient'
import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs'
import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs'
import { parseWeboutResponse } from './utils'
import { parseWeboutResponse, SASJS_LOGS_SEPARATOR } from './utils'
import { getTokens } from './auth/getTokens'
export class SASjsApiClient {
@@ -64,9 +64,14 @@ export class SASjsApiClient {
/**
* Executes code on a SASJS server.
* @param code - a string of code to execute.
* @param runTime - a string to representing runTime for code execution
* @param authConfig - an object for authentication.
*/
public async executeScript(code: string, authConfig?: AuthConfig) {
public async executeScript(
code: string,
runTime: string = 'sas',
authConfig?: AuthConfig
) {
let access_token = (authConfig || {}).access_token
if (authConfig) {
;({ access_token } = await getTokens(
@@ -79,12 +84,10 @@ export class SASjsApiClient {
let parsedSasjsServerLog = ''
await this.requestClient
.post('SASjsApi/code/execute', { code }, access_token)
.post('SASjsApi/code/execute', { code, runTime }, access_token)
.then((res: any) => {
if (res.result?.log) {
parsedSasjsServerLog = res.result.log
.map((logLine: any) => logLine.line)
.join('\n')
if (res.log) {
parsedSasjsServerLog = res.log.split(SASJS_LOGS_SEPARATOR)[1]
}
})
.catch((err) => {

View File

@@ -1,7 +1,8 @@
import {
getValidJson,
parseSasViyaDebugResponse,
parseWeboutResponse
parseWeboutResponse,
SASJS_LOGS_SEPARATOR
} from '../utils'
import { UploadFile } from '../types/UploadFile'
import {
@@ -80,9 +81,29 @@ export class FileUploader extends BaseJobExecutor {
this.requestClient
.post(uploadUrl, formData, undefined, 'application/json', headers)
.then(async (res: any) => {
this.requestClient.appendRequest(res, sasJob, config.debug)
const parsedSasjsLog =
this.serverType === ServerType.Sasjs
? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
: res.result.log
let jsonResponse = res.result
const parsedSasjsServerWebout =
this.serverType === ServerType.Sasjs
? typeof res.result === 'string'
? getValidJson(res.log?.split(SASJS_LOGS_SEPARATOR)[0])
: res.result
: undefined
const resObj =
this.serverType === ServerType.Sasjs
? {
result: parsedSasjsServerWebout,
log: parsedSasjsLog
}
: res
this.requestClient.appendRequest(resObj, sasJob, config.debug)
let jsonResponse = resObj.result
if (config.debug) {
switch (this.serverType) {
@@ -99,25 +120,7 @@ export class FileUploader extends BaseJobExecutor {
? parseWeboutResponse(res.result, uploadUrl)
: res.result
break
case ServerType.Sasjs:
if (typeof res.result._webout === 'object') {
jsonResponse = res.result._webout
} else {
const webout = parseWeboutResponse(
res.result._webout,
uploadUrl
)
jsonResponse = getValidJson(webout)
}
break
}
} else if (this.serverType === ServerType.Sasjs) {
jsonResponse = getValidJson(res.result._webout)
} else {
jsonResponse =
typeof res.result === 'string'
? getValidJson(res.result)
: res.result
}
resolve(jsonResponse)

View File

@@ -17,7 +17,8 @@ import {
isRelativePath,
parseSasViyaDebugResponse,
appendExtraResponseAttributes,
getValidJson
getValidJson,
SASJS_LOGS_SEPARATOR
} from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
@@ -164,33 +165,37 @@ export class WebJobExecutor extends BaseJobExecutor {
contentType
)
.then(async (res: any) => {
const parsedSasjsServerLog =
const parsedSasjsLog =
this.serverType === ServerType.Sasjs
? res.result.log.map((logLine: any) => logLine.line).join('\n')
? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
: res.result.log
const parsedSasjsServerWebout =
this.serverType === ServerType.Sasjs
? typeof res.result === 'string'
? getValidJson(res.log?.split(SASJS_LOGS_SEPARATOR)[0])
: res.result
: undefined
const resObj =
this.serverType === ServerType.Sasjs
? {
result: res.result._webout,
log: parsedSasjsServerLog
result: parsedSasjsServerWebout,
log: parsedSasjsLog
}
: res
if (
this.serverType === ServerType.Sasjs &&
res.result._webout.length < 1
) {
if (this.serverType === ServerType.Sasjs && res.result.length < 1) {
throw new JobExecutionError(
0,
`No webout was returned by job ${program}. Server type is SASJS and the calling function is WebJobExecutor. Please check the SAS log for more info.`,
parsedSasjsServerLog
parsedSasjsLog
)
}
this.requestClient!.appendRequest(resObj, sasJob, config.debug)
let jsonResponse = res.result
let jsonResponse = resObj.result
if (config.debug) {
switch (this.serverType) {
@@ -207,21 +212,11 @@ export class WebJobExecutor extends BaseJobExecutor {
? parseWeboutResponse(res.result, apiUrl)
: res.result
break
case ServerType.Sasjs:
if (typeof res.result._webout === 'object') {
jsonResponse = res.result._webout
} else {
const webout = parseWeboutResponse(res.result._webout, apiUrl)
jsonResponse = getValidJson(webout)
}
break
}
} else if (this.serverType === ServerType.Sasjs) {
jsonResponse = getValidJson(res.result._webout)
}
const responseObject = appendExtraResponseAttributes(
{ result: jsonResponse, log: parsedSasjsServerLog },
{ result: jsonResponse, log: parsedSasjsLog },
extraResponseAttributes
)
resolve(responseObject)
@@ -261,9 +256,7 @@ export class WebJobExecutor extends BaseJobExecutor {
})
if (loginCallback) await loginCallback()
} else {
reject(new ErrorResponse(e?.message, e))
}
} else reject(new ErrorResponse(e?.message, e))
})
})

View File

@@ -133,26 +133,6 @@ export class RequestClient implements HttpClient {
} else {
sasWork = response.log
}
} else if (response?.result?.log) {
//In this scenario we know we got the response from SASJS server
//Log is array of `{ line: '' }` so we need to convert it back to text
//To be able to parse it with current functions.
let log: string = ''
if (typeof log !== 'string') {
log = response.result.log
.map((logLine: any) => logLine.line)
.join('\n')
}
sourceCode = parseSourceCode(log)
generatedCode = parseGeneratedCode(log)
if (response?.result?._webout) {
sasWork = response.result._webout.WORK
} else {
sasWork = log
}
} else if (response?.result) {
sourceCode = parseSourceCode(response.result)
generatedCode = parseGeneratedCode(response.result)

2
src/utils/constants.ts Normal file
View File

@@ -0,0 +1,2 @@
export const SASJS_LOGS_SEPARATOR =
'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'

View File

@@ -2,6 +2,7 @@ export * from './appendExtraResponseAttributes'
export * from './asyncForEach'
export * from './compareTimestamps'
export * from './convertToCsv'
export * from './constants'
export * from './createAxiosInstance'
export * from './delay'
export * from './fetchLogByChunks'