mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-15 18:54:36 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a69ebd0fd3 | ||
|
|
0f47326bb6 | ||
|
|
2d6efa2437 |
@@ -1,11 +1,24 @@
|
|||||||
import * as NodeFormData from 'form-data'
|
import * as NodeFormData from 'form-data'
|
||||||
import { AuthConfig, ServerType, ServicePackSASjs } from '@sasjs/utils/types'
|
import { AuthConfig, ServerType, ServicePackSASjs } from '@sasjs/utils/types'
|
||||||
|
import { prefixMessage } from '@sasjs/utils/error'
|
||||||
import { ExecutionQuery } from './types'
|
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 { getTokens } from './auth/getTokens'
|
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 {
|
export class SASjsApiClient {
|
||||||
constructor(private requestClient: RequestClient) {}
|
constructor(private requestClient: RequestClient) {}
|
||||||
|
|
||||||
@@ -118,18 +131,28 @@ export class SASjsApiClient {
|
|||||||
code: string,
|
code: string,
|
||||||
runTime: string = 'sas',
|
runTime: string = 'sas',
|
||||||
authConfig?: AuthConfig
|
authConfig?: AuthConfig
|
||||||
) {
|
): Promise<ScriptExecutionResult> {
|
||||||
const access_token = await this.getAccessTokenForRequest(authConfig)
|
const access_token = await this.getAccessTokenForRequest(authConfig)
|
||||||
|
const executionResult: ScriptExecutionResult = { log: '' }
|
||||||
let parsedSasjsServerLog = ''
|
|
||||||
|
|
||||||
await this.requestClient
|
await this.requestClient
|
||||||
.post('SASjsApi/code/execute', { code, runTime }, access_token)
|
.post('SASjsApi/code/execute', { code, runTime }, access_token)
|
||||||
.then((res: any) => {
|
.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)
|
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 { RequestClient } from '../request/RequestClient'
|
||||||
|
|
||||||
import { isRelativePath, appendExtraResponseAttributes } from '../utils'
|
import {
|
||||||
|
isRelativePath,
|
||||||
|
appendExtraResponseAttributes,
|
||||||
|
getValidJson
|
||||||
|
} from '../utils'
|
||||||
import { BaseJobExecutor } from './JobExecutor'
|
import { BaseJobExecutor } from './JobExecutor'
|
||||||
|
|
||||||
export class SasjsJobExecutor extends BaseJobExecutor {
|
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)
|
this.requestClient!.appendRequest(res, sasJob, config.debug)
|
||||||
|
|
||||||
const responseObject = appendExtraResponseAttributes(
|
const responseObject = appendExtraResponseAttributes(
|
||||||
res,
|
res,
|
||||||
extraResponseAttributes
|
extraResponseAttributes
|
||||||
)
|
)
|
||||||
|
|
||||||
resolve(responseObject)
|
resolve(responseObject)
|
||||||
})
|
})
|
||||||
.catch(async (e: Error) => {
|
.catch(async (e: Error) => {
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import { RequestClient } from './RequestClient'
|
import { RequestClient } from './RequestClient'
|
||||||
import { AxiosResponse } from 'axios'
|
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.
|
* Specific request client for SASJS.
|
||||||
@@ -27,7 +35,7 @@ export class SasjsRequestClient extends RequestClient {
|
|||||||
protected parseResponse<T>(response: AxiosResponse<any>) {
|
protected parseResponse<T>(response: AxiosResponse<any>) {
|
||||||
const etag = response?.headers ? response.headers['etag'] : ''
|
const etag = response?.headers ? response.headers['etag'] : ''
|
||||||
let parsedResponse = {}
|
let parsedResponse = {}
|
||||||
let log
|
let webout, log, printOutput
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof response.data === 'string') {
|
if (typeof response.data === 'string') {
|
||||||
@@ -38,17 +46,26 @@ export class SasjsRequestClient extends RequestClient {
|
|||||||
} catch {
|
} catch {
|
||||||
if (response.data.includes(SASJS_LOGS_SEPARATOR)) {
|
if (response.data.includes(SASJS_LOGS_SEPARATOR)) {
|
||||||
const splittedResponse = response.data.split(SASJS_LOGS_SEPARATOR)
|
const splittedResponse = response.data.split(SASJS_LOGS_SEPARATOR)
|
||||||
|
|
||||||
|
webout = splittedResponse[0]
|
||||||
|
if (webout) parsedResponse = webout
|
||||||
|
|
||||||
log = splittedResponse[1]
|
log = splittedResponse[1]
|
||||||
if (splittedResponse[0].trim())
|
printOutput = splittedResponse[2]
|
||||||
parsedResponse = getValidJson(splittedResponse[0])
|
} else {
|
||||||
} else parsedResponse = response.data
|
parsedResponse = response.data
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
const returnResult: SasjsParsedResponse<T> = {
|
||||||
result: parsedResponse as T,
|
result: parsedResponse as T,
|
||||||
log,
|
log,
|
||||||
etag,
|
etag,
|
||||||
status: response.status
|
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)
|
return JSON.parse(str)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e instanceof JsonParseArrayError) throw e
|
if (e instanceof JsonParseArrayError) throw e
|
||||||
|
|
||||||
throw new InvalidJsonError()
|
throw new InvalidJsonError()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user