@sisense/sdk-rest-client 1.21.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/authenticator.d.ts +1 -1
- package/dist/authenticator.js +4 -1
- package/dist/cjs/authenticator.d.ts +13 -0
- package/dist/cjs/authenticator.js +32 -0
- package/dist/cjs/base-authenticator.d.ts +16 -0
- package/dist/cjs/base-authenticator.js +34 -0
- package/dist/cjs/bearer-authenticator.d.ts +16 -0
- package/dist/cjs/bearer-authenticator.js +34 -0
- package/dist/cjs/constants.d.ts +1 -0
- package/dist/cjs/constants.js +4 -0
- package/dist/cjs/fusion-authenticator.d.ts +23 -0
- package/dist/cjs/fusion-authenticator.js +38 -0
- package/dist/cjs/helpers.d.ts +14 -0
- package/dist/cjs/helpers.js +41 -0
- package/dist/cjs/http-client.d.ts +19 -0
- package/dist/cjs/http-client.js +85 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.js +39 -0
- package/dist/cjs/interceptors.d.ts +3 -0
- package/dist/cjs/interceptors.js +69 -0
- package/dist/cjs/interfaces.d.ts +9 -0
- package/dist/cjs/interfaces.js +2 -0
- package/dist/cjs/package.json +12 -0
- package/dist/cjs/password-authenticator.d.ts +18 -0
- package/dist/cjs/password-authenticator.js +70 -0
- package/dist/cjs/sso-authenticator.d.ts +18 -0
- package/dist/cjs/sso-authenticator.js +100 -0
- package/dist/cjs/translation/initialize-i18n.d.ts +2 -0
- package/dist/cjs/translation/initialize-i18n.js +14 -0
- package/dist/cjs/translation/resources/en.d.ts +30 -0
- package/dist/cjs/translation/resources/en.js +18 -0
- package/dist/cjs/translation/resources/index.d.ts +35 -0
- package/dist/cjs/translation/resources/index.js +16 -0
- package/dist/cjs/translation/resources/uk.d.ts +5 -0
- package/dist/cjs/translation/resources/uk.js +18 -0
- package/dist/cjs/translation/translatable-error.d.ts +6 -0
- package/dist/cjs/translation/translatable-error.js +18 -0
- package/dist/cjs/wat-authenticator.d.ts +19 -0
- package/dist/cjs/wat-authenticator.js +78 -0
- package/dist/helpers.js +2 -0
- package/dist/http-client.d.ts +1 -0
- package/dist/http-client.js +10 -4
- package/dist/index.d.ts +1 -0
- package/dist/password-authenticator.js +3 -1
- package/dist/sso-authenticator.js +8 -5
- package/dist/translation/resources/en.d.ts +14 -0
- package/dist/translation/resources/index.d.ts +6 -0
- package/dist/translation/resources/index.js +6 -0
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
- package/dist/wat-authenticator.js +2 -1
- package/package.json +3 -3
package/dist/authenticator.d.ts
CHANGED
|
@@ -9,5 +9,5 @@ declare type AuthenticatorConfig = {
|
|
|
9
9
|
enableSilentPreAuth?: boolean;
|
|
10
10
|
useFusionAuth?: boolean;
|
|
11
11
|
};
|
|
12
|
-
export declare function getAuthenticator({ url, username, password, token, wat, ssoEnabled, enableSilentPreAuth, useFusionAuth, }: AuthenticatorConfig): Authenticator | null;
|
|
12
|
+
export declare function getAuthenticator({ url: rawUrl, username, password, token, wat, ssoEnabled, enableSilentPreAuth, useFusionAuth, }: AuthenticatorConfig): Authenticator | null;
|
|
13
13
|
export {};
|
package/dist/authenticator.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
/* eslint-disable max-params */
|
|
2
|
+
import { normalizeUrl } from '@sisense/sdk-common';
|
|
1
3
|
import { PasswordAuthenticator } from './password-authenticator.js';
|
|
2
4
|
import { BearerAuthenticator } from './bearer-authenticator.js';
|
|
3
5
|
import { WatAuthenticator } from './wat-authenticator.js';
|
|
4
6
|
import { SsoAuthenticator } from './sso-authenticator.js';
|
|
5
7
|
import { FusionAuthenticator } from './fusion-authenticator.js';
|
|
6
|
-
export function getAuthenticator({ url, username, password, token, wat, ssoEnabled = false, enableSilentPreAuth = false, useFusionAuth = false, }) {
|
|
8
|
+
export function getAuthenticator({ url: rawUrl, username, password, token, wat, ssoEnabled = false, enableSilentPreAuth = false, useFusionAuth = false, }) {
|
|
9
|
+
const url = normalizeUrl(rawUrl);
|
|
7
10
|
// sso overrides all other auth methods
|
|
8
11
|
if (ssoEnabled) {
|
|
9
12
|
return new SsoAuthenticator(url, enableSilentPreAuth);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Authenticator } from './interfaces.js';
|
|
2
|
+
declare type AuthenticatorConfig = {
|
|
3
|
+
url: string;
|
|
4
|
+
username?: string;
|
|
5
|
+
password?: string;
|
|
6
|
+
token?: string | null;
|
|
7
|
+
wat?: string | null;
|
|
8
|
+
ssoEnabled?: boolean;
|
|
9
|
+
enableSilentPreAuth?: boolean;
|
|
10
|
+
useFusionAuth?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare function getAuthenticator({ url: rawUrl, username, password, token, wat, ssoEnabled, enableSilentPreAuth, useFusionAuth, }: AuthenticatorConfig): Authenticator | null;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAuthenticator = void 0;
|
|
4
|
+
/* eslint-disable max-params */
|
|
5
|
+
const sdk_common_1 = require("@sisense/sdk-common");
|
|
6
|
+
const password_authenticator_js_1 = require("./password-authenticator.js");
|
|
7
|
+
const bearer_authenticator_js_1 = require("./bearer-authenticator.js");
|
|
8
|
+
const wat_authenticator_js_1 = require("./wat-authenticator.js");
|
|
9
|
+
const sso_authenticator_js_1 = require("./sso-authenticator.js");
|
|
10
|
+
const fusion_authenticator_js_1 = require("./fusion-authenticator.js");
|
|
11
|
+
function getAuthenticator({ url: rawUrl, username, password, token, wat, ssoEnabled = false, enableSilentPreAuth = false, useFusionAuth = false, }) {
|
|
12
|
+
const url = (0, sdk_common_1.normalizeUrl)(rawUrl);
|
|
13
|
+
// sso overrides all other auth methods
|
|
14
|
+
if (ssoEnabled) {
|
|
15
|
+
return new sso_authenticator_js_1.SsoAuthenticator(url, enableSilentPreAuth);
|
|
16
|
+
}
|
|
17
|
+
// username/password or tokens are chosen relative to priority
|
|
18
|
+
if (username && password) {
|
|
19
|
+
return new password_authenticator_js_1.PasswordAuthenticator(url, username, password);
|
|
20
|
+
}
|
|
21
|
+
if (token) {
|
|
22
|
+
return new bearer_authenticator_js_1.BearerAuthenticator(url, token);
|
|
23
|
+
}
|
|
24
|
+
if (wat) {
|
|
25
|
+
return new wat_authenticator_js_1.WatAuthenticator(url, wat);
|
|
26
|
+
}
|
|
27
|
+
if (useFusionAuth) {
|
|
28
|
+
return new fusion_authenticator_js_1.FusionAuthenticator();
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
exports.getAuthenticator = getAuthenticator;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Authenticator } from './interfaces.js';
|
|
2
|
+
export declare class BaseAuthenticator implements Authenticator {
|
|
3
|
+
readonly type: Authenticator['type'];
|
|
4
|
+
private _valid;
|
|
5
|
+
protected _authenticating: boolean;
|
|
6
|
+
protected _tried: boolean;
|
|
7
|
+
protected _resolve: (value: boolean) => void;
|
|
8
|
+
protected readonly _result: Promise<boolean>;
|
|
9
|
+
protected constructor(type: Authenticator['type']);
|
|
10
|
+
isValid(): boolean;
|
|
11
|
+
invalidate(): void;
|
|
12
|
+
authenticate(): Promise<boolean>;
|
|
13
|
+
isAuthenticating(): boolean;
|
|
14
|
+
authenticated(): Promise<boolean>;
|
|
15
|
+
applyHeader(headers: HeadersInit): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseAuthenticator = void 0;
|
|
4
|
+
class BaseAuthenticator {
|
|
5
|
+
constructor(type) {
|
|
6
|
+
this._valid = true;
|
|
7
|
+
this._authenticating = false;
|
|
8
|
+
this._tried = false;
|
|
9
|
+
this._result = new Promise((resolve) => {
|
|
10
|
+
this._resolve = resolve;
|
|
11
|
+
});
|
|
12
|
+
this.type = type;
|
|
13
|
+
}
|
|
14
|
+
isValid() {
|
|
15
|
+
return this._valid;
|
|
16
|
+
}
|
|
17
|
+
invalidate() {
|
|
18
|
+
this._valid = false;
|
|
19
|
+
}
|
|
20
|
+
authenticate() {
|
|
21
|
+
return this._result;
|
|
22
|
+
}
|
|
23
|
+
isAuthenticating() {
|
|
24
|
+
return this._authenticating;
|
|
25
|
+
}
|
|
26
|
+
authenticated() {
|
|
27
|
+
return this._result;
|
|
28
|
+
}
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
|
30
|
+
applyHeader(headers) {
|
|
31
|
+
// Do nothing
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.BaseAuthenticator = BaseAuthenticator;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Authenticator } from './interfaces.js';
|
|
2
|
+
import { BaseAuthenticator } from './base-authenticator.js';
|
|
3
|
+
export declare class BearerAuthenticator extends BaseAuthenticator {
|
|
4
|
+
readonly bearer: string;
|
|
5
|
+
constructor(url: string, bearer: string);
|
|
6
|
+
applyHeader(headers: HeadersInit): void;
|
|
7
|
+
authenticate(): Promise<boolean>;
|
|
8
|
+
authenticated(): Promise<boolean>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Checks if an authenticator is a BearerAuthenticator.
|
|
12
|
+
*
|
|
13
|
+
* @param authenticator - The authenticator to check.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare function isBearerAuthenticator(authenticator: Authenticator): authenticator is BearerAuthenticator;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBearerAuthenticator = exports.BearerAuthenticator = void 0;
|
|
4
|
+
const base_authenticator_js_1 = require("./base-authenticator.js");
|
|
5
|
+
const helpers_js_1 = require("./helpers.js");
|
|
6
|
+
class BearerAuthenticator extends base_authenticator_js_1.BaseAuthenticator {
|
|
7
|
+
constructor(url, bearer) {
|
|
8
|
+
super('bearer');
|
|
9
|
+
this.bearer = bearer;
|
|
10
|
+
this._resolve(true);
|
|
11
|
+
}
|
|
12
|
+
applyHeader(headers) {
|
|
13
|
+
const authHeader = 'Bearer ' + this.bearer;
|
|
14
|
+
(0, helpers_js_1.appendHeaders)(headers, { Authorization: authHeader });
|
|
15
|
+
}
|
|
16
|
+
authenticate() {
|
|
17
|
+
// TODO: implement authentication test
|
|
18
|
+
return this._result;
|
|
19
|
+
}
|
|
20
|
+
authenticated() {
|
|
21
|
+
return this._result;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.BearerAuthenticator = BearerAuthenticator;
|
|
25
|
+
/**
|
|
26
|
+
* Checks if an authenticator is a BearerAuthenticator.
|
|
27
|
+
*
|
|
28
|
+
* @param authenticator - The authenticator to check.
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
function isBearerAuthenticator(authenticator) {
|
|
32
|
+
return authenticator.type === 'bearer';
|
|
33
|
+
}
|
|
34
|
+
exports.isBearerAuthenticator = isBearerAuthenticator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ERROR_PREFIX = "[request-error]";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { Authenticator } from './interfaces.js';
|
|
3
|
+
import { BaseAuthenticator } from './base-authenticator.js';
|
|
4
|
+
export declare type FusionWindow = Window & typeof globalThis & {
|
|
5
|
+
prism: {
|
|
6
|
+
user: {
|
|
7
|
+
_id: string | undefined;
|
|
8
|
+
};
|
|
9
|
+
} | undefined;
|
|
10
|
+
};
|
|
11
|
+
export declare class FusionAuthenticator extends BaseAuthenticator {
|
|
12
|
+
constructor();
|
|
13
|
+
private antiCsrfToken;
|
|
14
|
+
authenticate(): Promise<boolean>;
|
|
15
|
+
applyHeader(headers: HeadersInit): void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the authenticator is SSO authenticator
|
|
19
|
+
*
|
|
20
|
+
* @param authenticator - authenticator to check
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export declare function isFusionAuthenticator(authenticator: Authenticator): authenticator is FusionAuthenticator;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference lib="dom" />
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isFusionAuthenticator = exports.FusionAuthenticator = void 0;
|
|
5
|
+
const base_authenticator_js_1 = require("./base-authenticator.js");
|
|
6
|
+
const helpers_js_1 = require("./helpers.js");
|
|
7
|
+
class FusionAuthenticator extends base_authenticator_js_1.BaseAuthenticator {
|
|
8
|
+
constructor() {
|
|
9
|
+
super('fusion');
|
|
10
|
+
}
|
|
11
|
+
authenticate() {
|
|
12
|
+
var _a, _b, _c, _d, _e;
|
|
13
|
+
const hasUser = !!((_b = (_a = window.prism) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b._id);
|
|
14
|
+
this._resolve(hasUser);
|
|
15
|
+
const antiCsrfToken = (_e = (_d = (_c = window.document) === null || _c === void 0 ? void 0 : _c.cookie) === null || _d === void 0 ? void 0 : _d.match(/(?:^|;\s*)XSRF-TOKEN=([^;]+)/)) === null || _e === void 0 ? void 0 : _e[1];
|
|
16
|
+
if (antiCsrfToken)
|
|
17
|
+
this.antiCsrfToken = antiCsrfToken;
|
|
18
|
+
return this._result;
|
|
19
|
+
}
|
|
20
|
+
applyHeader(headers) {
|
|
21
|
+
if (this.antiCsrfToken) {
|
|
22
|
+
(0, helpers_js_1.appendHeaders)(headers, {
|
|
23
|
+
'X-Xsrf-Token': this.antiCsrfToken,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.FusionAuthenticator = FusionAuthenticator;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if the authenticator is SSO authenticator
|
|
31
|
+
*
|
|
32
|
+
* @param authenticator - authenticator to check
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
function isFusionAuthenticator(authenticator) {
|
|
36
|
+
return authenticator.type === 'fusion';
|
|
37
|
+
}
|
|
38
|
+
exports.isFusionAuthenticator = isFusionAuthenticator;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const appendHeaders: (existingHeaders: HeadersInit, additionalHeaders: {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
}) => void;
|
|
4
|
+
export declare const addQueryParamsToUrl: (url: string, params: {
|
|
5
|
+
[key: string]: string;
|
|
6
|
+
}) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Checks if API token or WAT token is pending (e.g., being generated)
|
|
9
|
+
*
|
|
10
|
+
* @param token - API token
|
|
11
|
+
* @param wat - WAT token
|
|
12
|
+
* @returns true if the token is pending
|
|
13
|
+
*/
|
|
14
|
+
export declare const isAuthTokenPending: (token?: string | null, wat?: string | null) => boolean;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isAuthTokenPending = exports.addQueryParamsToUrl = exports.appendHeaders = void 0;
|
|
4
|
+
const appendHeaders = (existingHeaders, additionalHeaders) => {
|
|
5
|
+
for (const [headerName, headerValue] of Object.entries(additionalHeaders)) {
|
|
6
|
+
if (Array.isArray(existingHeaders)) {
|
|
7
|
+
existingHeaders.push([headerName, headerValue]);
|
|
8
|
+
}
|
|
9
|
+
else if (typeof existingHeaders.set === 'function') {
|
|
10
|
+
existingHeaders.set(headerName, headerValue);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
14
|
+
existingHeaders[headerName] = headerValue;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
exports.appendHeaders = appendHeaders;
|
|
19
|
+
const addQueryParamsToUrl = (url, params) => {
|
|
20
|
+
if (!url || typeof url !== 'string')
|
|
21
|
+
return url;
|
|
22
|
+
// can't just append to the url because it might already have a query string
|
|
23
|
+
const urlObject = new URL(url);
|
|
24
|
+
for (const [paramName, paramValue] of Object.entries(params)) {
|
|
25
|
+
urlObject.searchParams.append(paramName, paramValue);
|
|
26
|
+
}
|
|
27
|
+
// replace the trailing slash if there is one
|
|
28
|
+
return urlObject.toString().replace(/\/([?&])/, '$1');
|
|
29
|
+
};
|
|
30
|
+
exports.addQueryParamsToUrl = addQueryParamsToUrl;
|
|
31
|
+
/**
|
|
32
|
+
* Checks if API token or WAT token is pending (e.g., being generated)
|
|
33
|
+
*
|
|
34
|
+
* @param token - API token
|
|
35
|
+
* @param wat - WAT token
|
|
36
|
+
* @returns true if the token is pending
|
|
37
|
+
*/
|
|
38
|
+
const isAuthTokenPending = (token, wat) => {
|
|
39
|
+
return token === null || wat === null;
|
|
40
|
+
};
|
|
41
|
+
exports.isAuthTokenPending = isAuthTokenPending;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { Authenticator } from './interfaces.js';
|
|
3
|
+
export interface HttpClientRequestConfig {
|
|
4
|
+
skipTrackingParam?: boolean;
|
|
5
|
+
nonJSONBody?: boolean;
|
|
6
|
+
returnBlob?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class HttpClient {
|
|
9
|
+
readonly auth: Authenticator;
|
|
10
|
+
readonly url: string;
|
|
11
|
+
readonly env: string;
|
|
12
|
+
constructor(url: string, auth: Authenticator, env: string);
|
|
13
|
+
login(): Promise<boolean>;
|
|
14
|
+
call<T>(url: string, config: RequestInit, requestConfig?: HttpClientRequestConfig): Promise<T | undefined>;
|
|
15
|
+
post<T = unknown>(endpoint: string, data: unknown, options?: RequestInit, abortSignal?: AbortSignal, config?: HttpClientRequestConfig): Promise<T | undefined>;
|
|
16
|
+
patch<T = unknown>(endpoint: string, data: unknown, options?: RequestInit, abortSignal?: AbortSignal, config?: HttpClientRequestConfig): Promise<T | undefined>;
|
|
17
|
+
get<T = unknown>(endpoint: string, request?: RequestInit, config?: HttpClientRequestConfig): Promise<T | undefined>;
|
|
18
|
+
delete<T = void>(endpoint: string, request?: RequestInit, config?: HttpClientRequestConfig): Promise<T | undefined>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HttpClient = void 0;
|
|
13
|
+
/// <reference lib="dom" />
|
|
14
|
+
const sdk_common_1 = require("@sisense/sdk-common");
|
|
15
|
+
const interceptors_js_1 = require("./interceptors.js");
|
|
16
|
+
const sso_authenticator_js_1 = require("./sso-authenticator.js");
|
|
17
|
+
const helpers_js_1 = require("./helpers.js");
|
|
18
|
+
class HttpClient {
|
|
19
|
+
constructor(url, auth, env) {
|
|
20
|
+
this.url = (0, sdk_common_1.normalizeUrl)(url);
|
|
21
|
+
this.auth = auth;
|
|
22
|
+
this.env = env;
|
|
23
|
+
}
|
|
24
|
+
login() {
|
|
25
|
+
return this.auth.authenticate();
|
|
26
|
+
}
|
|
27
|
+
call(url, config, requestConfig) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
if (this.auth.isAuthenticating()) {
|
|
30
|
+
yield this.auth.authenticated();
|
|
31
|
+
}
|
|
32
|
+
config.headers = config.headers || {};
|
|
33
|
+
if ((0, sso_authenticator_js_1.isSsoAuthenticator)(this.auth)) {
|
|
34
|
+
// allows cookies to be sent
|
|
35
|
+
config.credentials = 'include';
|
|
36
|
+
}
|
|
37
|
+
this.auth.applyHeader(config.headers);
|
|
38
|
+
const fetchUrl = (requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.skipTrackingParam)
|
|
39
|
+
? url
|
|
40
|
+
: (0, helpers_js_1.addQueryParamsToUrl)(url, {
|
|
41
|
+
trc: this.env,
|
|
42
|
+
});
|
|
43
|
+
const response = yield fetch(fetchUrl, config)
|
|
44
|
+
.then((0, interceptors_js_1.getResponseInterceptor)(this.auth))
|
|
45
|
+
.catch(interceptors_js_1.errorInterceptor);
|
|
46
|
+
if (response.status === 204 || // No content
|
|
47
|
+
response.status === 304 // Not modified
|
|
48
|
+
) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
return ((requestConfig === null || requestConfig === void 0 ? void 0 : requestConfig.returnBlob)
|
|
52
|
+
? response.blob()
|
|
53
|
+
: response.json().catch((e) => {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
// some of APIs in Sisense returns 200 with empty body - so it's not possible
|
|
56
|
+
// to understand definitely is it empty or not until you will try to parse it
|
|
57
|
+
if (!((_b = (_a = e === null || e === void 0 ? void 0 : e.message) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, 'Unexpected end of JSON input'))) {
|
|
58
|
+
throw e;
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
// eslint-disable-next-line max-params
|
|
64
|
+
post(endpoint, data, options = {}, abortSignal, config) {
|
|
65
|
+
const request = Object.assign({ method: 'POST', body: ((config === null || config === void 0 ? void 0 : config.nonJSONBody) ? data : JSON.stringify(data)), headers: {
|
|
66
|
+
Accept: 'application/json, text/plain, */*',
|
|
67
|
+
'Content-Type': 'application/json;charset=UTF-8',
|
|
68
|
+
}, signal: abortSignal }, options);
|
|
69
|
+
return this.call(this.url + endpoint, request, config);
|
|
70
|
+
}
|
|
71
|
+
patch(endpoint, data, options = {}, abortSignal, config) {
|
|
72
|
+
const request = Object.assign({ method: 'PATCH', body: ((config === null || config === void 0 ? void 0 : config.nonJSONBody) ? data : JSON.stringify(data)), headers: {
|
|
73
|
+
Accept: 'application/json, text/plain, */*',
|
|
74
|
+
'Content-Type': 'application/json;charset=UTF-8',
|
|
75
|
+
}, signal: abortSignal }, options);
|
|
76
|
+
return this.call(this.url + endpoint, request, config);
|
|
77
|
+
}
|
|
78
|
+
get(endpoint, request = {}, config) {
|
|
79
|
+
return this.call(this.url + endpoint, Object.assign(Object.assign({}, request), { method: 'GET' }), config);
|
|
80
|
+
}
|
|
81
|
+
delete(endpoint, request = {}, config) {
|
|
82
|
+
return this.call(this.url + endpoint, Object.assign(Object.assign({}, request), { method: 'DELETE' }), config);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.HttpClient = HttpClient;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import './translation/initialize-i18n.js';
|
|
2
|
+
export * from './interfaces.js';
|
|
3
|
+
export { PasswordAuthenticator } from './password-authenticator.js';
|
|
4
|
+
export { SsoAuthenticator, isSsoAuthenticator } from './sso-authenticator.js';
|
|
5
|
+
export { FusionAuthenticator, isFusionAuthenticator } from './fusion-authenticator.js';
|
|
6
|
+
export { BearerAuthenticator, isBearerAuthenticator } from './bearer-authenticator.js';
|
|
7
|
+
export { WatAuthenticator, isWatAuthenticator } from './wat-authenticator.js';
|
|
8
|
+
export { getAuthenticator } from './authenticator.js';
|
|
9
|
+
export { isAuthTokenPending } from './helpers.js';
|
|
10
|
+
export { HttpClient } from './http-client.js';
|
|
11
|
+
export type { TranslationDictionary, PACKAGE_NAMESPACE as translationNamespace, } from './translation/resources/index.js';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.HttpClient = exports.isAuthTokenPending = exports.getAuthenticator = exports.isWatAuthenticator = exports.WatAuthenticator = exports.isBearerAuthenticator = exports.BearerAuthenticator = exports.isFusionAuthenticator = exports.FusionAuthenticator = exports.isSsoAuthenticator = exports.SsoAuthenticator = exports.PasswordAuthenticator = void 0;
|
|
18
|
+
require("./translation/initialize-i18n.js");
|
|
19
|
+
__exportStar(require("./interfaces.js"), exports);
|
|
20
|
+
var password_authenticator_js_1 = require("./password-authenticator.js");
|
|
21
|
+
Object.defineProperty(exports, "PasswordAuthenticator", { enumerable: true, get: function () { return password_authenticator_js_1.PasswordAuthenticator; } });
|
|
22
|
+
var sso_authenticator_js_1 = require("./sso-authenticator.js");
|
|
23
|
+
Object.defineProperty(exports, "SsoAuthenticator", { enumerable: true, get: function () { return sso_authenticator_js_1.SsoAuthenticator; } });
|
|
24
|
+
Object.defineProperty(exports, "isSsoAuthenticator", { enumerable: true, get: function () { return sso_authenticator_js_1.isSsoAuthenticator; } });
|
|
25
|
+
var fusion_authenticator_js_1 = require("./fusion-authenticator.js");
|
|
26
|
+
Object.defineProperty(exports, "FusionAuthenticator", { enumerable: true, get: function () { return fusion_authenticator_js_1.FusionAuthenticator; } });
|
|
27
|
+
Object.defineProperty(exports, "isFusionAuthenticator", { enumerable: true, get: function () { return fusion_authenticator_js_1.isFusionAuthenticator; } });
|
|
28
|
+
var bearer_authenticator_js_1 = require("./bearer-authenticator.js");
|
|
29
|
+
Object.defineProperty(exports, "BearerAuthenticator", { enumerable: true, get: function () { return bearer_authenticator_js_1.BearerAuthenticator; } });
|
|
30
|
+
Object.defineProperty(exports, "isBearerAuthenticator", { enumerable: true, get: function () { return bearer_authenticator_js_1.isBearerAuthenticator; } });
|
|
31
|
+
var wat_authenticator_js_1 = require("./wat-authenticator.js");
|
|
32
|
+
Object.defineProperty(exports, "WatAuthenticator", { enumerable: true, get: function () { return wat_authenticator_js_1.WatAuthenticator; } });
|
|
33
|
+
Object.defineProperty(exports, "isWatAuthenticator", { enumerable: true, get: function () { return wat_authenticator_js_1.isWatAuthenticator; } });
|
|
34
|
+
var authenticator_js_1 = require("./authenticator.js");
|
|
35
|
+
Object.defineProperty(exports, "getAuthenticator", { enumerable: true, get: function () { return authenticator_js_1.getAuthenticator; } });
|
|
36
|
+
var helpers_js_1 = require("./helpers.js");
|
|
37
|
+
Object.defineProperty(exports, "isAuthTokenPending", { enumerable: true, get: function () { return helpers_js_1.isAuthTokenPending; } });
|
|
38
|
+
var http_client_js_1 = require("./http-client.js");
|
|
39
|
+
Object.defineProperty(exports, "HttpClient", { enumerable: true, get: function () { return http_client_js_1.HttpClient; } });
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorInterceptor = exports.getResponseInterceptor = void 0;
|
|
4
|
+
const bearer_authenticator_js_1 = require("./bearer-authenticator.js");
|
|
5
|
+
const wat_authenticator_js_1 = require("./wat-authenticator.js");
|
|
6
|
+
const password_authenticator_js_1 = require("./password-authenticator.js");
|
|
7
|
+
const sso_authenticator_js_1 = require("./sso-authenticator.js");
|
|
8
|
+
const translatable_error_js_1 = require("./translation/translatable-error.js");
|
|
9
|
+
function handleErrorResponse(response) {
|
|
10
|
+
if (!response.ok) {
|
|
11
|
+
throw new translatable_error_js_1.TranslatableError('errors.responseError', {
|
|
12
|
+
status: response.status.toString(),
|
|
13
|
+
statusText: response.statusText,
|
|
14
|
+
context: response.statusText ? 'withStatusText' : 'onlyStatus',
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return response;
|
|
18
|
+
}
|
|
19
|
+
function handleUnauthorizedResponse(response, auth) {
|
|
20
|
+
auth.invalidate();
|
|
21
|
+
// skip login redirect for token auth
|
|
22
|
+
if ((0, password_authenticator_js_1.isPasswordAuthenticator)(auth)) {
|
|
23
|
+
throw new translatable_error_js_1.TranslatableError('errors.passwordAuthFailed');
|
|
24
|
+
}
|
|
25
|
+
if ((0, bearer_authenticator_js_1.isBearerAuthenticator)(auth) || (0, wat_authenticator_js_1.isWatAuthenticator)(auth)) {
|
|
26
|
+
throw new translatable_error_js_1.TranslatableError('errors.tokenAuthFailed');
|
|
27
|
+
}
|
|
28
|
+
if ((0, sso_authenticator_js_1.isSsoAuthenticator)(auth) && !auth.isAuthenticating()) {
|
|
29
|
+
// try to reauthenticate
|
|
30
|
+
void auth.authenticate();
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the given response error indicates a Network error.
|
|
36
|
+
*
|
|
37
|
+
* It is impossible to distinguish between a CORS error and other network errors, such as
|
|
38
|
+
* 'net::ERR_SSL_PROTOCOL_ERROR' and 'net::ERR_EMPTY_RESPONSE'. This information is hidden by the browser.
|
|
39
|
+
*
|
|
40
|
+
* @param responseError - The error object received from the failed response.
|
|
41
|
+
*/
|
|
42
|
+
function isNetworkError(responseError) {
|
|
43
|
+
return !!(responseError.message === 'Failed to fetch' && responseError.name === 'TypeError');
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Handles a Network error.
|
|
47
|
+
*
|
|
48
|
+
* @returns A promise that rejects with an error message.
|
|
49
|
+
*/
|
|
50
|
+
function handleNetworkError() {
|
|
51
|
+
return Promise.reject(new translatable_error_js_1.TranslatableError('errors.networkError'));
|
|
52
|
+
}
|
|
53
|
+
const getResponseInterceptor = (auth) => (response) => {
|
|
54
|
+
if (response.status === 401) {
|
|
55
|
+
return handleUnauthorizedResponse(response, auth);
|
|
56
|
+
}
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
return handleErrorResponse(response);
|
|
59
|
+
}
|
|
60
|
+
return response;
|
|
61
|
+
};
|
|
62
|
+
exports.getResponseInterceptor = getResponseInterceptor;
|
|
63
|
+
const errorInterceptor = (error) => {
|
|
64
|
+
if (isNetworkError(error)) {
|
|
65
|
+
return handleNetworkError();
|
|
66
|
+
}
|
|
67
|
+
return Promise.reject(error);
|
|
68
|
+
};
|
|
69
|
+
exports.errorInterceptor = errorInterceptor;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Authenticator {
|
|
2
|
+
readonly type: 'password' | 'bearer' | 'wat' | 'sso' | 'base' | 'fusion';
|
|
3
|
+
isValid: () => boolean;
|
|
4
|
+
invalidate: () => void;
|
|
5
|
+
authenticate: () => Promise<boolean>;
|
|
6
|
+
isAuthenticating: () => boolean;
|
|
7
|
+
authenticated: () => Promise<boolean>;
|
|
8
|
+
applyHeader: (headers: HeadersInit) => void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { Authenticator } from './interfaces.js';
|
|
3
|
+
import { BaseAuthenticator } from './base-authenticator.js';
|
|
4
|
+
export declare class PasswordAuthenticator extends BaseAuthenticator {
|
|
5
|
+
private readonly url;
|
|
6
|
+
private readonly body;
|
|
7
|
+
private _authheader;
|
|
8
|
+
constructor(url: string, user: string, pass: string);
|
|
9
|
+
authenticate(): Promise<boolean>;
|
|
10
|
+
applyHeader(headers: HeadersInit): void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Checks if an authenticator is a PasswordAuthenticator.
|
|
14
|
+
*
|
|
15
|
+
* @param authenticator - the authenticator to check
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export declare function isPasswordAuthenticator(authenticator: Authenticator): authenticator is PasswordAuthenticator;
|