mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
Merge pull request #806 from sasjs/sasjs-job-execution
fix(job-execution): fixed webout for SASJS server type
This commit is contained in:
@@ -1,11 +1,24 @@
|
||||
import * as NodeFormData from 'form-data'
|
||||
import { AuthConfig, ServerType, ServicePackSASjs } from '@sasjs/utils/types'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import { ExecutionQuery } from './types'
|
||||
import { RequestClient } from './request/RequestClient'
|
||||
import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs'
|
||||
import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs'
|
||||
import { getTokens } from './auth/getTokens'
|
||||
|
||||
// TODO: move to sasjs/utils
|
||||
export interface SASjsAuthResponse {
|
||||
access_token: string
|
||||
refresh_token: string
|
||||
}
|
||||
|
||||
export interface ScriptExecutionResult {
|
||||
log: string
|
||||
webout?: string
|
||||
printOutput?: string
|
||||
}
|
||||
|
||||
export class SASjsApiClient {
|
||||
constructor(private requestClient: RequestClient) {}
|
||||
|
||||
@@ -118,18 +131,28 @@ export class SASjsApiClient {
|
||||
code: string,
|
||||
runTime: string = 'sas',
|
||||
authConfig?: AuthConfig
|
||||
) {
|
||||
): Promise<ScriptExecutionResult> {
|
||||
const access_token = await this.getAccessTokenForRequest(authConfig)
|
||||
|
||||
let parsedSasjsServerLog = ''
|
||||
const executionResult: ScriptExecutionResult = { log: '' }
|
||||
|
||||
await this.requestClient
|
||||
.post('SASjsApi/code/execute', { code, runTime }, access_token)
|
||||
.then((res: any) => {
|
||||
if (res.log) parsedSasjsServerLog = res.log
|
||||
const { log, printOutput, result: webout } = res
|
||||
|
||||
executionResult.log = log
|
||||
|
||||
if (printOutput) executionResult.printOutput = printOutput
|
||||
if (webout) executionResult.webout = webout
|
||||
})
|
||||
.catch((err) => {
|
||||
throw prefixMessage(
|
||||
err,
|
||||
'Error while sending POST request to execute code. '
|
||||
)
|
||||
})
|
||||
|
||||
return parsedSasjsServerLog
|
||||
return executionResult
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,9 +175,3 @@ export class SASjsApiClient {
|
||||
return refreshTokensForSasjs(this.requestClient, refreshToken)
|
||||
}
|
||||
}
|
||||
|
||||
// todo move to sasjs/utils
|
||||
export interface SASjsAuthResponse {
|
||||
access_token: string
|
||||
refresh_token: string
|
||||
}
|
||||
|
||||
@@ -13,7 +13,11 @@ import { generateFileUploadForm } from '../file/generateFileUploadForm'
|
||||
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
|
||||
import { isRelativePath, appendExtraResponseAttributes } from '../utils'
|
||||
import {
|
||||
isRelativePath,
|
||||
appendExtraResponseAttributes,
|
||||
getValidJson
|
||||
} from '../utils'
|
||||
import { BaseJobExecutor } from './JobExecutor'
|
||||
|
||||
export class SasjsJobExecutor extends BaseJobExecutor {
|
||||
@@ -89,12 +93,16 @@ export class SasjsJobExecutor extends BaseJobExecutor {
|
||||
)
|
||||
}
|
||||
|
||||
const { result } = res.result
|
||||
if (result && result.trim()) res.result = getValidJson(result)
|
||||
|
||||
this.requestClient!.appendRequest(res, sasJob, config.debug)
|
||||
|
||||
const responseObject = appendExtraResponseAttributes(
|
||||
res,
|
||||
extraResponseAttributes
|
||||
)
|
||||
|
||||
resolve(responseObject)
|
||||
})
|
||||
.catch(async (e: Error) => {
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { RequestClient } from './RequestClient'
|
||||
import { AxiosResponse } from 'axios'
|
||||
import { SASJS_LOGS_SEPARATOR, getValidJson } from '../utils'
|
||||
import { SASJS_LOGS_SEPARATOR } from '../utils'
|
||||
|
||||
interface SasjsParsedResponse<T> {
|
||||
result: T
|
||||
log: string
|
||||
etag: string
|
||||
status: number
|
||||
printOutput?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific request client for SASJS.
|
||||
@@ -27,7 +35,7 @@ export class SasjsRequestClient extends RequestClient {
|
||||
protected parseResponse<T>(response: AxiosResponse<any>) {
|
||||
const etag = response?.headers ? response.headers['etag'] : ''
|
||||
let parsedResponse = {}
|
||||
let log
|
||||
let webout, log, printOutput
|
||||
|
||||
try {
|
||||
if (typeof response.data === 'string') {
|
||||
@@ -38,17 +46,26 @@ export class SasjsRequestClient extends RequestClient {
|
||||
} catch {
|
||||
if (response.data.includes(SASJS_LOGS_SEPARATOR)) {
|
||||
const splittedResponse = response.data.split(SASJS_LOGS_SEPARATOR)
|
||||
|
||||
webout = splittedResponse[0]
|
||||
if (webout) parsedResponse = webout
|
||||
|
||||
log = splittedResponse[1]
|
||||
if (splittedResponse[0].trim())
|
||||
parsedResponse = getValidJson(splittedResponse[0])
|
||||
} else parsedResponse = response.data
|
||||
printOutput = splittedResponse[2]
|
||||
} else {
|
||||
parsedResponse = response.data
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
const returnResult: SasjsParsedResponse<T> = {
|
||||
result: parsedResponse as T,
|
||||
log,
|
||||
etag,
|
||||
status: response.status
|
||||
}
|
||||
|
||||
if (printOutput) returnResult.printOutput = printOutput
|
||||
|
||||
return returnResult
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ export const getValidJson = (str: string | object): object => {
|
||||
return JSON.parse(str)
|
||||
} catch (e: any) {
|
||||
if (e instanceof JsonParseArrayError) throw e
|
||||
|
||||
throw new InvalidJsonError()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user