mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-08 04:50:06 +00:00
chore(*): replace fetch calls with axios
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = /<button.+onClick.+logout/gm.test(responseText)
|
||||
let loginForm: any = null
|
||||
|
||||
@@ -194,12 +153,6 @@ export class AuthManager {
|
||||
* Logs out of the configured SAS server.
|
||||
*/
|
||||
public logOut() {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(this.logoutUrl)
|
||||
.then(() => {
|
||||
resolve(true)
|
||||
})
|
||||
.catch((err: Error) => reject(err))
|
||||
})
|
||||
return this.httpClient.get(this.logoutUrl).then(() => true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user