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

fix: created sasjsJobExecutor class and overrided parseResponse for sasjsRequestClient

This commit is contained in:
2022-08-23 15:47:50 +05:00
parent 4a319f1aef
commit c551cd0311
8 changed files with 208 additions and 96 deletions

View File

@@ -81,29 +81,9 @@ export class FileUploader extends BaseJobExecutor {
this.requestClient
.post(uploadUrl, formData, undefined, 'application/json', headers)
.then(async (res: any) => {
const parsedSasjsLog =
this.serverType === ServerType.Sasjs
? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
: res.result.log
this.requestClient.appendRequest(res, sasJob, config.debug)
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
let jsonResponse = res.result
if (config.debug) {
switch (this.serverType) {
@@ -121,6 +101,11 @@ export class FileUploader extends BaseJobExecutor {
: res.result
break
}
} else if (this.serverType !== ServerType.Sasjs) {
jsonResponse =
typeof res.result === 'string'
? getValidJson(res.result)
: res.result
}
resolve(jsonResponse)

View File

@@ -1,6 +1,6 @@
import { AuthConfig, ServerType } from '@sasjs/utils/types'
import { ExtraResponseAttributes } from '@sasjs/utils/types'
import { asyncForEach } from '../utils'
import { asyncForEach, isRelativePath } from '../utils'
export type ExecuteFunction = () => Promise<any>
@@ -45,4 +45,17 @@ export abstract class BaseJobExecutor implements JobExecutor {
protected appendWaitingRequest(request: ExecuteFunction) {
this.waitingRequests.push(request)
}
protected getRequestParams(config: any): any {
const requestParams: any = {}
if (config.debug) {
requestParams['_omittextlog'] = 'false'
requestParams['_omitsessionresults'] = 'false'
requestParams['_debug'] = 131
}
return requestParams
}
}

View File

@@ -102,7 +102,7 @@ export class Sas9JobExecutor extends BaseJobExecutor {
return requestPromise
}
private getRequestParams(config: any): any {
protected getRequestParams(config: any): any {
const requestParams: any = {}
if (config.debug) {

View File

@@ -0,0 +1,141 @@
import * as NodeFormData from 'form-data'
import {
AuthConfig,
ExtraResponseAttributes,
ServerType
} from '@sasjs/utils/types'
import {
ErrorResponse,
JobExecutionError,
LoginRequiredError
} from '../types/errors'
import { generateFileUploadForm } from '../file/generateFileUploadForm'
import { RequestClient } from '../request/RequestClient'
import { isRelativePath, appendExtraResponseAttributes } from '../utils'
import { BaseJobExecutor } from './JobExecutor'
export class SasjsJobExecutor extends BaseJobExecutor {
constructor(
serverUrl: string,
private jobsPath: string,
private requestClient: RequestClient
) {
super(serverUrl, ServerType.Sasjs)
}
async execute(
sasJob: string,
data: any,
config: any,
loginRequiredCallback?: any,
authConfig?: AuthConfig,
extraResponseAttributes: ExtraResponseAttributes[] = []
) {
const loginCallback = loginRequiredCallback
const program =
isRelativePath(sasJob) && config.appLoc
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
: sasJob
let apiUrl = `${config.serverUrl}${this.jobsPath}/?${'_program=' + program}`
let requestParams = {
...this.getRequestParams(config)
}
/**
* Use the available form data object (FormData in Browser, NodeFormData in
* Node)
*/
let formData =
typeof FormData === 'undefined' ? new NodeFormData() : new FormData()
if (data) {
// file upload approach
try {
formData = generateFileUploadForm(formData, data)
} catch (e: any) {
return Promise.reject(new ErrorResponse(e?.message, e))
}
}
for (const key in requestParams) {
if (requestParams.hasOwnProperty(key)) {
formData.append(key, requestParams[key])
}
}
/* The NodeFormData object does not set the request header - so, set it */
const contentType =
formData instanceof NodeFormData && typeof FormData === 'undefined'
? `multipart/form-data; boundary=${formData.getBoundary()}`
: undefined
const requestPromise = new Promise((resolve, reject) => {
this.requestClient!.post(
apiUrl,
formData,
authConfig?.access_token,
contentType
)
.then(async (res: any) => {
if (Object.entries(res.result).length < 1) {
throw new JobExecutionError(
0,
`No webout was returned by job ${program}. Please check the SAS log for more info.`,
res.log
)
}
this.requestClient!.appendRequest(res, sasJob, config.debug)
const responseObject = appendExtraResponseAttributes(
res,
extraResponseAttributes
)
resolve(responseObject)
})
.catch(async (e: Error) => {
if (e instanceof JobExecutionError) {
this.requestClient!.appendRequest(e, sasJob, config.debug)
reject(new ErrorResponse(e?.message, e))
}
if (e instanceof LoginRequiredError) {
if (!loginRequiredCallback) {
reject(
new ErrorResponse(
'Request is not authenticated. Make sure .env file exists with valid credentials.',
e
)
)
}
this.appendWaitingRequest(() => {
return this.execute(
sasJob,
data,
config,
loginRequiredCallback,
authConfig,
extraResponseAttributes
).then(
(res: any) => {
resolve(res)
},
(err: any) => {
reject(err)
}
)
})
if (loginCallback) await loginCallback()
} else reject(new ErrorResponse(e?.message, e))
})
})
return requestPromise
}
}

View File

@@ -16,13 +16,10 @@ import { SASViyaApiClient } from '../SASViyaApiClient'
import {
isRelativePath,
parseSasViyaDebugResponse,
appendExtraResponseAttributes,
getValidJson,
SASJS_LOGS_SEPARATOR
appendExtraResponseAttributes
} from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { Server } from 'https'
export interface WaitingRequstPromise {
promise: Promise<any> | null
@@ -122,7 +119,6 @@ export class WebJobExecutor extends BaseJobExecutor {
const stringifiedData = JSON.stringify(data)
if (
config.serverType === ServerType.Sas9 ||
config.serverType === ServerType.Sasjs ||
stringifiedData.length > 500000 ||
stringifiedData.includes(';')
) {
@@ -165,37 +161,9 @@ export class WebJobExecutor extends BaseJobExecutor {
contentType
)
.then(async (res: any) => {
const parsedSasjsLog =
this.serverType === ServerType.Sasjs
? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
: res.result.log
this.requestClient!.appendRequest(res, sasJob, config.debug)
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
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.`,
parsedSasjsLog
)
}
this.requestClient!.appendRequest(resObj, sasJob, config.debug)
let jsonResponse = resObj.result
let jsonResponse = res.result
if (config.debug) {
switch (this.serverType) {
@@ -216,7 +184,7 @@ export class WebJobExecutor extends BaseJobExecutor {
}
const responseObject = appendExtraResponseAttributes(
{ result: jsonResponse, log: parsedSasjsLog },
{ result: jsonResponse, log: res.log },
extraResponseAttributes
)
resolve(responseObject)
@@ -294,39 +262,4 @@ export class WebJobExecutor extends BaseJobExecutor {
}
return uri
}
private getRequestParams(config: any): any {
const requestParams: any = {}
if (config.debug) {
requestParams['_omittextlog'] = 'false'
requestParams['_omitsessionresults'] = 'false'
requestParams['_debug'] = 131
}
return requestParams
}
private parseSAS9ErrorResponse(response: string) {
const logLines = response.split('\n')
const parsedLines: string[] = []
let firstErrorLineIndex: number = -1
logLines.map((line: string, index: number) => {
if (
line.toLowerCase().includes('error') &&
!line.toLowerCase().includes('this request completed with errors.') &&
firstErrorLineIndex === -1
) {
firstErrorLineIndex = index
}
})
for (let i = firstErrorLineIndex - 10; i <= firstErrorLineIndex + 10; i++) {
parsedLines.push(logLines[i])
}
return parsedLines.join(', ')
}
}

View File

@@ -4,3 +4,4 @@ export * from './JesJobExecutor'
export * from './JobExecutor'
export * from './Sas9JobExecutor'
export * from './WebJobExecutor'
export * from './SasjsJobExecutor'