mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-04 03:00:05 +00:00
chore(imprvements): code changes for fileUploader
This commit is contained in:
@@ -3,21 +3,17 @@ import { UploadFile } from './types/UploadFile'
|
|||||||
import { ErrorResponse, LoginRequiredError } from './types/errors'
|
import { ErrorResponse, LoginRequiredError } from './types/errors'
|
||||||
import { RequestClient } from './request/RequestClient'
|
import { RequestClient } from './request/RequestClient'
|
||||||
import { ServerType } from '@sasjs/utils/types'
|
import { ServerType } from '@sasjs/utils/types'
|
||||||
import SASjs from './SASjs'
|
|
||||||
import { Server } from 'https'
|
|
||||||
import { SASjsConfig } from './types'
|
import { SASjsConfig } from './types'
|
||||||
import { config } from 'process'
|
|
||||||
|
|
||||||
export class FileUploader {
|
export class FileUploader {
|
||||||
constructor(
|
constructor(private jobsPath: string, private requestClient: RequestClient) {}
|
||||||
private sasjsConfig: SASjsConfig,
|
|
||||||
private jobsPath: string,
|
|
||||||
private requestClient: RequestClient
|
|
||||||
) {
|
|
||||||
if (this.sasjsConfig.serverUrl) isUrl(this.sasjsConfig.serverUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
public uploadFile(sasJob: string, files: UploadFile[], params: any) {
|
public async uploadFile(
|
||||||
|
sasJob: string,
|
||||||
|
files: UploadFile[],
|
||||||
|
params: any,
|
||||||
|
config: SASjsConfig
|
||||||
|
) {
|
||||||
if (files?.length < 1)
|
if (files?.length < 1)
|
||||||
return Promise.reject(
|
return Promise.reject(
|
||||||
new ErrorResponse('At least one file must be provided.')
|
new ErrorResponse('At least one file must be provided.')
|
||||||
@@ -33,8 +29,8 @@ export class FileUploader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const program = this.sasjsConfig.appLoc
|
const program = config.appLoc
|
||||||
? this.sasjsConfig.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||||
: sasJob
|
: sasJob
|
||||||
const uploadUrl = `${this.jobsPath}/?${
|
const uploadUrl = `${this.jobsPath}/?${
|
||||||
'_program=' + program
|
'_program=' + program
|
||||||
@@ -48,12 +44,9 @@ export class FileUploader {
|
|||||||
|
|
||||||
const csrfToken = this.requestClient.getCsrfToken('file')
|
const csrfToken = this.requestClient.getCsrfToken('file')
|
||||||
if (csrfToken) formData.append('_csrf', csrfToken.value)
|
if (csrfToken) formData.append('_csrf', csrfToken.value)
|
||||||
if (this.sasjsConfig.debug) formData.append('_debug', '131')
|
if (config.debug) formData.append('_debug', '131')
|
||||||
if (
|
if (config.serverType === ServerType.SasViya && config.contextName)
|
||||||
this.sasjsConfig.serverType === ServerType.SasViya &&
|
formData.append('_contextname', config.contextName)
|
||||||
this.sasjsConfig.contextName
|
|
||||||
)
|
|
||||||
formData.append('_contextname', this.sasjsConfig.contextName)
|
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'cache-control': 'no-cache',
|
'cache-control': 'no-cache',
|
||||||
@@ -66,15 +59,12 @@ export class FileUploader {
|
|||||||
return this.requestClient
|
return this.requestClient
|
||||||
.post(uploadUrl, formData, undefined, 'application/json', headers)
|
.post(uploadUrl, formData, undefined, 'application/json', headers)
|
||||||
.then(async (res) => {
|
.then(async (res) => {
|
||||||
this.requestClient!.appendRequest(res, sasJob, this.sasjsConfig.debug)
|
this.requestClient.appendRequest(res, sasJob, config.debug)
|
||||||
if (
|
if (config.serverType === ServerType.SasViya && config.debug) {
|
||||||
this.sasjsConfig.serverType === ServerType.SasViya &&
|
|
||||||
this.sasjsConfig.debug
|
|
||||||
) {
|
|
||||||
const jsonResponse = await parseSasViyaDebugResponse(
|
const jsonResponse = await parseSasViyaDebugResponse(
|
||||||
res.result as string,
|
res.result as string,
|
||||||
this.requestClient,
|
this.requestClient,
|
||||||
this.sasjsConfig.serverUrl
|
config.serverUrl
|
||||||
)
|
)
|
||||||
return jsonResponse
|
return jsonResponse
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/SASjs.ts
24
src/SASjs.ts
@@ -507,7 +507,7 @@ export default class SASjs {
|
|||||||
...this.sasjsConfig,
|
...this.sasjsConfig,
|
||||||
...config
|
...config
|
||||||
}
|
}
|
||||||
await this.setupConfiguration()
|
this.setupConfiguration()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -573,22 +573,16 @@ export default class SASjs {
|
|||||||
* @param params - request URL parameters.
|
* @param params - request URL parameters.
|
||||||
* @param overrideSasjsConfig - object to override existing config (optional)
|
* @param overrideSasjsConfig - object to override existing config (optional)
|
||||||
*/
|
*/
|
||||||
public uploadFile(
|
public async uploadFile(
|
||||||
sasJob: string,
|
sasJob: string,
|
||||||
files: UploadFile[],
|
files: UploadFile[],
|
||||||
params: any,
|
params: any,
|
||||||
overrideSasjsConfig?: any
|
overrideSasjsConfig?: any
|
||||||
) {
|
) {
|
||||||
const fileUploader = overrideSasjsConfig
|
return await this.fileUploader!.uploadFile(sasJob, files, params, {
|
||||||
? new FileUploader(
|
...this.sasjsConfig,
|
||||||
{ ...this.sasjsConfig, ...overrideSasjsConfig },
|
...overrideSasjsConfig
|
||||||
this.jobsPath,
|
})
|
||||||
this.requestClient!
|
|
||||||
)
|
|
||||||
: this.fileUploader ||
|
|
||||||
new FileUploader(this.sasjsConfig, this.jobsPath, this.requestClient!)
|
|
||||||
|
|
||||||
return fileUploader.uploadFile(sasJob, files, params)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -990,11 +984,7 @@ export default class SASjs {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.fileUploader = new FileUploader(
|
this.fileUploader = new FileUploader(this.jobsPath, this.requestClient)
|
||||||
this.sasjsConfig,
|
|
||||||
this.jobsPath,
|
|
||||||
this.requestClient
|
|
||||||
)
|
|
||||||
|
|
||||||
this.webJobExecutor = new WebJobExecutor(
|
this.webJobExecutor = new WebJobExecutor(
|
||||||
this.sasjsConfig.serverUrl,
|
this.sasjsConfig.serverUrl,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { AuthConfig, ServerType } from '@sasjs/utils/types'
|
import { AuthConfig, ServerType } from '@sasjs/utils/types'
|
||||||
import { SASjsRequest } from '../types'
|
|
||||||
import { ExtraResponseAttributes } from '@sasjs/utils/types'
|
import { ExtraResponseAttributes } from '@sasjs/utils/types'
|
||||||
import { asyncForEach, parseGeneratedCode, parseSourceCode } from '../utils'
|
import { asyncForEach } from '../utils'
|
||||||
|
|
||||||
export type ExecuteFunction = () => Promise<any>
|
export type ExecuteFunction = () => Promise<any>
|
||||||
|
|
||||||
@@ -21,7 +20,6 @@ export abstract class BaseJobExecutor implements JobExecutor {
|
|||||||
constructor(protected serverUrl: string, protected serverType: ServerType) {}
|
constructor(protected serverUrl: string, protected serverType: ServerType) {}
|
||||||
|
|
||||||
private waitingRequests: ExecuteFunction[] = []
|
private waitingRequests: ExecuteFunction[] = []
|
||||||
private requests: SASjsRequest[] = []
|
|
||||||
|
|
||||||
abstract execute(
|
abstract execute(
|
||||||
sasJob: string,
|
sasJob: string,
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
// file upload approach
|
// file upload approach
|
||||||
try {
|
try {
|
||||||
formData = generateFileUploadForm(formData, data)
|
formData = generateFileUploadForm(formData, data)
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
return Promise.reject(new ErrorResponse(e?.message, e))
|
return Promise.reject(new ErrorResponse(e?.message, e))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -100,7 +100,7 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
generateTableUploadForm(formData, data)
|
generateTableUploadForm(formData, data)
|
||||||
formData = newFormData
|
formData = newFormData
|
||||||
requestParams = { ...requestParams, ...params }
|
requestParams = { ...requestParams, ...params }
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
return Promise.reject(new ErrorResponse(e?.message, e))
|
return Promise.reject(new ErrorResponse(e?.message, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ export class RequestClient implements HttpClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public post<T>(
|
public async post<T>(
|
||||||
url: string,
|
url: string,
|
||||||
data: any,
|
data: any,
|
||||||
accessToken: string | undefined,
|
accessToken: string | undefined,
|
||||||
@@ -286,7 +286,7 @@ export class RequestClient implements HttpClient {
|
|||||||
result: response.data,
|
result: response.data,
|
||||||
etag: response.headers['etag'] as string
|
etag: response.headers['etag'] as string
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
const response = e.response as AxiosResponse
|
const response = e.response as AxiosResponse
|
||||||
if (response?.status === 403 || response?.status === 449) {
|
if (response?.status === 403 || response?.status === 449) {
|
||||||
this.parseAndSetFileUploadCsrfToken(response)
|
this.parseAndSetFileUploadCsrfToken(response)
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ describe('FileUploader', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fileUploader = new FileUploader(
|
const fileUploader = new FileUploader(
|
||||||
config,
|
|
||||||
'/jobs/path',
|
'/jobs/path',
|
||||||
new RequestClient('https://sample.server.com')
|
new RequestClient('https://sample.server.com')
|
||||||
)
|
)
|
||||||
@@ -51,7 +50,7 @@ describe('FileUploader', () => {
|
|||||||
Promise.resolve({ data: sampleResponse })
|
Promise.resolve({ data: sampleResponse })
|
||||||
)
|
)
|
||||||
|
|
||||||
const res = await fileUploader.uploadFile(sasJob, files, params)
|
const res = await fileUploader.uploadFile(sasJob, files, params, config)
|
||||||
|
|
||||||
expect(res).toEqual(JSON.parse(sampleResponse))
|
expect(res).toEqual(JSON.parse(sampleResponse))
|
||||||
})
|
})
|
||||||
@@ -62,7 +61,7 @@ describe('FileUploader', () => {
|
|||||||
const params = { table: 'libtable' }
|
const params = { table: 'libtable' }
|
||||||
|
|
||||||
const err = await fileUploader
|
const err = await fileUploader
|
||||||
.uploadFile(sasJob, files, params)
|
.uploadFile(sasJob, files, params, config)
|
||||||
.catch((err: any) => err)
|
.catch((err: any) => err)
|
||||||
expect(err.error.message).toEqual('At least one file must be provided.')
|
expect(err.error.message).toEqual('At least one file must be provided.')
|
||||||
})
|
})
|
||||||
@@ -72,7 +71,7 @@ describe('FileUploader', () => {
|
|||||||
const { files, params } = prepareFilesAndParams()
|
const { files, params } = prepareFilesAndParams()
|
||||||
|
|
||||||
const err = await fileUploader
|
const err = await fileUploader
|
||||||
.uploadFile(sasJob, files, params)
|
.uploadFile(sasJob, files, params, config)
|
||||||
.catch((err: any) => err)
|
.catch((err: any) => err)
|
||||||
expect(err.error.message).toEqual('sasJob must be provided.')
|
expect(err.error.message).toEqual('sasJob must be provided.')
|
||||||
})
|
})
|
||||||
@@ -86,7 +85,7 @@ describe('FileUploader', () => {
|
|||||||
const { files, params } = prepareFilesAndParams()
|
const { files, params } = prepareFilesAndParams()
|
||||||
|
|
||||||
const err = await fileUploader
|
const err = await fileUploader
|
||||||
.uploadFile(sasJob, files, params)
|
.uploadFile(sasJob, files, params, config)
|
||||||
.catch((err: any) => err)
|
.catch((err: any) => err)
|
||||||
expect(err.error.message).toEqual('You must be logged in to upload a file.')
|
expect(err.error.message).toEqual('You must be logged in to upload a file.')
|
||||||
})
|
})
|
||||||
@@ -100,7 +99,7 @@ describe('FileUploader', () => {
|
|||||||
const { files, params } = prepareFilesAndParams()
|
const { files, params } = prepareFilesAndParams()
|
||||||
|
|
||||||
const err = await fileUploader
|
const err = await fileUploader
|
||||||
.uploadFile(sasJob, files, params)
|
.uploadFile(sasJob, files, params, config)
|
||||||
.catch((err: any) => err)
|
.catch((err: any) => err)
|
||||||
expect(err.error.message).toEqual('File upload request failed.')
|
expect(err.error.message).toEqual('File upload request failed.')
|
||||||
})
|
})
|
||||||
@@ -114,7 +113,7 @@ describe('FileUploader', () => {
|
|||||||
const { files, params } = prepareFilesAndParams()
|
const { files, params } = prepareFilesAndParams()
|
||||||
|
|
||||||
const err = await fileUploader
|
const err = await fileUploader
|
||||||
.uploadFile(sasJob, files, params)
|
.uploadFile(sasJob, files, params, config)
|
||||||
.catch((err: any) => err)
|
.catch((err: any) => err)
|
||||||
expect(err.error.message).toEqual('File upload request failed.')
|
expect(err.error.message).toEqual('File upload request failed.')
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user