@sasjs/adapter
Version:
JavaScript adapter for SAS
121 lines (120 loc) • 5.01 kB
TypeScript
/// <reference types="node" />
import { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import * as https from 'https';
import { CsrfToken } from '..';
import { SASjsRequest, HttpClient, VerboseMode } from '../types';
export declare class RequestClient implements HttpClient {
protected baseUrl: string;
private requests;
private requestsLimit;
private httpInterceptor?;
private verboseMode;
protected csrfToken: CsrfToken;
protected fileUploadCsrfToken: CsrfToken | undefined;
protected httpClient: AxiosInstance;
constructor(baseUrl: string, httpsAgentOptions?: https.AgentOptions, requestsLimit?: number, verboseMode?: VerboseMode);
setConfig(baseUrl: string, httpsAgentOptions?: https.AgentOptions): void;
saveLocalStorageToken(accessToken: string, refreshToken: string): void;
getCsrfToken(type?: 'general' | 'file'): CsrfToken | undefined;
clearCsrfTokens(): void;
clearLocalStorageTokens(): void;
getBaseUrl(): string;
/**
* this method returns all requests, an array of SASjsRequest type
* @returns SASjsRequest[]
*/
getRequests: () => SASjsRequest[];
/**
* this method clears the requests array, i.e set to empty
*/
clearRequests: () => void;
/**
* this method appends the response from sasjs request to requests array
* @param response - response from sasjs request
* @param program - name of program
* @param debug - a boolean that indicates whether debug was enabled or not
*/
appendRequest(response: any, program: string, debug: boolean): void;
get<T>(url: string, accessToken: string | undefined, contentType?: string, overrideHeaders?: {
[key: string]: string | number;
}, debug?: boolean): Promise<{
result: T;
etag: string;
status: number;
}>;
/**
* @param contentType Newer version of Axios is more strict so if you don't
* set the contentType to `form data` while sending a FormData object
* application/json will be used by default, axios won’t treat it as FormData.
* Instead, it serializes data as JSON—resulting in a payload like
* {"sometable":{}} and we lose the multipart/form-data formatting.
*/
post<T>(url: string, data: any, accessToken: string | undefined, contentType?: string, overrideHeaders?: {
[key: string]: string | number;
}, additionalSettings?: {
[key: string]: string | number;
}): Promise<{
result: T;
etag: string;
}>;
put<T>(url: string, data: any, accessToken: string | undefined, overrideHeaders?: {
[key: string]: string | number;
}): Promise<{
result: T;
etag: string;
}>;
delete<T>(url: string, accessToken?: string): Promise<{
result: T;
etag: string;
}>;
patch<T>(url: string, data?: any, accessToken?: string): Promise<{
result: T;
etag: string;
}>;
uploadFile(url: string, content: string, accessToken?: string): Promise<any>;
authorize: (response: string) => Promise<any>;
/**
* Adds colors to the string.
* If verboseMode is set to 'bleached', colors should be disabled
* @param str - string to be prettified.
* @returns - prettified string
*/
private prettifyString;
/**
* Formats HTTP request/response body.
* @param body - HTTP request/response body.
* @returns - formatted string.
*/
private parseInterceptedBody;
private handleAxiosResponse;
private handleAxiosError;
private formatHeaders;
/**
* Sets verbose mode.
* @param verboseMode - value of the verbose mode, can be true, false or bleached(without extra colors).
*/
setVerboseMode: (verboseMode: VerboseMode) => void;
/**
* Turns on verbose mode to log every HTTP response.
* @param successCallBack - function that should be triggered on every HTTP response with the status 2**.
* @param errorCallBack - function that should be triggered on every HTTP response with the status different from 2**.
*/
enableVerboseMode: (successCallBack?: (response: AxiosResponse) => AxiosResponse<any, any, {}>, errorCallBack?: (error: AxiosError) => AxiosError<unknown, any>) => void;
/**
* Turns off verbose mode to log every HTTP response.
*/
disableVerboseMode: () => void;
protected getHeaders: (accessToken: string | undefined, contentType: string) => any;
protected parseAndSetFileUploadCsrfToken: (response: AxiosResponse) => void;
protected parseAndSetCsrfToken: (response: AxiosResponse) => void;
private parseCsrfToken;
protected handleError: (e: any, callback: any, debug?: boolean) => Promise<any>;
protected parseResponse<T>(response: AxiosResponse<any>): {
result: T;
etag: any;
log?: string | undefined;
status: number;
};
private createHttpClient;
}
export declare const throwIfError: (response: AxiosResponse) => void;