mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-13 23:20:05 +00:00
feat(file-upload): move functionality into FileUploader
This commit is contained in:
95
src/FileUploader.ts
Normal file
95
src/FileUploader.ts
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import { isLogInRequired, needsRetry } from "./utils";
|
||||||
|
import { CsrfToken } from "./types/CsrfToken";
|
||||||
|
|
||||||
|
const requestRetryLimit = 5;
|
||||||
|
|
||||||
|
export class FileUploader {
|
||||||
|
constructor(
|
||||||
|
private appLoc: string,
|
||||||
|
private serverUrl: string,
|
||||||
|
private jobsPath: string,
|
||||||
|
private csrfToken: CsrfToken | null = null
|
||||||
|
) {}
|
||||||
|
private retryCount = 0;
|
||||||
|
|
||||||
|
public uploadFile(sasJob: string, file: File, fileName: string, params: any) {
|
||||||
|
if (!file) throw new Error("File must be provided");
|
||||||
|
if (!fileName) throw new Error("File name must be provided");
|
||||||
|
|
||||||
|
let paramsString = "";
|
||||||
|
|
||||||
|
for (let param in params) {
|
||||||
|
if (params.hasOwnProperty(param)) {
|
||||||
|
paramsString += `&${param}=${params[param]}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const program = this.appLoc
|
||||||
|
? this.appLoc.replace(/\/?$/, "/") + sasJob.replace(/^\//, "")
|
||||||
|
: sasJob;
|
||||||
|
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||||
|
"_program=" + program
|
||||||
|
}${paramsString}`;
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
"cache-control": "no-cache",
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
formData.append("file", file);
|
||||||
|
formData.append("filename", fileName);
|
||||||
|
if (this.csrfToken) formData.append("_csrf", this.csrfToken.value);
|
||||||
|
|
||||||
|
fetch(uploadUrl, {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
referrerPolicy: "same-origin",
|
||||||
|
headers,
|
||||||
|
})
|
||||||
|
.then(async (response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
if (response.status === 403) {
|
||||||
|
const tokenHeader = response.headers.get("X-CSRF-HEADER");
|
||||||
|
|
||||||
|
if (tokenHeader) {
|
||||||
|
const token = response.headers.get(tokenHeader);
|
||||||
|
this.csrfToken = {
|
||||||
|
headerName: tokenHeader,
|
||||||
|
value: token || "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.text();
|
||||||
|
})
|
||||||
|
.then((responseText) => {
|
||||||
|
if (isLogInRequired(responseText))
|
||||||
|
reject("You must be logged in to upload a fle");
|
||||||
|
|
||||||
|
if (needsRetry(responseText)) {
|
||||||
|
if (this.retryCount < requestRetryLimit) {
|
||||||
|
this.retryCount++;
|
||||||
|
this.uploadFile(sasJob, file, fileName, params).then(
|
||||||
|
(res: any) => resolve(res),
|
||||||
|
(err: any) => reject(err)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
this.retryCount = 0;
|
||||||
|
reject(responseText);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.retryCount = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
resolve(JSON.parse(responseText));
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
99
src/SASjs.ts
99
src/SASjs.ts
@@ -24,6 +24,7 @@ import {
|
|||||||
} from "./types";
|
} from "./types";
|
||||||
import { SASViyaApiClient } from "./SASViyaApiClient";
|
import { SASViyaApiClient } from "./SASViyaApiClient";
|
||||||
import { SAS9ApiClient } from "./SAS9ApiClient";
|
import { SAS9ApiClient } from "./SAS9ApiClient";
|
||||||
|
import { FileUploader } from "./FileUploader";
|
||||||
|
|
||||||
const defaultConfig: SASjsConfig = {
|
const defaultConfig: SASjsConfig = {
|
||||||
serverUrl: "",
|
serverUrl: "",
|
||||||
@@ -50,12 +51,12 @@ export default class SASjs {
|
|||||||
private csrfTokenApi: CsrfToken | null = null;
|
private csrfTokenApi: CsrfToken | null = null;
|
||||||
private csrfTokenWeb: CsrfToken | null = null;
|
private csrfTokenWeb: CsrfToken | null = null;
|
||||||
private retryCountRequest: number = 0;
|
private retryCountRequest: number = 0;
|
||||||
private retryCountFileUpload: number = 0;
|
|
||||||
private sasjsRequests: SASjsRequest[] = [];
|
private sasjsRequests: SASjsRequest[] = [];
|
||||||
private sasjsWaitingRequests: SASjsWaitingRequest[] = [];
|
private sasjsWaitingRequests: SASjsWaitingRequest[] = [];
|
||||||
private userName: string = "";
|
private userName: string = "";
|
||||||
private sasViyaApiClient: SASViyaApiClient | null = null;
|
private sasViyaApiClient: SASViyaApiClient | null = null;
|
||||||
private sas9ApiClient: SAS9ApiClient | null = null;
|
private sas9ApiClient: SAS9ApiClient | null = null;
|
||||||
|
private fileUploader: FileUploader | null = null;
|
||||||
|
|
||||||
constructor(config?: any) {
|
constructor(config?: any) {
|
||||||
this.sasjsConfig = {
|
this.sasjsConfig = {
|
||||||
@@ -383,83 +384,15 @@ export default class SASjs {
|
|||||||
* @param params - Request URL paramaters
|
* @param params - Request URL paramaters
|
||||||
*/
|
*/
|
||||||
public uploadFile(sasJob: string, file: File, fileName: string, params: any) {
|
public uploadFile(sasJob: string, file: File, fileName: string, params: any) {
|
||||||
if (!file) throw new Error("File must be provided");
|
const fileUploader =
|
||||||
if (!fileName) throw new Error("File name must be provided");
|
this.fileUploader ||
|
||||||
|
new FileUploader(
|
||||||
let paramsString = "";
|
this.sasjsConfig.appLoc,
|
||||||
|
this.sasjsConfig.serverUrl,
|
||||||
for (let param in params) {
|
this.jobsPath,
|
||||||
if (params.hasOwnProperty(param)) {
|
this.csrfTokenWeb
|
||||||
paramsString += `&${param}=${params[param]}`;
|
);
|
||||||
}
|
return fileUploader.uploadFile(sasJob, file, fileName, params);
|
||||||
}
|
|
||||||
|
|
||||||
const program = this.sasjsConfig.appLoc
|
|
||||||
? this.sasjsConfig.appLoc.replace(/\/?$/, "/") + sasJob.replace(/^\//, "")
|
|
||||||
: sasJob;
|
|
||||||
const uploadUrl = `${this.sasjsConfig.serverUrl}${this.jobsPath}/?${'_program=' + program}${paramsString}`;
|
|
||||||
|
|
||||||
let headers = {
|
|
||||||
"cache-control": "no-cache"
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let formData = new FormData();
|
|
||||||
|
|
||||||
formData.append("file", file)
|
|
||||||
formData.append("filename", fileName)
|
|
||||||
if (this.csrfTokenWeb) formData.append('_csrf', this.csrfTokenWeb.value)
|
|
||||||
|
|
||||||
fetch(uploadUrl, {
|
|
||||||
method: "POST",
|
|
||||||
body: formData,
|
|
||||||
referrerPolicy: "same-origin",
|
|
||||||
headers
|
|
||||||
})
|
|
||||||
.then(async (response) => {
|
|
||||||
if (!response.ok) {
|
|
||||||
if (response.status === 403) {
|
|
||||||
const tokenHeader = response.headers.get("X-CSRF-HEADER");
|
|
||||||
|
|
||||||
if (tokenHeader) {
|
|
||||||
const token = response.headers.get(tokenHeader);
|
|
||||||
this.csrfTokenWeb = {
|
|
||||||
headerName: tokenHeader,
|
|
||||||
value: token || ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.text();
|
|
||||||
})
|
|
||||||
.then((responseText) => {
|
|
||||||
if (isLogInRequired(responseText)) reject('You must be logged in to upload a fle');
|
|
||||||
|
|
||||||
if (
|
|
||||||
(needsRetry(responseText))
|
|
||||||
) {
|
|
||||||
if (this.retryCountFileUpload < requestRetryLimit) {
|
|
||||||
this.retryCountFileUpload++;
|
|
||||||
this.uploadFile(sasJob, file, fileName, params).then(
|
|
||||||
(res: any) => resolve(res),
|
|
||||||
(err: any) => reject(err)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
this.retryCountFileUpload = 0;
|
|
||||||
reject(responseText);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.retryCountFileUpload = 0;
|
|
||||||
|
|
||||||
try {
|
|
||||||
resolve(JSON.parse(responseText))
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -816,8 +749,8 @@ export default class SASjs {
|
|||||||
const token = response.headers.get(tokenHeader);
|
const token = response.headers.get(tokenHeader);
|
||||||
this.csrfTokenWeb = {
|
this.csrfTokenWeb = {
|
||||||
headerName: tokenHeader,
|
headerName: tokenHeader,
|
||||||
value: token || ''
|
value: token || "",
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1185,6 +1118,12 @@ export default class SASjs {
|
|||||||
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl);
|
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl);
|
||||||
else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl);
|
else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.fileUploader = new FileUploader(
|
||||||
|
this.sasjsConfig.appLoc,
|
||||||
|
this.sasjsConfig.serverUrl,
|
||||||
|
this.jobsPath
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private setLoginUrl = (matches: RegExpExecArray) => {
|
private setLoginUrl = (matches: RegExpExecArray) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user