From 75e3fd018d016795a3538dd56d3b3648ecfe0640 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Mon, 18 Jan 2021 09:22:10 +0000 Subject: [PATCH] chore(*): replace fetch calls with axios --- src/FileUploader.ts | 20 ++++++++-------- src/SAS9ApiClient.ts | 28 ++++++++++++---------- src/auth/auth.ts | 57 ++++---------------------------------------- 3 files changed, 31 insertions(+), 74 deletions(-) diff --git a/src/FileUploader.ts b/src/FileUploader.ts index 8e0b643..e101e2c 100644 --- a/src/FileUploader.ts +++ b/src/FileUploader.ts @@ -2,18 +2,22 @@ import { isLogInRequired, needsRetry, isUrl } from './utils' import { CsrfToken } from './types/CsrfToken' import { UploadFile } from './types/UploadFile' import { ErrorResponse } from './types' +import axios, { AxiosInstance } from 'axios' const requestRetryLimit = 5 export class FileUploader { + private httpClient: AxiosInstance + constructor( private appLoc: string, - private serverUrl: string, + serverUrl: string, private jobsPath: string, private setCsrfTokenWeb: any, private csrfToken: CsrfToken | null = null ) { if (serverUrl) isUrl(serverUrl) + this.httpClient = axios.create({ baseURL: serverUrl }) } private retryCount = 0 @@ -36,7 +40,7 @@ export class FileUploader { const program = this.appLoc ? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '') : sasJob - const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${ + const uploadUrl = `${this.jobsPath}/?${ '_program=' + program }${paramsString}` @@ -52,14 +56,10 @@ export class FileUploader { if (this.csrfToken) formData.append('_csrf', this.csrfToken.value) - fetch(uploadUrl, { - method: 'POST', - body: formData, - referrerPolicy: 'same-origin', - headers - }) + this.httpClient + .post(uploadUrl, formData, { responseType: 'text', headers }) .then(async (response) => { - if (!response.ok) { + if (response.status !== 200) { if (response.status === 403) { const tokenHeader = response.headers.get('X-CSRF-HEADER') @@ -75,7 +75,7 @@ export class FileUploader { } } - return response.text() + return response.data }) .then((responseText) => { if (isLogInRequired(responseText)) diff --git a/src/SAS9ApiClient.ts b/src/SAS9ApiClient.ts index b9256ad..473ddcf 100644 --- a/src/SAS9ApiClient.ts +++ b/src/SAS9ApiClient.ts @@ -1,3 +1,4 @@ +import axios, { AxiosInstance } from 'axios' import { isUrl } from './utils' /** @@ -5,8 +6,11 @@ import { isUrl } from './utils' * */ export class SAS9ApiClient { + private httpClient: AxiosInstance + constructor(private serverUrl: string) { if (serverUrl) isUrl(serverUrl) + this.httpClient = axios.create({ baseURL: this.serverUrl }) } /** @@ -38,18 +42,18 @@ export class SAS9ApiClient { repositoryName: string ) { const requestPayload = linesOfCode.join('\n') - const executeScriptRequest = { - method: 'PUT', - headers: { - Accept: 'application/json' - }, - body: `command=${requestPayload}` - } - const executeScriptResponse = await fetch( - `${this.serverUrl}/sas/servers/${serverName}/cmd?repositoryName=${repositoryName}`, - executeScriptRequest - ).then((res) => res.text()) - return executeScriptResponse + const executeScriptResponse = await this.httpClient.put( + `/sas/servers/${serverName}/cmd?repositoryName=${repositoryName}`, + `command=${requestPayload}`, + { + headers: { + Accept: 'application/json' + }, + responseType: 'text' + } + ) + + return executeScriptResponse.data } } diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 160531d..86460bd 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -83,49 +83,6 @@ export class AuthManager { isLoggedIn: !!loggedIn, userName: this.userName } - - return { - isLoggedIn: isLogInSuccess(loginResponse), - userName: this.userName - } - - return fetch(this.loginUrl, { - method: 'POST', - credentials: 'include', - referrerPolicy: 'same-origin', - body: loginParamsStr, - headers: new Headers({ - 'Content-Type': 'application/x-www-form-urlencoded' - }) - }) - .then((response) => response.text()) - .then(async (responseText) => { - let loggedIn - - if (isAuthorizeFormRequired(responseText)) { - const authFormResponse = await parseAndSubmitAuthorizeForm( - responseText, - this.serverUrl - ) - } else { - loggedIn = isLogInSuccess(responseText) - } - - if (!loggedIn) { - const currentSession = await this.checkSession() - loggedIn = currentSession.isLoggedIn - } - - if (loggedIn) { - this.loginCallback() - } - - return { - isLoggedIn: loggedIn, - userName: this.userName - } - }) - .catch((e) => Promise.reject(e)) } /** @@ -133,8 +90,10 @@ export class AuthManager { * @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`. */ public async checkSession() { - const loginResponse = await fetch(this.loginUrl.replace('.do', '')) - const responseText = await loginResponse.text() + const loginResponse = await axios.get(this.loginUrl.replace('.do', ''), { + responseType: 'text' + }) + const responseText = await loginResponse.data const isLoggedIn = / { - fetch(this.logoutUrl) - .then(() => { - resolve(true) - }) - .catch((err: Error) => reject(err)) - }) + return this.httpClient.get(this.logoutUrl).then(() => true) } }