mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-05 11:40:06 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc28dfe7f0 | ||
| 2ec2147245 | |||
| 9ca4732f76 |
46
src/SASjs.ts
46
src/SASjs.ts
@@ -27,6 +27,7 @@ import {
|
|||||||
ComputeJobExecutor,
|
ComputeJobExecutor,
|
||||||
JesJobExecutor,
|
JesJobExecutor,
|
||||||
Sas9JobExecutor,
|
Sas9JobExecutor,
|
||||||
|
SasJsJobExecutor,
|
||||||
FileUploader
|
FileUploader
|
||||||
} from './job-execution'
|
} from './job-execution'
|
||||||
import { ErrorResponse } from './types/errors'
|
import { ErrorResponse } from './types/errors'
|
||||||
@@ -62,6 +63,7 @@ export default class SASjs {
|
|||||||
private computeJobExecutor: JobExecutor | null = null
|
private computeJobExecutor: JobExecutor | null = null
|
||||||
private jesJobExecutor: JobExecutor | null = null
|
private jesJobExecutor: JobExecutor | null = null
|
||||||
private sas9JobExecutor: JobExecutor | null = null
|
private sas9JobExecutor: JobExecutor | null = null
|
||||||
|
private sasJsJobExecutor: JobExecutor | null = null
|
||||||
|
|
||||||
constructor(config?: Partial<SASjsConfig>) {
|
constructor(config?: Partial<SASjsConfig>) {
|
||||||
this.sasjsConfig = {
|
this.sasjsConfig = {
|
||||||
@@ -686,14 +688,35 @@ export default class SASjs {
|
|||||||
// status is true if the data passes validation checks above
|
// status is true if the data passes validation checks above
|
||||||
if (validationResult.status) {
|
if (validationResult.status) {
|
||||||
if (config.serverType === ServerType.Sasjs) {
|
if (config.serverType === ServerType.Sasjs) {
|
||||||
return await this.webJobExecutor!.execute(
|
/**
|
||||||
sasJob,
|
* When sending the JSON data object to SAS, it is first converted to
|
||||||
data,
|
* a set of specially formatted CSVs. These are passed as multi-part
|
||||||
config,
|
* form data (converted to input files in the backend SAS session by the
|
||||||
loginRequiredCallback,
|
* API). When running outside of a browser, the FormData object is not
|
||||||
authConfig,
|
* available. So in this case, a slightly different executor is invoked,
|
||||||
extraResponseAttributes
|
* 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 (
|
} else if (
|
||||||
config.serverType === ServerType.SasViya &&
|
config.serverType === ServerType.SasViya &&
|
||||||
config.useComputeApi !== undefined &&
|
config.useComputeApi !== undefined &&
|
||||||
@@ -1116,6 +1139,13 @@ export default class SASjs {
|
|||||||
this.sasViyaApiClient!
|
this.sasViyaApiClient!
|
||||||
)
|
)
|
||||||
|
|
||||||
|
this.sasJsJobExecutor = new SasJsJobExecutor(
|
||||||
|
this.sasjsConfig.serverUrl,
|
||||||
|
this.sasjsConfig.serverType!,
|
||||||
|
this.jobsPath,
|
||||||
|
this.requestClient
|
||||||
|
)
|
||||||
|
|
||||||
this.sas9JobExecutor = new Sas9JobExecutor(
|
this.sas9JobExecutor = new Sas9JobExecutor(
|
||||||
this.sasjsConfig.serverUrl,
|
this.sasjsConfig.serverUrl,
|
||||||
this.sasjsConfig.serverType!,
|
this.sasjsConfig.serverType!,
|
||||||
|
|||||||
@@ -37,10 +37,7 @@ export class SASjsApiClient {
|
|||||||
}>(
|
}>(
|
||||||
'SASjsApi/drive/deploy',
|
'SASjsApi/drive/deploy',
|
||||||
{ fileTree: members, appLoc: appLoc },
|
{ fileTree: members, appLoc: appLoc },
|
||||||
access_token,
|
access_token
|
||||||
undefined,
|
|
||||||
{},
|
|
||||||
{ maxContentLength: Infinity, maxBodyLength: Infinity }
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return Promise.resolve(result)
|
return Promise.resolve(result)
|
||||||
|
|||||||
@@ -1,19 +1,9 @@
|
|||||||
import * as NodeFormData from 'form-data'
|
|
||||||
import { convertToCSV } from '../utils/convertToCsv'
|
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 = (
|
export const generateFileUploadForm = (
|
||||||
formData: FormData | NodeFormData,
|
formData: FormData,
|
||||||
data: any
|
data: any
|
||||||
): FormData | NodeFormData => {
|
): FormData => {
|
||||||
for (const tableName in data) {
|
for (const tableName in data) {
|
||||||
if (!Array.isArray(data[tableName])) continue
|
if (!Array.isArray(data[tableName])) continue
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
import * as NodeFormData from 'form-data'
|
|
||||||
import { convertToCSV } from '../utils/convertToCsv'
|
import { convertToCSV } from '../utils/convertToCsv'
|
||||||
import { splitChunks } from '../utils/splitChunks'
|
import { splitChunks } from '../utils/splitChunks'
|
||||||
|
|
||||||
export const generateTableUploadForm = (
|
export const generateTableUploadForm = (formData: FormData, data: any) => {
|
||||||
formData: FormData | NodeFormData,
|
|
||||||
data: any
|
|
||||||
) => {
|
|
||||||
const sasjsTables = []
|
const sasjsTables = []
|
||||||
const requestParams: any = {}
|
const requestParams: any = {}
|
||||||
let tableCounter = 0
|
let tableCounter = 0
|
||||||
|
|||||||
131
src/job-execution/SasJsJobExecutor.ts
Normal file
131
src/job-execution/SasJsJobExecutor.ts
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import * as NodeFormData from 'form-data'
|
|
||||||
import {
|
import {
|
||||||
AuthConfig,
|
AuthConfig,
|
||||||
ExtraResponseAttributes,
|
ExtraResponseAttributes,
|
||||||
@@ -109,12 +108,9 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
...this.getRequestParams(config)
|
...this.getRequestParams(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// FormData is only valid in browser
|
||||||
* Use the available form data object (FormData in Browser, NodeFormData in
|
// FormData is a part of JS web API (not included in native NodeJS).
|
||||||
* Node)
|
let formData = new FormData()
|
||||||
*/
|
|
||||||
let formData =
|
|
||||||
typeof FormData === 'undefined' ? new NodeFormData() : new FormData()
|
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
const stringifiedData = JSON.stringify(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) => {
|
const requestPromise = new Promise((resolve, reject) => {
|
||||||
this.requestClient!.post(
|
this.requestClient!.post(apiUrl, formData, authConfig?.access_token)
|
||||||
apiUrl,
|
|
||||||
formData,
|
|
||||||
authConfig?.access_token,
|
|
||||||
contentType
|
|
||||||
)
|
|
||||||
.then(async (res: any) => {
|
.then(async (res: any) => {
|
||||||
const parsedSasjsServerLog =
|
const parsedSasjsServerLog =
|
||||||
this.serverType === ServerType.Sasjs
|
this.serverType === ServerType.Sasjs
|
||||||
|
|||||||
@@ -3,4 +3,5 @@ export * from './FileUploader'
|
|||||||
export * from './JesJobExecutor'
|
export * from './JesJobExecutor'
|
||||||
export * from './JobExecutor'
|
export * from './JobExecutor'
|
||||||
export * from './Sas9JobExecutor'
|
export * from './Sas9JobExecutor'
|
||||||
|
export * from './SasJsJobExecutor'
|
||||||
export * from './WebJobExecutor'
|
export * from './WebJobExecutor'
|
||||||
|
|||||||
@@ -207,8 +207,7 @@ export class RequestClient implements HttpClient {
|
|||||||
data: any,
|
data: any,
|
||||||
accessToken: string | undefined,
|
accessToken: string | undefined,
|
||||||
contentType = 'application/json',
|
contentType = 'application/json',
|
||||||
overrideHeaders: { [key: string]: string | number } = {},
|
overrideHeaders: { [key: string]: string | number } = {}
|
||||||
additionalSettings: { [key: string]: string | number } = {}
|
|
||||||
): Promise<{ result: T; etag: string }> {
|
): Promise<{ result: T; etag: string }> {
|
||||||
const headers = {
|
const headers = {
|
||||||
...this.getHeaders(accessToken, contentType),
|
...this.getHeaders(accessToken, contentType),
|
||||||
@@ -216,11 +215,7 @@ export class RequestClient implements HttpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return this.httpClient
|
return this.httpClient
|
||||||
.post<T>(url, data, {
|
.post<T>(url, data, { headers, withCredentials: true })
|
||||||
headers,
|
|
||||||
withCredentials: true,
|
|
||||||
...additionalSettings
|
|
||||||
})
|
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
throwIfError(response)
|
throwIfError(response)
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ export const getValidJson = (str: string | object): object => {
|
|||||||
|
|
||||||
if (typeof str === 'object') return str
|
if (typeof str === 'object') return str
|
||||||
|
|
||||||
if (str === '') return {}
|
|
||||||
|
|
||||||
return JSON.parse(str)
|
return JSON.parse(str)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof JsonParseArrayError) throw e
|
if (e instanceof JsonParseArrayError) throw e
|
||||||
|
|||||||
Reference in New Issue
Block a user