mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
Merge pull request #823 from sasjs/@sasjs/server-response-fix
feat(sasjs-request-client): improved parseResponse method
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import {
|
||||
getValidJson,
|
||||
parseSasViyaDebugResponse,
|
||||
parseWeboutResponse,
|
||||
SASJS_LOGS_SEPARATOR
|
||||
parseWeboutResponse
|
||||
} from '../utils'
|
||||
import { UploadFile } from '../types/UploadFile'
|
||||
import {
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { RequestClient } from './RequestClient'
|
||||
import { AxiosResponse } from 'axios'
|
||||
import { SASJS_LOGS_SEPARATOR } from '../utils'
|
||||
|
||||
interface SasjsParsedResponse<T> {
|
||||
export interface SasjsParsedResponse<T> {
|
||||
result: T
|
||||
log: string
|
||||
etag: string
|
||||
@@ -45,13 +44,30 @@ export class SasjsRequestClient extends RequestClient {
|
||||
}
|
||||
} catch {
|
||||
if (response.data.includes(SASJS_LOGS_SEPARATOR)) {
|
||||
const splittedResponse = response.data.split(SASJS_LOGS_SEPARATOR)
|
||||
const { data } = response
|
||||
const splittedResponse: string[] = data.split(SASJS_LOGS_SEPARATOR)
|
||||
|
||||
webout = splittedResponse[0]
|
||||
webout = splittedResponse.splice(0, 1)[0]
|
||||
if (webout !== undefined) parsedResponse = webout
|
||||
|
||||
log = splittedResponse[1]
|
||||
printOutput = splittedResponse[2]
|
||||
// log can contain nested logs
|
||||
const logs = splittedResponse.splice(0, splittedResponse.length - 1)
|
||||
|
||||
// tests if string ends with SASJS_LOGS_SEPARATOR
|
||||
const endingWithLogSepRegExp = new RegExp(`${SASJS_LOGS_SEPARATOR}$`)
|
||||
|
||||
// at this point splittedResponse can contain only one item
|
||||
const lastChunk = splittedResponse[0]
|
||||
|
||||
if (lastChunk) {
|
||||
// if the last chunk doesn't end with SASJS_LOGS_SEPARATOR, then it is a printOutput
|
||||
// else the last chunk is part of the log and has to be joined
|
||||
if (!endingWithLogSepRegExp.test(data)) printOutput = lastChunk
|
||||
else if (logs.length > 1) logs.push(lastChunk)
|
||||
}
|
||||
|
||||
// join logs into single log with SASJS_LOGS_SEPARATOR
|
||||
log = logs.join(SASJS_LOGS_SEPARATOR)
|
||||
} else {
|
||||
parsedResponse = response.data
|
||||
}
|
||||
@@ -59,7 +75,7 @@ export class SasjsRequestClient extends RequestClient {
|
||||
|
||||
const returnResult: SasjsParsedResponse<T> = {
|
||||
result: parsedResponse as T,
|
||||
log,
|
||||
log: log || '',
|
||||
etag,
|
||||
status: response.status
|
||||
}
|
||||
@@ -69,3 +85,6 @@ export class SasjsRequestClient extends RequestClient {
|
||||
return returnResult
|
||||
}
|
||||
}
|
||||
|
||||
export const SASJS_LOGS_SEPARATOR =
|
||||
'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'
|
||||
|
||||
172
src/request/spec/SasjsRequestClient.spec.ts
Normal file
172
src/request/spec/SasjsRequestClient.spec.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import {
|
||||
SASJS_LOGS_SEPARATOR,
|
||||
SasjsRequestClient,
|
||||
SasjsParsedResponse
|
||||
} from '../SasjsRequestClient'
|
||||
import { AxiosResponse } from 'axios'
|
||||
|
||||
describe('SasjsRequestClient', () => {
|
||||
const requestClient = new SasjsRequestClient('')
|
||||
const etag = 'etag'
|
||||
const status = 200
|
||||
|
||||
const webout = `hello`
|
||||
const log = `1 The SAS System Tuesday, 25 July 2023 12:51:00
|
||||
|
||||
|
||||
PROC MIGRATE will preserve current SAS file attributes and is
|
||||
recommended for converting all your SAS libraries from any
|
||||
SAS 8 release to SAS 9. For details and examples, please see
|
||||
http://support.sas.com/rnd/migration/index.html
|
||||
|
||||
|
||||
|
||||
NOTE: SAS initialization used:
|
||||
real time 0.01 seconds
|
||||
cpu time 0.02 seconds
|
||||
|
||||
|
||||
`
|
||||
const printOutput = 'printOutPut'
|
||||
|
||||
describe('parseResponse', () => {})
|
||||
|
||||
it('should parse response with 1 log', () => {
|
||||
const response: AxiosResponse<any> = {
|
||||
data: `${webout}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${log}
|
||||
${SASJS_LOGS_SEPARATOR}`,
|
||||
status,
|
||||
statusText: 'ok',
|
||||
headers: { etag },
|
||||
config: {}
|
||||
}
|
||||
|
||||
const expectedParsedResponse: SasjsParsedResponse<string> = {
|
||||
result: `${webout}
|
||||
`,
|
||||
log: `
|
||||
${log}
|
||||
`,
|
||||
etag,
|
||||
status
|
||||
}
|
||||
|
||||
expect(requestClient['parseResponse'](response)).toEqual(
|
||||
expectedParsedResponse
|
||||
)
|
||||
})
|
||||
|
||||
it('should parse response with 1 log and printOutput', () => {
|
||||
const response: AxiosResponse<any> = {
|
||||
data: `${webout}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${log}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${printOutput}`,
|
||||
status,
|
||||
statusText: 'ok',
|
||||
headers: { etag },
|
||||
config: {}
|
||||
}
|
||||
|
||||
const expectedParsedResponse: SasjsParsedResponse<string> = {
|
||||
result: `${webout}
|
||||
`,
|
||||
log: `
|
||||
${log}
|
||||
`,
|
||||
etag,
|
||||
status,
|
||||
printOutput: `
|
||||
${printOutput}`
|
||||
}
|
||||
|
||||
expect(requestClient['parseResponse'](response)).toEqual(
|
||||
expectedParsedResponse
|
||||
)
|
||||
})
|
||||
|
||||
it('should parse response with nested logs', () => {
|
||||
const logWithNestedLog = `root log start
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${log}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
root log end`
|
||||
|
||||
const response: AxiosResponse<any> = {
|
||||
data: `${webout}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${logWithNestedLog}
|
||||
${SASJS_LOGS_SEPARATOR}`,
|
||||
status,
|
||||
statusText: 'ok',
|
||||
headers: { etag },
|
||||
config: {}
|
||||
}
|
||||
|
||||
const expectedParsedResponse: SasjsParsedResponse<string> = {
|
||||
result: `${webout}
|
||||
`,
|
||||
log: `
|
||||
${logWithNestedLog}
|
||||
`,
|
||||
etag,
|
||||
status
|
||||
}
|
||||
|
||||
expect(requestClient['parseResponse'](response)).toEqual(
|
||||
expectedParsedResponse
|
||||
)
|
||||
})
|
||||
|
||||
it('should parse response with nested logs and printOutput', () => {
|
||||
const logWithNestedLog = `root log start
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${log}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
log with indentation
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${log}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
some SAS code containing ${SASJS_LOGS_SEPARATOR}
|
||||
root log end`
|
||||
|
||||
const response: AxiosResponse<any> = {
|
||||
data: `${webout}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${logWithNestedLog}
|
||||
${SASJS_LOGS_SEPARATOR}
|
||||
${printOutput}`,
|
||||
status,
|
||||
statusText: 'ok',
|
||||
headers: { etag },
|
||||
config: {}
|
||||
}
|
||||
|
||||
const expectedParsedResponse: SasjsParsedResponse<string> = {
|
||||
result: `${webout}
|
||||
`,
|
||||
log: `
|
||||
${logWithNestedLog}
|
||||
`,
|
||||
etag,
|
||||
status,
|
||||
printOutput: `
|
||||
${printOutput}`
|
||||
}
|
||||
|
||||
expect(requestClient['parseResponse'](response)).toEqual(
|
||||
expectedParsedResponse
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('SASJS_LOGS_SEPARATOR', () => {
|
||||
it('SASJS_LOGS_SEPARATOR should be hardcoded', () => {
|
||||
expect(SASJS_LOGS_SEPARATOR).toEqual(
|
||||
'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1,2 +0,0 @@
|
||||
export const SASJS_LOGS_SEPARATOR =
|
||||
'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'
|
||||
@@ -2,7 +2,6 @@ export * from './appendExtraResponseAttributes'
|
||||
export * from './asyncForEach'
|
||||
export * from './compareTimestamps'
|
||||
export * from './convertToCsv'
|
||||
export * from './constants'
|
||||
export * from './createAxiosInstance'
|
||||
export * from './delay'
|
||||
export * from './fetchLogByChunks'
|
||||
|
||||
Reference in New Issue
Block a user