1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-16 16:40:06 +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 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. * @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]) 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 { RequestClient } from './request/RequestClient'
import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs' import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs'
import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs' import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs'
import { parseWeboutResponse } from './utils' import { parseWeboutResponse, SASJS_LOGS_SEPARATOR } from './utils'
import { getTokens } from './auth/getTokens' import { getTokens } from './auth/getTokens'
export class SASjsApiClient { export class SASjsApiClient {
@@ -64,9 +64,14 @@ export class SASjsApiClient {
/** /**
* Executes code on a SASJS server. * Executes code on a SASJS server.
* @param code - a string of code to execute. * @param code - a string of code to execute.
* @param runTime - a string to representing runTime for code execution
* @param authConfig - an object for authentication. * @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 let access_token = (authConfig || {}).access_token
if (authConfig) { if (authConfig) {
;({ access_token } = await getTokens( ;({ access_token } = await getTokens(
@@ -79,12 +84,10 @@ export class SASjsApiClient {
let parsedSasjsServerLog = '' let parsedSasjsServerLog = ''
await this.requestClient await this.requestClient
.post('SASjsApi/code/execute', { code }, access_token) .post('SASjsApi/code/execute', { code, runTime }, access_token)
.then((res: any) => { .then((res: any) => {
if (res.result?.log) { if (res.log) {
parsedSasjsServerLog = res.result.log parsedSasjsServerLog = res.log.split(SASJS_LOGS_SEPARATOR)[1]
.map((logLine: any) => logLine.line)
.join('\n')
} }
}) })
.catch((err) => { .catch((err) => {

View File

@@ -1,7 +1,8 @@
import { import {
getValidJson, getValidJson,
parseSasViyaDebugResponse, parseSasViyaDebugResponse,
parseWeboutResponse parseWeboutResponse,
SASJS_LOGS_SEPARATOR
} from '../utils' } from '../utils'
import { UploadFile } from '../types/UploadFile' import { UploadFile } from '../types/UploadFile'
import { import {
@@ -80,9 +81,29 @@ export class FileUploader extends BaseJobExecutor {
this.requestClient this.requestClient
.post(uploadUrl, formData, undefined, 'application/json', headers) .post(uploadUrl, formData, undefined, 'application/json', headers)
.then(async (res: any) => { .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) { if (config.debug) {
switch (this.serverType) { switch (this.serverType) {
@@ -99,25 +120,7 @@ export class FileUploader extends BaseJobExecutor {
? parseWeboutResponse(res.result, uploadUrl) ? parseWeboutResponse(res.result, uploadUrl)
: res.result : res.result
break 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) resolve(jsonResponse)

View File

@@ -17,7 +17,8 @@ import {
isRelativePath, isRelativePath,
parseSasViyaDebugResponse, parseSasViyaDebugResponse,
appendExtraResponseAttributes, appendExtraResponseAttributes,
getValidJson getValidJson,
SASJS_LOGS_SEPARATOR
} from '../utils' } from '../utils'
import { BaseJobExecutor } from './JobExecutor' import { BaseJobExecutor } from './JobExecutor'
import { parseWeboutResponse } from '../utils/parseWeboutResponse' import { parseWeboutResponse } from '../utils/parseWeboutResponse'
@@ -164,33 +165,37 @@ export class WebJobExecutor extends BaseJobExecutor {
contentType contentType
) )
.then(async (res: any) => { .then(async (res: any) => {
const parsedSasjsServerLog = const parsedSasjsLog =
this.serverType === ServerType.Sasjs this.serverType === ServerType.Sasjs
? res.result.log.map((logLine: any) => logLine.line).join('\n') ? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
: res.result.log : 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 = const resObj =
this.serverType === ServerType.Sasjs this.serverType === ServerType.Sasjs
? { ? {
result: res.result._webout, result: parsedSasjsServerWebout,
log: parsedSasjsServerLog log: parsedSasjsLog
} }
: res : res
if ( if (this.serverType === ServerType.Sasjs && res.result.length < 1) {
this.serverType === ServerType.Sasjs &&
res.result._webout.length < 1
) {
throw new JobExecutionError( throw new JobExecutionError(
0, 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.`, `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) this.requestClient!.appendRequest(resObj, sasJob, config.debug)
let jsonResponse = res.result let jsonResponse = resObj.result
if (config.debug) { if (config.debug) {
switch (this.serverType) { switch (this.serverType) {
@@ -207,21 +212,11 @@ export class WebJobExecutor extends BaseJobExecutor {
? parseWeboutResponse(res.result, apiUrl) ? parseWeboutResponse(res.result, apiUrl)
: res.result : res.result
break 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( const responseObject = appendExtraResponseAttributes(
{ result: jsonResponse, log: parsedSasjsServerLog }, { result: jsonResponse, log: parsedSasjsLog },
extraResponseAttributes extraResponseAttributes
) )
resolve(responseObject) resolve(responseObject)
@@ -261,9 +256,7 @@ export class WebJobExecutor extends BaseJobExecutor {
}) })
if (loginCallback) await loginCallback() if (loginCallback) await loginCallback()
} else { } else reject(new ErrorResponse(e?.message, e))
reject(new ErrorResponse(e?.message, e))
}
}) })
}) })

View File

@@ -133,26 +133,6 @@ export class RequestClient implements HttpClient {
} else { } else {
sasWork = response.log 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) { } else if (response?.result) {
sourceCode = parseSourceCode(response.result) sourceCode = parseSourceCode(response.result)
generatedCode = parseGeneratedCode(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 './asyncForEach'
export * from './compareTimestamps' export * from './compareTimestamps'
export * from './convertToCsv' export * from './convertToCsv'
export * from './constants'
export * from './createAxiosInstance' export * from './createAxiosInstance'
export * from './delay' export * from './delay'
export * from './fetchLogByChunks' export * from './fetchLogByChunks'