mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
Merge branch 'api-execution' of https://github.com/sasjs/adapter into api-execution
This commit is contained in:
@@ -20,6 +20,7 @@ export class SASViyaApiClient {
|
||||
private serverUrl: string,
|
||||
private rootFolderName: string,
|
||||
private contextName: string,
|
||||
private setCsrfToken: (csrfToken: CsrfToken) => void,
|
||||
private rootFolderMap = new Map<string, Job[]>()
|
||||
) {
|
||||
if (!rootFolderName) {
|
||||
@@ -28,7 +29,7 @@ export class SASViyaApiClient {
|
||||
}
|
||||
private csrfToken: CsrfToken | null = null;
|
||||
private rootFolder: Folder | null = null;
|
||||
private sessionManager = new SessionManager(this.serverUrl, this.contextName);
|
||||
private sessionManager = new SessionManager(this.serverUrl, this.contextName, this.setCsrfToken);
|
||||
|
||||
/**
|
||||
* Returns a map containing the directory structure in the currently set root folder.
|
||||
@@ -1025,8 +1026,9 @@ export class SASViyaApiClient {
|
||||
return `/folders/folders/${folder.id}`;
|
||||
}
|
||||
|
||||
setCsrfToken = (csrfToken: CsrfToken) => {
|
||||
setCsrfTokenLocal = (csrfToken: CsrfToken) => {
|
||||
this.csrfToken = csrfToken;
|
||||
this.setCsrfToken(csrfToken);
|
||||
};
|
||||
|
||||
private async request<T>(
|
||||
@@ -1040,6 +1042,6 @@ export class SASViyaApiClient {
|
||||
[this.csrfToken.headerName]: this.csrfToken.value,
|
||||
};
|
||||
}
|
||||
return await makeRequest<T>(url, options, this.setCsrfToken, contentType);
|
||||
return await makeRequest<T>(url, options, this.setCsrfTokenLocal, contentType);
|
||||
}
|
||||
}
|
||||
|
||||
32
src/SASjs.ts
32
src/SASjs.ts
@@ -20,6 +20,7 @@ import {
|
||||
SASjsRequest,
|
||||
SASjsWaitingRequest,
|
||||
ServerType,
|
||||
CsrfToken,
|
||||
} from "./types";
|
||||
import { SASViyaApiClient } from "./SASViyaApiClient";
|
||||
import { SAS9ApiClient } from "./SAS9ApiClient";
|
||||
@@ -46,8 +47,7 @@ export default class SASjs {
|
||||
private jobsPath: string = "";
|
||||
private logoutUrl: string = "";
|
||||
private loginUrl: string = "";
|
||||
private _csrf: string | null = null;
|
||||
private _csrfHeader: string | null = null;
|
||||
private csrfToken: CsrfToken | null = null;
|
||||
private retryCount: number = 0;
|
||||
private sasjsRequests: SASjsRequest[] = [];
|
||||
private sasjsWaitingRequests: SASjsWaitingRequest[] = [];
|
||||
@@ -239,7 +239,7 @@ export default class SASjs {
|
||||
*
|
||||
*/
|
||||
public getCsrf() {
|
||||
return this._csrf;
|
||||
return this.csrfToken?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -452,7 +452,8 @@ export default class SASjs {
|
||||
sasApiClient = new SASViyaApiClient(
|
||||
serverUrl,
|
||||
appLoc,
|
||||
this.sasjsConfig.contextName
|
||||
this.sasjsConfig.contextName,
|
||||
this.setCsrfToken
|
||||
);
|
||||
} else if (this.sasjsConfig.serverType === ServerType.SAS9) {
|
||||
sasApiClient = new SAS9ApiClient(serverUrl);
|
||||
@@ -517,7 +518,7 @@ export default class SASjs {
|
||||
return JSON.parse(response!.result);
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e && e.includes(401)) {
|
||||
if (e && e.status === 401) {
|
||||
if (loginRequiredCallback) loginRequiredCallback(true);
|
||||
sasjsWaitingRequest.requestPromise.resolve = resolve;
|
||||
sasjsWaitingRequest.requestPromise.reject = reject;
|
||||
@@ -698,8 +699,8 @@ export default class SASjs {
|
||||
reject({ MESSAGE: errorMsg });
|
||||
}
|
||||
const headers: any = {};
|
||||
if (this._csrfHeader && this._csrf) {
|
||||
headers[this._csrfHeader] = this._csrf;
|
||||
if (this.csrfToken) {
|
||||
headers[this.csrfToken.headerName] = this.csrfToken.value;
|
||||
}
|
||||
fetch(apiUrl, {
|
||||
method: "POST",
|
||||
@@ -714,8 +715,10 @@ export default class SASjs {
|
||||
|
||||
if (tokenHeader) {
|
||||
const token = response.headers.get(tokenHeader);
|
||||
this._csrfHeader = tokenHeader;
|
||||
this._csrf = token;
|
||||
this.csrfToken = {
|
||||
headerName: tokenHeader,
|
||||
value: token || ''
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -810,6 +813,10 @@ export default class SASjs {
|
||||
return sasjsWaitingRequest.requestPromise.promise;
|
||||
}
|
||||
|
||||
private setCsrfToken = (csrfToken: CsrfToken) => {
|
||||
this.csrfToken = csrfToken;
|
||||
};
|
||||
|
||||
private async resendWaitingRequests() {
|
||||
for (const sasjsWaitingRequest of this.sasjsWaitingRequests) {
|
||||
this.request(
|
||||
@@ -832,8 +839,8 @@ export default class SASjs {
|
||||
private getRequestParams(): any {
|
||||
const requestParams: any = {};
|
||||
|
||||
if (this._csrf) {
|
||||
requestParams["_csrf"] = this._csrf;
|
||||
if (this.csrfToken) {
|
||||
requestParams["_csrf"] = this.csrfToken.value;
|
||||
}
|
||||
|
||||
if (this.sasjsConfig.debug) {
|
||||
@@ -1070,7 +1077,8 @@ export default class SASjs {
|
||||
this.sasViyaApiClient = new SASViyaApiClient(
|
||||
this.sasjsConfig.serverUrl,
|
||||
this.sasjsConfig.appLoc,
|
||||
this.sasjsConfig.contextName
|
||||
this.sasjsConfig.contextName,
|
||||
this.setCsrfToken
|
||||
);
|
||||
}
|
||||
if (this.sasjsConfig.serverType === ServerType.SAS9) {
|
||||
|
||||
@@ -4,7 +4,11 @@ import { asyncForEach, makeRequest } from "./utils";
|
||||
const MAX_SESSION_COUNT = 1;
|
||||
|
||||
export class SessionManager {
|
||||
constructor(private serverUrl: string, private contextName: string) {}
|
||||
constructor(
|
||||
private serverUrl: string,
|
||||
private contextName: string,
|
||||
private setCsrfToken: (csrfToken: CsrfToken) => void
|
||||
) {}
|
||||
private sessions: Session[] = [];
|
||||
private currentContext: Context | null = null;
|
||||
private csrfToken: CsrfToken | null = null;
|
||||
@@ -132,7 +136,10 @@ export class SessionManager {
|
||||
return await makeRequest<T>(
|
||||
url,
|
||||
options,
|
||||
(token) => (this.csrfToken = token),
|
||||
(token) => {
|
||||
this.csrfToken = token;
|
||||
this.setCsrfToken(token);
|
||||
},
|
||||
contentType
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user