mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-18 17:40:06 +00:00
fix: extraResponseAttributes for WebJobExecutor + sasjs-tests
This commit is contained in:
@@ -49,7 +49,7 @@ export const basicTests = (
|
|||||||
|
|
||||||
const newAdapterIns = new SASjs(adapter.getSasjsConfig())
|
const newAdapterIns = new SASjs(adapter.getSasjsConfig())
|
||||||
|
|
||||||
return newAdapterIns.checkSession()
|
return await newAdapterIns.checkSession()
|
||||||
},
|
},
|
||||||
assertion: (response: any) =>
|
assertion: (response: any) =>
|
||||||
response?.isLoggedIn && response?.userName === userName
|
response?.isLoggedIn && response?.userName === userName
|
||||||
@@ -61,7 +61,7 @@ export const basicTests = (
|
|||||||
test: async () => {
|
test: async () => {
|
||||||
await adapter.logOut()
|
await adapter.logOut()
|
||||||
await adapter.logIn('invalid', 'invalid')
|
await adapter.logIn('invalid', 'invalid')
|
||||||
return adapter.logIn(userName, password)
|
return await adapter.logIn(userName, password)
|
||||||
},
|
},
|
||||||
assertion: (response: any) =>
|
assertion: (response: any) =>
|
||||||
response && response.isLoggedIn && response.userName === userName
|
response && response.isLoggedIn && response.userName === userName
|
||||||
@@ -164,7 +164,7 @@ export const basicTests = (
|
|||||||
description:
|
description:
|
||||||
'Should complete successful request with extra attributes present in response',
|
'Should complete successful request with extra attributes present in response',
|
||||||
test: async () => {
|
test: async () => {
|
||||||
const config = {
|
const config: Partial<SASjsConfig> = {
|
||||||
useComputeApi: false
|
useComputeApi: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
} from '../types/errors'
|
} from '../types/errors'
|
||||||
import { ExtraResponseAttributes } from '@sasjs/utils/types'
|
import { ExtraResponseAttributes } from '@sasjs/utils/types'
|
||||||
import { BaseJobExecutor } from './JobExecutor'
|
import { BaseJobExecutor } from './JobExecutor'
|
||||||
|
import { appendExtraResponseAttributes } from '../utils'
|
||||||
|
|
||||||
export class JesJobExecutor extends BaseJobExecutor {
|
export class JesJobExecutor extends BaseJobExecutor {
|
||||||
constructor(serverUrl: string, private sasViyaApiClient: SASViyaApiClient) {
|
constructor(serverUrl: string, private sasViyaApiClient: SASViyaApiClient) {
|
||||||
@@ -29,21 +30,10 @@ export class JesJobExecutor extends BaseJobExecutor {
|
|||||||
.then((response: any) => {
|
.then((response: any) => {
|
||||||
this.appendRequest(response, sasJob, config.debug)
|
this.appendRequest(response, sasJob, config.debug)
|
||||||
|
|
||||||
let responseObject = {}
|
const responseObject = appendExtraResponseAttributes(
|
||||||
|
response,
|
||||||
if (extraResponseAttributes && extraResponseAttributes.length > 0) {
|
extraResponseAttributes
|
||||||
const extraAttributes = extraResponseAttributes.reduce(
|
)
|
||||||
(map: any, obj: any) => ((map[obj] = response[obj]), map),
|
|
||||||
{}
|
|
||||||
)
|
|
||||||
|
|
||||||
responseObject = {
|
|
||||||
result: response.result,
|
|
||||||
...extraAttributes
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
responseObject = response.result
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(responseObject)
|
resolve(responseObject)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import { ServerType } from '@sasjs/utils/types'
|
import {
|
||||||
|
AuthConfig,
|
||||||
|
ExtraResponseAttributes,
|
||||||
|
ServerType
|
||||||
|
} from '@sasjs/utils/types'
|
||||||
import {
|
import {
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
JobExecutionError,
|
JobExecutionError,
|
||||||
@@ -12,7 +16,8 @@ import { SASViyaApiClient } from '../SASViyaApiClient'
|
|||||||
import {
|
import {
|
||||||
isRelativePath,
|
isRelativePath,
|
||||||
getValidJson,
|
getValidJson,
|
||||||
parseSasViyaDebugResponse
|
parseSasViyaDebugResponse,
|
||||||
|
appendExtraResponseAttributes
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
import { BaseJobExecutor } from './JobExecutor'
|
import { BaseJobExecutor } from './JobExecutor'
|
||||||
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||||
@@ -37,7 +42,9 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
sasJob: string,
|
sasJob: string,
|
||||||
data: any,
|
data: any,
|
||||||
config: any,
|
config: any,
|
||||||
loginRequiredCallback?: any
|
loginRequiredCallback?: any,
|
||||||
|
authConfig?: AuthConfig,
|
||||||
|
extraResponseAttributes: ExtraResponseAttributes[] = []
|
||||||
) {
|
) {
|
||||||
const loginCallback = loginRequiredCallback || (() => Promise.resolve())
|
const loginCallback = loginRequiredCallback || (() => Promise.resolve())
|
||||||
const program = isRelativePath(sasJob)
|
const program = isRelativePath(sasJob)
|
||||||
@@ -113,27 +120,33 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
const requestPromise = new Promise((resolve, reject) => {
|
const requestPromise = new Promise((resolve, reject) => {
|
||||||
this.requestClient!.post(apiUrl, formData, undefined)
|
this.requestClient!.post(apiUrl, formData, undefined)
|
||||||
.then(async (res: any) => {
|
.then(async (res: any) => {
|
||||||
if (this.serverType === ServerType.SasViya && config.debug) {
|
let jsonResponse = res.result
|
||||||
const jsonResponse = await parseSasViyaDebugResponse(
|
|
||||||
res.result,
|
|
||||||
this.requestClient,
|
|
||||||
this.serverUrl
|
|
||||||
)
|
|
||||||
this.appendRequest(res, sasJob, config.debug)
|
|
||||||
resolve(jsonResponse)
|
|
||||||
}
|
|
||||||
if (this.serverType === ServerType.Sas9 && config.debug) {
|
|
||||||
let jsonResponse = res.result
|
|
||||||
if (typeof res.result === 'string')
|
|
||||||
jsonResponse = parseWeboutResponse(res.result, apiUrl)
|
|
||||||
|
|
||||||
getValidJson(jsonResponse)
|
if (config.debug) {
|
||||||
this.appendRequest(res, sasJob, config.debug)
|
switch (this.serverType) {
|
||||||
resolve(res.result)
|
case ServerType.SasViya:
|
||||||
|
jsonResponse = await parseSasViyaDebugResponse(
|
||||||
|
res.result,
|
||||||
|
this.requestClient,
|
||||||
|
this.serverUrl
|
||||||
|
)
|
||||||
|
break
|
||||||
|
case ServerType.Sas9:
|
||||||
|
jsonResponse =
|
||||||
|
typeof res.result === 'string'
|
||||||
|
? parseWeboutResponse(res.result, apiUrl)
|
||||||
|
: res.result
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.appendRequest(res, sasJob, config.debug)
|
this.appendRequest(res, sasJob, config.debug)
|
||||||
getValidJson(res.result as string)
|
|
||||||
resolve(res.result)
|
const responseObject = appendExtraResponseAttributes(
|
||||||
|
{ result: jsonResponse },
|
||||||
|
extraResponseAttributes
|
||||||
|
)
|
||||||
|
resolve(responseObject)
|
||||||
})
|
})
|
||||||
.catch(async (e: Error) => {
|
.catch(async (e: Error) => {
|
||||||
if (e instanceof JobExecutionError) {
|
if (e instanceof JobExecutionError) {
|
||||||
@@ -150,7 +163,9 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
sasJob,
|
sasJob,
|
||||||
data,
|
data,
|
||||||
config,
|
config,
|
||||||
loginRequiredCallback
|
loginRequiredCallback,
|
||||||
|
authConfig,
|
||||||
|
extraResponseAttributes
|
||||||
).then(
|
).then(
|
||||||
(res: any) => {
|
(res: any) => {
|
||||||
resolve(res)
|
resolve(res)
|
||||||
|
|||||||
22
src/utils/appendExtraResponseAttributes.ts
Normal file
22
src/utils/appendExtraResponseAttributes.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { ExtraResponseAttributes } from '@sasjs/utils/types'
|
||||||
|
|
||||||
|
export async function appendExtraResponseAttributes(
|
||||||
|
response: any,
|
||||||
|
extraResponseAttributes: ExtraResponseAttributes[]
|
||||||
|
) {
|
||||||
|
let responseObject = {}
|
||||||
|
|
||||||
|
if (extraResponseAttributes?.length) {
|
||||||
|
const extraAttributes = extraResponseAttributes.reduce(
|
||||||
|
(map: any, obj: any) => ((map[obj] = response[obj]), map),
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
responseObject = {
|
||||||
|
result: response.result,
|
||||||
|
...extraAttributes
|
||||||
|
}
|
||||||
|
} else responseObject = response.result
|
||||||
|
|
||||||
|
return responseObject
|
||||||
|
}
|
||||||
@@ -15,3 +15,4 @@ export * from './parseWeboutResponse'
|
|||||||
export * from './fetchLogByChunks'
|
export * from './fetchLogByChunks'
|
||||||
export * from './getValidJson'
|
export * from './getValidJson'
|
||||||
export * from './parseViyaDebugResponse'
|
export * from './parseViyaDebugResponse'
|
||||||
|
export * from './appendExtraResponseAttributes'
|
||||||
|
|||||||
Reference in New Issue
Block a user