mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-18 09:30: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 { CsrfToken } from './types/CsrfToken'
|
||||||
import { UploadFile } from './types/UploadFile'
|
import { UploadFile } from './types/UploadFile'
|
||||||
import { ErrorResponse } from './types'
|
import { ErrorResponse } from './types'
|
||||||
|
import axios, { AxiosInstance } from 'axios'
|
||||||
|
|
||||||
const requestRetryLimit = 5
|
const requestRetryLimit = 5
|
||||||
|
|
||||||
export class FileUploader {
|
export class FileUploader {
|
||||||
|
private httpClient: AxiosInstance
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private appLoc: string,
|
private appLoc: string,
|
||||||
private serverUrl: string,
|
serverUrl: string,
|
||||||
private jobsPath: string,
|
private jobsPath: string,
|
||||||
private setCsrfTokenWeb: any,
|
private setCsrfTokenWeb: any,
|
||||||
private csrfToken: CsrfToken | null = null
|
private csrfToken: CsrfToken | null = null
|
||||||
) {
|
) {
|
||||||
if (serverUrl) isUrl(serverUrl)
|
if (serverUrl) isUrl(serverUrl)
|
||||||
|
this.httpClient = axios.create({ baseURL: serverUrl })
|
||||||
}
|
}
|
||||||
|
|
||||||
private retryCount = 0
|
private retryCount = 0
|
||||||
@@ -36,7 +40,7 @@ export class FileUploader {
|
|||||||
const program = this.appLoc
|
const program = this.appLoc
|
||||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||||
: sasJob
|
: sasJob
|
||||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
const uploadUrl = `${this.jobsPath}/?${
|
||||||
'_program=' + program
|
'_program=' + program
|
||||||
}${paramsString}`
|
}${paramsString}`
|
||||||
|
|
||||||
@@ -52,14 +56,10 @@ export class FileUploader {
|
|||||||
|
|
||||||
if (this.csrfToken) formData.append('_csrf', this.csrfToken.value)
|
if (this.csrfToken) formData.append('_csrf', this.csrfToken.value)
|
||||||
|
|
||||||
fetch(uploadUrl, {
|
this.httpClient
|
||||||
method: 'POST',
|
.post(uploadUrl, formData, { responseType: 'text', headers })
|
||||||
body: formData,
|
|
||||||
referrerPolicy: 'same-origin',
|
|
||||||
headers
|
|
||||||
})
|
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
if (!response.ok) {
|
if (response.status !== 200) {
|
||||||
if (response.status === 403) {
|
if (response.status === 403) {
|
||||||
const tokenHeader = response.headers.get('X-CSRF-HEADER')
|
const tokenHeader = response.headers.get('X-CSRF-HEADER')
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ export class FileUploader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.text()
|
return response.data
|
||||||
})
|
})
|
||||||
.then((responseText) => {
|
.then((responseText) => {
|
||||||
if (isLogInRequired(responseText))
|
if (isLogInRequired(responseText))
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import axios, { AxiosInstance } from 'axios'
|
||||||
import { isUrl } from './utils'
|
import { isUrl } from './utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5,8 +6,11 @@ import { isUrl } from './utils'
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export class SAS9ApiClient {
|
export class SAS9ApiClient {
|
||||||
|
private httpClient: AxiosInstance
|
||||||
|
|
||||||
constructor(private serverUrl: string) {
|
constructor(private serverUrl: string) {
|
||||||
if (serverUrl) isUrl(serverUrl)
|
if (serverUrl) isUrl(serverUrl)
|
||||||
|
this.httpClient = axios.create({ baseURL: this.serverUrl })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,18 +42,18 @@ export class SAS9ApiClient {
|
|||||||
repositoryName: string
|
repositoryName: string
|
||||||
) {
|
) {
|
||||||
const requestPayload = linesOfCode.join('\n')
|
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,
|
isLoggedIn: !!loggedIn,
|
||||||
userName: this.userName
|
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`.
|
* @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`.
|
||||||
*/
|
*/
|
||||||
public async checkSession() {
|
public async checkSession() {
|
||||||
const loginResponse = await fetch(this.loginUrl.replace('.do', ''))
|
const loginResponse = await axios.get(this.loginUrl.replace('.do', ''), {
|
||||||
const responseText = await loginResponse.text()
|
responseType: 'text'
|
||||||
|
})
|
||||||
|
const responseText = await loginResponse.data
|
||||||
const isLoggedIn = /<button.+onClick.+logout/gm.test(responseText)
|
const isLoggedIn = /<button.+onClick.+logout/gm.test(responseText)
|
||||||
let loginForm: any = null
|
let loginForm: any = null
|
||||||
|
|
||||||
@@ -194,12 +153,6 @@ export class AuthManager {
|
|||||||
* Logs out of the configured SAS server.
|
* Logs out of the configured SAS server.
|
||||||
*/
|
*/
|
||||||
public logOut() {
|
public logOut() {
|
||||||
return new Promise((resolve, reject) => {
|
return this.httpClient.get(this.logoutUrl).then(() => true)
|
||||||
fetch(this.logoutUrl)
|
|
||||||
.then(() => {
|
|
||||||
resolve(true)
|
|
||||||
})
|
|
||||||
.catch((err: Error) => reject(err))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user