@venizia/ardor-kernel 0.4.8-1 → 0.4.8-10
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
package/dist/common/keys.js
CHANGED
|
@@ -1,31 +1,71 @@
|
|
|
1
|
+
import { getError } from '@venizia/ignis-inversion';
|
|
2
|
+
/** The container keys the kernel binds and reads. */
|
|
1
3
|
export class CoreBindings {
|
|
4
|
+
/** The application itself, bound by `preConfigure`. */
|
|
2
5
|
static { this.APPLICATION_INSTANCE = '@app/application/instance'; }
|
|
6
|
+
/** The `IApplicationInfo`, bound by `preConfigure`. */
|
|
3
7
|
static { this.APPLICATION_INFO = '@app/application/info'; }
|
|
8
|
+
/** The auth provider. */
|
|
4
9
|
static { this.DEFAULT_AUTH_PROVIDER = '@app/application/auth/default'; }
|
|
10
|
+
/** The i18n provider. */
|
|
5
11
|
static { this.DEFAULT_I18N_PROVIDER = '@app/application/i18n/default'; }
|
|
12
|
+
/** The REST data provider. */
|
|
6
13
|
static { this.DEFAULT_REST_DATA_PROVIDER = '@app/application/data/rest/default'; }
|
|
14
|
+
/** The `IAuthProviderOptions`. */
|
|
7
15
|
static { this.AUTH_PROVIDER_OPTIONS = '@app/application/options/auth'; }
|
|
16
|
+
/** The `DefaultAuthService`. */
|
|
8
17
|
static { this.DEFAULT_AUTH_SERVICE = '@app/application/service/auth/default'; }
|
|
18
|
+
/** The `IRestDataProviderOptions`. */
|
|
9
19
|
static { this.REST_DATA_PROVIDER_OPTIONS = '@app/application/options/rest/data'; }
|
|
20
|
+
/** The options of the i18n provider. */
|
|
10
21
|
static { this.I18N_PROVIDER_OPTIONS = '@app/application/options/i18n'; }
|
|
11
22
|
/** The `features()` an application mounts, bound by `preConfigure()`. */
|
|
12
23
|
static { this.FEATURES = '@app/application/features'; }
|
|
13
24
|
/** An `IPermissionProvider`, bound by the application; unbound means every check passes. */
|
|
14
25
|
static { this.PERMISSION_PROVIDER = '@app/application/permission/provider'; }
|
|
15
26
|
}
|
|
27
|
+
/** The `localStorage` keys the kernel owns. */
|
|
16
28
|
export class LocalStorageKeys {
|
|
17
29
|
/** Everything under this prefix is the session: `DefaultAuthService.cleanUp` removes it all. */
|
|
18
30
|
static { this.PREFIX_AUTH = '@app/auth/'; }
|
|
19
|
-
/** The
|
|
31
|
+
/** The data of the signed-in user: sign-out removes everything under it. Preferences that outlive a sign-out go under `@app/prefs/`. */
|
|
20
32
|
static { this.PREFIX_STORE = '@app/store/'; }
|
|
33
|
+
/** The stored `IAuthTokenRecord`, as JSON. */
|
|
21
34
|
static { this.KEY_AUTH_TOKEN = '@app/auth/token'; }
|
|
35
|
+
/** The stored identity (`userId`, `username`, `referenceId`, `provider`), as JSON. */
|
|
22
36
|
static { this.KEY_AUTH_IDENTITY = '@app/auth/identity'; }
|
|
37
|
+
/** The stored roles, as a JSON array of names. */
|
|
23
38
|
static { this.KEY_AUTH_PERMISSION = '@app/auth/permission'; }
|
|
39
|
+
/** The three session keys. */
|
|
24
40
|
static { this.SCHEME_SET = new Set([
|
|
25
41
|
this.KEY_AUTH_TOKEN,
|
|
26
42
|
this.KEY_AUTH_IDENTITY,
|
|
27
43
|
this.KEY_AUTH_PERMISSION,
|
|
28
44
|
]); }
|
|
45
|
+
/**
|
|
46
|
+
* The keys of one application. With no `id` they are the constants above, so an application that is
|
|
47
|
+
* alone on its origin changes nothing. With an `id` (`IApplicationInfo.id`) the session sits under
|
|
48
|
+
* `@app/auth/<id>/` and the store under `@app/store/<id>/`, so two applications on one origin share
|
|
49
|
+
* no session and no sign-out sweeps the other's data. The `id` is letters, digits, `.`, `_` and `-`,
|
|
50
|
+
* and is not made of dots only.
|
|
51
|
+
*/
|
|
52
|
+
static scoped(opts = {}) {
|
|
53
|
+
const { id } = opts;
|
|
54
|
+
if (id !== undefined && !(typeof id === 'string' && /^(?!\.+$)[A-Za-z0-9._-]+$/.test(id))) {
|
|
55
|
+
throw getError({
|
|
56
|
+
message: "[LocalStorageKeys.scoped] IApplicationInfo.id is letters, digits, '.', '_' and '-', and not dots only",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const scope = id === undefined ? '' : `${id}/`;
|
|
60
|
+
return {
|
|
61
|
+
authPrefix: `${this.PREFIX_AUTH}${scope}`,
|
|
62
|
+
storePrefix: `${this.PREFIX_STORE}${scope}`,
|
|
63
|
+
token: `${this.PREFIX_AUTH}${scope}token`,
|
|
64
|
+
identity: `${this.PREFIX_AUTH}${scope}identity`,
|
|
65
|
+
permission: `${this.PREFIX_AUTH}${scope}permission`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** Whether `value` is a session key in `SCHEME_SET`. */
|
|
29
69
|
static isValid(value) {
|
|
30
70
|
return this.SCHEME_SET.has(value);
|
|
31
71
|
}
|
package/dist/common/types.d.ts
CHANGED
|
@@ -3,41 +3,65 @@ import type { TBuildInfoRecord } from '@venizia/ignis-helpers/core';
|
|
|
3
3
|
import type { TClass } from '@venizia/ignis-inversion';
|
|
4
4
|
import type { TExtraRequest } from '@venizia/ignis-kernel/repository';
|
|
5
5
|
import type { Environments, RequestBodyTypes, RequestCountData, RequestMethods, RequestTypes, StorageAreas } from './constants.js';
|
|
6
|
+
/** A record id: a string or a number. */
|
|
6
7
|
export type IdType = string | number;
|
|
7
8
|
export type { AnyObject, AnyType, TConstValue, TNumberConstValue, TPrettify, TStringConstValue, ValueOf, ValueOptional, ValueOptionalExcept, ValueOrPromise, };
|
|
9
|
+
/** One of the `RequestMethods` values. */
|
|
8
10
|
export type TRequestMethod = TStringConstValue<typeof RequestMethods>;
|
|
11
|
+
/** One of the `Environments` values. */
|
|
9
12
|
export type TEnvironment = TStringConstValue<typeof Environments>;
|
|
13
|
+
/** What `IDataProvider.send` takes. */
|
|
10
14
|
export interface ISendParams {
|
|
15
|
+
/** Required: `send` throws without it. */
|
|
11
16
|
method?: TRequestMethod;
|
|
17
|
+
/** How the response is read. `RequestTypes.SEND` when absent. */
|
|
12
18
|
requestType?: TRequestType;
|
|
19
|
+
/** How `body` is encoded; it goes through as JSON when absent. */
|
|
13
20
|
bodyType?: TRequestBodyType;
|
|
14
21
|
body?: unknown;
|
|
22
|
+
/** Sent as the query string of the request. */
|
|
15
23
|
query?: Record<string, unknown>;
|
|
24
|
+
/** Added to the headers of this request. */
|
|
16
25
|
headers?: Record<string, string | number>;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the response carries a count; sent in `x-request-count`.
|
|
28
|
+
* @default RequestCountData.DATA_ONLY
|
|
29
|
+
*/
|
|
17
30
|
requestCountData?: TConstValue<typeof RequestCountData>;
|
|
18
31
|
/** The list extras to ask the route for, sent in `x-request-extra`. */
|
|
19
32
|
extra?: TExtraRequest;
|
|
20
33
|
}
|
|
21
|
-
/**
|
|
22
|
-
export type ISendResponse<T = unknown> = INetworkResponse<T>;
|
|
34
|
+
/** One of the `RequestBodyTypes` values. */
|
|
23
35
|
export type TRequestBodyType = TStringConstValue<typeof RequestBodyTypes>;
|
|
36
|
+
/** One of the `StorageAreas` values. */
|
|
24
37
|
export type TStorageArea = TStringConstValue<typeof StorageAreas>;
|
|
25
38
|
/** How `BrowserStorage` turns a value into text and back. `decode` may throw: `get` then gives the fallback. */
|
|
26
39
|
export interface IStorageCodec<TValue> {
|
|
27
40
|
encode: (value: TValue) => string;
|
|
28
41
|
decode: (text: string) => TValue;
|
|
29
42
|
}
|
|
43
|
+
/** One of the `RequestTypes` values. */
|
|
30
44
|
export type TRequestType = TStringConstValue<typeof RequestTypes>;
|
|
45
|
+
/** What `DefaultNetworkRequestService.getRequestProps` takes. */
|
|
31
46
|
export interface IGetRequestPropsParams {
|
|
47
|
+
/** Decides whether the request goes without a token (`noAuthPaths`, `noAuthPathRegex`). */
|
|
32
48
|
resource: string;
|
|
49
|
+
/**
|
|
50
|
+
* Whether the response carries a count.
|
|
51
|
+
* @default RequestCountData.DATA_ONLY
|
|
52
|
+
*/
|
|
33
53
|
requestCountData?: TConstValue<typeof RequestCountData>;
|
|
34
54
|
body?: unknown;
|
|
55
|
+
/** How `body` is encoded. */
|
|
35
56
|
bodyType?: TRequestBodyType;
|
|
57
|
+
/** Gives the request channel. */
|
|
36
58
|
restDataProviderOptions: IRestDataProviderOptions;
|
|
59
|
+
/** Gives the name the tracing id starts with. */
|
|
37
60
|
applicationInfo: IApplicationInfo;
|
|
38
61
|
/** The list extras to ask the route for, sent in `x-request-extra`. */
|
|
39
62
|
extra?: TExtraRequest;
|
|
40
63
|
}
|
|
64
|
+
/** The headers and the encoded body of a request. */
|
|
41
65
|
export interface IGetRequestPropsResult {
|
|
42
66
|
headers?: HeadersInit;
|
|
43
67
|
body?: unknown;
|
|
@@ -59,18 +83,48 @@ export interface ISignInSession {
|
|
|
59
83
|
/** Data only: where the auth provider talks to. Behaviour is in `DefaultAuthProvider`'s protected hooks. */
|
|
60
84
|
export interface IAuthProviderOptions {
|
|
61
85
|
paths?: {
|
|
62
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* Where `login` posts: the IGNIS auth controller's route.
|
|
88
|
+
* @default '/auth/sign-in'
|
|
89
|
+
*/
|
|
63
90
|
signIn?: string;
|
|
64
91
|
/** Where `checkAuth` validates the stored token. Default: not called; the stored token is enough. */
|
|
65
92
|
checkAuth?: string;
|
|
66
|
-
/**
|
|
67
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Where the admin goes once `login` resolves. `false`: nowhere, `login` resolves with
|
|
95
|
+
* `redirectTo: false` and the page stays (a guest or kiosk shell).
|
|
96
|
+
* @default '/'
|
|
97
|
+
*/
|
|
98
|
+
afterLogin?: string | false;
|
|
68
99
|
};
|
|
69
100
|
}
|
|
101
|
+
/** How a request recovers from a 401. */
|
|
70
102
|
export interface IAuthRecoveryOptions {
|
|
71
|
-
/**
|
|
103
|
+
/**
|
|
104
|
+
* A request to this path never starts a refresh: it is the refresh. Listing it in `noAuthPaths`
|
|
105
|
+
* as well keeps the expired token off that request.
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { configuration, CoreBindings, provide } from '@venizia/ardor';
|
|
109
|
+
*
|
|
110
|
+
* @configuration()
|
|
111
|
+
* export class ApiConfiguration {
|
|
112
|
+
* @provide({ key: CoreBindings.REST_DATA_PROVIDER_OPTIONS })
|
|
113
|
+
* restDataProviderOptions() {
|
|
114
|
+
* return {
|
|
115
|
+
* url: 'https://api.example.com',
|
|
116
|
+
* // The refresh path is also a no-auth path: the expired token stays off that request, so the
|
|
117
|
+
* // server identifies the session another way, such as a cookie.
|
|
118
|
+
* noAuthPaths: ['/auth/sign-in', '/auth/refresh'],
|
|
119
|
+
* authRecovery: { refreshTokenPath: '/auth/refresh' },
|
|
120
|
+
* };
|
|
121
|
+
* }
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
72
125
|
refreshTokenPath?: string;
|
|
73
126
|
}
|
|
127
|
+
/** One pattern or a list; a string is compiled with `new RegExp(...)`. */
|
|
74
128
|
export type TNoAuthPathRegex = string | RegExp | Array<string | RegExp>;
|
|
75
129
|
/**
|
|
76
130
|
* Data only. A request that matches goes without an `Authorization` header; to send none at all (an
|
|
@@ -87,14 +141,23 @@ export interface INoAuthOptions {
|
|
|
87
141
|
*/
|
|
88
142
|
noAuthPathRegex?: TNoAuthPathRegex;
|
|
89
143
|
}
|
|
144
|
+
/** Data only: where and how the REST data provider sends. */
|
|
90
145
|
export interface IRestDataProviderOptions extends INoAuthOptions {
|
|
146
|
+
/** The base URL requests are sent to. */
|
|
91
147
|
url: string;
|
|
92
|
-
/**
|
|
148
|
+
/**
|
|
149
|
+
* Sent in `x-request-channel`.
|
|
150
|
+
* @default RequestChannel.WEB
|
|
151
|
+
*/
|
|
93
152
|
requestTracingChannel?: string;
|
|
153
|
+
/** Sent with every request. */
|
|
94
154
|
headers?: HeadersInit;
|
|
155
|
+
/** Enables the refresh-and-retry after a 401. */
|
|
95
156
|
authRecovery?: IAuthRecoveryOptions;
|
|
96
157
|
}
|
|
158
|
+
/** The token an application is signed in with. */
|
|
97
159
|
export interface IAuthTokenRecord {
|
|
160
|
+
/** The authorization scheme; the service sends `Bearer` when it is empty or absent. */
|
|
98
161
|
type?: string;
|
|
99
162
|
value: string;
|
|
100
163
|
provider?: string;
|
|
@@ -116,7 +179,9 @@ export interface IDataSourceAuth {
|
|
|
116
179
|
}
|
|
117
180
|
/** What `DefaultNetworkRequestService.doRequest` answers. */
|
|
118
181
|
export interface INetworkResponse<TResult = unknown> {
|
|
182
|
+
/** The response body, or its rows for a list. */
|
|
119
183
|
data: TResult;
|
|
184
|
+
/** The count the server sent, in the body or in `x-response-count`; absent when it sent none. */
|
|
120
185
|
count?: number;
|
|
121
186
|
/** GET_LIST and GET_MANY_REFERENCE: the total of `Content-Range`, else the page's own row count. */
|
|
122
187
|
total?: number;
|
|
@@ -136,6 +201,7 @@ export interface IFeatureBase {
|
|
|
136
201
|
/**
|
|
137
202
|
* The feature's `@configuration()` classes, by value. Referencing them keeps a bundler from
|
|
138
203
|
* dropping a module nothing else imports, and `start()` checks each was discovered.
|
|
204
|
+
* @default []
|
|
139
205
|
*/
|
|
140
206
|
configurations?: Array<TClass<unknown>>;
|
|
141
207
|
/** What the feature checks, in the application's vocabulary. Declared for a role editor; ARDOR enforces nothing. */
|
|
@@ -154,7 +220,11 @@ export interface IArtifactIndex {
|
|
|
154
220
|
services?: ReadonlyArray<TClass<unknown>>;
|
|
155
221
|
controllers?: ReadonlyArray<TClass<unknown>>;
|
|
156
222
|
}
|
|
157
|
-
/**
|
|
223
|
+
/**
|
|
224
|
+
* Answers whether the current actor may act on a resource. Bound by the application under
|
|
225
|
+
* `CoreBindings.PERMISSION_PROVIDER`. It decides what the interface shows, not what a user is allowed
|
|
226
|
+
* to do: the backend must check every permission on every request.
|
|
227
|
+
*/
|
|
158
228
|
export interface IPermissionProvider {
|
|
159
229
|
canAccess(opts: {
|
|
160
230
|
resource: string;
|
|
@@ -162,18 +232,31 @@ export interface IPermissionProvider {
|
|
|
162
232
|
record?: unknown;
|
|
163
233
|
}): Promise<boolean>;
|
|
164
234
|
}
|
|
235
|
+
/** The contract of an application: lifecycle hooks, registration of artifacts, and `start`. */
|
|
165
236
|
export interface IArdorApplication {
|
|
237
|
+
/** Binds the application instance and info, then the discovered, feature and listed classes. */
|
|
166
238
|
preConfigure(): ValueOrPromise<void>;
|
|
239
|
+
/** Runs after `preConfigure` in `start`. */
|
|
167
240
|
postConfigure(): ValueOrPromise<void>;
|
|
241
|
+
/** The application's own bindings, bound last in `preConfigure`. */
|
|
168
242
|
bindContext(): ValueOrPromise<void>;
|
|
243
|
+
/** Classes bound by key as singletons. */
|
|
169
244
|
bindingList(): Record<string, TClass<unknown>>;
|
|
245
|
+
/** The features this application mounts. */
|
|
170
246
|
features(): Array<IFeatureBase>;
|
|
247
|
+
/** The artifact indexes `start` checks every stereotyped class against. */
|
|
171
248
|
artifacts(): Array<IArtifactIndex>;
|
|
249
|
+
/** Binds `value` under `<scope>.<ClassName>` as a singleton. */
|
|
172
250
|
injectable<T>(scope: string, value: TClass<T>, tags?: Array<string>): void;
|
|
251
|
+
/** Binds `value` in the service namespace. */
|
|
173
252
|
service<T>(value: TClass<T>): unknown;
|
|
253
|
+
/** Binds `value` in the repository namespace. */
|
|
174
254
|
repository<T>(value: TClass<T>): unknown;
|
|
255
|
+
/** Binds `value` in the data source namespace. */
|
|
175
256
|
dataSource<T>(value: TClass<T>): unknown;
|
|
257
|
+
/** Binds `value` in the component namespace. */
|
|
176
258
|
component<T>(value: TClass<T>): unknown;
|
|
259
|
+
/** Runs the whole startup: resolves the indexes, then `preConfigure`, then `postConfigure`. */
|
|
177
260
|
start(): ValueOrPromise<void>;
|
|
178
261
|
}
|
|
179
262
|
type TDeepPath = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
@@ -187,13 +270,21 @@ type TIsAny<TValue> = 0 extends 1 & TValue ? true : false;
|
|
|
187
270
|
export type TPaths<T, DeepLevel extends number = 10> = DeepLevel extends 0 ? never : T extends Array<AnyType> | TPathLeaf ? never : T extends object ? {
|
|
188
271
|
[K in keyof T]-?: K extends string | number ? TIsAny<T[K]> extends true ? `${K}` : NonNullable<T[K]> extends Array<AnyType> | TPathLeaf ? `${K}` : NonNullable<T[K]> extends object ? `${K}` | `${K}.${TPaths<NonNullable<T[K]>, TDeepPath[DeepLevel]>}` : `${K}` : never;
|
|
189
272
|
}[keyof T] : never;
|
|
273
|
+
/** The dotted paths of an object type, to `DeepLevel` levels (10 by default); unlike `TPaths`, only the paths down to a leaf value. */
|
|
190
274
|
export type TFullPaths<T, DeepLevel extends number = 10> = DeepLevel extends never ? never : T extends Array<infer U> ? TFullPaths<U> : T extends object ? {
|
|
191
275
|
[K in keyof T]-?: K extends string | number ? T[K] extends Array<any> | object ? `${K}.${TFullPaths<T[K], TDeepPath[DeepLevel]>}` : `${K}` : never;
|
|
192
276
|
}[keyof T] : never;
|
|
277
|
+
/** What an application is: bound under `CoreBindings.APPLICATION_INFO`, and the name a tracing id starts with. */
|
|
193
278
|
export interface IApplicationInfo {
|
|
194
279
|
name: string;
|
|
195
280
|
version: string;
|
|
196
281
|
description: string;
|
|
282
|
+
/**
|
|
283
|
+
* Scopes the session and store keys in browser storage to this application (`LocalStorageKeys.scoped`),
|
|
284
|
+
* so two applications on one origin do not share a session. Give every application of an origin an id.
|
|
285
|
+
* Without one the keys are the unscoped ones.
|
|
286
|
+
*/
|
|
287
|
+
id?: string;
|
|
197
288
|
author?: {
|
|
198
289
|
name: string;
|
|
199
290
|
email: string;
|
|
@@ -2,14 +2,18 @@ import { type IAuthTokenRecord } from '../common/index.js';
|
|
|
2
2
|
/** The one place that reads and writes the stored auth token, with the browser guard. */
|
|
3
3
|
export declare class AuthTokenStore {
|
|
4
4
|
private static readonly logger;
|
|
5
|
-
/** The stored token, or `undefined` when none is stored, it is unusable, or there is no `localStorage`. */
|
|
6
|
-
static read(
|
|
5
|
+
/** The stored token (under `key`, `LocalStorageKeys.KEY_AUTH_TOKEN` by default), or `undefined` when none is stored, it is unusable, or there is no `localStorage`. */
|
|
6
|
+
static read(opts?: {
|
|
7
|
+
key?: string;
|
|
8
|
+
}): IAuthTokenRecord | undefined;
|
|
9
|
+
/** Stores the token with its `provider` under `key` (`LocalStorageKeys.KEY_AUTH_TOKEN` by default). Throws when `localStorage` is not available. */
|
|
7
10
|
static write(opts: {
|
|
8
11
|
token: {
|
|
9
12
|
value: string;
|
|
10
13
|
type: string;
|
|
11
14
|
};
|
|
12
15
|
provider: string;
|
|
16
|
+
key?: string;
|
|
13
17
|
}): void;
|
|
14
18
|
}
|
|
15
19
|
//# sourceMappingURL=auth-token-store.d.ts.map
|
|
@@ -4,12 +4,12 @@ import { LocalStorageKeys } from '../common/index.js';
|
|
|
4
4
|
/** The one place that reads and writes the stored auth token, with the browser guard. */
|
|
5
5
|
export class AuthTokenStore {
|
|
6
6
|
static { this.logger = LoggerFactory.getLogger(['AuthTokenStore']); }
|
|
7
|
-
/** The stored token, or `undefined` when none is stored, it is unusable, or there is no `localStorage`. */
|
|
8
|
-
static read() {
|
|
7
|
+
/** The stored token (under `key`, `LocalStorageKeys.KEY_AUTH_TOKEN` by default), or `undefined` when none is stored, it is unusable, or there is no `localStorage`. */
|
|
8
|
+
static read(opts = {}) {
|
|
9
9
|
if (typeof localStorage === 'undefined') {
|
|
10
10
|
return undefined;
|
|
11
11
|
}
|
|
12
|
-
const stored = localStorage.getItem(LocalStorageKeys.KEY_AUTH_TOKEN);
|
|
12
|
+
const stored = localStorage.getItem(opts.key ?? LocalStorageKeys.KEY_AUTH_TOKEN);
|
|
13
13
|
if (!stored?.length) {
|
|
14
14
|
return undefined;
|
|
15
15
|
}
|
|
@@ -23,13 +23,14 @@ export class AuthTokenStore {
|
|
|
23
23
|
return undefined;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
+
/** Stores the token with its `provider` under `key` (`LocalStorageKeys.KEY_AUTH_TOKEN` by default). Throws when `localStorage` is not available. */
|
|
26
27
|
static write(opts) {
|
|
27
28
|
// Not a silent skip: a sign-in that is not kept would look like it worked.
|
|
28
29
|
if (typeof localStorage === 'undefined') {
|
|
29
30
|
throw getError({ message: '[write] localStorage is not available' });
|
|
30
31
|
}
|
|
31
32
|
const { token, provider } = opts;
|
|
32
|
-
localStorage.setItem(LocalStorageKeys.KEY_AUTH_TOKEN, JSON.stringify(Object.assign({}, token, { provider })));
|
|
33
|
+
localStorage.setItem(opts.key ?? LocalStorageKeys.KEY_AUTH_TOKEN, JSON.stringify(Object.assign({}, token, { provider })));
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
//# sourceMappingURL=auth-token-store.js.map
|
|
@@ -15,6 +15,7 @@ export declare class AuthRecovery {
|
|
|
15
15
|
refresh: () => Promise<boolean>;
|
|
16
16
|
/** Runs once when the refresh threw. */
|
|
17
17
|
onFailure: () => Promise<void>;
|
|
18
|
+
/** Whether the request, given by its paths, goes without a token; such a request never recovers. */
|
|
18
19
|
isNoAuthPath: (paths: string[]) => boolean;
|
|
19
20
|
});
|
|
20
21
|
/** Once for every 401 in flight: the callers share one `refresh`. `true` when the token was refreshed. */
|
|
@@ -6,6 +6,7 @@ export interface IEncodedBody {
|
|
|
6
6
|
}
|
|
7
7
|
/** The body types that go through as they are, under the JSON content type. */
|
|
8
8
|
type TPassThroughBodyType = typeof RequestBodyTypes.JSON | typeof RequestBodyTypes.NONE | typeof RequestBodyTypes.BINARY;
|
|
9
|
+
/** Turns a request body into what `fetch` sends, by `RequestBodyTypes`. */
|
|
9
10
|
export declare class RequestBodyEncoder {
|
|
10
11
|
/**
|
|
11
12
|
* The body as it is sent and the content type to send it under. The overloads give the type of what
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getError } from '@venizia/ignis-inversion';
|
|
2
2
|
import { RequestBodyTypes } from '../../common/index.js';
|
|
3
3
|
import { ValueUtility } from '../../utilities/index.js';
|
|
4
|
+
/** Turns a request body into what `fetch` sends, by `RequestBodyTypes`. */
|
|
4
5
|
export class RequestBodyEncoder {
|
|
5
6
|
static encode(opts) {
|
|
6
7
|
const { bodyType, body } = opts;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type INetworkResponse, RequestCountData, type TConstValue, type TRequestType } from '../../common/index.js';
|
|
2
|
+
/** Reads a `Response` into an `INetworkResponse`, and an error response into an error. */
|
|
2
3
|
export declare class ResponseReader {
|
|
3
4
|
/** The server wraps an error body under this key; both transports unwrap it. */
|
|
4
5
|
static readonly ERROR_ROOT_KEY = "error";
|
|
@@ -8,6 +9,13 @@ export declare class ResponseReader {
|
|
|
8
9
|
type: TRequestType;
|
|
9
10
|
requestCountData?: TConstValue<typeof RequestCountData>;
|
|
10
11
|
}): Promise<INetworkResponse<TResult>>;
|
|
12
|
+
/**
|
|
13
|
+
* Shapes a parsed body by request type. `GET_LIST` and `GET_MANY_REFERENCE` give `data` as an
|
|
14
|
+
* array and `total` from `Content-Range`, else the row count; with `RequestCountData.DATA_WITH_COUNT`
|
|
15
|
+
* the body must be `{ data, count }`, or it throws. Any other type gives `data` as is, and `count`
|
|
16
|
+
* from `x-response-count` when that is a number; with `DATA_WITH_COUNT` it returns the
|
|
17
|
+
* `{ data, count }` body unchanged.
|
|
18
|
+
*/
|
|
11
19
|
static convert<TData = unknown>(opts: {
|
|
12
20
|
response: {
|
|
13
21
|
data: TData | {
|
|
@@ -5,6 +5,7 @@ import { HeaderConstants, RequestCountData, RequestTypes, } from '../../common/i
|
|
|
5
5
|
const isCountedBody = (value) => {
|
|
6
6
|
return typeof value === 'object' && value !== null && 'data' in value && 'count' in value;
|
|
7
7
|
};
|
|
8
|
+
/** Reads a `Response` into an `INetworkResponse`, and an error response into an error. */
|
|
8
9
|
export class ResponseReader {
|
|
9
10
|
/** The server wraps an error body under this key; both transports unwrap it. */
|
|
10
11
|
static { this.ERROR_ROOT_KEY = 'error'; }
|
|
@@ -41,6 +42,13 @@ export class ResponseReader {
|
|
|
41
42
|
});
|
|
42
43
|
return extra ? { ...converted, extra } : converted;
|
|
43
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Shapes a parsed body by request type. `GET_LIST` and `GET_MANY_REFERENCE` give `data` as an
|
|
47
|
+
* array and `total` from `Content-Range`, else the row count; with `RequestCountData.DATA_WITH_COUNT`
|
|
48
|
+
* the body must be `{ data, count }`, or it throws. Any other type gives `data` as is, and `count`
|
|
49
|
+
* from `x-response-count` when that is a number; with `DATA_WITH_COUNT` it returns the
|
|
50
|
+
* `{ data, count }` body unchanged.
|
|
51
|
+
*/
|
|
44
52
|
static convert(opts) {
|
|
45
53
|
const { response: { data, headers }, type, requestCountData, } = opts;
|
|
46
54
|
switch (type) {
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { TStorageArea } from './common/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* A `ResizeObserver` whose `observe()` records the element. `flush` delivers a size to the observers
|
|
4
|
+
* of an element; a callback runs at once, so code that waits for an animation frame still needs the
|
|
5
|
+
* test's own frame control.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FakeResizeObserver implements ResizeObserver {
|
|
8
|
+
readonly callback: ResizeObserverCallback;
|
|
9
|
+
/** The observers created since the last `install`, in creation order. */
|
|
10
|
+
static all: Array<FakeResizeObserver>;
|
|
11
|
+
/** The elements observed now. */
|
|
12
|
+
readonly targets: Array<Element>;
|
|
13
|
+
/** The options of each `observe` call, in order. */
|
|
14
|
+
readonly options: Array<ResizeObserverOptions | undefined>;
|
|
15
|
+
/** Every element passed to `unobserve`, in order. */
|
|
16
|
+
readonly unobserved: Array<Element>;
|
|
17
|
+
isDisconnected: boolean;
|
|
18
|
+
constructor(callback: ResizeObserverCallback);
|
|
19
|
+
/** Replaces `globalThis.ResizeObserver` with the fake and forgets the earlier observers. */
|
|
20
|
+
static install(): () => void;
|
|
21
|
+
/** Calls every observer of `element` with one entry of that size, for the content and the border box alike. */
|
|
22
|
+
static flush(opts: {
|
|
23
|
+
element: Element;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
}): void;
|
|
27
|
+
observe(target: Element, options?: ResizeObserverOptions): void;
|
|
28
|
+
unobserve(target: Element): void;
|
|
29
|
+
disconnect(): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* An `IntersectionObserver` whose `observe()` records the element and reports nothing by itself: `settle`
|
|
33
|
+
* delivers the first report of a browser, `flush` delivers an entry to the observers of an element.
|
|
34
|
+
*/
|
|
35
|
+
export declare class FakeIntersectionObserver implements IntersectionObserver {
|
|
36
|
+
#private;
|
|
37
|
+
readonly callback: IntersectionObserverCallback;
|
|
38
|
+
readonly options?: IntersectionObserverInit | undefined;
|
|
39
|
+
/** The observers created since the last `install`, in creation order. */
|
|
40
|
+
static all: Array<FakeIntersectionObserver>;
|
|
41
|
+
/** How many observers were created since the last `install`. */
|
|
42
|
+
static get created(): number;
|
|
43
|
+
/** The observers created since the last `install` that are not disconnected. */
|
|
44
|
+
static live(): Array<FakeIntersectionObserver>;
|
|
45
|
+
readonly root: null;
|
|
46
|
+
readonly rootMargin = "";
|
|
47
|
+
readonly scrollMargin = "";
|
|
48
|
+
readonly thresholds: ReadonlyArray<number>;
|
|
49
|
+
/** The elements observed now. */
|
|
50
|
+
readonly targets: Array<Element>;
|
|
51
|
+
isDisconnected: boolean;
|
|
52
|
+
constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit | undefined);
|
|
53
|
+
/** Replaces `globalThis.IntersectionObserver` with the fake and forgets the earlier observers. */
|
|
54
|
+
static install(): () => void;
|
|
55
|
+
/** Calls every observer of `element` with one entry. `ratio` is 1 when the element intersects and 0 when not, unless given. */
|
|
56
|
+
static flush(opts: {
|
|
57
|
+
element: Element;
|
|
58
|
+
isIntersecting: boolean;
|
|
59
|
+
ratio?: number;
|
|
60
|
+
}): void;
|
|
61
|
+
/**
|
|
62
|
+
* The first report of a browser: every live observer is called, once per target it has not reported
|
|
63
|
+
* on yet, with the entry for `isIntersecting` (not intersecting by default). A target already
|
|
64
|
+
* reported on, by `settle` or `flush`, is skipped.
|
|
65
|
+
*/
|
|
66
|
+
static settle(opts?: {
|
|
67
|
+
isIntersecting?: boolean;
|
|
68
|
+
ratio?: number;
|
|
69
|
+
}): void;
|
|
70
|
+
observe(target: Element): void;
|
|
71
|
+
unobserve(target: Element): void;
|
|
72
|
+
disconnect(): void;
|
|
73
|
+
takeRecords(): Array<IntersectionObserverEntry>;
|
|
74
|
+
}
|
|
75
|
+
/** The `MediaQueryList` that `matchMedia` answers under `FakeMatchMedia.install`: one per query, `false` until `set`. */
|
|
76
|
+
export declare class FakeMatchMedia extends EventTarget {
|
|
77
|
+
#private;
|
|
78
|
+
readonly media: string;
|
|
79
|
+
matches: boolean;
|
|
80
|
+
/** The queries asked since the last `install`, with the list each one got. */
|
|
81
|
+
static readonly queries: Map<string, FakeMatchMedia>;
|
|
82
|
+
constructor(media: string, matches?: boolean);
|
|
83
|
+
/** How many distinct `change` listeners are subscribed now, to assert a subscribe and an unsubscribe. */
|
|
84
|
+
get listenerCount(): number;
|
|
85
|
+
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
|
|
86
|
+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
|
|
87
|
+
/** Replaces `window.matchMedia` with a function that answers the list of the query. */
|
|
88
|
+
static install(): () => void;
|
|
89
|
+
/** The list of `query`, made on the first ask. */
|
|
90
|
+
static of(query: string): FakeMatchMedia;
|
|
91
|
+
/** Sets whether `query` matches and tells its listeners with a `change` event. */
|
|
92
|
+
static set(opts: {
|
|
93
|
+
query: string;
|
|
94
|
+
matches: boolean;
|
|
95
|
+
}): void;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A `Storage` that behaves like the real one: stored keys are own properties, so `Object.keys` lists
|
|
99
|
+
* them. Each way to fail has its own switch, and each can change at any time: `failReads` makes
|
|
100
|
+
* `getItem` throw `SecurityError`, `failListing` makes `key` and `length` throw it, `failWrites` makes
|
|
101
|
+
* `setItem` and `clear` throw `QuotaExceededError` and `removeItem` throw `SecurityError`. A key named like a member of the fake (`getItem`,
|
|
102
|
+
* `length`, `writes`, `failWrites` and the rest) would shadow it, so `setItem` throws a `TypeError`
|
|
103
|
+
* for it, where the real `Storage` accepts it.
|
|
104
|
+
*/
|
|
105
|
+
export declare class FakeStorage implements Storage {
|
|
106
|
+
#private;
|
|
107
|
+
[name: string]: unknown;
|
|
108
|
+
get failReads(): boolean;
|
|
109
|
+
set failReads(value: boolean);
|
|
110
|
+
get failListing(): boolean;
|
|
111
|
+
set failListing(value: boolean);
|
|
112
|
+
get failWrites(): boolean;
|
|
113
|
+
set failWrites(value: boolean);
|
|
114
|
+
/** The values passed to `setItem` that the storage accepted, in order. */
|
|
115
|
+
get writes(): Array<string>;
|
|
116
|
+
get length(): number;
|
|
117
|
+
/** Makes this the `localStorage` (the default) or the `sessionStorage` of the global. */
|
|
118
|
+
install(area?: TStorageArea): () => void;
|
|
119
|
+
getItem(key: string): string | null;
|
|
120
|
+
setItem(key: string, value: string): void;
|
|
121
|
+
removeItem(key: string): void;
|
|
122
|
+
clear(): void;
|
|
123
|
+
key(index: number): string | null;
|
|
124
|
+
}
|
|
125
|
+
/** A `navigator.clipboard` that keeps the text in memory; `isDenied` makes both methods reject as a refused permission does. */
|
|
126
|
+
export declare class FakeClipboard {
|
|
127
|
+
/** The text of every `writeText`, in order. */
|
|
128
|
+
readonly written: Array<string>;
|
|
129
|
+
/** What `readText` answers; `writeText` replaces it. */
|
|
130
|
+
text: string;
|
|
131
|
+
isDenied: boolean;
|
|
132
|
+
/** Makes this the `navigator.clipboard`. */
|
|
133
|
+
install(): () => void;
|
|
134
|
+
writeText(text: string): Promise<void>;
|
|
135
|
+
readText(): Promise<string>;
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=testing.d.ts.map
|