mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-12 09:44:36 +00:00
Compare commits
1 Commits
v3.3.0
...
error-stat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
596c1de5cb |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -22,8 +22,8 @@ import { pollJobState } from './api/viya/pollJobState'
|
|||||||
import { getTokens } from './auth/getTokens'
|
import { getTokens } from './auth/getTokens'
|
||||||
import { uploadTables } from './api/viya/uploadTables'
|
import { uploadTables } from './api/viya/uploadTables'
|
||||||
import { executeScript } from './api/viya/executeScript'
|
import { executeScript } from './api/viya/executeScript'
|
||||||
import { getAccessTokenForViya } from './auth/getAccessTokenForViya'
|
import { getAccessToken } from './auth/getAccessToken'
|
||||||
import { refreshTokensForViya } from './auth/refreshTokensForViya'
|
import { refreshTokens } from './auth/refreshTokens'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client for interfacing with the SAS Viya REST API.
|
* A client for interfacing with the SAS Viya REST API.
|
||||||
@@ -534,26 +534,21 @@ export class SASViyaApiClient {
|
|||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
authCode: string
|
authCode: string
|
||||||
): Promise<SasAuthResponse> {
|
): Promise<SasAuthResponse> {
|
||||||
return getAccessTokenForViya(
|
return getAccessToken(this.requestClient, clientId, clientSecret, authCode)
|
||||||
this.requestClient,
|
|
||||||
clientId,
|
|
||||||
clientSecret,
|
|
||||||
authCode
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exchanges the refresh token for an access token for the given client.
|
* Exchanges the refresh token for an access token for the given client.
|
||||||
* @param clientId - the client ID to authenticate with.
|
* @param clientId - the client ID to authenticate with.
|
||||||
* @param clientSecret - the client secret to authenticate with.
|
* @param clientSecret - the client secret to authenticate with.
|
||||||
* @param refreshToken - the refresh token received from the server.
|
* @param authCode - the refresh token received from the server.
|
||||||
*/
|
*/
|
||||||
public async refreshTokens(
|
public async refreshTokens(
|
||||||
clientId: string,
|
clientId: string,
|
||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
refreshToken: string
|
refreshToken: string
|
||||||
) {
|
) {
|
||||||
return refreshTokensForViya(
|
return refreshTokens(
|
||||||
this.requestClient,
|
this.requestClient,
|
||||||
clientId,
|
clientId,
|
||||||
clientSecret,
|
clientSecret,
|
||||||
|
|||||||
130
src/SASjs.ts
130
src/SASjs.ts
@@ -11,17 +11,15 @@ import {
|
|||||||
} from './types'
|
} from './types'
|
||||||
import { SASViyaApiClient } from './SASViyaApiClient'
|
import { SASViyaApiClient } from './SASViyaApiClient'
|
||||||
import { SAS9ApiClient } from './SAS9ApiClient'
|
import { SAS9ApiClient } from './SAS9ApiClient'
|
||||||
import { SASjsApiClient, SASjsAuthResponse } from './SASjsApiClient'
|
import { SASjsApiClient } from './SASjsApiClient'
|
||||||
import { AuthManager } from './auth'
|
import { AuthManager } from './auth'
|
||||||
import {
|
import {
|
||||||
ServerType,
|
ServerType,
|
||||||
MacroVar,
|
MacroVar,
|
||||||
AuthConfig,
|
AuthConfig,
|
||||||
ExtraResponseAttributes,
|
ExtraResponseAttributes
|
||||||
SasAuthResponse
|
|
||||||
} from '@sasjs/utils/types'
|
} from '@sasjs/utils/types'
|
||||||
import { RequestClient } from './request/RequestClient'
|
import { RequestClient } from './request/RequestClient'
|
||||||
import { SasjsRequestClient } from './request/SasjsRequestClient'
|
|
||||||
import {
|
import {
|
||||||
JobExecutor,
|
JobExecutor,
|
||||||
WebJobExecutor,
|
WebJobExecutor,
|
||||||
@@ -55,7 +53,7 @@ export default class SASjs {
|
|||||||
private jobsPath: string = ''
|
private jobsPath: string = ''
|
||||||
private sasViyaApiClient: SASViyaApiClient | null = null
|
private sasViyaApiClient: SASViyaApiClient | null = null
|
||||||
private sas9ApiClient: SAS9ApiClient | null = null
|
private sas9ApiClient: SAS9ApiClient | null = null
|
||||||
private sasJSApiClient: SASjsApiClient | null = null
|
private SASjsApiClient: SASjsApiClient | null = null
|
||||||
private fileUploader: FileUploader | null = null
|
private fileUploader: FileUploader | null = null
|
||||||
private authManager: AuthManager | null = null
|
private authManager: AuthManager | null = null
|
||||||
private requestClient: RequestClient | null = null
|
private requestClient: RequestClient | null = null
|
||||||
@@ -82,7 +80,7 @@ export default class SASjs {
|
|||||||
userName: string,
|
userName: string,
|
||||||
password: string
|
password: string
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('executeScriptSAS9', [ServerType.Sas9])
|
this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
|
||||||
|
|
||||||
return await this.sas9ApiClient?.executeScript(
|
return await this.sas9ApiClient?.executeScript(
|
||||||
linesOfCode,
|
linesOfCode,
|
||||||
@@ -96,7 +94,7 @@ export default class SASjs {
|
|||||||
* @param accessToken - an access token for an authorized user.
|
* @param accessToken - an access token for an authorized user.
|
||||||
*/
|
*/
|
||||||
public async getComputeContexts(accessToken: string) {
|
public async getComputeContexts(accessToken: string) {
|
||||||
this.isMethodSupported('getComputeContexts', [ServerType.SasViya])
|
this.isMethodSupported('getComputeContexts', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getComputeContexts(accessToken)
|
return await this.sasViyaApiClient!.getComputeContexts(accessToken)
|
||||||
}
|
}
|
||||||
@@ -106,7 +104,7 @@ export default class SASjs {
|
|||||||
* @param accessToken - an access token for an authorized user.
|
* @param accessToken - an access token for an authorized user.
|
||||||
*/
|
*/
|
||||||
public async getLauncherContexts(accessToken: string) {
|
public async getLauncherContexts(accessToken: string) {
|
||||||
this.isMethodSupported('getLauncherContexts', [ServerType.SasViya])
|
this.isMethodSupported('getLauncherContexts', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getLauncherContexts(accessToken)
|
return await this.sasViyaApiClient!.getLauncherContexts(accessToken)
|
||||||
}
|
}
|
||||||
@@ -115,7 +113,7 @@ export default class SASjs {
|
|||||||
* Gets default(system) launcher contexts.
|
* Gets default(system) launcher contexts.
|
||||||
*/
|
*/
|
||||||
public getDefaultComputeContexts() {
|
public getDefaultComputeContexts() {
|
||||||
this.isMethodSupported('getDefaultComputeContexts', [ServerType.SasViya])
|
this.isMethodSupported('getDefaultComputeContexts', ServerType.SasViya)
|
||||||
|
|
||||||
return this.sasViyaApiClient!.getDefaultComputeContexts()
|
return this.sasViyaApiClient!.getDefaultComputeContexts()
|
||||||
}
|
}
|
||||||
@@ -125,7 +123,7 @@ export default class SASjs {
|
|||||||
* @param authConfig - an access token, refresh token, client and secret for an authorized user.
|
* @param authConfig - an access token, refresh token, client and secret for an authorized user.
|
||||||
*/
|
*/
|
||||||
public async getExecutableContexts(authConfig: AuthConfig) {
|
public async getExecutableContexts(authConfig: AuthConfig) {
|
||||||
this.isMethodSupported('getExecutableContexts', [ServerType.SasViya])
|
this.isMethodSupported('getExecutableContexts', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getExecutableContexts(authConfig)
|
return await this.sasViyaApiClient!.getExecutableContexts(authConfig)
|
||||||
}
|
}
|
||||||
@@ -147,7 +145,7 @@ export default class SASjs {
|
|||||||
accessToken: string,
|
accessToken: string,
|
||||||
authorizedUsers?: string[]
|
authorizedUsers?: string[]
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('createComputeContext', [ServerType.SasViya])
|
this.isMethodSupported('createComputeContext', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.createComputeContext(
|
return await this.sasViyaApiClient!.createComputeContext(
|
||||||
contextName,
|
contextName,
|
||||||
@@ -172,7 +170,7 @@ export default class SASjs {
|
|||||||
launchType: string,
|
launchType: string,
|
||||||
accessToken: string
|
accessToken: string
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('createLauncherContext', [ServerType.SasViya])
|
this.isMethodSupported('createLauncherContext', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.createLauncherContext(
|
return await this.sasViyaApiClient!.createLauncherContext(
|
||||||
contextName,
|
contextName,
|
||||||
@@ -193,7 +191,7 @@ export default class SASjs {
|
|||||||
editedContext: EditContextInput,
|
editedContext: EditContextInput,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('editComputeContext', [ServerType.SasViya])
|
this.isMethodSupported('editComputeContext', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.editComputeContext(
|
return await this.sasViyaApiClient!.editComputeContext(
|
||||||
contextName,
|
contextName,
|
||||||
@@ -208,7 +206,7 @@ export default class SASjs {
|
|||||||
* @param accessToken - an access token for an authorized user.
|
* @param accessToken - an access token for an authorized user.
|
||||||
*/
|
*/
|
||||||
public async deleteComputeContext(contextName: string, accessToken?: string) {
|
public async deleteComputeContext(contextName: string, accessToken?: string) {
|
||||||
this.isMethodSupported('deleteComputeContext', [ServerType.SasViya])
|
this.isMethodSupported('deleteComputeContext', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.deleteComputeContext(
|
return await this.sasViyaApiClient!.deleteComputeContext(
|
||||||
contextName,
|
contextName,
|
||||||
@@ -226,7 +224,7 @@ export default class SASjs {
|
|||||||
contextName: string,
|
contextName: string,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('getComputeContextByName', [ServerType.SasViya])
|
this.isMethodSupported('getComputeContextByName', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getComputeContextByName(
|
return await this.sasViyaApiClient!.getComputeContextByName(
|
||||||
contextName,
|
contextName,
|
||||||
@@ -240,7 +238,7 @@ export default class SASjs {
|
|||||||
* @param accessToken - an access token for an authorized user.
|
* @param accessToken - an access token for an authorized user.
|
||||||
*/
|
*/
|
||||||
public async getComputeContextById(contextId: string, accessToken?: string) {
|
public async getComputeContextById(contextId: string, accessToken?: string) {
|
||||||
this.isMethodSupported('getComputeContextById', [ServerType.SasViya])
|
this.isMethodSupported('getComputeContextById', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getComputeContextById(
|
return await this.sasViyaApiClient!.getComputeContextById(
|
||||||
contextId,
|
contextId,
|
||||||
@@ -249,7 +247,7 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async createSession(contextName: string, accessToken: string) {
|
public async createSession(contextName: string, accessToken: string) {
|
||||||
this.isMethodSupported('createSession', [ServerType.SasViya])
|
this.isMethodSupported('createSession', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.createSession(contextName, accessToken)
|
return await this.sasViyaApiClient!.createSession(contextName, accessToken)
|
||||||
}
|
}
|
||||||
@@ -269,7 +267,7 @@ export default class SASjs {
|
|||||||
authConfig?: AuthConfig,
|
authConfig?: AuthConfig,
|
||||||
debug?: boolean
|
debug?: boolean
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('executeScriptSASViya', [ServerType.SasViya])
|
this.isMethodSupported('executeScriptSASViya', ServerType.SasViya)
|
||||||
if (!contextName) {
|
if (!contextName) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Context name is undefined. Please set a `contextName` in your SASjs or override config.'
|
'Context name is undefined. Please set a `contextName` in your SASjs or override config.'
|
||||||
@@ -359,7 +357,7 @@ export default class SASjs {
|
|||||||
* @param accessToken - the access token to authorize the request.
|
* @param accessToken - the access token to authorize the request.
|
||||||
*/
|
*/
|
||||||
public async getFolder(folderPath: string, accessToken?: string) {
|
public async getFolder(folderPath: string, accessToken?: string) {
|
||||||
this.isMethodSupported('getFolder', [ServerType.SasViya])
|
this.isMethodSupported('getFolder', ServerType.SasViya)
|
||||||
return await this.sasViyaApiClient!.getFolder(folderPath, accessToken)
|
return await this.sasViyaApiClient!.getFolder(folderPath, accessToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,7 +367,7 @@ export default class SASjs {
|
|||||||
* @param accessToken - an access token for authorizing the request.
|
* @param accessToken - an access token for authorizing the request.
|
||||||
*/
|
*/
|
||||||
public async deleteFolder(folderPath: string, accessToken: string) {
|
public async deleteFolder(folderPath: string, accessToken: string) {
|
||||||
this.isMethodSupported('deleteFolder', [ServerType.SasViya])
|
this.isMethodSupported('deleteFolder', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken)
|
return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken)
|
||||||
}
|
}
|
||||||
@@ -384,7 +382,7 @@ export default class SASjs {
|
|||||||
accessToken?: string,
|
accessToken?: string,
|
||||||
limit?: number
|
limit?: number
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('listFolder', [ServerType.SasViya])
|
this.isMethodSupported('listFolder', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient?.listFolder(
|
return await this.sasViyaApiClient?.listFolder(
|
||||||
sourceFolder,
|
sourceFolder,
|
||||||
@@ -406,7 +404,7 @@ export default class SASjs {
|
|||||||
targetFolderName: string,
|
targetFolderName: string,
|
||||||
accessToken: string
|
accessToken: string
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('moveFolder', [ServerType.SasViya])
|
this.isMethodSupported('moveFolder', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient?.moveFolder(
|
return await this.sasViyaApiClient?.moveFolder(
|
||||||
sourceFolder,
|
sourceFolder,
|
||||||
@@ -424,7 +422,7 @@ export default class SASjs {
|
|||||||
accessToken?: string,
|
accessToken?: string,
|
||||||
sasApiClient?: SASViyaApiClient
|
sasApiClient?: SASViyaApiClient
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('createJobDefinition', [ServerType.SasViya])
|
this.isMethodSupported('createJobDefinition', ServerType.SasViya)
|
||||||
|
|
||||||
if (sasApiClient)
|
if (sasApiClient)
|
||||||
return await sasApiClient!.createJobDefinition(
|
return await sasApiClient!.createJobDefinition(
|
||||||
@@ -444,7 +442,7 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getAuthCode(clientId: string) {
|
public async getAuthCode(clientId: string) {
|
||||||
this.isMethodSupported('getAuthCode', [ServerType.SasViya])
|
this.isMethodSupported('getAuthCode', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getAuthCode(clientId)
|
return await this.sasViyaApiClient!.getAuthCode(clientId)
|
||||||
}
|
}
|
||||||
@@ -459,14 +457,8 @@ export default class SASjs {
|
|||||||
clientId: string,
|
clientId: string,
|
||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
authCode: string
|
authCode: string
|
||||||
): Promise<SasAuthResponse | SASjsAuthResponse> {
|
) {
|
||||||
this.isMethodSupported('getAccessToken', [
|
this.isMethodSupported('getAccessToken', ServerType.SasViya)
|
||||||
ServerType.SasViya,
|
|
||||||
ServerType.Sasjs
|
|
||||||
])
|
|
||||||
|
|
||||||
if (this.sasjsConfig.serverType === ServerType.Sasjs)
|
|
||||||
return await this.sasJSApiClient!.getAccessToken(clientId, authCode)
|
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.getAccessToken(
|
return await this.sasViyaApiClient!.getAccessToken(
|
||||||
clientId,
|
clientId,
|
||||||
@@ -475,24 +467,12 @@ export default class SASjs {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Exchanges the refresh token for an access token for the given client.
|
|
||||||
* @param clientId - the client ID to authenticate with.
|
|
||||||
* @param clientSecret - the client secret to authenticate with.
|
|
||||||
* @param refreshToken - the refresh token received from the server.
|
|
||||||
*/
|
|
||||||
public async refreshTokens(
|
public async refreshTokens(
|
||||||
clientId: string,
|
clientId: string,
|
||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
refreshToken: string
|
refreshToken: string
|
||||||
): Promise<SasAuthResponse | SASjsAuthResponse> {
|
) {
|
||||||
this.isMethodSupported('refreshTokens', [
|
this.isMethodSupported('refreshTokens', ServerType.SasViya)
|
||||||
ServerType.SasViya,
|
|
||||||
ServerType.Sasjs
|
|
||||||
])
|
|
||||||
|
|
||||||
if (this.sasjsConfig.serverType === ServerType.Sasjs)
|
|
||||||
return await this.sasJSApiClient!.refreshTokens(refreshToken)
|
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.refreshTokens(
|
return await this.sasViyaApiClient!.refreshTokens(
|
||||||
clientId,
|
clientId,
|
||||||
@@ -502,7 +482,7 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async deleteClient(clientId: string, accessToken: string) {
|
public async deleteClient(clientId: string, accessToken: string) {
|
||||||
this.isMethodSupported('deleteClient', [ServerType.SasViya])
|
this.isMethodSupported('deleteClient', ServerType.SasViya)
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.deleteClient(clientId, accessToken)
|
return await this.sasViyaApiClient!.deleteClient(clientId, accessToken)
|
||||||
}
|
}
|
||||||
@@ -548,39 +528,29 @@ export default class SASjs {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a session is active, or login is required.
|
* Checks whether a session is active, or login is required.
|
||||||
|
* @param accessToken - an optional access token is required for SASjs server type.
|
||||||
* @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(accessToken?: string) {
|
||||||
return this.authManager!.checkSession()
|
return this.authManager!.checkSession(accessToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs into the SAS server with the supplied credentials.
|
* Logs into the SAS server with the supplied credentials.
|
||||||
* @param username - a string representing the username.
|
* @param username - a string representing the username.
|
||||||
* @param password - a string representing the password.
|
* @param password - a string representing the password.
|
||||||
* @param clientId - a string representing the client ID.
|
|
||||||
*/
|
*/
|
||||||
public async logIn(
|
public async logIn(
|
||||||
username?: string,
|
username?: string,
|
||||||
password?: string,
|
password?: string,
|
||||||
clientId?: string,
|
|
||||||
options: LoginOptions = {}
|
options: LoginOptions = {}
|
||||||
): Promise<LoginResult> {
|
): Promise<LoginResult> {
|
||||||
if (this.sasjsConfig.loginMechanism === LoginMechanism.Default) {
|
if (this.sasjsConfig.loginMechanism === LoginMechanism.Default) {
|
||||||
if (!username || !password)
|
if (!username || !password) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'A username and password are required when using the default login mechanism.'
|
'A username and password are required when using the default login mechanism.'
|
||||||
)
|
)
|
||||||
|
|
||||||
if (this.sasjsConfig.serverType === ServerType.Sasjs) {
|
|
||||||
if (!clientId)
|
|
||||||
throw new Error(
|
|
||||||
'A username, password and clientId are required when using the default login mechanism with server type SASJS.'
|
|
||||||
)
|
|
||||||
|
|
||||||
return this.authManager!.logInSasjs(username, password, clientId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.authManager!.logIn(username, password)
|
return this.authManager!.logIn(username, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -595,9 +565,10 @@ export default class SASjs {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs out of the configured SAS server.
|
* Logs out of the configured SAS server.
|
||||||
|
* @param accessToken - an optional access token is required for SASjs server type.
|
||||||
*/
|
*/
|
||||||
public logOut() {
|
public logOut(accessToken?: string) {
|
||||||
return this.authManager!.logOut()
|
return this.authManager!.logOut(accessToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -806,7 +777,7 @@ export default class SASjs {
|
|||||||
accessToken?: string,
|
accessToken?: string,
|
||||||
isForced = false
|
isForced = false
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('deployServicePack', [ServerType.SasViya])
|
this.isMethodSupported('deployServicePack', ServerType.SasViya)
|
||||||
|
|
||||||
let sasApiClient: any = null
|
let sasApiClient: any = null
|
||||||
if (serverUrl || appLoc) {
|
if (serverUrl || appLoc) {
|
||||||
@@ -861,11 +832,11 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async deployToSASjs(members: [FolderMember, ServiceMember]) {
|
public async deployToSASjs(members: [FolderMember, ServiceMember]) {
|
||||||
return await this.sasJSApiClient?.deploy(members, this.sasjsConfig.appLoc)
|
return await this.SASjsApiClient?.deploy(members, this.sasjsConfig.appLoc)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async executeJobSASjs(query: ExecutionQuery) {
|
public async executeJobSASjs(query: ExecutionQuery) {
|
||||||
return await this.sasJSApiClient?.executeJob(query)
|
return await this.SASjsApiClient?.executeJob(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -901,7 +872,7 @@ export default class SASjs {
|
|||||||
...config
|
...config
|
||||||
}
|
}
|
||||||
|
|
||||||
this.isMethodSupported('startComputeJob', [ServerType.SasViya])
|
this.isMethodSupported('startComputeJob', ServerType.SasViya)
|
||||||
if (!config.contextName) {
|
if (!config.contextName) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Context name is undefined. Please set a `contextName` in your SASjs or override config.'
|
'Context name is undefined. Please set a `contextName` in your SASjs or override config.'
|
||||||
@@ -993,11 +964,7 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.requestClient) {
|
if (!this.requestClient) {
|
||||||
const RequestClientClass =
|
this.requestClient = new RequestClient(
|
||||||
this.sasjsConfig.serverType === ServerType.Sasjs
|
|
||||||
? SasjsRequestClient
|
|
||||||
: RequestClient
|
|
||||||
this.requestClient = new RequestClientClass(
|
|
||||||
this.sasjsConfig.serverUrl,
|
this.sasjsConfig.serverUrl,
|
||||||
this.sasjsConfig.httpsAgentOptions
|
this.sasjsConfig.httpsAgentOptions
|
||||||
)
|
)
|
||||||
@@ -1053,10 +1020,10 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.sasjsConfig.serverType === ServerType.Sasjs) {
|
if (this.sasjsConfig.serverType === ServerType.Sasjs) {
|
||||||
if (this.sasJSApiClient) {
|
if (this.SASjsApiClient) {
|
||||||
this.sasJSApiClient.setConfig(this.sasjsConfig.serverUrl)
|
this.SASjsApiClient.setConfig(this.sasjsConfig.serverUrl)
|
||||||
} else {
|
} else {
|
||||||
this.sasJSApiClient = new SASjsApiClient(
|
this.SASjsApiClient = new SASjsApiClient(
|
||||||
this.sasjsConfig.serverUrl,
|
this.sasjsConfig.serverUrl,
|
||||||
this.requestClient
|
this.requestClient
|
||||||
)
|
)
|
||||||
@@ -1150,15 +1117,12 @@ export default class SASjs {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private isMethodSupported(method: string, serverTypes: ServerType[]) {
|
private isMethodSupported(method: string, serverType: string) {
|
||||||
if (
|
if (this.sasjsConfig.serverType !== serverType) {
|
||||||
!this.sasjsConfig.serverType ||
|
|
||||||
!serverTypes.includes(this.sasjsConfig.serverType)
|
|
||||||
) {
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Method '${method}' is only supported on ${serverTypes.join(
|
`Method '${method}' is only supported on ${
|
||||||
', '
|
serverType === ServerType.Sas9 ? 'SAS9' : 'SAS Viya'
|
||||||
)} servers.`
|
} servers.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
import { FolderMember, ServiceMember, ExecutionQuery } from './types'
|
import { FolderMember, ServiceMember, ExecutionQuery } from './types'
|
||||||
import { RequestClient } from './request/RequestClient'
|
import { RequestClient } from './request/RequestClient'
|
||||||
import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs'
|
|
||||||
import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs'
|
|
||||||
import { getAuthCodeForSasjs } from './auth/getAuthCodeForSasjs'
|
|
||||||
|
|
||||||
export class SASjsApiClient {
|
export class SASjsApiClient {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -39,44 +36,4 @@ export class SASjsApiClient {
|
|||||||
|
|
||||||
return Promise.resolve(result)
|
return Promise.resolve(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Exchanges the auth code for an access token for the given client.
|
|
||||||
* @param clientId - the client ID to authenticate with.
|
|
||||||
* @param authCode - the auth code received from the server.
|
|
||||||
*/
|
|
||||||
public async getAccessToken(
|
|
||||||
clientId: string,
|
|
||||||
authCode: string
|
|
||||||
): Promise<SASjsAuthResponse> {
|
|
||||||
return getAccessTokenForSasjs(this.requestClient, clientId, authCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exchanges the refresh token for an access token.
|
|
||||||
* @param refreshToken - the refresh token received from the server.
|
|
||||||
*/
|
|
||||||
public async refreshTokens(refreshToken: string): Promise<SASjsAuthResponse> {
|
|
||||||
return refreshTokensForSasjs(this.requestClient, refreshToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs a login authenticate and returns an auth code for the given client.
|
|
||||||
* @param username - a string representing the username.
|
|
||||||
* @param password - a string representing the password.
|
|
||||||
* @param clientId - the client ID to authenticate with.
|
|
||||||
*/
|
|
||||||
public async getAuthCode(
|
|
||||||
username: string,
|
|
||||||
password: string,
|
|
||||||
clientId: string
|
|
||||||
) {
|
|
||||||
return getAuthCodeForSasjs(this.requestClient, username, password, clientId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo move to sasjs/utils
|
|
||||||
export interface SASjsAuthResponse {
|
|
||||||
access_token: string
|
|
||||||
refresh_token: string
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ export class SessionManager {
|
|||||||
) {
|
) {
|
||||||
if (stateLink) {
|
if (stateLink) {
|
||||||
if (this.debug && !this.printedSessionState.printed) {
|
if (this.debug && !this.printedSessionState.printed) {
|
||||||
logger.info(`Polling: ${this.serverUrl + stateLink.href}`)
|
logger.info('Polling session status...')
|
||||||
|
|
||||||
this.printedSessionState.printed = true
|
this.printedSessionState.printed = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -206,11 +206,10 @@ const doPoll = async (
|
|||||||
|
|
||||||
pollCount++
|
pollCount++
|
||||||
|
|
||||||
const jobHref = postedJob.links.find((l: Link) => l.rel === 'self')!.href
|
|
||||||
|
|
||||||
if (pollOptions?.streamLog) {
|
if (pollOptions?.streamLog) {
|
||||||
|
const jobUrl = postedJob.links.find((l: Link) => l.rel === 'self')
|
||||||
const { result: job } = await requestClient.get<Job>(
|
const { result: job } = await requestClient.get<Job>(
|
||||||
jobHref,
|
jobUrl!.href,
|
||||||
authConfig?.access_token
|
authConfig?.access_token
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -232,7 +231,7 @@ const doPoll = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (debug && printedState !== state) {
|
if (debug && printedState !== state) {
|
||||||
logger.info(`Polling: ${requestClient.getBaseUrl() + jobHref}/state`)
|
logger.info('Polling job status...')
|
||||||
logger.info(`Current job state: ${state}`)
|
logger.info(`Current job state: ${state}`)
|
||||||
|
|
||||||
printedState = state
|
printedState = state
|
||||||
|
|||||||
@@ -9,10 +9,7 @@ import * as isNodeModule from '../../../utils/isNode'
|
|||||||
import { PollOptions } from '../../../types'
|
import { PollOptions } from '../../../types'
|
||||||
import { WriteStream } from 'fs'
|
import { WriteStream } from 'fs'
|
||||||
|
|
||||||
const baseUrl = 'http://localhost'
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
requestClient['httpClient'].defaults.baseURL = baseUrl
|
|
||||||
|
|
||||||
const defaultPollOptions: PollOptions = {
|
const defaultPollOptions: PollOptions = {
|
||||||
maxPollCount: 100,
|
maxPollCount: 100,
|
||||||
pollInterval: 500,
|
pollInterval: 500,
|
||||||
@@ -198,7 +195,7 @@ describe('pollJobState', () => {
|
|||||||
expect((process as any).logger.info).toHaveBeenCalledTimes(4)
|
expect((process as any).logger.info).toHaveBeenCalledTimes(4)
|
||||||
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
1,
|
1,
|
||||||
`Polling: ${baseUrl}/job/state`
|
'Polling job status...'
|
||||||
)
|
)
|
||||||
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
2,
|
2,
|
||||||
@@ -206,7 +203,7 @@ describe('pollJobState', () => {
|
|||||||
)
|
)
|
||||||
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
3,
|
3,
|
||||||
`Polling: ${baseUrl}/job/state`
|
'Polling job status...'
|
||||||
)
|
)
|
||||||
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
4,
|
4,
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import { ServerType } from '@sasjs/utils/types'
|
|||||||
import { RequestClient } from '../request/RequestClient'
|
import { RequestClient } from '../request/RequestClient'
|
||||||
import { LoginOptions, LoginResult } from '../types/Login'
|
import { LoginOptions, LoginResult } from '../types/Login'
|
||||||
import { serialize } from '../utils'
|
import { serialize } from '../utils'
|
||||||
import { getAccessTokenForSasjs } from './getAccessTokenForSasjs'
|
|
||||||
import { getAuthCodeForSasjs } from './getAuthCodeForSasjs'
|
|
||||||
import { openWebPage } from './openWebPage'
|
import { openWebPage } from './openWebPage'
|
||||||
import { verifySas9Login } from './verifySas9Login'
|
import { verifySas9Login } from './verifySas9Login'
|
||||||
import { verifySasViyaLogin } from './verifySasViyaLogin'
|
import { verifySasViyaLogin } from './verifySasViyaLogin'
|
||||||
@@ -83,39 +81,6 @@ export class AuthManager {
|
|||||||
return { isLoggedIn: false, userName: '' }
|
return { isLoggedIn: false, userName: '' }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs into the SAS server with the supplied credentials.
|
|
||||||
* @param userName - a string representing the username.
|
|
||||||
* @param password - a string representing the password.
|
|
||||||
* @param clientId - a string representing the client ID.
|
|
||||||
* @returns - a boolean `isLoggedin` and a string `username`
|
|
||||||
*/
|
|
||||||
public async logInSasjs(
|
|
||||||
username: string,
|
|
||||||
password: string,
|
|
||||||
clientId: string
|
|
||||||
): Promise<LoginResult> {
|
|
||||||
const isLoggedIn = await this.sendLoginRequestSasjs(
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
clientId
|
|
||||||
)
|
|
||||||
.then((res) => {
|
|
||||||
this.userName = username
|
|
||||||
this.requestClient.saveLocalStorageToken(
|
|
||||||
res.access_token,
|
|
||||||
res.refresh_token
|
|
||||||
)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
.catch(() => false)
|
|
||||||
|
|
||||||
return {
|
|
||||||
isLoggedIn,
|
|
||||||
userName: this.userName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs into the SAS server with the supplied credentials.
|
* Logs into the SAS server with the supplied credentials.
|
||||||
* @param username - a string representing the username.
|
* @param username - a string representing the username.
|
||||||
@@ -215,40 +180,27 @@ export class AuthManager {
|
|||||||
return loginResponse
|
return loginResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendLoginRequestSasjs(
|
|
||||||
username: string,
|
|
||||||
password: string,
|
|
||||||
clientId: string
|
|
||||||
) {
|
|
||||||
const authCode = await getAuthCodeForSasjs(
|
|
||||||
this.requestClient,
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
clientId
|
|
||||||
)
|
|
||||||
return getAccessTokenForSasjs(this.requestClient, clientId, authCode)
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a session is active, or login is required.
|
* Checks whether a session is active, or login is required.
|
||||||
|
* @param accessToken - an optional access token is required for SASjs server type.
|
||||||
* @returns - a promise which resolves with an object containing three values
|
* @returns - a promise which resolves with an object containing three values
|
||||||
* - a boolean `isLoggedIn`
|
* - a boolean `isLoggedIn`
|
||||||
* - a string `userName` and
|
* - a string `userName` and
|
||||||
* - a form `loginForm` if not loggedin.
|
* - a form `loginForm` if not loggedin.
|
||||||
*/
|
*/
|
||||||
public async checkSession(): Promise<{
|
public async checkSession(accessToken?: string): Promise<{
|
||||||
isLoggedIn: boolean
|
isLoggedIn: boolean
|
||||||
userName: string
|
userName: string
|
||||||
loginForm?: any
|
loginForm?: any
|
||||||
}> {
|
}> {
|
||||||
const { isLoggedIn, userName } = await this.fetchUserName()
|
const { isLoggedIn, userName } = await this.fetchUserName(accessToken)
|
||||||
let loginForm = null
|
let loginForm = null
|
||||||
|
|
||||||
if (!isLoggedIn) {
|
if (!isLoggedIn && this.serverType !== ServerType.Sasjs) {
|
||||||
//We will logout to make sure cookies are removed and login form is presented
|
//We will logout to make sure cookies are removed and login form is presented
|
||||||
//Residue can happen in case of session expiration
|
//Residue can happen in case of session expiration
|
||||||
await this.logOut()
|
await this.logOut()
|
||||||
|
|
||||||
if (this.serverType !== ServerType.Sasjs)
|
|
||||||
loginForm = await this.getNewLoginForm()
|
loginForm = await this.getNewLoginForm()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,7 +221,7 @@ export class AuthManager {
|
|||||||
return await this.getLoginForm(formResponse)
|
return await this.getLoginForm(formResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
private async fetchUserName(): Promise<{
|
private async fetchUserName(accessToken?: string): Promise<{
|
||||||
isLoggedIn: boolean
|
isLoggedIn: boolean
|
||||||
userName: string
|
userName: string
|
||||||
}> {
|
}> {
|
||||||
@@ -280,8 +232,9 @@ export class AuthManager {
|
|||||||
? `${this.serverUrl}/SASStoredProcess`
|
? `${this.serverUrl}/SASStoredProcess`
|
||||||
: `${this.serverUrl}/SASjsApi/session`
|
: `${this.serverUrl}/SASjsApi/session`
|
||||||
|
|
||||||
|
// Access token is required for server type `SASjs`
|
||||||
const { result: loginResponse } = await this.requestClient
|
const { result: loginResponse } = await this.requestClient
|
||||||
.get<string>(url, undefined, 'text/plain')
|
.get<string>(url, accessToken, 'text/plain')
|
||||||
.catch((err: any) => {
|
.catch((err: any) => {
|
||||||
return { result: 'authErr' }
|
return { result: 'authErr' }
|
||||||
})
|
})
|
||||||
@@ -362,19 +315,11 @@ export class AuthManager {
|
|||||||
* Logs out of the configured SAS server.
|
* Logs out of the configured SAS server.
|
||||||
* @param accessToken - an optional access token is required for SASjs server type.
|
* @param accessToken - an optional access token is required for SASjs server type.
|
||||||
*/
|
*/
|
||||||
public async logOut() {
|
public logOut(accessToken?: string) {
|
||||||
if (this.serverType === ServerType.Sasjs) {
|
if (this.serverType === ServerType.Sasjs) {
|
||||||
return this.requestClient
|
return this.requestClient.post(this.logoutUrl, undefined, accessToken)
|
||||||
.delete(this.logoutUrl)
|
|
||||||
.catch(() => true)
|
|
||||||
.finally(() => {
|
|
||||||
this.requestClient.clearLocalStorageTokens()
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.requestClient.clearCsrfTokens()
|
this.requestClient.clearCsrfTokens()
|
||||||
|
|
||||||
return this.requestClient.get(this.logoutUrl, undefined).then(() => true)
|
return this.requestClient.get(this.logoutUrl, undefined).then(() => true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { RequestClient } from '../request/RequestClient'
|
|||||||
* @param clientSecret - the client secret to authenticate with.
|
* @param clientSecret - the client secret to authenticate with.
|
||||||
* @param authCode - the auth code received from the server.
|
* @param authCode - the auth code received from the server.
|
||||||
*/
|
*/
|
||||||
export async function getAccessTokenForViya(
|
export async function getAccessToken(
|
||||||
requestClient: RequestClient,
|
requestClient: RequestClient,
|
||||||
clientId: string,
|
clientId: string,
|
||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { prefixMessage } from '@sasjs/utils/error'
|
|
||||||
import { RequestClient } from '../request/RequestClient'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exchanges the auth code for an access token for the given client.
|
|
||||||
* @param requestClient - the pre-configured HTTP request client
|
|
||||||
* @param clientId - the client ID to authenticate with.
|
|
||||||
* @param authCode - the auth code received from the server.
|
|
||||||
*/
|
|
||||||
export async function getAccessTokenForSasjs(
|
|
||||||
requestClient: RequestClient,
|
|
||||||
clientId: string,
|
|
||||||
authCode: string
|
|
||||||
) {
|
|
||||||
const url = '/SASjsApi/auth/token'
|
|
||||||
const data = {
|
|
||||||
clientId,
|
|
||||||
code: authCode
|
|
||||||
}
|
|
||||||
|
|
||||||
return await requestClient
|
|
||||||
.post(url, data, undefined)
|
|
||||||
.then((res) => {
|
|
||||||
const sasAuth = res.result as {
|
|
||||||
accessToken: string
|
|
||||||
refreshToken: string
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
access_token: sasAuth.accessToken,
|
|
||||||
refresh_token: sasAuth.refreshToken
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
throw prefixMessage(err, 'Error while getting access token. ')
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { prefixMessage } from '@sasjs/utils/error'
|
|
||||||
import { RequestClient } from '../request/RequestClient'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs a login authenticate and returns an auth code for the given client.
|
|
||||||
* @param requestClient - the pre-configured HTTP request client
|
|
||||||
* @param username - a string representing the username.
|
|
||||||
* @param password - a string representing the password.
|
|
||||||
* @param clientId - the client ID to authenticate with.
|
|
||||||
*/
|
|
||||||
export const getAuthCodeForSasjs = async (
|
|
||||||
requestClient: RequestClient,
|
|
||||||
username: string,
|
|
||||||
password: string,
|
|
||||||
clientId: string
|
|
||||||
) => {
|
|
||||||
const url = '/SASjsApi/auth/authorize'
|
|
||||||
const data = { username, password, clientId }
|
|
||||||
|
|
||||||
const { code: authCode } = await requestClient
|
|
||||||
.post<{ code: string }>(url, data, undefined)
|
|
||||||
.then((res) => res.result)
|
|
||||||
.catch((err) => {
|
|
||||||
throw prefixMessage(
|
|
||||||
err,
|
|
||||||
'Error while authenticating with provided username, password and clientId. '
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
return authCode
|
|
||||||
}
|
|
||||||
@@ -3,21 +3,18 @@ import {
|
|||||||
isRefreshTokenExpiring,
|
isRefreshTokenExpiring,
|
||||||
hasTokenExpired
|
hasTokenExpired
|
||||||
} from '@sasjs/utils/auth'
|
} from '@sasjs/utils/auth'
|
||||||
import { AuthConfig, ServerType } from '@sasjs/utils/types'
|
import { AuthConfig } from '@sasjs/utils/types'
|
||||||
import { RequestClient } from '../request/RequestClient'
|
import { RequestClient } from '../request/RequestClient'
|
||||||
import { refreshTokensForViya } from './refreshTokensForViya'
|
import { refreshTokens } from './refreshTokens'
|
||||||
import { refreshTokensForSasjs } from './refreshTokensForSasjs'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the auth configuration, refreshing the tokens if necessary.
|
* Returns the auth configuration, refreshing the tokens if necessary.
|
||||||
* @param requestClient - the pre-configured HTTP request client
|
* @param requestClient - the pre-configured HTTP request client
|
||||||
* @param authConfig - an object containing a client ID, secret, access token and refresh token
|
* @param authConfig - an object containing a client ID, secret, access token and refresh token
|
||||||
* @param serverType - server type for which refreshing the tokens, defaults to SASVIYA
|
|
||||||
*/
|
*/
|
||||||
export async function getTokens(
|
export async function getTokens(
|
||||||
requestClient: RequestClient,
|
requestClient: RequestClient,
|
||||||
authConfig: AuthConfig,
|
authConfig: AuthConfig
|
||||||
serverType: ServerType = ServerType.SasViya
|
|
||||||
): Promise<AuthConfig> {
|
): Promise<AuthConfig> {
|
||||||
const logger = process.logger || console
|
const logger = process.logger || console
|
||||||
let { access_token, refresh_token, client, secret } = authConfig
|
let { access_token, refresh_token, client, secret } = authConfig
|
||||||
@@ -32,16 +29,12 @@ export async function getTokens(
|
|||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
}
|
}
|
||||||
logger.info('Refreshing access and refresh tokens.')
|
logger.info('Refreshing access and refresh tokens.')
|
||||||
const tokens =
|
;({ access_token, refresh_token } = await refreshTokens(
|
||||||
serverType === ServerType.SasViya
|
|
||||||
? await refreshTokensForViya(
|
|
||||||
requestClient,
|
requestClient,
|
||||||
client,
|
client,
|
||||||
secret,
|
secret,
|
||||||
refresh_token
|
refresh_token
|
||||||
)
|
))
|
||||||
: await refreshTokensForSasjs(requestClient, refresh_token)
|
|
||||||
;({ access_token, refresh_token } = tokens)
|
|
||||||
}
|
}
|
||||||
return { access_token, refresh_token, client, secret }
|
return { access_token, refresh_token, client, secret }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import { RequestClient } from '../request/RequestClient'
|
|||||||
* @param requestClient - the pre-configured HTTP request client
|
* @param requestClient - the pre-configured HTTP request client
|
||||||
* @param clientId - the client ID to authenticate with.
|
* @param clientId - the client ID to authenticate with.
|
||||||
* @param clientSecret - the client secret to authenticate with.
|
* @param clientSecret - the client secret to authenticate with.
|
||||||
* @param refreshToken - the refresh token received from the server.
|
* @param authCode - the refresh token received from the server.
|
||||||
*/
|
*/
|
||||||
export async function refreshTokensForViya(
|
export async function refreshTokens(
|
||||||
requestClient: RequestClient,
|
requestClient: RequestClient,
|
||||||
clientId: string,
|
clientId: string,
|
||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import { prefixMessage } from '@sasjs/utils/error'
|
|
||||||
import { RequestClient } from '../request/RequestClient'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exchanges the refresh token for an access token for the given client.
|
|
||||||
* @param requestClient - the pre-configured HTTP request client
|
|
||||||
* @param refreshToken - the refresh token received from the server.
|
|
||||||
*/
|
|
||||||
export async function refreshTokensForSasjs(
|
|
||||||
requestClient: RequestClient,
|
|
||||||
refreshToken: string
|
|
||||||
) {
|
|
||||||
const url = '/SASjsApi/auth/refresh'
|
|
||||||
const headers = {
|
|
||||||
Authorization: 'Bearer ' + refreshToken
|
|
||||||
}
|
|
||||||
|
|
||||||
const authResponse = await requestClient
|
|
||||||
.post(url, undefined, undefined, undefined, headers)
|
|
||||||
.then((res) => {
|
|
||||||
const sasAuth = res.result as {
|
|
||||||
accessToken: string
|
|
||||||
refreshToken: string
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
access_token: sasAuth.accessToken,
|
|
||||||
refresh_token: sasAuth.refreshToken
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
throw prefixMessage(err, 'Error while refreshing tokens')
|
|
||||||
})
|
|
||||||
|
|
||||||
return authResponse
|
|
||||||
}
|
|
||||||
@@ -2,11 +2,11 @@ import { AuthConfig } from '@sasjs/utils'
|
|||||||
import * as NodeFormData from 'form-data'
|
import * as NodeFormData from 'form-data'
|
||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
import { generateToken, mockAuthResponse } from './mockResponses'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { getAccessTokenForViya } from '../getAccessTokenForViya'
|
import { getAccessToken } from '../getAccessToken'
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
|
|
||||||
describe('getAccessTokenForViya', () => {
|
describe('getAccessToken', () => {
|
||||||
it('should attempt to refresh tokens', async () => {
|
it('should attempt to refresh tokens', async () => {
|
||||||
setupMocks()
|
setupMocks()
|
||||||
const access_token = generateToken(30)
|
const access_token = generateToken(30)
|
||||||
@@ -26,7 +26,7 @@ describe('getAccessTokenForViya', () => {
|
|||||||
authConfig.client + ':' + authConfig.secret
|
authConfig.client + ':' + authConfig.secret
|
||||||
).toString('base64')
|
).toString('base64')
|
||||||
|
|
||||||
await getAccessTokenForViya(
|
await getAccessToken(
|
||||||
requestClient,
|
requestClient,
|
||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
@@ -58,7 +58,7 @@ describe('getAccessTokenForViya', () => {
|
|||||||
.spyOn(requestClient, 'post')
|
.spyOn(requestClient, 'post')
|
||||||
.mockImplementation(() => Promise.reject('Token Error'))
|
.mockImplementation(() => Promise.reject('Token Error'))
|
||||||
|
|
||||||
const error = await getAccessTokenForViya(
|
const error = await getAccessToken(
|
||||||
requestClient,
|
requestClient,
|
||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
import { AuthConfig } from '@sasjs/utils'
|
|
||||||
import { generateToken, mockSasjsAuthResponse } from './mockResponses'
|
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
|
||||||
import { getAccessTokenForSasjs } from '../getAccessTokenForSasjs'
|
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
|
||||||
|
|
||||||
describe('getAccessTokenForSasjs', () => {
|
|
||||||
it('should attempt to refresh tokens', async () => {
|
|
||||||
setupMocks()
|
|
||||||
const access_token = generateToken(30)
|
|
||||||
const refresh_token = generateToken(30)
|
|
||||||
const authConfig: AuthConfig = {
|
|
||||||
access_token,
|
|
||||||
refresh_token,
|
|
||||||
client: 'cl13nt',
|
|
||||||
secret: 's3cr3t'
|
|
||||||
}
|
|
||||||
jest
|
|
||||||
.spyOn(requestClient, 'post')
|
|
||||||
.mockImplementation(() =>
|
|
||||||
Promise.resolve({ result: mockSasjsAuthResponse, etag: '' })
|
|
||||||
)
|
|
||||||
|
|
||||||
await getAccessTokenForSasjs(
|
|
||||||
requestClient,
|
|
||||||
authConfig.client,
|
|
||||||
authConfig.refresh_token
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(requestClient.post).toHaveBeenCalledWith(
|
|
||||||
'/SASjsApi/auth/token',
|
|
||||||
{ clientId: authConfig.client, code: authConfig.refresh_token },
|
|
||||||
undefined
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle errors while refreshing tokens', async () => {
|
|
||||||
setupMocks()
|
|
||||||
const access_token = generateToken(30)
|
|
||||||
const refresh_token = generateToken(30)
|
|
||||||
const authConfig: AuthConfig = {
|
|
||||||
access_token,
|
|
||||||
refresh_token,
|
|
||||||
client: 'cl13nt',
|
|
||||||
secret: 's3cr3t'
|
|
||||||
}
|
|
||||||
jest
|
|
||||||
.spyOn(requestClient, 'post')
|
|
||||||
.mockImplementation(() => Promise.reject('Token Error'))
|
|
||||||
|
|
||||||
const error = await getAccessTokenForSasjs(
|
|
||||||
requestClient,
|
|
||||||
authConfig.client,
|
|
||||||
authConfig.refresh_token
|
|
||||||
).catch((e) => e)
|
|
||||||
|
|
||||||
expect(error).toContain('Error while getting access token')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
const setupMocks = () => {
|
|
||||||
jest.restoreAllMocks()
|
|
||||||
jest.mock('../../request/RequestClient')
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { AuthConfig } from '@sasjs/utils'
|
import { AuthConfig } from '@sasjs/utils'
|
||||||
import * as refreshTokensModule from '../refreshTokensForViya'
|
import * as refreshTokensModule from '../refreshTokens'
|
||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
import { generateToken, mockAuthResponse } from './mockResponses'
|
||||||
import { getTokens } from '../getTokens'
|
import { getTokens } from '../getTokens'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
@@ -20,7 +20,7 @@ describe('getTokens', () => {
|
|||||||
|
|
||||||
await getTokens(requestClient, authConfig)
|
await getTokens(requestClient, authConfig)
|
||||||
|
|
||||||
expect(refreshTokensModule.refreshTokensForViya).toHaveBeenCalledWith(
|
expect(refreshTokensModule.refreshTokens).toHaveBeenCalledWith(
|
||||||
requestClient,
|
requestClient,
|
||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
@@ -41,7 +41,7 @@ describe('getTokens', () => {
|
|||||||
|
|
||||||
await getTokens(requestClient, authConfig)
|
await getTokens(requestClient, authConfig)
|
||||||
|
|
||||||
expect(refreshTokensModule.refreshTokensForViya).toHaveBeenCalledWith(
|
expect(refreshTokensModule.refreshTokens).toHaveBeenCalledWith(
|
||||||
requestClient,
|
requestClient,
|
||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
@@ -71,9 +71,9 @@ describe('getTokens', () => {
|
|||||||
const setupMocks = () => {
|
const setupMocks = () => {
|
||||||
jest.restoreAllMocks()
|
jest.restoreAllMocks()
|
||||||
jest.mock('../../request/RequestClient')
|
jest.mock('../../request/RequestClient')
|
||||||
jest.mock('../refreshTokensForViya')
|
jest.mock('../refreshTokens')
|
||||||
|
|
||||||
jest
|
jest
|
||||||
.spyOn(refreshTokensModule, 'refreshTokensForViya')
|
.spyOn(refreshTokensModule, 'refreshTokens')
|
||||||
.mockImplementation(() => Promise.resolve(mockAuthResponse))
|
.mockImplementation(() => Promise.resolve(mockAuthResponse))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,6 @@ export const mockAuthResponse: SasAuthResponse = {
|
|||||||
jti: 'test'
|
jti: 'test'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mockSasjsAuthResponse = {
|
|
||||||
access_token: 'acc355',
|
|
||||||
refresh_token: 'r3fr35h'
|
|
||||||
}
|
|
||||||
|
|
||||||
export const generateToken = (timeToLiveSeconds: number): string => {
|
export const generateToken = (timeToLiveSeconds: number): string => {
|
||||||
const exp =
|
const exp =
|
||||||
new Date(new Date().getTime() + timeToLiveSeconds * 1000).getTime() / 1000
|
new Date(new Date().getTime() + timeToLiveSeconds * 1000).getTime() / 1000
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import { AuthConfig } from '@sasjs/utils'
|
|||||||
import * as NodeFormData from 'form-data'
|
import * as NodeFormData from 'form-data'
|
||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
import { generateToken, mockAuthResponse } from './mockResponses'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { refreshTokensForViya } from '../refreshTokensForViya'
|
import { refreshTokens } from '../refreshTokens'
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
|
|
||||||
describe('refreshTokensForViya', () => {
|
describe('refreshTokens', () => {
|
||||||
it('should attempt to refresh tokens', async () => {
|
it('should attempt to refresh tokens', async () => {
|
||||||
setupMocks()
|
setupMocks()
|
||||||
const access_token = generateToken(30)
|
const access_token = generateToken(30)
|
||||||
@@ -26,7 +26,7 @@ describe('refreshTokensForViya', () => {
|
|||||||
authConfig.client + ':' + authConfig.secret
|
authConfig.client + ':' + authConfig.secret
|
||||||
).toString('base64')
|
).toString('base64')
|
||||||
|
|
||||||
await refreshTokensForViya(
|
await refreshTokens(
|
||||||
requestClient,
|
requestClient,
|
||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
@@ -58,7 +58,7 @@ describe('refreshTokensForViya', () => {
|
|||||||
.spyOn(requestClient, 'post')
|
.spyOn(requestClient, 'post')
|
||||||
.mockImplementation(() => Promise.reject('Token Error'))
|
.mockImplementation(() => Promise.reject('Token Error'))
|
||||||
|
|
||||||
const error = await refreshTokensForViya(
|
const error = await refreshTokens(
|
||||||
requestClient,
|
requestClient,
|
||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
|
||||||
import { refreshTokensForSasjs } from '../refreshTokensForSasjs'
|
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
|
||||||
|
|
||||||
describe('refreshTokensForSasjs', () => {
|
|
||||||
it('should attempt to refresh tokens', async () => {
|
|
||||||
setupMocks()
|
|
||||||
const refresh_token = generateToken(30)
|
|
||||||
jest
|
|
||||||
.spyOn(requestClient, 'post')
|
|
||||||
.mockImplementation(() =>
|
|
||||||
Promise.resolve({ result: mockAuthResponse, etag: '' })
|
|
||||||
)
|
|
||||||
|
|
||||||
await refreshTokensForSasjs(requestClient, refresh_token)
|
|
||||||
|
|
||||||
expect(requestClient.post).toHaveBeenCalledWith(
|
|
||||||
'/SASjsApi/auth/refresh',
|
|
||||||
undefined,
|
|
||||||
undefined,
|
|
||||||
undefined,
|
|
||||||
{ Authorization: `Bearer ${refresh_token}` }
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should handle errors while refreshing tokens', async () => {
|
|
||||||
setupMocks()
|
|
||||||
const refresh_token = generateToken(30)
|
|
||||||
jest
|
|
||||||
.spyOn(requestClient, 'post')
|
|
||||||
.mockImplementation(() => Promise.reject('Token Error'))
|
|
||||||
|
|
||||||
const error = await refreshTokensForSasjs(
|
|
||||||
requestClient,
|
|
||||||
refresh_token
|
|
||||||
).catch((e) => e)
|
|
||||||
|
|
||||||
expect(error).toContain('Error while refreshing tokens')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
const setupMocks = () => {
|
|
||||||
jest.restoreAllMocks()
|
|
||||||
jest.mock('../../request/RequestClient')
|
|
||||||
}
|
|
||||||
@@ -144,6 +144,7 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const requestPromise = new Promise((resolve, reject) => {
|
const requestPromise = new Promise((resolve, reject) => {
|
||||||
|
// Access token is required for server type `SASjs`
|
||||||
this.requestClient!.post(apiUrl, formData, authConfig?.access_token)
|
this.requestClient!.post(apiUrl, formData, authConfig?.access_token)
|
||||||
.then(async (res: any) => {
|
.then(async (res: any) => {
|
||||||
const resObj =
|
const resObj =
|
||||||
@@ -157,6 +158,11 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
|
|
||||||
let jsonResponse = res.result
|
let jsonResponse = res.result
|
||||||
|
|
||||||
|
if (this.serverType === ServerType.Sasjs) {
|
||||||
|
const webout = parseWeboutResponse(res.result._webout, apiUrl)
|
||||||
|
jsonResponse = getValidJson(webout)
|
||||||
|
}
|
||||||
|
|
||||||
if (config.debug) {
|
if (config.debug) {
|
||||||
switch (this.serverType) {
|
switch (this.serverType) {
|
||||||
case ServerType.SasViya:
|
case ServerType.SasViya:
|
||||||
@@ -172,13 +178,7 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
? parseWeboutResponse(res.result, apiUrl)
|
? parseWeboutResponse(res.result, apiUrl)
|
||||||
: res.result
|
: res.result
|
||||||
break
|
break
|
||||||
case ServerType.Sasjs:
|
|
||||||
const webout = parseWeboutResponse(res.result._webout, apiUrl)
|
|
||||||
jsonResponse = getValidJson(webout)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
} else if (this.serverType === ServerType.Sasjs) {
|
|
||||||
jsonResponse = getValidJson(res.result._webout)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseObject = appendExtraResponseAttributes(
|
const responseObject = appendExtraResponseAttributes(
|
||||||
|
|||||||
@@ -48,9 +48,7 @@ export interface HttpClient {
|
|||||||
): Promise<{ result: T; etag: string }>
|
): Promise<{ result: T; etag: string }>
|
||||||
|
|
||||||
getCsrfToken(type: 'general' | 'file'): CsrfToken | undefined
|
getCsrfToken(type: 'general' | 'file'): CsrfToken | undefined
|
||||||
saveLocalStorageToken(accessToken: string, refreshToken: string): void
|
|
||||||
clearCsrfTokens(): void
|
clearCsrfTokens(): void
|
||||||
clearLocalStorageTokens(): void
|
|
||||||
getBaseUrl(): string
|
getBaseUrl(): string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,11 +70,6 @@ export class RequestClient implements HttpClient {
|
|||||||
this.createHttpClient(baseUrl, httpsAgentOptions)
|
this.createHttpClient(baseUrl, httpsAgentOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
public saveLocalStorageToken(accessToken: string, refreshToken: string) {
|
|
||||||
localStorage.setItem('accessToken', accessToken)
|
|
||||||
localStorage.setItem('refreshToken', refreshToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
public getCsrfToken(type: 'general' | 'file' = 'general') {
|
public getCsrfToken(type: 'general' | 'file' = 'general') {
|
||||||
return type === 'file' ? this.fileUploadCsrfToken : this.csrfToken
|
return type === 'file' ? this.fileUploadCsrfToken : this.csrfToken
|
||||||
}
|
}
|
||||||
@@ -85,10 +78,6 @@ export class RequestClient implements HttpClient {
|
|||||||
this.csrfToken = { headerName: '', value: '' }
|
this.csrfToken = { headerName: '', value: '' }
|
||||||
this.fileUploadCsrfToken = { headerName: '', value: '' }
|
this.fileUploadCsrfToken = { headerName: '', value: '' }
|
||||||
}
|
}
|
||||||
public clearLocalStorageTokens() {
|
|
||||||
localStorage.setItem('accessToken', '')
|
|
||||||
localStorage.setItem('refreshToken', '')
|
|
||||||
}
|
|
||||||
|
|
||||||
public getBaseUrl() {
|
public getBaseUrl() {
|
||||||
return this.httpClient.defaults.baseURL || ''
|
return this.httpClient.defaults.baseURL || ''
|
||||||
@@ -195,7 +184,9 @@ export class RequestClient implements HttpClient {
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
debug
|
debug
|
||||||
)
|
).catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while handling error. ')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +206,6 @@ export class RequestClient implements HttpClient {
|
|||||||
.post<T>(url, data, { headers, withCredentials: true })
|
.post<T>(url, data, { headers, withCredentials: true })
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
throwIfError(response)
|
throwIfError(response)
|
||||||
|
|
||||||
return this.parseResponse<T>(response)
|
return this.parseResponse<T>(response)
|
||||||
})
|
})
|
||||||
.catch(async (e) => {
|
.catch(async (e) => {
|
||||||
@@ -465,8 +455,6 @@ export class RequestClient implements HttpClient {
|
|||||||
|
|
||||||
if (e instanceof LoginRequiredError) {
|
if (e instanceof LoginRequiredError) {
|
||||||
this.clearCsrfTokens()
|
this.clearCsrfTokens()
|
||||||
|
|
||||||
throw e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response?.status === 403 || response?.status === 449) {
|
if (response?.status === 403 || response?.status === 449) {
|
||||||
@@ -489,8 +477,7 @@ export class RequestClient implements HttpClient {
|
|||||||
else return
|
else return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.message) throw e
|
throw e
|
||||||
else throw prefixMessage(e, 'Error while handling error. ')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected parseResponse<T>(response: AxiosResponse<any>) {
|
protected parseResponse<T>(response: AxiosResponse<any>) {
|
||||||
@@ -542,9 +529,8 @@ export class RequestClient implements HttpClient {
|
|||||||
|
|
||||||
this.httpClient = createAxiosInstance(baseUrl, httpsAgent)
|
this.httpClient = createAxiosInstance(baseUrl, httpsAgent)
|
||||||
|
|
||||||
this.httpClient.defaults.validateStatus = (status) => {
|
this.httpClient.defaults.validateStatus = (status) =>
|
||||||
return status >= 200 && status <= 401
|
status >= 200 && status < 401
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -641,7 +627,7 @@ const parseError = (data: string) => {
|
|||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
const log = parts[1].split('<pre>')[1].split('</pre>')[0]
|
const log = parts[1].split('<pre>')[1].split('</pre>')[0]
|
||||||
const message = `This request completed with errors.`
|
const message = `This request completed with errors.`
|
||||||
return new JobExecutionError(404, message, log)
|
return new JobExecutionError(500, message, log)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export class Sas9RequestClient extends RequestClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async post<T>(
|
public post<T>(
|
||||||
url: string,
|
url: string,
|
||||||
data: any,
|
data: any,
|
||||||
accessToken: string | undefined,
|
accessToken: string | undefined,
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user