1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-03 10:40:06 +00:00

Compare commits

..

3 Commits

Author SHA1 Message Date
Allan Bowe
cc28dfe7f0 fix: tidying up logic and adding descriptive comments 2022-02-21 19:40:24 +00:00
2ec2147245 chore: add detailed comment 2022-02-21 23:22:57 +05:00
9ca4732f76 fix: call sasJsJobExecutor only from cli 2022-02-21 23:08:55 +05:00
9 changed files with 180 additions and 57 deletions

View File

@@ -27,6 +27,7 @@ import {
ComputeJobExecutor,
JesJobExecutor,
Sas9JobExecutor,
SasJsJobExecutor,
FileUploader
} from './job-execution'
import { ErrorResponse } from './types/errors'
@@ -62,6 +63,7 @@ export default class SASjs {
private computeJobExecutor: JobExecutor | null = null
private jesJobExecutor: JobExecutor | null = null
private sas9JobExecutor: JobExecutor | null = null
private sasJsJobExecutor: JobExecutor | null = null
constructor(config?: Partial<SASjsConfig>) {
this.sasjsConfig = {
@@ -686,14 +688,35 @@ export default class SASjs {
// status is true if the data passes validation checks above
if (validationResult.status) {
if (config.serverType === ServerType.Sasjs) {
return await this.webJobExecutor!.execute(
sasJob,
data,
config,
loginRequiredCallback,
authConfig,
extraResponseAttributes
)
/**
* When sending the JSON data object to SAS, it is first converted to
* a set of specially formatted CSVs. These are passed as multi-part
* form data (converted to input files in the backend SAS session by the
* API). When running outside of a browser, the FormData object is not
* available. So in this case, a slightly different executor is invoked,
* which is similar to the sas9JobExecutor.
*/
if (typeof FormData === 'undefined') {
// cli invocation
return await this.sasJsJobExecutor!.execute(
sasJob,
data,
config,
loginRequiredCallback,
authConfig,
extraResponseAttributes
)
} else {
// web invocation
return await this.webJobExecutor!.execute(
sasJob,
data,
config,
loginRequiredCallback,
authConfig,
extraResponseAttributes
)
}
} else if (
config.serverType === ServerType.SasViya &&
config.useComputeApi !== undefined &&
@@ -1116,6 +1139,13 @@ export default class SASjs {
this.sasViyaApiClient!
)
this.sasJsJobExecutor = new SasJsJobExecutor(
this.sasjsConfig.serverUrl,
this.sasjsConfig.serverType!,
this.jobsPath,
this.requestClient
)
this.sas9JobExecutor = new Sas9JobExecutor(
this.sasjsConfig.serverUrl,
this.sasjsConfig.serverType!,

View File

@@ -37,10 +37,7 @@ export class SASjsApiClient {
}>(
'SASjsApi/drive/deploy',
{ fileTree: members, appLoc: appLoc },
access_token,
undefined,
{},
{ maxContentLength: Infinity, maxBodyLength: Infinity }
access_token
)
return Promise.resolve(result)

View File

@@ -1,19 +1,9 @@
import * as NodeFormData from 'form-data'
import { convertToCSV } from '../utils/convertToCsv'
/**
* One of the approaches SASjs takes to send tables-formatted JSON (see README)
* to SAS is as multipart form data, where each table is provided as a specially
* formatted CSV file.
* @param formData Different objects are used depending on whether the adapter is
* running in the browser, or in the CLI
* @param data Special, tables-formatted JSON (see README)
* @returns Populated formData
*/
export const generateFileUploadForm = (
formData: FormData | NodeFormData,
formData: FormData,
data: any
): FormData | NodeFormData => {
): FormData => {
for (const tableName in data) {
if (!Array.isArray(data[tableName])) continue

View File

@@ -1,11 +1,7 @@
import * as NodeFormData from 'form-data'
import { convertToCSV } from '../utils/convertToCsv'
import { splitChunks } from '../utils/splitChunks'
export const generateTableUploadForm = (
formData: FormData | NodeFormData,
data: any
) => {
export const generateTableUploadForm = (formData: FormData, data: any) => {
const sasjsTables = []
const requestParams: any = {}
let tableCounter = 0

View File

@@ -0,0 +1,131 @@
import {
AuthConfig,
ExtraResponseAttributes,
ServerType
} from '@sasjs/utils/types'
import {
ErrorResponse,
JobExecutionError,
LoginRequiredError
} from '../types/errors'
import { RequestClient } from '../request/RequestClient'
import {
isRelativePath,
appendExtraResponseAttributes,
getValidJson
} from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
export class SasJsJobExecutor extends BaseJobExecutor {
constructor(
serverUrl: string,
serverType: ServerType,
private jobsPath: string,
private requestClient: RequestClient
) {
super(serverUrl, serverType)
}
async execute(
sasJob: string,
data: any,
config: any,
loginRequiredCallback?: any,
authConfig?: AuthConfig,
extraResponseAttributes: ExtraResponseAttributes[] = []
) {
const loginCallback = loginRequiredCallback || (() => Promise.resolve())
const program = isRelativePath(sasJob)
? config.appLoc
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
: sasJob
: sasJob
let apiUrl = `${config.serverUrl}${this.jobsPath}/?${'_program=' + program}`
const requestParams = this.getRequestParams(config)
const requestPromise = new Promise((resolve, reject) => {
this.requestClient!.post(
apiUrl,
{ ...requestParams, ...data },
authConfig?.access_token
)
.then(async (res: any) => {
const parsedSasjsServerLog = res.result.log
.map((logLine: any) => logLine.line)
.join('\n')
const resObj = {
result: res.result._webout,
log: parsedSasjsServerLog
}
this.requestClient!.appendRequest(resObj, sasJob, config.debug)
let jsonResponse = res.result
if (config.debug) {
if (typeof res.result._webout === 'object') {
jsonResponse = res.result._webout
} else {
const webout = parseWeboutResponse(res.result._webout, apiUrl)
jsonResponse = getValidJson(webout)
}
} else {
jsonResponse = getValidJson(res.result._webout)
}
const responseObject = appendExtraResponseAttributes(
{ result: jsonResponse },
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) {
this.appendWaitingRequest(() => {
return this.execute(
sasJob,
data,
config,
loginRequiredCallback,
authConfig,
extraResponseAttributes
).then(
(res: any) => {
resolve(res)
},
(err: any) => {
reject(err)
}
)
})
await loginCallback()
} else {
reject(new ErrorResponse(e?.message, e))
}
})
})
return requestPromise
}
private getRequestParams(config: any): any {
const requestParams: any = {}
if (config.debug) {
requestParams['_omittextlog'] = 'false'
requestParams['_omitsessionresults'] = 'false'
requestParams['_debug'] = 131
}
return requestParams
}
}

View File

@@ -1,4 +1,3 @@
import * as NodeFormData from 'form-data'
import {
AuthConfig,
ExtraResponseAttributes,
@@ -109,12 +108,9 @@ export class WebJobExecutor extends BaseJobExecutor {
...this.getRequestParams(config)
}
/**
* Use the available form data object (FormData in Browser, NodeFormData in
* Node)
*/
let formData =
typeof FormData === 'undefined' ? new NodeFormData() : new FormData()
// FormData is only valid in browser
// FormData is a part of JS web API (not included in native NodeJS).
let formData = new FormData()
if (data) {
const stringifiedData = JSON.stringify(data)
@@ -149,19 +145,8 @@ export class WebJobExecutor extends BaseJobExecutor {
}
}
/* 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
)
this.requestClient!.post(apiUrl, formData, authConfig?.access_token)
.then(async (res: any) => {
const parsedSasjsServerLog =
this.serverType === ServerType.Sasjs

View File

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

View File

@@ -207,8 +207,7 @@ export class RequestClient implements HttpClient {
data: any,
accessToken: string | undefined,
contentType = 'application/json',
overrideHeaders: { [key: string]: string | number } = {},
additionalSettings: { [key: string]: string | number } = {}
overrideHeaders: { [key: string]: string | number } = {}
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
@@ -216,11 +215,7 @@ export class RequestClient implements HttpClient {
}
return this.httpClient
.post<T>(url, data, {
headers,
withCredentials: true,
...additionalSettings
})
.post<T>(url, data, { headers, withCredentials: true })
.then((response) => {
throwIfError(response)

View File

@@ -12,8 +12,6 @@ export const getValidJson = (str: string | object): object => {
if (typeof str === 'object') return str
if (str === '') return {}
return JSON.parse(str)
} catch (e) {
if (e instanceof JsonParseArrayError) throw e