@emilgroup/billing-v2-sdk 0.0.0-stage → 1.0.1-beta.1
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/.openapi-generator/FILES +93 -0
- package/.openapi-generator/VERSION +1 -0
- package/.openapi-generator-ignore +23 -0
- package/README.md +50 -2
- package/api/bill-runs-api.ts +468 -0
- package/api/bills-api.ts +145 -0
- package/api/invoices-api.ts +1530 -0
- package/api/jobs-api.ts +756 -0
- package/api/policy-billings-api.ts +591 -0
- package/api.ts +35 -0
- package/base.ts +588 -0
- package/common.ts +199 -0
- package/configuration.ts +130 -0
- package/dist/api/bill-runs-api.d.ts +266 -0
- package/dist/api/bill-runs-api.js +466 -0
- package/dist/api/bills-api.d.ts +87 -0
- package/dist/api/bills-api.js +207 -0
- package/dist/api/invoices-api.d.ts +847 -0
- package/dist/api/invoices-api.js +1353 -0
- package/dist/api/jobs-api.d.ts +426 -0
- package/dist/api/jobs-api.js +708 -0
- package/dist/api/policy-billings-api.d.ts +332 -0
- package/dist/api/policy-billings-api.js +561 -0
- package/dist/api.d.ts +16 -0
- package/dist/api.js +34 -0
- package/dist/base.d.ts +120 -0
- package/dist/base.js +604 -0
- package/dist/common.d.ts +91 -0
- package/dist/common.js +277 -0
- package/dist/configuration.d.ts +106 -0
- package/dist/configuration.js +54 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +36 -0
- package/dist/models/any.d.ts +25 -0
- package/dist/models/any.js +15 -0
- package/dist/models/bill-item.d.ts +132 -0
- package/dist/models/bill-item.js +15 -0
- package/dist/models/bill-run.d.ts +145 -0
- package/dist/models/bill-run.js +15 -0
- package/dist/models/bill.d.ts +194 -0
- package/dist/models/bill.js +15 -0
- package/dist/models/cancel-job-response.d.ts +25 -0
- package/dist/models/cancel-job-response.js +15 -0
- package/dist/models/create-correction-invoices-request.d.ts +72 -0
- package/dist/models/create-correction-invoices-request.js +15 -0
- package/dist/models/create-correction-invoices-response.d.ts +31 -0
- package/dist/models/create-correction-invoices-response.js +15 -0
- package/dist/models/create-detail-export-job-request.d.ts +56 -0
- package/dist/models/create-detail-export-job-request.js +15 -0
- package/dist/models/create-detail-export-job-response.d.ts +25 -0
- package/dist/models/create-detail-export-job-response.js +15 -0
- package/dist/models/create-estimated-invoice-for-interval-request.d.ts +42 -0
- package/dist/models/create-estimated-invoice-for-interval-request.js +15 -0
- package/dist/models/create-estimated-invoice-for-interval-response.d.ts +25 -0
- package/dist/models/create-estimated-invoice-for-interval-response.js +15 -0
- package/dist/models/create-estimated-invoice-request.d.ts +36 -0
- package/dist/models/create-estimated-invoice-request.js +15 -0
- package/dist/models/create-estimated-invoice-response.d.ts +25 -0
- package/dist/models/create-estimated-invoice-response.js +15 -0
- package/dist/models/create-export-job-request.d.ts +68 -0
- package/dist/models/create-export-job-request.js +15 -0
- package/dist/models/create-export-job-response.d.ts +25 -0
- package/dist/models/create-export-job-response.js +15 -0
- package/dist/models/create-initial-invoice-request.d.ts +72 -0
- package/dist/models/create-initial-invoice-request.js +15 -0
- package/dist/models/create-initial-invoice-response.d.ts +25 -0
- package/dist/models/create-initial-invoice-response.js +15 -0
- package/dist/models/create-invoice-request.d.ts +67 -0
- package/dist/models/create-invoice-request.js +15 -0
- package/dist/models/create-invoice-response.d.ts +25 -0
- package/dist/models/create-invoice-response.js +15 -0
- package/dist/models/create-invoice-status-body.d.ts +24 -0
- package/dist/models/create-invoice-status-body.js +15 -0
- package/dist/models/create-invoice-status-response.d.ts +25 -0
- package/dist/models/create-invoice-status-response.js +15 -0
- package/dist/models/create-item-request.d.ts +36 -0
- package/dist/models/create-item-request.js +15 -0
- package/dist/models/create-policy-billing-request.d.ts +42 -0
- package/dist/models/create-policy-billing-request.js +15 -0
- package/dist/models/create-policy-billing-response.d.ts +25 -0
- package/dist/models/create-policy-billing-response.js +15 -0
- package/dist/models/create-recurring-invoice-request.d.ts +72 -0
- package/dist/models/create-recurring-invoice-request.js +15 -0
- package/dist/models/create-recurring-invoice-response.d.ts +31 -0
- package/dist/models/create-recurring-invoice-response.js +15 -0
- package/dist/models/create-termination-invoice-request.d.ts +60 -0
- package/dist/models/create-termination-invoice-request.js +15 -0
- package/dist/models/create-termination-invoice-response.d.ts +25 -0
- package/dist/models/create-termination-invoice-response.js +15 -0
- package/dist/models/create-withdraw-invoice-request.d.ts +48 -0
- package/dist/models/create-withdraw-invoice-request.js +15 -0
- package/dist/models/create-withdraw-invoice-response.d.ts +25 -0
- package/dist/models/create-withdraw-invoice-response.js +15 -0
- package/dist/models/estimate-history-entry.d.ts +36 -0
- package/dist/models/estimate-history-entry.js +15 -0
- package/dist/models/export-column.d.ts +36 -0
- package/dist/models/export-column.js +15 -0
- package/dist/models/filter-field.d.ts +110 -0
- package/dist/models/filter-field.js +15 -0
- package/dist/models/filter-value.d.ts +37 -0
- package/dist/models/filter-value.js +15 -0
- package/dist/models/get-bill-response.d.ts +25 -0
- package/dist/models/get-bill-response.js +15 -0
- package/dist/models/get-bill-run-filter-catalog-response.d.ts +31 -0
- package/dist/models/get-bill-run-filter-catalog-response.js +15 -0
- package/dist/models/get-bill-run-response.d.ts +25 -0
- package/dist/models/get-bill-run-response.js +15 -0
- package/dist/models/get-invoice-filter-catalog-response.d.ts +31 -0
- package/dist/models/get-invoice-filter-catalog-response.js +15 -0
- package/dist/models/get-invoice-response.d.ts +25 -0
- package/dist/models/get-invoice-response.js +15 -0
- package/dist/models/get-job-download-response.d.ts +25 -0
- package/dist/models/get-job-download-response.js +15 -0
- package/dist/models/get-job-filter-catalog-response.d.ts +31 -0
- package/dist/models/get-job-filter-catalog-response.js +15 -0
- package/dist/models/get-job-response.d.ts +25 -0
- package/dist/models/get-job-response.js +15 -0
- package/dist/models/get-policies-billing-date-filter-catalog-response.d.ts +31 -0
- package/dist/models/get-policies-billing-date-filter-catalog-response.js +15 -0
- package/dist/models/get-policies-billing-filter-catalog-response.d.ts +31 -0
- package/dist/models/get-policies-billing-filter-catalog-response.js +15 -0
- package/dist/models/get-policy-billing-response.d.ts +25 -0
- package/dist/models/get-policy-billing-response.js +15 -0
- package/dist/models/highlight.d.ts +30 -0
- package/dist/models/highlight.js +15 -0
- package/dist/models/index.d.ts +75 -0
- package/dist/models/index.js +91 -0
- package/dist/models/invoice-item.d.ts +204 -0
- package/dist/models/invoice-item.js +15 -0
- package/dist/models/invoice-payment.d.ts +108 -0
- package/dist/models/invoice-payment.js +15 -0
- package/dist/models/invoice-payments.d.ts +25 -0
- package/dist/models/invoice-payments.js +15 -0
- package/dist/models/invoice-status.d.ts +54 -0
- package/dist/models/invoice-status.js +15 -0
- package/dist/models/invoice.d.ts +275 -0
- package/dist/models/invoice.js +15 -0
- package/dist/models/job-download.d.ts +36 -0
- package/dist/models/job-download.js +15 -0
- package/dist/models/job-error.d.ts +30 -0
- package/dist/models/job-error.js +15 -0
- package/dist/models/job-progress.d.ts +36 -0
- package/dist/models/job-progress.js +15 -0
- package/dist/models/job.d.ts +134 -0
- package/dist/models/job.js +15 -0
- package/dist/models/list-bill-runs-response.d.ts +37 -0
- package/dist/models/list-bill-runs-response.js +15 -0
- package/dist/models/list-invoices-response.d.ts +37 -0
- package/dist/models/list-invoices-response.js +15 -0
- package/dist/models/list-jobs-response.d.ts +37 -0
- package/dist/models/list-jobs-response.js +15 -0
- package/dist/models/list-policies-billing-dates-response.d.ts +37 -0
- package/dist/models/list-policies-billing-dates-response.js +15 -0
- package/dist/models/list-policies-billings-response.d.ts +37 -0
- package/dist/models/list-policies-billings-response.js +15 -0
- package/dist/models/list-request.d.ts +60 -0
- package/dist/models/list-request.js +15 -0
- package/dist/models/localized-text.d.ts +30 -0
- package/dist/models/localized-text.js +15 -0
- package/dist/models/match.d.ts +69 -0
- package/dist/models/match.js +15 -0
- package/dist/models/payment-term.d.ts +42 -0
- package/dist/models/payment-term.js +15 -0
- package/dist/models/policy-billing.d.ts +139 -0
- package/dist/models/policy-billing.js +15 -0
- package/dist/models/request-bill-run-request.d.ts +30 -0
- package/dist/models/request-bill-run-request.js +15 -0
- package/dist/models/request-bill-run-response.d.ts +25 -0
- package/dist/models/request-bill-run-response.js +15 -0
- package/dist/models/resolved-billing-config.d.ts +130 -0
- package/dist/models/resolved-billing-config.js +15 -0
- package/dist/models/revert-invoice-request.d.ts +24 -0
- package/dist/models/revert-invoice-request.js +15 -0
- package/dist/models/revert-invoice-response.d.ts +25 -0
- package/dist/models/revert-invoice-response.js +15 -0
- package/dist/models/slice.d.ts +30 -0
- package/dist/models/slice.js +15 -0
- package/dist/models/status.d.ts +37 -0
- package/dist/models/status.js +15 -0
- package/dist/models/tax.d.ts +30 -0
- package/dist/models/tax.js +15 -0
- package/dist/models/update-policy-billing-body.d.ts +24 -0
- package/dist/models/update-policy-billing-body.js +15 -0
- package/dist/models/update-policy-billing-response.d.ts +25 -0
- package/dist/models/update-policy-billing-response.js +15 -0
- package/git_push.sh +57 -0
- package/index.ts +19 -0
- package/models/any.ts +32 -0
- package/models/bill-item.ts +138 -0
- package/models/bill-run.ts +151 -0
- package/models/bill.ts +200 -0
- package/models/cancel-job-response.ts +31 -0
- package/models/create-correction-invoices-request.ts +78 -0
- package/models/create-correction-invoices-response.ts +37 -0
- package/models/create-detail-export-job-request.ts +60 -0
- package/models/create-detail-export-job-response.ts +31 -0
- package/models/create-estimated-invoice-for-interval-request.ts +48 -0
- package/models/create-estimated-invoice-for-interval-response.ts +31 -0
- package/models/create-estimated-invoice-request.ts +42 -0
- package/models/create-estimated-invoice-response.ts +31 -0
- package/models/create-export-job-request.ts +74 -0
- package/models/create-export-job-response.ts +31 -0
- package/models/create-initial-invoice-request.ts +78 -0
- package/models/create-initial-invoice-response.ts +31 -0
- package/models/create-invoice-request.ts +73 -0
- package/models/create-invoice-response.ts +31 -0
- package/models/create-invoice-status-body.ts +30 -0
- package/models/create-invoice-status-response.ts +31 -0
- package/models/create-item-request.ts +42 -0
- package/models/create-policy-billing-request.ts +48 -0
- package/models/create-policy-billing-response.ts +31 -0
- package/models/create-recurring-invoice-request.ts +78 -0
- package/models/create-recurring-invoice-response.ts +37 -0
- package/models/create-termination-invoice-request.ts +66 -0
- package/models/create-termination-invoice-response.ts +31 -0
- package/models/create-withdraw-invoice-request.ts +54 -0
- package/models/create-withdraw-invoice-response.ts +31 -0
- package/models/estimate-history-entry.ts +42 -0
- package/models/export-column.ts +42 -0
- package/models/filter-field.ts +116 -0
- package/models/filter-value.ts +43 -0
- package/models/get-bill-response.ts +31 -0
- package/models/get-bill-run-filter-catalog-response.ts +37 -0
- package/models/get-bill-run-response.ts +31 -0
- package/models/get-invoice-filter-catalog-response.ts +37 -0
- package/models/get-invoice-response.ts +31 -0
- package/models/get-job-download-response.ts +31 -0
- package/models/get-job-filter-catalog-response.ts +37 -0
- package/models/get-job-response.ts +31 -0
- package/models/get-policies-billing-date-filter-catalog-response.ts +37 -0
- package/models/get-policies-billing-filter-catalog-response.ts +37 -0
- package/models/get-policy-billing-response.ts +31 -0
- package/models/highlight.ts +36 -0
- package/models/index.ts +75 -0
- package/models/invoice-item.ts +210 -0
- package/models/invoice-payment.ts +114 -0
- package/models/invoice-payments.ts +31 -0
- package/models/invoice-status.ts +60 -0
- package/models/invoice.ts +281 -0
- package/models/job-download.ts +42 -0
- package/models/job-error.ts +36 -0
- package/models/job-progress.ts +42 -0
- package/models/job.ts +140 -0
- package/models/list-bill-runs-response.ts +43 -0
- package/models/list-invoices-response.ts +43 -0
- package/models/list-jobs-response.ts +43 -0
- package/models/list-policies-billing-dates-response.ts +43 -0
- package/models/list-policies-billings-response.ts +43 -0
- package/models/list-request.ts +66 -0
- package/models/localized-text.ts +36 -0
- package/models/match.ts +75 -0
- package/models/payment-term.ts +48 -0
- package/models/policy-billing.ts +145 -0
- package/models/request-bill-run-request.ts +36 -0
- package/models/request-bill-run-response.ts +31 -0
- package/models/resolved-billing-config.ts +134 -0
- package/models/revert-invoice-request.ts +30 -0
- package/models/revert-invoice-response.ts +31 -0
- package/models/slice.ts +36 -0
- package/models/status.ts +43 -0
- package/models/tax.ts +36 -0
- package/models/update-policy-billing-body.ts +30 -0
- package/models/update-policy-billing-response.ts +31 -0
- package/package.json +25 -4
- package/tsconfig.json +23 -0
package/base.ts
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* EMIL Billing Service
|
|
5
|
+
* The billing service: bills, bill runs, invoices and policy billings.
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 2.0
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import { Configuration } from "./configuration";
|
|
17
|
+
import { defaultStorage } from "./common";
|
|
18
|
+
// Some imports not used depending on template conditions
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export const BASE_PATH = "https://apiv2.emil.de".replace(/\/+$/, "");
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @export
|
|
27
|
+
*/
|
|
28
|
+
export const COLLECTION_FORMATS = {
|
|
29
|
+
csv: ",",
|
|
30
|
+
ssv: " ",
|
|
31
|
+
tsv: "\t",
|
|
32
|
+
pipes: "|",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface SessionClass {
|
|
36
|
+
sub: string;
|
|
37
|
+
username?: string;
|
|
38
|
+
email?: string;
|
|
39
|
+
tenantSlug?: string;
|
|
40
|
+
permissions?: string;
|
|
41
|
+
expiresAt?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface LoginClass {
|
|
45
|
+
accessToken: string;
|
|
46
|
+
permissions: string;
|
|
47
|
+
authMode?: string;
|
|
48
|
+
session?: SessionClass;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface SwitchWorkspaceRequest {
|
|
52
|
+
username: string;
|
|
53
|
+
targetWorkspace: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SwitchWorkspaceResponseClass {
|
|
57
|
+
accessToken: string;
|
|
58
|
+
permissions: string;
|
|
59
|
+
authMode?: string;
|
|
60
|
+
session?: SessionClass;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export enum Environment {
|
|
64
|
+
Production = 'https://apiv2.emil.de',
|
|
65
|
+
Test = 'https://apiv2-test.emil.de',
|
|
66
|
+
Staging = 'https://apiv2-staging.emil.de',
|
|
67
|
+
Development = 'https://apiv2-dev.emil.de',
|
|
68
|
+
ProductionZurich = 'https://eu-central-2.apiv2.emil.de',
|
|
69
|
+
StagingZurich = 'https://eu-central-2.apiv2-staging.emil.de',
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let _retry_count = 0
|
|
73
|
+
let _retry = null
|
|
74
|
+
|
|
75
|
+
// Refreshes in flight per account (BaseAPI.shareRefresh).
|
|
76
|
+
const refreshesInFlight = new Map<string, Promise<LoginClass>>();
|
|
77
|
+
|
|
78
|
+
export function resetRetry() {
|
|
79
|
+
_retry_count = 0
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
*
|
|
84
|
+
* @export
|
|
85
|
+
* @interface RequestArgs
|
|
86
|
+
*/
|
|
87
|
+
export interface RequestArgs {
|
|
88
|
+
url: string;
|
|
89
|
+
options: AxiosRequestConfig;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface TokenData {
|
|
93
|
+
accessToken?: string;
|
|
94
|
+
username?: string;
|
|
95
|
+
permissions?: string;
|
|
96
|
+
// 'both' or 'session' when authservice keeps the tokens in a server session.
|
|
97
|
+
authMode?: string;
|
|
98
|
+
sub?: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const NETWORK_ERROR_MESSAGE = "Network Error";
|
|
102
|
+
const TOKEN_DATA = 'APP_TOKEN';
|
|
103
|
+
// Sent only to authservice, to ask for a server session.
|
|
104
|
+
const AUTH_MODE_HEADER = 'X-Emil-Auth-Mode';
|
|
105
|
+
// Names the account on every call; one browser session can hold several.
|
|
106
|
+
const ACCOUNT_HEADER = 'X-Emil-Account';
|
|
107
|
+
const SESSION_MODES = ['both', 'session'];
|
|
108
|
+
const RESEND_SAFE_METHODS = ['get', 'head', 'options'];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* @export
|
|
113
|
+
* @class BaseAPI
|
|
114
|
+
*/
|
|
115
|
+
export class BaseAPI {
|
|
116
|
+
protected configuration: Configuration | undefined;
|
|
117
|
+
private tokenData?: TokenData;
|
|
118
|
+
|
|
119
|
+
constructor(configuration?: Configuration,
|
|
120
|
+
protected basePath: string = BASE_PATH,
|
|
121
|
+
protected axios: AxiosInstance = globalAxios) {
|
|
122
|
+
|
|
123
|
+
this.loadTokenData();
|
|
124
|
+
|
|
125
|
+
if (configuration) {
|
|
126
|
+
// Two accounts in one browser share APP_TOKEN: never take the token or session account saved for
|
|
127
|
+
// another account. The mode stays (an app naming its account keeps its session). Saved with the next refresh.
|
|
128
|
+
const ownUser = configuration.username;
|
|
129
|
+
const storedUser = this.tokenData.username;
|
|
130
|
+
if (ownUser && storedUser && ownUser.toLowerCase() !== storedUser.toLowerCase()) {
|
|
131
|
+
delete this.tokenData.accessToken;
|
|
132
|
+
delete this.tokenData.sub;
|
|
133
|
+
this.tokenData.username = ownUser;
|
|
134
|
+
}
|
|
135
|
+
const { accessToken } = this.tokenData;
|
|
136
|
+
this.configuration = configuration;
|
|
137
|
+
this.basePath = configuration.basePath || this.basePath;
|
|
138
|
+
|
|
139
|
+
if (configuration.cookieAuth) {
|
|
140
|
+
this.configuration.username = this.configuration.username || this.tokenData.username;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (this.usesSession()) {
|
|
144
|
+
this.useSessionAuth();
|
|
145
|
+
} else {
|
|
146
|
+
// Use config token if provided, otherwise use tokenData token
|
|
147
|
+
const configToken = this.configuration.accessToken;
|
|
148
|
+
const storedToken = accessToken ? `Bearer ${accessToken}` : '';
|
|
149
|
+
this.configuration.accessToken = configToken || storedToken;
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
const { accessToken, username } = this.tokenData;
|
|
153
|
+
|
|
154
|
+
this.configuration = new Configuration({
|
|
155
|
+
basePath: this.basePath,
|
|
156
|
+
accessToken: accessToken ? `Bearer ${accessToken}` : '',
|
|
157
|
+
username,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.attachInterceptor(axios);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The app's own account wins over APP_TOKEN, which holds whichever tab wrote last.
|
|
165
|
+
private sessionAccount(): string | undefined {
|
|
166
|
+
return this.configuration.account || this.tokenData.sub;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Apps may pass the address as the second argument only; authservice is on the same host.
|
|
170
|
+
private authBasePath(): string {
|
|
171
|
+
return this.configuration.basePath || this.basePath;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private usesSession(): boolean {
|
|
175
|
+
return !!this.configuration.cookieAuth && SESSION_MODES.indexOf(this.tokenData.authMode) !== -1 && !!this.sessionAccount();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private useSessionAuth() {
|
|
179
|
+
const { headers, ...baseOptions } = this.configuration.baseOptions || {};
|
|
180
|
+
this.configuration.accessToken = '';
|
|
181
|
+
this.configuration.baseOptions = {
|
|
182
|
+
...baseOptions,
|
|
183
|
+
withCredentials: true,
|
|
184
|
+
headers: { ...headers, [ACCOUNT_HEADER]: this.sessionAccount() },
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private useHeaderAuth(accessToken: string, keepSessionData = false) {
|
|
189
|
+
this.configuration.accessToken = accessToken ? `Bearer ${accessToken}` : '';
|
|
190
|
+
this.tokenData.accessToken = accessToken;
|
|
191
|
+
// An app without cookieAuth does not own the session fields: its refresh leaves them for the session apps.
|
|
192
|
+
if (!keepSessionData || this.configuration.cookieAuth) {
|
|
193
|
+
delete this.tokenData.authMode;
|
|
194
|
+
delete this.tokenData.sub;
|
|
195
|
+
}
|
|
196
|
+
if (this.configuration.cookieAuth) {
|
|
197
|
+
const { withCredentials, headers, ...baseOptions } = this.configuration.baseOptions || {};
|
|
198
|
+
const { [ACCOUNT_HEADER]: account, ...otherHeaders } = headers || {};
|
|
199
|
+
this.configuration.baseOptions = { ...baseOptions, headers: otherHeaders };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private applyTokenReply({ accessToken, authMode, session }: LoginClass, fromRefresh = false) {
|
|
204
|
+
if (this.configuration.cookieAuth && SESSION_MODES.indexOf(authMode) !== -1 && session?.sub) {
|
|
205
|
+
this.tokenData.authMode = authMode;
|
|
206
|
+
this.tokenData.sub = session.sub;
|
|
207
|
+
if (this.configuration.account) {
|
|
208
|
+
// A workspace switch moves the app to another account.
|
|
209
|
+
this.configuration.account = session.sub;
|
|
210
|
+
}
|
|
211
|
+
// Both keeps the token for apps not moved yet; session mode keeps no token at all.
|
|
212
|
+
if (authMode === 'both' && accessToken) {
|
|
213
|
+
this.tokenData.accessToken = accessToken;
|
|
214
|
+
} else {
|
|
215
|
+
delete this.tokenData.accessToken;
|
|
216
|
+
}
|
|
217
|
+
this.useSessionAuth();
|
|
218
|
+
} else {
|
|
219
|
+
this.useHeaderAuth(accessToken, fromRefresh);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private sessionHeaders(): Record<string, string> {
|
|
224
|
+
if (!this.configuration.cookieAuth) {
|
|
225
|
+
return {};
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
[AUTH_MODE_HEADER]: 'session',
|
|
229
|
+
...(this.usesSession() && { [ACCOUNT_HEADER]: this.sessionAccount() }),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private withCurrentAuth(config: AxiosRequestConfig): AxiosRequestConfig {
|
|
234
|
+
delete config.headers['Authorization'];
|
|
235
|
+
delete config.headers[ACCOUNT_HEADER];
|
|
236
|
+
if (this.usesSession()) {
|
|
237
|
+
config.headers[ACCOUNT_HEADER] = this.sessionAccount();
|
|
238
|
+
config.withCredentials = true;
|
|
239
|
+
} else if (this.configuration.accessToken) {
|
|
240
|
+
config.headers['Authorization'] = this.configuration.accessToken;
|
|
241
|
+
}
|
|
242
|
+
return config;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Both keeps the token, so a session call the browser could not complete can be sent again with it.
|
|
246
|
+
private canRetryWithToken(config: AxiosRequestConfig): boolean {
|
|
247
|
+
return !!config.withCredentials && !!config.headers?.[ACCOUNT_HEADER]
|
|
248
|
+
&& !!this.tokenData.accessToken && this.tokenData.authMode !== 'session';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// A network error can come after the server ran the call, so only a call that is safe to repeat is sent again.
|
|
252
|
+
private canResend(config: AxiosRequestConfig): boolean {
|
|
253
|
+
return RESEND_SAFE_METHODS.indexOf((config.method || 'get').toLowerCase()) !== -1
|
|
254
|
+
|| Object.keys(config.headers || {}).some((name) => name.toLowerCase() === 'idempotency-key');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private switchToToken() {
|
|
258
|
+
this.useHeaderAuth(this.tokenData.accessToken);
|
|
259
|
+
this.storeTokenData(this.tokenData);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private retryWithToken(axios: AxiosInstance, config: AxiosRequestConfig) {
|
|
263
|
+
this.switchToToken();
|
|
264
|
+
config.withCredentials = false;
|
|
265
|
+
return axios(this.withCurrentAuth(config));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
selectEnvironment(env: Environment) {
|
|
269
|
+
this.selectBasePath(env);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
selectBasePath(path: string) {
|
|
273
|
+
this.configuration.basePath = path;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
getPermissions(): Array<string> {
|
|
277
|
+
if (!this.tokenData?.permissions) {
|
|
278
|
+
return [];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return this.tokenData.permissions.split(',');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async authorize(username: string, password: string, targetWorkspace?: string): Promise<void> {
|
|
285
|
+
const options: AxiosRequestConfig = {
|
|
286
|
+
method: 'POST',
|
|
287
|
+
url: `${this.authBasePath()}/authservice/v1/login`,
|
|
288
|
+
headers: { 'Content-Type': 'application/json', ...(this.configuration.cookieAuth && { [AUTH_MODE_HEADER]: 'session' }) },
|
|
289
|
+
data: {
|
|
290
|
+
username,
|
|
291
|
+
password,
|
|
292
|
+
},
|
|
293
|
+
withCredentials: true,
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const response = await globalAxios.request<LoginClass>(options);
|
|
297
|
+
|
|
298
|
+
this.configuration.username = username;
|
|
299
|
+
this.tokenData.username = username;
|
|
300
|
+
this.tokenData.permissions = response.data.permissions;
|
|
301
|
+
this.applyTokenReply(response.data);
|
|
302
|
+
|
|
303
|
+
// Switch workspace if provided
|
|
304
|
+
if (targetWorkspace) {
|
|
305
|
+
await this.switchWorkspace(targetWorkspace);
|
|
306
|
+
} else {
|
|
307
|
+
// Only store if no workspace switch (since switchWorkspace will store after switching)
|
|
308
|
+
this.storeTokenData({
|
|
309
|
+
...this.tokenData
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.storeTokenData({
|
|
314
|
+
...this.tokenData
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async switchWorkspace(targetWorkspace: string): Promise<void> {
|
|
319
|
+
const options: AxiosRequestConfig = {
|
|
320
|
+
method: 'POST',
|
|
321
|
+
url: `${this.authBasePath()}/authservice/v1/workspaces/switch`,
|
|
322
|
+
headers: {
|
|
323
|
+
'Content-Type': 'application/json',
|
|
324
|
+
...this.sessionHeaders(),
|
|
325
|
+
...(!this.usesSession() && !!this.configuration.accessToken && { 'Authorization': this.configuration.accessToken as string }),
|
|
326
|
+
},
|
|
327
|
+
data: {
|
|
328
|
+
username: this.configuration.username,
|
|
329
|
+
targetWorkspace,
|
|
330
|
+
} as SwitchWorkspaceRequest,
|
|
331
|
+
withCredentials: true,
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const response = await globalAxios.request<SwitchWorkspaceResponseClass>(options);
|
|
335
|
+
|
|
336
|
+
this.tokenData.permissions = response.data.permissions;
|
|
337
|
+
this.applyTokenReply(response.data);
|
|
338
|
+
|
|
339
|
+
this.storeTokenData({
|
|
340
|
+
...this.tokenData
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async refreshTokenInternal(): Promise<LoginClass> {
|
|
345
|
+
const { username } = this.configuration;
|
|
346
|
+
|
|
347
|
+
if (!username) {
|
|
348
|
+
// This object cannot refresh at all, so the session it relied on is over for it.
|
|
349
|
+
const cannotRefresh: any = new Error('Failed to refresh token.');
|
|
350
|
+
cannotRefresh.sessionExpired = true;
|
|
351
|
+
throw cannotRefresh;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const refreshTokenAxios = globalAxios.create()
|
|
355
|
+
refreshTokenAxios.interceptors.response.use(response => {
|
|
356
|
+
const { permissions, accessToken, authMode } = response.data;
|
|
357
|
+
// Decided before the merge below: newer session data must not keep the new token from this object.
|
|
358
|
+
const onSession = this.usesSession();
|
|
359
|
+
|
|
360
|
+
// Newer data saved for this account (e.g. the session mode the shell saved later) wins over the copy
|
|
361
|
+
// read when the object was made; data of another account or another workspace (sub) is never taken.
|
|
362
|
+
const latest = ((typeof window !== 'undefined' && defaultStorage().get<TokenData>(TOKEN_DATA)) || {}) as TokenData;
|
|
363
|
+
const ownSub = this.sessionAccount();
|
|
364
|
+
if (latest.username?.toLowerCase() === username.toLowerCase()
|
|
365
|
+
&& (!ownSub || !latest.sub || latest.sub === ownSub)) {
|
|
366
|
+
// A bare reset ({username}, an app's account check) keeps this copy's other fields; any fuller record
|
|
367
|
+
// is the truth, so fields it lacks were removed on purpose (e.g. a rollback) and stay removed.
|
|
368
|
+
const bareReset = Object.keys(latest).every((key) => key === 'username');
|
|
369
|
+
this.tokenData = bareReset ? { ...this.tokenData, ...latest } : { ...latest };
|
|
370
|
+
}
|
|
371
|
+
// The reply is for this account: label it, or an SDK object of another account would take the token.
|
|
372
|
+
if (this.tokenData.username?.toLowerCase() !== username.toLowerCase()) {
|
|
373
|
+
this.tokenData.username = username;
|
|
374
|
+
}
|
|
375
|
+
this.tokenData.permissions = permissions;
|
|
376
|
+
// Keep the new token for the page's next SDK objects; session data is left as it is.
|
|
377
|
+
if (accessToken && !onSession) {
|
|
378
|
+
this.tokenData.accessToken = accessToken;
|
|
379
|
+
this.configuration.accessToken = `Bearer ${accessToken}`;
|
|
380
|
+
} else if (accessToken && authMode === 'both') {
|
|
381
|
+
// Both: this app stays on the session; the token is kept for apps not moved yet.
|
|
382
|
+
this.tokenData.accessToken = accessToken;
|
|
383
|
+
}
|
|
384
|
+
this.storeTokenData(this.tokenData);
|
|
385
|
+
|
|
386
|
+
return response;
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
const options: AxiosRequestConfig = {
|
|
390
|
+
method: 'POST',
|
|
391
|
+
url: `${this.authBasePath()}/authservice/v1/refresh-token`,
|
|
392
|
+
headers: {
|
|
393
|
+
'Content-Type': 'application/json',
|
|
394
|
+
...this.sessionHeaders(),
|
|
395
|
+
},
|
|
396
|
+
data: { username: username },
|
|
397
|
+
withCredentials: true,
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const response = await refreshTokenAxios.request<LoginClass>(options);
|
|
401
|
+
return response.data;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private storeTokenData(tokenData?: TokenData) {
|
|
405
|
+
if (typeof window !== 'undefined') {
|
|
406
|
+
defaultStorage().set<TokenData>(TOKEN_DATA, tokenData);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
public loadTokenData() {
|
|
411
|
+
if (typeof window !== 'undefined') {
|
|
412
|
+
this.tokenData = defaultStorage().get<TokenData>(TOKEN_DATA) || {};
|
|
413
|
+
} else {
|
|
414
|
+
this.tokenData = {};
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
public cleanTokenData() {
|
|
419
|
+
this.storeTokenData(null);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Marks each request with the account (username) of the SDK object that sent it: only SDK objects of that
|
|
423
|
+
// account refresh and resend it, never an object of another account in the same app.
|
|
424
|
+
private static readonly REQUEST_OWNER = '_emilSdkOwner';
|
|
425
|
+
|
|
426
|
+
private ownAccount(): string {
|
|
427
|
+
return (this.configuration.username || '').toLowerCase();
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
protected withOwner(options?: AxiosRequestConfig): AxiosRequestConfig {
|
|
431
|
+
// An object without a username cannot refresh itself: its requests stay unmarked, as before.
|
|
432
|
+
if (!this.ownAccount()) {
|
|
433
|
+
return { ...options };
|
|
434
|
+
}
|
|
435
|
+
return { ...options, [BaseAPI.REQUEST_OWNER]: this.ownAccount() } as AxiosRequestConfig;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Sent for another account: only that account's SDK objects handle it. Unmarked requests as before.
|
|
439
|
+
private sentByOtherAccount(config?: AxiosRequestConfig): boolean {
|
|
440
|
+
const owner = (config as Record<string, unknown> | undefined)?.[BaseAPI.REQUEST_OWNER];
|
|
441
|
+
return owner !== undefined && owner !== this.ownAccount();
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
// One refresh per account; an object without a username keeps its own, which fails as before.
|
|
446
|
+
private sharedRefresh(): Promise<LoginClass> {
|
|
447
|
+
const account = this.ownAccount();
|
|
448
|
+
if (!account) {
|
|
449
|
+
return this.refreshTokenInternal();
|
|
450
|
+
}
|
|
451
|
+
return BaseAPI.shareRefresh(refreshesInFlight, account, () => this.refreshTokenInternal());
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private refreshRunning(): boolean {
|
|
455
|
+
const account = this.ownAccount();
|
|
456
|
+
return !!account && refreshesInFlight.has(account);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Joins the refresh already in flight for this key, or starts one: calls refused at the same time wait for
|
|
460
|
+
// one refresh instead of each starting their own (a refresh token may be single-use). The entry is removed
|
|
461
|
+
// when the refresh settles.
|
|
462
|
+
private static shareRefresh<K, T>(inFlight: Map<K, Promise<T>>, key: K, start: () => Promise<T>): Promise<T> {
|
|
463
|
+
const running = inFlight.get(key);
|
|
464
|
+
if (running) {
|
|
465
|
+
return running;
|
|
466
|
+
}
|
|
467
|
+
const refresh = start();
|
|
468
|
+
inFlight.set(key, refresh);
|
|
469
|
+
const settled = () => { inFlight.delete(key); };
|
|
470
|
+
refresh.then(settled, settled);
|
|
471
|
+
return refresh;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// A refused call that joins a refresh already running is not a new try: only started refreshes count toward
|
|
475
|
+
// the limit, so every call refused at the same time is retried.
|
|
476
|
+
private mayRefreshAfterNetworkError(): boolean {
|
|
477
|
+
if (this.refreshRunning()) {
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
if (_retry_count >= 4) {
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
_retry_count++;
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
// Rejects as before (the refresh's response body, or its error). Marks it as an ended session only when the
|
|
489
|
+
// refresh was refused (401 or 403) or could not run (no username, marked where it is thrown); a server error
|
|
490
|
+
// or a network error says nothing about the session, so an app must not log the user out for it. A body that
|
|
491
|
+
// is not an object cannot carry the mark and is kept as it is.
|
|
492
|
+
private rejectEndedSession(error: any): Promise<never> {
|
|
493
|
+
const rejection = error.response?.data || error;
|
|
494
|
+
const status = error.response?.status;
|
|
495
|
+
if (typeof rejection === 'object' && (status === 401 || status === 403)) {
|
|
496
|
+
rejection.sessionExpired = true;
|
|
497
|
+
}
|
|
498
|
+
return Promise.reject(rejection);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
private attachInterceptor(axios: AxiosInstance) {
|
|
502
|
+
axios.interceptors.response.use(
|
|
503
|
+
(res) => {
|
|
504
|
+
return res;
|
|
505
|
+
},
|
|
506
|
+
async (err) => {
|
|
507
|
+
let originalConfig = err.config;
|
|
508
|
+
if (this.sentByOtherAccount(originalConfig)) {
|
|
509
|
+
return Promise.reject(err);
|
|
510
|
+
}
|
|
511
|
+
if (err.response && !(err.response instanceof XMLHttpRequest)) { // sometimes buggy and is of type request
|
|
512
|
+
// Access Token was expired
|
|
513
|
+
if ((err.response.status === 401 || err.response.status === 403)
|
|
514
|
+
&& !originalConfig._retry) {
|
|
515
|
+
originalConfig._retry = true;
|
|
516
|
+
try {
|
|
517
|
+
const reply = await this.sharedRefresh();
|
|
518
|
+
this.tokenData.permissions = reply.permissions;
|
|
519
|
+
this.applyTokenReply(reply, true);
|
|
520
|
+
this.storeTokenData(this.tokenData);
|
|
521
|
+
|
|
522
|
+
if (!this.usesSession()) {
|
|
523
|
+
return axios(this.withCurrentAuth(originalConfig));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// Both mode: if the browser does not send the session cookie (another site), use the token.
|
|
527
|
+
return axios(this.withCurrentAuth(originalConfig)).catch((retryError) => {
|
|
528
|
+
if (retryError?.response?.status !== 401 || !this.tokenData.accessToken) {
|
|
529
|
+
return Promise.reject(retryError);
|
|
530
|
+
}
|
|
531
|
+
return this.retryWithToken(axios, originalConfig);
|
|
532
|
+
});
|
|
533
|
+
} catch (_error) {
|
|
534
|
+
return this.rejectEndedSession(_error);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
} else if (err.message === NETWORK_ERROR_MESSAGE && this.canRetryWithToken(originalConfig)) {
|
|
538
|
+
// Both mode: the browser blocked the session call or hid its reply; later calls use the token.
|
|
539
|
+
if (!this.canResend(originalConfig)) {
|
|
540
|
+
this.switchToToken();
|
|
541
|
+
return Promise.reject(err);
|
|
542
|
+
}
|
|
543
|
+
return this.retryWithToken(axios, originalConfig);
|
|
544
|
+
} else if (err.message === NETWORK_ERROR_MESSAGE
|
|
545
|
+
&& (originalConfig.headers.hasOwnProperty('Authorization')
|
|
546
|
+
|| (this.configuration.cookieAuth && originalConfig.withCredentials && this.canResend(originalConfig)))
|
|
547
|
+
&& this.mayRefreshAfterNetworkError()
|
|
548
|
+
) {
|
|
549
|
+
try {
|
|
550
|
+
// In session mode an unreadable 401 (no CORS headers on the error reply) also lands here.
|
|
551
|
+
const reply = await this.sharedRefresh();
|
|
552
|
+
this.tokenData.permissions = reply.permissions;
|
|
553
|
+
|
|
554
|
+
_retry = true;
|
|
555
|
+
this.applyTokenReply(reply, true);
|
|
556
|
+
this.storeTokenData(this.tokenData);
|
|
557
|
+
|
|
558
|
+
// The limit is for refreshes that do not help; one that worked starts the count again.
|
|
559
|
+
// Not awaited, so a failed retry reaches the app as the same axios error as before.
|
|
560
|
+
return axios.request({
|
|
561
|
+
...this.withCurrentAuth(originalConfig),
|
|
562
|
+
}).then((retried) => {
|
|
563
|
+
_retry_count = 0;
|
|
564
|
+
return retried;
|
|
565
|
+
});
|
|
566
|
+
} catch (_error) {
|
|
567
|
+
return this.rejectEndedSession(_error);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return Promise.reject(err);
|
|
571
|
+
}
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
*
|
|
578
|
+
* @export
|
|
579
|
+
* @class RequiredError
|
|
580
|
+
* @extends {Error}
|
|
581
|
+
*/
|
|
582
|
+
export class RequiredError extends Error {
|
|
583
|
+
override name: "RequiredError" = "RequiredError";
|
|
584
|
+
constructor(public field: string, msg?: string) {
|
|
585
|
+
super(msg);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|