@venizia/ardor-kernel 0.4.8-1 → 0.4.8-11
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/README.md +10 -6
- package/dist/base/applications/abstract.d.ts +17 -1
- package/dist/base/applications/abstract.js +7 -0
- package/dist/base/providers/base.d.ts +2 -0
- package/dist/base/providers/base.js +1 -0
- package/dist/base/services/auth.d.ts +23 -2
- package/dist/base/services/auth.js +48 -13
- package/dist/base/services/base.d.ts +1 -0
- package/dist/base/services/base.js +1 -0
- package/dist/base/services/network-request.d.ts +63 -6
- package/dist/base/services/network-request.js +121 -17
- package/dist/common/constants.d.ts +47 -0
- package/dist/common/constants.js +47 -0
- package/dist/common/keys.d.ts +37 -1
- package/dist/common/keys.js +41 -1
- package/dist/common/types.d.ts +99 -8
- package/dist/helpers/auth-token-store.d.ts +6 -2
- package/dist/helpers/auth-token-store.js +5 -4
- package/dist/helpers/networks/auth-recovery.d.ts +1 -0
- package/dist/helpers/networks/request-body-encoder.d.ts +1 -0
- package/dist/helpers/networks/request-body-encoder.js +1 -0
- package/dist/helpers/networks/response-reader.d.ts +8 -0
- package/dist/helpers/networks/response-reader.js +8 -0
- package/dist/testing.d.ts +137 -0
- package/dist/testing.js +367 -0
- package/dist/utilities/browser-storage.d.ts +15 -3
- package/dist/utilities/browser-storage.js +26 -7
- package/dist/utilities/browser.d.ts +2 -0
- package/dist/utilities/browser.js +2 -0
- package/dist/utilities/file.d.ts +1 -0
- package/dist/utilities/file.js +1 -0
- package/dist/utilities/runtime-config.d.ts +4 -0
- package/dist/utilities/runtime-config.js +1 -0
- package/dist/utilities/storage-codecs.d.ts +1 -0
- package/dist/utilities/storage-codecs.js +1 -0
- package/dist/utilities/value.d.ts +3 -0
- package/dist/utilities/value.js +3 -0
- package/package.json +23 -14
|
@@ -2,7 +2,7 @@ import { getError } from '@venizia/ignis-inversion';
|
|
|
2
2
|
import { NodeFetchNetworkRequest } from '@venizia/ignis-helpers/core';
|
|
3
3
|
import { UuidV4Generator } from '@venizia/ignis-helpers/uuid';
|
|
4
4
|
import { HttpExtraRequest } from '@venizia/ignis-kernel/repository';
|
|
5
|
-
import { App, HeaderConstants, RequestChannel, RequestCountData, RequestMethods, RequestTypes, } from '../../common/index.js';
|
|
5
|
+
import { App, HeaderConstants, LocalStorageKeys, RequestChannel, RequestCountData, RequestMethods, RequestTypes, } from '../../common/index.js';
|
|
6
6
|
import { AuthRecovery, AuthTokenStore, NetworkHeaders, RequestBodyEncoder, ResponseReader, } from '../../helpers/index.js';
|
|
7
7
|
import { BaseService } from './base.js';
|
|
8
8
|
/**
|
|
@@ -33,19 +33,37 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
|
-
* The
|
|
37
|
-
*
|
|
36
|
+
* The token under the unscoped session key (`AuthTokenStore.read`), `undefined` outside a browser, for an
|
|
37
|
+
* application with no `id`. With an application `id` the session is under other keys: use
|
|
38
|
+
* `createAuthTokenReader({ id })`, the `HttpDataSource` `getDataSourceAuth()` of the data provider, or `authService.getAuth`.
|
|
38
39
|
*/
|
|
39
40
|
static readAuthTokenFromStorage() {
|
|
40
41
|
return AuthTokenStore.read();
|
|
41
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* A reader of the stored token of the application with this `id` (`IApplicationInfo.id`): the same
|
|
45
|
+
* key `DefaultAuthService.getAuth` reads (`LocalStorageKeys.scoped({ id }).token`). Without an `id` it
|
|
46
|
+
* is `readAuthTokenFromStorage`. Throws for an invalid `id`, when it is created.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* super({ baseUrl: '/api', authTokenResolver: DefaultNetworkRequestService.createAuthTokenReader({ id: 'shop' }) });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
static createAuthTokenReader(opts = {}) {
|
|
54
|
+
const { token: key } = LocalStorageKeys.scoped({ id: opts.id });
|
|
55
|
+
return () => AuthTokenStore.read({ key });
|
|
56
|
+
}
|
|
42
57
|
// --- Hooks -----------------------------------------------------------------------------------
|
|
43
58
|
/**
|
|
44
59
|
* The token this service sends. The one way to supply a token: override it to read another
|
|
45
60
|
* store, or to hold the token in memory.
|
|
46
61
|
*/
|
|
47
62
|
resolveAuthToken() {
|
|
48
|
-
|
|
63
|
+
// The auth service knows where this application's session is kept (its `id` scopes the keys).
|
|
64
|
+
return this.authService
|
|
65
|
+
? (this.authService.getAuth() ?? undefined)
|
|
66
|
+
: DefaultNetworkRequestService.readAuthTokenFromStorage();
|
|
49
67
|
}
|
|
50
68
|
/**
|
|
51
69
|
* Whether a request goes without a token: its `resource` is in `noAuthPaths`, or the resource, or
|
|
@@ -73,27 +91,63 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
73
91
|
}
|
|
74
92
|
/**
|
|
75
93
|
* Refreshes the session after a 401. `true`: refreshed, the request is retried; `false`: this
|
|
76
|
-
* service has no refresh
|
|
77
|
-
*
|
|
78
|
-
*
|
|
94
|
+
* service has no refresh, or the session changed while the request was in flight (another user
|
|
95
|
+
* signed in, the user signed out, or another tab refreshed): the new token is not saved, and the
|
|
96
|
+
* failure of a refresh made for a session that is gone is not reported; a throw: the refresh
|
|
97
|
+
* failed and `onAuthFailure` runs. The default posts to `authRecovery.refreshTokenPath` with no
|
|
98
|
+
* body and saves the new token through `authService`, keeping the stored identity and roles;
|
|
99
|
+
* with no path or no `authService` it answers `false`.
|
|
79
100
|
*/
|
|
80
101
|
async refreshToken() {
|
|
81
102
|
const path = this.authRecovery?.refreshTokenPath;
|
|
82
|
-
|
|
103
|
+
const { authService } = this;
|
|
104
|
+
if (!path || !authService) {
|
|
83
105
|
return false;
|
|
84
106
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
headers: this.getRequestHeader({ resource: path }),
|
|
107
|
+
// The session this refresh is for: the answer belongs to it and to no other.
|
|
108
|
+
const readSession = () => ({
|
|
109
|
+
userId: `${authService.getUser().userId}`,
|
|
110
|
+
token: authService.getAuth()?.value,
|
|
90
111
|
});
|
|
91
|
-
const
|
|
112
|
+
const startedFor = readSession();
|
|
113
|
+
// `false`: another user, or none, is signed in now. `true`: the same user holds another token
|
|
114
|
+
// now (another tab refreshed, or a new sign-in), which the retry uses. `undefined`: unchanged.
|
|
115
|
+
const readChange = () => {
|
|
116
|
+
const now = readSession();
|
|
117
|
+
if (now.userId !== startedFor.userId) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return now.token === startedFor.token ? undefined : true;
|
|
121
|
+
};
|
|
122
|
+
let response;
|
|
123
|
+
try {
|
|
124
|
+
const result = await this.doRequest({
|
|
125
|
+
paths: [path],
|
|
126
|
+
method: RequestMethods.POST,
|
|
127
|
+
type: RequestTypes.SEND,
|
|
128
|
+
headers: this.getRequestHeader({ resource: path }),
|
|
129
|
+
});
|
|
130
|
+
response = result;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
const change = readChange();
|
|
134
|
+
if (change === undefined) {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
return change;
|
|
138
|
+
}
|
|
139
|
+
const user = authService.getUser();
|
|
140
|
+
const change = readChange();
|
|
92
141
|
// Signed out while the request was in flight: saving the token would start a session with no user.
|
|
93
142
|
if (user.userId === undefined) {
|
|
94
143
|
return false;
|
|
95
144
|
}
|
|
96
|
-
this.
|
|
145
|
+
// The session moved on: this answer is for the one that asked, so it is not saved.
|
|
146
|
+
if (change !== undefined) {
|
|
147
|
+
this.logger.warn('[refreshToken] The session changed while the refresh was in flight, its answer is dropped');
|
|
148
|
+
return change;
|
|
149
|
+
}
|
|
150
|
+
authService.saveAuth({
|
|
97
151
|
userId: user.userId,
|
|
98
152
|
username: user.username,
|
|
99
153
|
referenceId: user.referenceId,
|
|
@@ -128,7 +182,7 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
128
182
|
onAuthFailure() {
|
|
129
183
|
return Promise.resolve();
|
|
130
184
|
}
|
|
131
|
-
/** The tracing id of a request, sent in `x-request-
|
|
185
|
+
/** The tracing id of a request, sent in `x-request-id`. Default `<application name>_<uuid>`. */
|
|
132
186
|
buildRequestTracingId(opts) {
|
|
133
187
|
return `${opts.applicationInfo.name}_${UuidV4Generator.generate()}`;
|
|
134
188
|
}
|
|
@@ -189,6 +243,10 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
189
243
|
errorRootKey: ResponseReader.ERROR_ROOT_KEY,
|
|
190
244
|
};
|
|
191
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* The `token` (`<type> <value>`, `Bearer` when the type is empty) and `provider` of the stored
|
|
248
|
+
* token. Throws an error with status 401 when there is no token.
|
|
249
|
+
*/
|
|
192
250
|
getRequestAuthorizationHeader() {
|
|
193
251
|
const authToken = this.resolveAuthToken();
|
|
194
252
|
if (!authToken?.value) {
|
|
@@ -206,6 +264,7 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
206
264
|
setHeaders(opts) {
|
|
207
265
|
this.headers = NetworkHeaders.merge({ sources: [this.headers, opts.headers] });
|
|
208
266
|
}
|
|
267
|
+
/** Removes the headers set by name, in any case. */
|
|
209
268
|
removeHeaders(opts) {
|
|
210
269
|
if (!opts.keys?.length) {
|
|
211
270
|
return;
|
|
@@ -238,6 +297,10 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
238
297
|
[HeaderConstants.REQUEST_TRACING_ID]: this.buildRequestTracingId({ applicationInfo }),
|
|
239
298
|
};
|
|
240
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* The session headers, plus `authorization` and `x-auth-provider` unless `resource` is a no-auth
|
|
302
|
+
* path. Throws a 401 error for any other resource when there is no token.
|
|
303
|
+
*/
|
|
241
304
|
getRequestHeader(opts) {
|
|
242
305
|
const { resource } = opts;
|
|
243
306
|
const defaultHeaders = this.getSessionHeaders();
|
|
@@ -252,10 +315,38 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
252
315
|
[HeaderConstants.AUTHORIZATION]: authHeader.token,
|
|
253
316
|
};
|
|
254
317
|
}
|
|
318
|
+
/**
|
|
319
|
+
* Whether the request stays where `baseUrl` points: no entry of `paths` carries a tab, a line break,
|
|
320
|
+
* a backslash or a dot segment (also written `%2E`), and the URL built from
|
|
321
|
+
* them, resolved the way a browser does, has the origin of the base and a path under it.
|
|
322
|
+
*/
|
|
323
|
+
static isUnderBase(opts) {
|
|
324
|
+
const { baseUrl, paths, url } = opts;
|
|
325
|
+
// The joined path, so a dot segment is found whichever entry holds it.
|
|
326
|
+
if (/[\\\t\n\r]|(^|\/)(\.|%2e){1,2}(\/|$)/i.test(paths.join('/'))) {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
try {
|
|
330
|
+
const origin = 'http://base.invalid';
|
|
331
|
+
const base = new URL(baseUrl, origin);
|
|
332
|
+
const target = new URL(url, origin);
|
|
333
|
+
const prefix = base.pathname.replace(/\/+$/, '');
|
|
334
|
+
return (target.origin === base.origin &&
|
|
335
|
+
(target.pathname === prefix || target.pathname.startsWith(`${prefix}/`)));
|
|
336
|
+
// eslint-disable-next-line venizia/no-silent-catch -- a URL that does not parse is the answer: the request is refused
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
255
342
|
/** The body as it is sent and the content type to send it under. Override to encode another way. */
|
|
256
343
|
encodeBody(opts) {
|
|
257
344
|
return RequestBodyEncoder.encode(opts);
|
|
258
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* The headers (session, auth, tracing, `x-request-count`, the `extra` header, and the content type
|
|
348
|
+
* of the encoded body) and the encoded body of a request. Throws as `getRequestHeader` does.
|
|
349
|
+
*/
|
|
259
350
|
getRequestProps(params) {
|
|
260
351
|
const { bodyType, body, resource, restDataProviderOptions, applicationInfo, requestCountData = RequestCountData.DATA_ONLY, extra, } = params;
|
|
261
352
|
const extraHeader = HttpExtraRequest.toHeader({ extra });
|
|
@@ -281,6 +372,12 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
281
372
|
convertResponse(opts) {
|
|
282
373
|
return ResponseReader.convert(opts);
|
|
283
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Sends the request and reads the response by `type`. Throws when `baseUrl` (the option, else the
|
|
377
|
+
* service's) is empty. A 401 on a path that can recover triggers `refresh` and, if it refreshed,
|
|
378
|
+
* one retry with the new token. Any other non-2xx response, or a retry that still fails, throws
|
|
379
|
+
* the error read from the response.
|
|
380
|
+
*/
|
|
284
381
|
async doRequest(opts) {
|
|
285
382
|
const { baseUrl = this.baseUrl, type, method, paths, body, headers, query, requestCountData, } = opts;
|
|
286
383
|
if (!baseUrl) {
|
|
@@ -289,6 +386,9 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
289
386
|
});
|
|
290
387
|
}
|
|
291
388
|
const url = this.networkRequest.getRequestUrl({ baseUrl, paths });
|
|
389
|
+
if (!DefaultNetworkRequestService.isUnderBase({ baseUrl, paths, url })) {
|
|
390
|
+
throw getError({ message: '[doRequest] Invalid path to send request!' });
|
|
391
|
+
}
|
|
292
392
|
const sendOnce = (sendHeaders) => {
|
|
293
393
|
return this.networkRequest.getNetworkService().send({
|
|
294
394
|
url,
|
|
@@ -298,8 +398,12 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
298
398
|
headers: sendHeaders,
|
|
299
399
|
}, this.logger);
|
|
300
400
|
};
|
|
401
|
+
// Who sends: a 401 that arrives after another user signed in is not this user's to refresh.
|
|
402
|
+
const sentBy = `${this.authService?.getUser().userId}`;
|
|
301
403
|
let fetched = await sendOnce(headers);
|
|
302
|
-
if (fetched.status === 401 &&
|
|
404
|
+
if (fetched.status === 401 &&
|
|
405
|
+
`${this.authService?.getUser().userId}` === sentBy &&
|
|
406
|
+
this.recovery.canRecover({ paths })) {
|
|
303
407
|
const isRefreshed = await this.refresh();
|
|
304
408
|
if (isRefreshed) {
|
|
305
409
|
const authHeader = this.getRequestAuthorizationHeader();
|
|
@@ -1,16 +1,26 @@
|
|
|
1
|
+
/** Values of the running environment, read once when the module loads. */
|
|
1
2
|
export declare class App {
|
|
3
|
+
/** The IANA name of the timezone of the runtime. */
|
|
2
4
|
static readonly TIMEZONE: string;
|
|
5
|
+
/** The offset from UTC in hours, positive east of UTC (the opposite sign of `getTimezoneOffset()`). */
|
|
3
6
|
static readonly TIMEZONE_OFFSET: number;
|
|
7
|
+
/** In milliseconds. */
|
|
4
8
|
static readonly DEFAULT_DEBOUNCE_TIME = 500;
|
|
5
9
|
}
|
|
10
|
+
/** Authorization schemes (`Basic`, `Bearer`) and strategies (`basic`, `jwt`). */
|
|
6
11
|
export declare class Authentication {
|
|
12
|
+
/** The scheme in the `authorization` header. */
|
|
7
13
|
static readonly TYPE_BASIC = "Basic";
|
|
8
14
|
static readonly TYPE_BEARER = "Bearer";
|
|
15
|
+
/** The name of the auth strategy. */
|
|
9
16
|
static readonly STRATEGY_BASIC = "basic";
|
|
10
17
|
static readonly STRATEGY_JWT = "jwt";
|
|
18
|
+
/** Every value of this class that `isValid` accepts. */
|
|
11
19
|
static readonly SCHEME_SET: Set<string>;
|
|
20
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
12
21
|
static isValid(value: string): boolean;
|
|
13
22
|
}
|
|
23
|
+
/** HTTP methods. */
|
|
14
24
|
export declare class RequestMethods {
|
|
15
25
|
static readonly HEAD = "HEAD";
|
|
16
26
|
static readonly OPTIONS = "OPTIONS";
|
|
@@ -19,10 +29,14 @@ export declare class RequestMethods {
|
|
|
19
29
|
static readonly PUT = "PUT";
|
|
20
30
|
static readonly PATCH = "PATCH";
|
|
21
31
|
static readonly DELETE = "DELETE";
|
|
32
|
+
/** Every value of this class that `isValid` accepts. */
|
|
22
33
|
static readonly SCHEME_SET: Set<string>;
|
|
34
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
23
35
|
static isValid(value: string): boolean;
|
|
24
36
|
}
|
|
37
|
+
/** What a request is for, which decides how its response is read: `SEND`, and react-admin's data provider calls. */
|
|
25
38
|
export declare class RequestTypes {
|
|
39
|
+
/** A request that is not a react-admin call. */
|
|
26
40
|
static readonly SEND = "SEND";
|
|
27
41
|
static readonly GET_LIST = "GET_LIST";
|
|
28
42
|
static readonly GET_ONE = "GET_ONE";
|
|
@@ -33,29 +47,43 @@ export declare class RequestTypes {
|
|
|
33
47
|
static readonly UPDATE_MANY = "UPDATE_MANY";
|
|
34
48
|
static readonly DELETE = "DELETE";
|
|
35
49
|
static readonly DELETE_MANY = "DELETE_MANY";
|
|
50
|
+
/** Every value of this class that `isValid` accepts. */
|
|
36
51
|
static readonly SCHEME_SET: Set<string>;
|
|
52
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
37
53
|
static isValid(value: string): boolean;
|
|
38
54
|
}
|
|
55
|
+
/** The Web Storage areas `BrowserStorage` reads and writes. */
|
|
39
56
|
export declare class StorageAreas {
|
|
57
|
+
/** `localStorage`. */
|
|
40
58
|
static readonly LOCAL = "local";
|
|
59
|
+
/** `sessionStorage`. */
|
|
41
60
|
static readonly SESSION = "session";
|
|
42
61
|
}
|
|
62
|
+
/** How a request body is encoded; see `RequestBodyEncoder`. */
|
|
43
63
|
export declare class RequestBodyTypes {
|
|
64
|
+
/** The body goes through as it is. */
|
|
44
65
|
static readonly NONE = "none";
|
|
45
66
|
static readonly FORM_DATA = "form-data";
|
|
46
67
|
static readonly FORM_URL_ENCODED = "x-www-form-urlencoded";
|
|
47
68
|
static readonly JSON = "json";
|
|
48
69
|
static readonly BINARY = "binary";
|
|
70
|
+
/** Every value of this class that `isValid` accepts. */
|
|
49
71
|
static readonly SCHEME_SET: Set<string>;
|
|
72
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
50
73
|
static isValid(value: string): boolean;
|
|
51
74
|
}
|
|
75
|
+
/** Names of the build environments. */
|
|
52
76
|
export declare class Environments {
|
|
53
77
|
static readonly DEVELOPMENT = "development";
|
|
54
78
|
static readonly PRODUCTION = "production";
|
|
79
|
+
/** Every value of this class that `isValid` accepts. */
|
|
55
80
|
static readonly SCHEME_SET: Set<string>;
|
|
81
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
56
82
|
static isValid(value: string): boolean;
|
|
57
83
|
}
|
|
84
|
+
/** Header names, lowercase, and patterns on header values. */
|
|
58
85
|
export declare class HeaderConstants {
|
|
86
|
+
/** The header on a request body and on a response. */
|
|
59
87
|
static readonly CONTENT_TYPE = "content-type";
|
|
60
88
|
/**
|
|
61
89
|
* content-range: <unit> <range-start>-<range-end>/<size>
|
|
@@ -63,24 +91,41 @@ export declare class HeaderConstants {
|
|
|
63
91
|
* The positions are zero-indexed, the end inclusive; the total is `*` if unknown.
|
|
64
92
|
*/
|
|
65
93
|
static readonly CONTENT_RANGE = "content-range";
|
|
94
|
+
/** Names the file of a file response. */
|
|
66
95
|
static readonly CONTENT_DISPOSITION = "content-disposition";
|
|
96
|
+
/** Carries `<type> <token>`. */
|
|
67
97
|
static readonly AUTHORIZATION = "authorization";
|
|
98
|
+
/** Names the provider of the token; sent only when the token names one. */
|
|
68
99
|
static readonly X_AUTH_PROVIDER = "x-auth-provider";
|
|
100
|
+
/** The locale of the request, the header `useRequestHeaderLocale` sets by default. */
|
|
69
101
|
static readonly X_LOCALE = "x-locale";
|
|
102
|
+
/** Carries `App.TIMEZONE`. */
|
|
70
103
|
static readonly TIMEZONE = "timezone";
|
|
104
|
+
/** Carries `App.TIMEZONE_OFFSET`, in hours. */
|
|
71
105
|
static readonly TIMEZONE_OFFSET = "timezone-offset";
|
|
106
|
+
/** Carries the tracing id of a request. */
|
|
72
107
|
static readonly REQUEST_TRACING_ID = "x-request-id";
|
|
108
|
+
/** Carries a `RequestChannel` value. */
|
|
73
109
|
static readonly REQUEST_CHANNEL = "x-request-channel";
|
|
110
|
+
/** Carries a `RequestCountData` value: what the request asks the server to return. */
|
|
74
111
|
static readonly REQUEST_COUNT_DATA = "x-request-count";
|
|
112
|
+
/** Carries the count a server sends with a response that is not a list. */
|
|
75
113
|
static readonly RESPONSE_COUNT_DATA = "x-response-count";
|
|
114
|
+
/** Content types read as text. */
|
|
76
115
|
static readonly TEXTUAL_CONTENT_TYPE_RE: RegExp;
|
|
116
|
+
/** A `content-disposition` that marks a file download. */
|
|
77
117
|
static readonly ATTACHMENT_CONTENT_DISPOSITION_RE: RegExp;
|
|
78
118
|
}
|
|
119
|
+
/** The channels a request says it comes from, sent in `x-request-channel`. */
|
|
79
120
|
export declare class RequestChannel {
|
|
121
|
+
/** A browser application; what a request says unless `requestTracingChannel` is set. */
|
|
80
122
|
static readonly WEB = "100_WEB";
|
|
123
|
+
/** Every value of this class that `isValid` accepts. */
|
|
81
124
|
static readonly SCHEME_SET: Set<string>;
|
|
125
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
82
126
|
static isValid(value: string): boolean;
|
|
83
127
|
}
|
|
128
|
+
/** Whether a response carries a count, sent in `x-request-count`. */
|
|
84
129
|
export declare class RequestCountData {
|
|
85
130
|
/**
|
|
86
131
|
* @description Only the data is returned in the response body
|
|
@@ -90,7 +135,9 @@ export declare class RequestCountData {
|
|
|
90
135
|
* @description Both data and count are returned in the response body
|
|
91
136
|
*/
|
|
92
137
|
static readonly DATA_WITH_COUNT = "1";
|
|
138
|
+
/** Every value of this class that `isValid` accepts. */
|
|
93
139
|
static readonly SCHEME_SET: Set<string>;
|
|
140
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
94
141
|
static isValid(value: string): boolean;
|
|
95
142
|
}
|
|
96
143
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/common/constants.js
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
|
+
/** Values of the running environment, read once when the module loads. */
|
|
1
2
|
export class App {
|
|
3
|
+
/** The IANA name of the timezone of the runtime. */
|
|
2
4
|
static { this.TIMEZONE = Intl.DateTimeFormat().resolvedOptions().timeZone; }
|
|
5
|
+
/** The offset from UTC in hours, positive east of UTC (the opposite sign of `getTimezoneOffset()`). */
|
|
3
6
|
static { this.TIMEZONE_OFFSET = -(new Date().getTimezoneOffset() / 60); }
|
|
7
|
+
/** In milliseconds. */
|
|
4
8
|
static { this.DEFAULT_DEBOUNCE_TIME = 500; }
|
|
5
9
|
}
|
|
10
|
+
/** Authorization schemes (`Basic`, `Bearer`) and strategies (`basic`, `jwt`). */
|
|
6
11
|
export class Authentication {
|
|
12
|
+
/** The scheme in the `authorization` header. */
|
|
7
13
|
static { this.TYPE_BASIC = 'Basic'; }
|
|
8
14
|
static { this.TYPE_BEARER = 'Bearer'; }
|
|
15
|
+
/** The name of the auth strategy. */
|
|
9
16
|
static { this.STRATEGY_BASIC = 'basic'; }
|
|
10
17
|
static { this.STRATEGY_JWT = 'jwt'; }
|
|
18
|
+
/** Every value of this class that `isValid` accepts. */
|
|
11
19
|
static { this.SCHEME_SET = new Set([
|
|
12
20
|
this.TYPE_BASIC,
|
|
13
21
|
this.TYPE_BEARER,
|
|
14
22
|
this.STRATEGY_BASIC,
|
|
15
23
|
this.STRATEGY_JWT,
|
|
16
24
|
]); }
|
|
25
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
17
26
|
static isValid(value) {
|
|
18
27
|
return this.SCHEME_SET.has(value);
|
|
19
28
|
}
|
|
20
29
|
}
|
|
30
|
+
/** HTTP methods. */
|
|
21
31
|
export class RequestMethods {
|
|
22
32
|
static { this.HEAD = 'HEAD'; }
|
|
23
33
|
static { this.OPTIONS = 'OPTIONS'; }
|
|
@@ -26,6 +36,7 @@ export class RequestMethods {
|
|
|
26
36
|
static { this.PUT = 'PUT'; }
|
|
27
37
|
static { this.PATCH = 'PATCH'; }
|
|
28
38
|
static { this.DELETE = 'DELETE'; }
|
|
39
|
+
/** Every value of this class that `isValid` accepts. */
|
|
29
40
|
static { this.SCHEME_SET = new Set([
|
|
30
41
|
this.HEAD,
|
|
31
42
|
this.OPTIONS,
|
|
@@ -35,11 +46,14 @@ export class RequestMethods {
|
|
|
35
46
|
this.PATCH,
|
|
36
47
|
this.DELETE,
|
|
37
48
|
]); }
|
|
49
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
38
50
|
static isValid(value) {
|
|
39
51
|
return this.SCHEME_SET.has(value);
|
|
40
52
|
}
|
|
41
53
|
}
|
|
54
|
+
/** What a request is for, which decides how its response is read: `SEND`, and react-admin's data provider calls. */
|
|
42
55
|
export class RequestTypes {
|
|
56
|
+
/** A request that is not a react-admin call. */
|
|
43
57
|
static { this.SEND = 'SEND'; }
|
|
44
58
|
// react-admin
|
|
45
59
|
static { this.GET_LIST = 'GET_LIST'; }
|
|
@@ -51,6 +65,7 @@ export class RequestTypes {
|
|
|
51
65
|
static { this.UPDATE_MANY = 'UPDATE_MANY'; }
|
|
52
66
|
static { this.DELETE = 'DELETE'; }
|
|
53
67
|
static { this.DELETE_MANY = 'DELETE_MANY'; }
|
|
68
|
+
/** Every value of this class that `isValid` accepts. */
|
|
54
69
|
static { this.SCHEME_SET = new Set([
|
|
55
70
|
this.SEND,
|
|
56
71
|
this.GET_ONE,
|
|
@@ -63,20 +78,27 @@ export class RequestTypes {
|
|
|
63
78
|
this.DELETE,
|
|
64
79
|
this.DELETE_MANY,
|
|
65
80
|
]); }
|
|
81
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
66
82
|
static isValid(value) {
|
|
67
83
|
return this.SCHEME_SET.has(value);
|
|
68
84
|
}
|
|
69
85
|
}
|
|
86
|
+
/** The Web Storage areas `BrowserStorage` reads and writes. */
|
|
70
87
|
export class StorageAreas {
|
|
88
|
+
/** `localStorage`. */
|
|
71
89
|
static { this.LOCAL = 'local'; }
|
|
90
|
+
/** `sessionStorage`. */
|
|
72
91
|
static { this.SESSION = 'session'; }
|
|
73
92
|
}
|
|
93
|
+
/** How a request body is encoded; see `RequestBodyEncoder`. */
|
|
74
94
|
export class RequestBodyTypes {
|
|
95
|
+
/** The body goes through as it is. */
|
|
75
96
|
static { this.NONE = 'none'; }
|
|
76
97
|
static { this.FORM_DATA = 'form-data'; }
|
|
77
98
|
static { this.FORM_URL_ENCODED = 'x-www-form-urlencoded'; }
|
|
78
99
|
static { this.JSON = 'json'; }
|
|
79
100
|
static { this.BINARY = 'binary'; }
|
|
101
|
+
/** Every value of this class that `isValid` accepts. */
|
|
80
102
|
static { this.SCHEME_SET = new Set([
|
|
81
103
|
this.NONE,
|
|
82
104
|
this.FORM_DATA,
|
|
@@ -84,19 +106,25 @@ export class RequestBodyTypes {
|
|
|
84
106
|
this.JSON,
|
|
85
107
|
this.BINARY,
|
|
86
108
|
]); }
|
|
109
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
87
110
|
static isValid(value) {
|
|
88
111
|
return this.SCHEME_SET.has(value);
|
|
89
112
|
}
|
|
90
113
|
}
|
|
114
|
+
/** Names of the build environments. */
|
|
91
115
|
export class Environments {
|
|
92
116
|
static { this.DEVELOPMENT = 'development'; }
|
|
93
117
|
static { this.PRODUCTION = 'production'; }
|
|
118
|
+
/** Every value of this class that `isValid` accepts. */
|
|
94
119
|
static { this.SCHEME_SET = new Set([this.DEVELOPMENT, this.PRODUCTION]); }
|
|
120
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
95
121
|
static isValid(value) {
|
|
96
122
|
return this.SCHEME_SET.has(value);
|
|
97
123
|
}
|
|
98
124
|
}
|
|
125
|
+
/** Header names, lowercase, and patterns on header values. */
|
|
99
126
|
export class HeaderConstants {
|
|
127
|
+
/** The header on a request body and on a response. */
|
|
100
128
|
static { this.CONTENT_TYPE = 'content-type'; }
|
|
101
129
|
/**
|
|
102
130
|
* content-range: <unit> <range-start>-<range-end>/<size>
|
|
@@ -104,27 +132,44 @@ export class HeaderConstants {
|
|
|
104
132
|
* The positions are zero-indexed, the end inclusive; the total is `*` if unknown.
|
|
105
133
|
*/
|
|
106
134
|
static { this.CONTENT_RANGE = 'content-range'; }
|
|
135
|
+
/** Names the file of a file response. */
|
|
107
136
|
static { this.CONTENT_DISPOSITION = 'content-disposition'; }
|
|
137
|
+
/** Carries `<type> <token>`. */
|
|
108
138
|
static { this.AUTHORIZATION = 'authorization'; }
|
|
139
|
+
/** Names the provider of the token; sent only when the token names one. */
|
|
109
140
|
static { this.X_AUTH_PROVIDER = 'x-auth-provider'; }
|
|
141
|
+
/** The locale of the request, the header `useRequestHeaderLocale` sets by default. */
|
|
110
142
|
static { this.X_LOCALE = 'x-locale'; }
|
|
111
143
|
// Lowercase: the service keys its header record by lowercased name.
|
|
144
|
+
/** Carries `App.TIMEZONE`. */
|
|
112
145
|
static { this.TIMEZONE = 'timezone'; }
|
|
146
|
+
/** Carries `App.TIMEZONE_OFFSET`, in hours. */
|
|
113
147
|
static { this.TIMEZONE_OFFSET = 'timezone-offset'; }
|
|
148
|
+
/** Carries the tracing id of a request. */
|
|
114
149
|
static { this.REQUEST_TRACING_ID = 'x-request-id'; }
|
|
150
|
+
/** Carries a `RequestChannel` value. */
|
|
115
151
|
static { this.REQUEST_CHANNEL = 'x-request-channel'; }
|
|
152
|
+
/** Carries a `RequestCountData` value: what the request asks the server to return. */
|
|
116
153
|
static { this.REQUEST_COUNT_DATA = 'x-request-count'; }
|
|
154
|
+
/** Carries the count a server sends with a response that is not a list. */
|
|
117
155
|
static { this.RESPONSE_COUNT_DATA = 'x-response-count'; }
|
|
156
|
+
/** Content types read as text. */
|
|
118
157
|
static { this.TEXTUAL_CONTENT_TYPE_RE = /^(application\/(json|.*\+json|xml|.*\+xml|x-www-form-urlencoded|javascript|graphql)|text\/)/i; }
|
|
158
|
+
/** A `content-disposition` that marks a file download. */
|
|
119
159
|
static { this.ATTACHMENT_CONTENT_DISPOSITION_RE = /^attachment/i; }
|
|
120
160
|
}
|
|
161
|
+
/** The channels a request says it comes from, sent in `x-request-channel`. */
|
|
121
162
|
export class RequestChannel {
|
|
163
|
+
/** A browser application; what a request says unless `requestTracingChannel` is set. */
|
|
122
164
|
static { this.WEB = '100_WEB'; }
|
|
165
|
+
/** Every value of this class that `isValid` accepts. */
|
|
123
166
|
static { this.SCHEME_SET = new Set([this.WEB]); }
|
|
167
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
124
168
|
static isValid(value) {
|
|
125
169
|
return this.SCHEME_SET.has(value);
|
|
126
170
|
}
|
|
127
171
|
}
|
|
172
|
+
/** Whether a response carries a count, sent in `x-request-count`. */
|
|
128
173
|
export class RequestCountData {
|
|
129
174
|
/**
|
|
130
175
|
* @description Only the data is returned in the response body
|
|
@@ -134,7 +179,9 @@ export class RequestCountData {
|
|
|
134
179
|
* @description Both data and count are returned in the response body
|
|
135
180
|
*/
|
|
136
181
|
static { this.DATA_WITH_COUNT = '1'; }
|
|
182
|
+
/** Every value of this class that `isValid` accepts. */
|
|
137
183
|
static { this.SCHEME_SET = new Set([this.DATA_ONLY, this.DATA_WITH_COUNT]); }
|
|
184
|
+
/** Whether `value` is in `SCHEME_SET`. */
|
|
138
185
|
static isValid(value) {
|
|
139
186
|
return this.SCHEME_SET.has(value);
|
|
140
187
|
}
|
package/dist/common/keys.d.ts
CHANGED
|
@@ -1,27 +1,63 @@
|
|
|
1
|
+
/** The container keys the kernel binds and reads. */
|
|
1
2
|
export declare class CoreBindings {
|
|
3
|
+
/** The application itself, bound by `preConfigure`. */
|
|
2
4
|
static readonly APPLICATION_INSTANCE = "@app/application/instance";
|
|
5
|
+
/** The `IApplicationInfo`, bound by `preConfigure`. */
|
|
3
6
|
static readonly APPLICATION_INFO = "@app/application/info";
|
|
7
|
+
/** The auth provider. */
|
|
4
8
|
static readonly DEFAULT_AUTH_PROVIDER = "@app/application/auth/default";
|
|
9
|
+
/** The i18n provider. */
|
|
5
10
|
static readonly DEFAULT_I18N_PROVIDER = "@app/application/i18n/default";
|
|
11
|
+
/** The REST data provider. */
|
|
6
12
|
static readonly DEFAULT_REST_DATA_PROVIDER = "@app/application/data/rest/default";
|
|
13
|
+
/** The `IAuthProviderOptions`. */
|
|
7
14
|
static readonly AUTH_PROVIDER_OPTIONS = "@app/application/options/auth";
|
|
15
|
+
/** The `DefaultAuthService`. */
|
|
8
16
|
static readonly DEFAULT_AUTH_SERVICE = "@app/application/service/auth/default";
|
|
17
|
+
/** The `IRestDataProviderOptions`. */
|
|
9
18
|
static readonly REST_DATA_PROVIDER_OPTIONS = "@app/application/options/rest/data";
|
|
19
|
+
/** The options of the i18n provider. */
|
|
10
20
|
static readonly I18N_PROVIDER_OPTIONS = "@app/application/options/i18n";
|
|
11
21
|
/** The `features()` an application mounts, bound by `preConfigure()`. */
|
|
12
22
|
static readonly FEATURES = "@app/application/features";
|
|
13
23
|
/** An `IPermissionProvider`, bound by the application; unbound means every check passes. */
|
|
14
24
|
static readonly PERMISSION_PROVIDER = "@app/application/permission/provider";
|
|
15
25
|
}
|
|
26
|
+
/** The session keys and the store prefix of one application, from `LocalStorageKeys.scoped`. */
|
|
27
|
+
export interface IScopedKeys {
|
|
28
|
+
/** Everything under it is this application's session: `DefaultAuthService.cleanUp` removes it all. */
|
|
29
|
+
authPrefix: string;
|
|
30
|
+
/** The data of this application's signed-in user; sign-out removes everything under it. */
|
|
31
|
+
storePrefix: string;
|
|
32
|
+
token: string;
|
|
33
|
+
identity: string;
|
|
34
|
+
permission: string;
|
|
35
|
+
}
|
|
36
|
+
/** The `localStorage` keys the kernel owns. */
|
|
16
37
|
export declare class LocalStorageKeys {
|
|
17
38
|
/** Everything under this prefix is the session: `DefaultAuthService.cleanUp` removes it all. */
|
|
18
39
|
static readonly PREFIX_AUTH = "@app/auth/";
|
|
19
|
-
/** The
|
|
40
|
+
/** The data of the signed-in user: sign-out removes everything under it. Preferences that outlive a sign-out go under `@app/prefs/`. */
|
|
20
41
|
static readonly PREFIX_STORE = "@app/store/";
|
|
42
|
+
/** The stored `IAuthTokenRecord`, as JSON. */
|
|
21
43
|
static readonly KEY_AUTH_TOKEN = "@app/auth/token";
|
|
44
|
+
/** The stored identity (`userId`, `username`, `referenceId`, `provider`), as JSON. */
|
|
22
45
|
static readonly KEY_AUTH_IDENTITY = "@app/auth/identity";
|
|
46
|
+
/** The stored roles, as a JSON array of names. */
|
|
23
47
|
static readonly KEY_AUTH_PERMISSION = "@app/auth/permission";
|
|
48
|
+
/** The three session keys. */
|
|
24
49
|
static readonly SCHEME_SET: Set<string>;
|
|
50
|
+
/**
|
|
51
|
+
* The keys of one application. With no `id` they are the constants above, so an application that is
|
|
52
|
+
* alone on its origin changes nothing. With an `id` (`IApplicationInfo.id`) the session sits under
|
|
53
|
+
* `@app/auth/<id>/` and the store under `@app/store/<id>/`, so two applications on one origin share
|
|
54
|
+
* no session and no sign-out sweeps the other's data. The `id` is letters, digits, `.`, `_` and `-`,
|
|
55
|
+
* and is not made of dots only.
|
|
56
|
+
*/
|
|
57
|
+
static scoped(opts?: {
|
|
58
|
+
id?: string;
|
|
59
|
+
}): IScopedKeys;
|
|
60
|
+
/** Whether `value` is a session key in `SCHEME_SET`. */
|
|
25
61
|
static isValid(value: string): boolean;
|
|
26
62
|
}
|
|
27
63
|
//# sourceMappingURL=keys.d.ts.map
|