@venizia/ardor-kernel 0.4.8-0 → 0.4.8-2
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 +3 -1
- 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 +11 -0
- package/dist/base/services/auth.js +8 -0
- 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 +53 -2
- package/dist/base/services/network-request.js +72 -6
- package/dist/common/constants.d.ts +47 -0
- package/dist/common/constants.js +47 -0
- package/dist/common/keys.d.ts +16 -0
- package/dist/common/keys.js +16 -0
- package/dist/common/types.d.ts +86 -4
- package/dist/helpers/auth-token-store.d.ts +1 -0
- package/dist/helpers/auth-token-store.js +1 -0
- 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/utilities/browser-clipboard.d.ts +17 -0
- package/dist/utilities/browser-clipboard.js +37 -0
- package/dist/utilities/browser-storage.d.ts +30 -0
- package/dist/utilities/browser-storage.js +26 -0
- 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/index.d.ts +2 -0
- package/dist/utilities/index.js +2 -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 +9 -0
- package/dist/utilities/storage-codecs.js +19 -0
- package/dist/utilities/value.d.ts +3 -0
- package/dist/utilities/value.js +3 -0
- package/package.json +14 -13
- package/dist/base/applications/abstract.d.ts.map +0 -1
- package/dist/base/applications/abstract.js.map +0 -1
- package/dist/base/applications/index.d.ts.map +0 -1
- package/dist/base/applications/index.js.map +0 -1
- package/dist/base/decorators/api.d.ts.map +0 -1
- package/dist/base/decorators/api.js.map +0 -1
- package/dist/base/decorators/index.d.ts.map +0 -1
- package/dist/base/decorators/index.js.map +0 -1
- package/dist/base/index.d.ts.map +0 -1
- package/dist/base/index.js.map +0 -1
- package/dist/base/metadata/index.d.ts.map +0 -1
- package/dist/base/metadata/index.js.map +0 -1
- package/dist/base/providers/base.d.ts.map +0 -1
- package/dist/base/providers/base.js.map +0 -1
- package/dist/base/providers/index.d.ts.map +0 -1
- package/dist/base/providers/index.js.map +0 -1
- package/dist/base/repositories/index.d.ts.map +0 -1
- package/dist/base/repositories/index.js.map +0 -1
- package/dist/base/services/auth.d.ts.map +0 -1
- package/dist/base/services/auth.js.map +0 -1
- package/dist/base/services/base.d.ts.map +0 -1
- package/dist/base/services/base.js.map +0 -1
- package/dist/base/services/index.d.ts.map +0 -1
- package/dist/base/services/index.js.map +0 -1
- package/dist/base/services/network-request.d.ts.map +0 -1
- package/dist/base/services/network-request.js.map +0 -1
- package/dist/common/constants.d.ts.map +0 -1
- package/dist/common/constants.js.map +0 -1
- package/dist/common/index.d.ts.map +0 -1
- package/dist/common/index.js.map +0 -1
- package/dist/common/keys.d.ts.map +0 -1
- package/dist/common/keys.js.map +0 -1
- package/dist/common/types.d.ts.map +0 -1
- package/dist/common/types.js.map +0 -1
- package/dist/helpers/auth-token-store.d.ts.map +0 -1
- package/dist/helpers/auth-token-store.js.map +0 -1
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/index.js.map +0 -1
- package/dist/helpers/networks/auth-recovery.d.ts.map +0 -1
- package/dist/helpers/networks/auth-recovery.js.map +0 -1
- package/dist/helpers/networks/headers.d.ts.map +0 -1
- package/dist/helpers/networks/headers.js.map +0 -1
- package/dist/helpers/networks/index.d.ts.map +0 -1
- package/dist/helpers/networks/index.js.map +0 -1
- package/dist/helpers/networks/request-body-encoder.d.ts.map +0 -1
- package/dist/helpers/networks/request-body-encoder.js.map +0 -1
- package/dist/helpers/networks/response-reader.d.ts.map +0 -1
- package/dist/helpers/networks/response-reader.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/utilities/browser-storage.d.ts.map +0 -1
- package/dist/utilities/browser-storage.js.map +0 -1
- package/dist/utilities/browser.d.ts.map +0 -1
- package/dist/utilities/browser.js.map +0 -1
- package/dist/utilities/file.d.ts.map +0 -1
- package/dist/utilities/file.js.map +0 -1
- package/dist/utilities/index.d.ts.map +0 -1
- package/dist/utilities/index.js.map +0 -1
- package/dist/utilities/runtime-config.d.ts.map +0 -1
- package/dist/utilities/runtime-config.js.map +0 -1
- package/dist/utilities/value.d.ts.map +0 -1
- package/dist/utilities/value.js.map +0 -1
package/README.md
CHANGED
|
@@ -4,9 +4,11 @@ The isomorphic core of [ARDOR](https://github.com/VENIZIA-AI/ardor). No React, n
|
|
|
4
4
|
layer a browser data client and a plain TypeScript consumer can both use.
|
|
5
5
|
|
|
6
6
|
```bash
|
|
7
|
-
bun add @venizia/ardor-kernel @venizia/ignis-inversion @venizia/ignis-filter reflect-metadata
|
|
7
|
+
bun add @venizia/ardor-kernel @venizia/ignis-inversion @venizia/ignis-filter @venizia/ignis-helpers @venizia/ignis-kernel reflect-metadata
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
+
`@venizia/ignis-connectors` is an optional peer, needed only for `./repository`.
|
|
11
|
+
|
|
10
12
|
Most applications install [`@venizia/ardor`](https://www.npmjs.com/package/@venizia/ardor) instead,
|
|
11
13
|
which re-exports this package.
|
|
12
14
|
|
|
@@ -7,8 +7,15 @@ export interface IArtifactRegistration {
|
|
|
7
7
|
namespace: string;
|
|
8
8
|
key: string;
|
|
9
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* The scope of the binding.
|
|
12
|
+
* @default The scope declared on the class, else `BindingScopes.SINGLETON`
|
|
13
|
+
*/
|
|
10
14
|
scope?: TBindingScope;
|
|
11
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* `false` refuses to replace a binding already under the key.
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
12
19
|
allowOverride?: boolean;
|
|
13
20
|
}
|
|
14
21
|
/**
|
|
@@ -17,7 +24,9 @@ export interface IArtifactRegistration {
|
|
|
17
24
|
* SINGLETON (IGNIS's server uses transient): a hook resolves on every render.
|
|
18
25
|
*/
|
|
19
26
|
export declare abstract class AbstractArdorApplication extends Container implements IArdorApplication {
|
|
27
|
+
/** The application's own bindings; `preConfigure` runs it last, so they win over discovered and feature bindings. */
|
|
20
28
|
abstract bindContext(): ValueOrPromise<void>;
|
|
29
|
+
/** The `IApplicationInfo` that `preConfigure` binds under `CoreBindings.APPLICATION_INFO`. */
|
|
21
30
|
abstract getAppInfo(): ValueOrPromise<IApplicationInfo>;
|
|
22
31
|
/** What `start()` resolved from `artifacts()`; unset when `preConfigure()` runs without it. */
|
|
23
32
|
private resolvedIndexes;
|
|
@@ -57,17 +66,24 @@ export declare abstract class AbstractArdorApplication extends Container impleme
|
|
|
57
66
|
* been discovered: one that was not means the feature's bindings were silently never made.
|
|
58
67
|
*/
|
|
59
68
|
private registerFeatures;
|
|
69
|
+
/** Runs after `preConfigure` in `start()`; empty by default, for a subclass to override. */
|
|
60
70
|
postConfigure(): ValueOrPromise<void>;
|
|
71
|
+
/** Binds `target` in the service namespace; returns the binding. */
|
|
61
72
|
service<T>(target: TClass<T>, opts?: IArtifactRegistration): Binding<T>;
|
|
73
|
+
/** Binds `target` in the repository namespace; returns the binding. */
|
|
62
74
|
repository<T>(target: TClass<T>, opts?: IArtifactRegistration): Binding<T>;
|
|
75
|
+
/** Binds `target` in the data source namespace; returns the binding. */
|
|
63
76
|
dataSource<T>(target: TClass<T>, opts?: IArtifactRegistration): Binding<T>;
|
|
77
|
+
/** Binds `target` in the component namespace; returns the binding. */
|
|
64
78
|
component<T>(target: TClass<T>, opts?: IArtifactRegistration): Binding<T>;
|
|
65
79
|
/** `<scope>.<ClassName>`, singleton, with tags. Prefer the per-kind methods above. */
|
|
66
80
|
injectable<T>(scope: string, value: TClass<T>, tags?: Array<string>): void;
|
|
81
|
+
/** Resolves the artifact indexes, then runs `preConfigure`, then `postConfigure`, in that order. */
|
|
67
82
|
start(): Promise<void>;
|
|
68
83
|
/** Key: explicit `binding` > the class's stereotype > `<namespace>.<ClassName>`. Same for scope. */
|
|
69
84
|
private registerArtifact;
|
|
70
85
|
}
|
|
86
|
+
/** The base to extend for an application: implement `bindContext` and `getAppInfo`. */
|
|
71
87
|
export declare abstract class BaseArdorApplication extends AbstractArdorApplication {
|
|
72
88
|
}
|
|
73
89
|
//# sourceMappingURL=abstract.d.ts.map
|
|
@@ -176,7 +176,9 @@ export class AbstractArdorApplication extends Container {
|
|
|
176
176
|
}
|
|
177
177
|
this.bind({ key: CoreBindings.FEATURES }).toValue(features);
|
|
178
178
|
}
|
|
179
|
+
/** Runs after `preConfigure` in `start()`; empty by default, for a subclass to override. */
|
|
179
180
|
postConfigure() { }
|
|
181
|
+
/** Binds `target` in the service namespace; returns the binding. */
|
|
180
182
|
service(target, opts) {
|
|
181
183
|
return this.registerArtifact({
|
|
182
184
|
target,
|
|
@@ -185,6 +187,7 @@ export class AbstractArdorApplication extends Container {
|
|
|
185
187
|
opts,
|
|
186
188
|
});
|
|
187
189
|
}
|
|
190
|
+
/** Binds `target` in the repository namespace; returns the binding. */
|
|
188
191
|
repository(target, opts) {
|
|
189
192
|
return this.registerArtifact({
|
|
190
193
|
target,
|
|
@@ -193,6 +196,7 @@ export class AbstractArdorApplication extends Container {
|
|
|
193
196
|
opts,
|
|
194
197
|
});
|
|
195
198
|
}
|
|
199
|
+
/** Binds `target` in the data source namespace; returns the binding. */
|
|
196
200
|
dataSource(target, opts) {
|
|
197
201
|
return this.registerArtifact({
|
|
198
202
|
target,
|
|
@@ -201,6 +205,7 @@ export class AbstractArdorApplication extends Container {
|
|
|
201
205
|
opts,
|
|
202
206
|
});
|
|
203
207
|
}
|
|
208
|
+
/** Binds `target` in the component namespace; returns the binding. */
|
|
204
209
|
component(target, opts) {
|
|
205
210
|
return this.registerArtifact({
|
|
206
211
|
target,
|
|
@@ -218,6 +223,7 @@ export class AbstractArdorApplication extends Container {
|
|
|
218
223
|
.setScope(BindingScopes.SINGLETON)
|
|
219
224
|
.setTags(...(tags ?? []));
|
|
220
225
|
}
|
|
226
|
+
/** Resolves the artifact indexes, then runs `preConfigure`, then `postConfigure`, in that order. */
|
|
221
227
|
async start() {
|
|
222
228
|
const result = await this.resolveArtifactIndexes();
|
|
223
229
|
this.resolvedIndexes = result;
|
|
@@ -245,6 +251,7 @@ export class AbstractArdorApplication extends Container {
|
|
|
245
251
|
.setScope(opts.opts?.scope ?? declared?.scope ?? BindingScopes.SINGLETON);
|
|
246
252
|
}
|
|
247
253
|
}
|
|
254
|
+
/** The base to extend for an application: implement `bindContext` and `getAppInfo`. */
|
|
248
255
|
export class BaseArdorApplication extends AbstractArdorApplication {
|
|
249
256
|
}
|
|
250
257
|
//# sourceMappingURL=abstract.js.map
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { BaseHelper } from '@venizia/ignis-helpers/core';
|
|
2
2
|
import type { Container, IProvider } from '@venizia/ignis-inversion';
|
|
3
|
+
/** Base of a provider: `value` yields what the provider supplies, resolved through the container. */
|
|
3
4
|
export declare abstract class BaseProvider<T> extends BaseHelper implements IProvider<T> {
|
|
5
|
+
/** Produces the provided value. */
|
|
4
6
|
abstract value(container: Container): T;
|
|
5
7
|
}
|
|
6
8
|
//# sourceMappingURL=base.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type AnyType, type IAuthTokenRecord, type IdType } from '../../common/index.js';
|
|
2
2
|
import { BaseService } from './base.js';
|
|
3
|
+
/** Keeps the signed-in session (token, identity, roles) in `localStorage`; everything under `LocalStorageKeys.PREFIX_AUTH`. */
|
|
3
4
|
export declare class DefaultAuthService extends BaseService {
|
|
4
5
|
private readonly listeners;
|
|
5
6
|
constructor();
|
|
@@ -15,14 +16,23 @@ export declare class DefaultAuthService extends BaseService {
|
|
|
15
16
|
* logged: the message of a JSON parse error quotes the text it could not parse, which can be a token.
|
|
16
17
|
*/
|
|
17
18
|
private parseStored;
|
|
19
|
+
/** The stored identity; `{}` when nothing usable is stored, also outside a browser. */
|
|
18
20
|
getUser(): AnyType;
|
|
21
|
+
/** The stored roles; empty when nothing usable is stored, also outside a browser. */
|
|
19
22
|
getRoles(): Set<string>;
|
|
20
23
|
/** The stored token, `null` for every visitor who has not signed in. */
|
|
21
24
|
getAuth(): IAuthTokenRecord | null;
|
|
25
|
+
/**
|
|
26
|
+
* Stores the token and identity, then notifies the subscribers. Throws when `localStorage` is
|
|
27
|
+
* not available. Roles are stored only when `roles` is given.
|
|
28
|
+
*/
|
|
22
29
|
saveAuth(opts: {
|
|
23
30
|
userId: number | string;
|
|
31
|
+
/** @default '' */
|
|
24
32
|
username?: string;
|
|
33
|
+
/** @default '' */
|
|
25
34
|
provider?: string;
|
|
35
|
+
/** @default '' */
|
|
26
36
|
referenceId?: IdType;
|
|
27
37
|
token: {
|
|
28
38
|
value: string;
|
|
@@ -34,6 +44,7 @@ export declare class DefaultAuthService extends BaseService {
|
|
|
34
44
|
*/
|
|
35
45
|
roles?: ReadonlyArray<string>;
|
|
36
46
|
}): void;
|
|
47
|
+
/** Removes every key under `LocalStorageKeys.PREFIX_AUTH`, then notifies the subscribers; does nothing outside a browser. */
|
|
37
48
|
cleanUp(): void;
|
|
38
49
|
}
|
|
39
50
|
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -2,6 +2,7 @@ import { getError } from '@venizia/ignis-inversion';
|
|
|
2
2
|
import { LocalStorageKeys } from '../../common/index.js';
|
|
3
3
|
import { AuthTokenStore } from '../../helpers/index.js';
|
|
4
4
|
import { BaseService } from './base.js';
|
|
5
|
+
/** Keeps the signed-in session (token, identity, roles) in `localStorage`; everything under `LocalStorageKeys.PREFIX_AUTH`. */
|
|
5
6
|
export class DefaultAuthService extends BaseService {
|
|
6
7
|
constructor() {
|
|
7
8
|
super({ scope: DefaultAuthService.name });
|
|
@@ -45,6 +46,7 @@ export class DefaultAuthService extends BaseService {
|
|
|
45
46
|
return undefined;
|
|
46
47
|
}
|
|
47
48
|
}
|
|
49
|
+
/** The stored identity; `{}` when nothing usable is stored, also outside a browser. */
|
|
48
50
|
getUser() {
|
|
49
51
|
const identity = this.parseStored({
|
|
50
52
|
key: LocalStorageKeys.KEY_AUTH_IDENTITY,
|
|
@@ -53,6 +55,7 @@ export class DefaultAuthService extends BaseService {
|
|
|
53
55
|
const isRecord = typeof identity === 'object' && identity !== null && !Array.isArray(identity);
|
|
54
56
|
return isRecord ? identity : {};
|
|
55
57
|
}
|
|
58
|
+
/** The stored roles; empty when nothing usable is stored, also outside a browser. */
|
|
56
59
|
getRoles() {
|
|
57
60
|
const stored = this.parseStored({
|
|
58
61
|
key: LocalStorageKeys.KEY_AUTH_PERMISSION,
|
|
@@ -65,6 +68,10 @@ export class DefaultAuthService extends BaseService {
|
|
|
65
68
|
getAuth() {
|
|
66
69
|
return AuthTokenStore.read() ?? null;
|
|
67
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Stores the token and identity, then notifies the subscribers. Throws when `localStorage` is
|
|
73
|
+
* not available. Roles are stored only when `roles` is given.
|
|
74
|
+
*/
|
|
68
75
|
saveAuth(opts) {
|
|
69
76
|
// Not a silent skip: a sign-in that is not kept would look like it worked.
|
|
70
77
|
if (typeof localStorage === 'undefined') {
|
|
@@ -80,6 +87,7 @@ export class DefaultAuthService extends BaseService {
|
|
|
80
87
|
}
|
|
81
88
|
this.notify();
|
|
82
89
|
}
|
|
90
|
+
/** Removes every key under `LocalStorageKeys.PREFIX_AUTH`, then notifies the subscribers; does nothing outside a browser. */
|
|
83
91
|
cleanUp() {
|
|
84
92
|
if (typeof localStorage === 'undefined') {
|
|
85
93
|
return;
|
|
@@ -1,24 +1,42 @@
|
|
|
1
1
|
import { NodeFetchNetworkRequest } from '@venizia/ignis-helpers/core';
|
|
2
2
|
import { type IApplicationInfo, type IAuthTokenRecord, type IAuthRecoveryOptions, type IDataSourceAuth, type IGetRequestPropsParams, type IGetRequestPropsResult, type INetworkResponse, type INoAuthOptions, RequestCountData, type TConstValue, type TNoAuthPathRegex, type TRequestMethod, type TRequestType } from '../../common/index.js';
|
|
3
3
|
import { type IEncodedBody, ResponseReader } from '../../helpers/index.js';
|
|
4
|
+
import type { DefaultAuthService } from './auth.js';
|
|
4
5
|
import { BaseService } from './base.js';
|
|
5
6
|
/**
|
|
6
7
|
* The options hold data (the base URL, headers, the no-auth lists, the refresh path); behaviour is
|
|
7
8
|
* in the protected hooks, each taking one `opts` object, which a subclass overrides.
|
|
8
9
|
*/
|
|
9
10
|
export declare class DefaultNetworkRequestService extends BaseService {
|
|
11
|
+
/** Resources sent without a token, matched exactly. */
|
|
10
12
|
protected noAuthPaths?: string[];
|
|
13
|
+
/** `noAuthPathRegex` compiled; a resource matching any is sent without a token. */
|
|
11
14
|
protected noAuthPathRegexes: RegExp[];
|
|
15
|
+
/** Headers added to every request, keyed by lowercase name. */
|
|
12
16
|
protected headers: Record<string, string>;
|
|
13
17
|
protected networkRequest: NodeFetchNetworkRequest;
|
|
18
|
+
/** The origin and base path requests are sent to; an empty one makes `doRequest` throw. */
|
|
14
19
|
protected baseUrl: string;
|
|
15
20
|
protected authRecovery?: IAuthRecoveryOptions;
|
|
21
|
+
protected authService?: DefaultAuthService;
|
|
16
22
|
private readonly recovery;
|
|
17
23
|
constructor(opts: INoAuthOptions & {
|
|
24
|
+
/** The name of the underlying network request helper. */
|
|
18
25
|
name: string;
|
|
26
|
+
/**
|
|
27
|
+
* An empty one makes `doRequest` refuse, unless it is given its own `baseUrl`.
|
|
28
|
+
* @default ''
|
|
29
|
+
*/
|
|
19
30
|
baseUrl?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Sent with every request; a name set later with `setHeaders` replaces it.
|
|
33
|
+
* @default {}
|
|
34
|
+
*/
|
|
20
35
|
headers?: HeadersInit;
|
|
36
|
+
/** Enables the refresh-and-retry after a 401. */
|
|
21
37
|
authRecovery?: IAuthRecoveryOptions;
|
|
38
|
+
/** What the default `refreshToken` saves the new token through. */
|
|
39
|
+
authService?: DefaultAuthService;
|
|
22
40
|
});
|
|
23
41
|
/**
|
|
24
42
|
* The stored token (`AuthTokenStore.read`), `undefined` outside a browser. The default of
|
|
@@ -41,12 +59,25 @@ export declare class DefaultNetworkRequestService extends BaseService {
|
|
|
41
59
|
}): boolean;
|
|
42
60
|
/**
|
|
43
61
|
* Refreshes the session after a 401. `true`: refreshed, the request is retried; `false`: this
|
|
44
|
-
* service has no refresh
|
|
62
|
+
* service has no refresh; a throw: the refresh failed and `onAuthFailure` runs. The default posts
|
|
63
|
+
* to `authRecovery.refreshTokenPath` with no body and saves the new token through `authService`,
|
|
64
|
+
* keeping the stored identity and roles; with no path or no `authService` it answers `false`.
|
|
45
65
|
*/
|
|
46
66
|
protected refreshToken(): Promise<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* The new token of a refresh response: `{ token: { value, type } }` or `{ token: '<jwt>' }`. A
|
|
69
|
+
* refresh keeps the same user, so nothing else is read. Override it for another shape. No token is
|
|
70
|
+
* a throw.
|
|
71
|
+
*/
|
|
72
|
+
protected readRefreshedToken(opts: {
|
|
73
|
+
data: unknown;
|
|
74
|
+
}): {
|
|
75
|
+
value: string;
|
|
76
|
+
type: string;
|
|
77
|
+
};
|
|
47
78
|
/** Runs once when `refreshToken` threw. The place to send the user back to sign-in. */
|
|
48
79
|
protected onAuthFailure(): Promise<void>;
|
|
49
|
-
/** The tracing id of a request, sent in `x-request-
|
|
80
|
+
/** The tracing id of a request, sent in `x-request-id`. Default `<application name>_<uuid>`. */
|
|
50
81
|
protected buildRequestTracingId(opts: {
|
|
51
82
|
applicationInfo: IApplicationInfo;
|
|
52
83
|
}): string;
|
|
@@ -65,6 +96,10 @@ export declare class DefaultNetworkRequestService extends BaseService {
|
|
|
65
96
|
* options), and a 401 joins this service's shared `refresh`. A no-auth path gets no token.
|
|
66
97
|
*/
|
|
67
98
|
getDataSourceAuth(opts?: Pick<IGetRequestPropsParams, 'restDataProviderOptions' | 'applicationInfo'>): IDataSourceAuth;
|
|
99
|
+
/**
|
|
100
|
+
* The `token` (`<type> <value>`, `Bearer` when the type is empty) and `provider` of the stored
|
|
101
|
+
* token. Throws an error with status 401 when there is no token.
|
|
102
|
+
*/
|
|
68
103
|
getRequestAuthorizationHeader(): {
|
|
69
104
|
provider: string | undefined;
|
|
70
105
|
token: string;
|
|
@@ -73,6 +108,7 @@ export declare class DefaultNetworkRequestService extends BaseService {
|
|
|
73
108
|
setHeaders(opts: {
|
|
74
109
|
headers: HeadersInit;
|
|
75
110
|
}): void;
|
|
111
|
+
/** Removes the headers set by name, in any case. */
|
|
76
112
|
removeHeaders(opts: {
|
|
77
113
|
keys: string[];
|
|
78
114
|
}): void;
|
|
@@ -80,6 +116,10 @@ export declare class DefaultNetworkRequestService extends BaseService {
|
|
|
80
116
|
protected getSessionHeaders(): Record<string, string>;
|
|
81
117
|
/** The request channel (`RequestChannel.WEB` unless configured) and a fresh tracing id. */
|
|
82
118
|
protected getTracingHeaders(opts: Pick<IGetRequestPropsParams, 'restDataProviderOptions' | 'applicationInfo'>): Record<string, string>;
|
|
119
|
+
/**
|
|
120
|
+
* The session headers, plus `authorization` and `x-auth-provider` unless `resource` is a no-auth
|
|
121
|
+
* path. Throws a 401 error for any other resource when there is no token.
|
|
122
|
+
*/
|
|
83
123
|
getRequestHeader(opts: {
|
|
84
124
|
resource: string;
|
|
85
125
|
}): Record<string, string>;
|
|
@@ -88,6 +128,10 @@ export declare class DefaultNetworkRequestService extends BaseService {
|
|
|
88
128
|
bodyType?: string;
|
|
89
129
|
body: unknown;
|
|
90
130
|
}): IEncodedBody;
|
|
131
|
+
/**
|
|
132
|
+
* The headers (session, auth, tracing, `x-request-count`, the `extra` header, and the content type
|
|
133
|
+
* of the encoded body) and the encoded body of a request. Throws as `getRequestHeader` does.
|
|
134
|
+
*/
|
|
91
135
|
getRequestProps(params: IGetRequestPropsParams): IGetRequestPropsResult;
|
|
92
136
|
/** Reads a response by the request type. See `ResponseReader.convert`. */
|
|
93
137
|
convertResponse<TData = unknown>(opts: Parameters<typeof ResponseReader.convert<TData>>[0]): {
|
|
@@ -95,7 +139,14 @@ export declare class DefaultNetworkRequestService extends BaseService {
|
|
|
95
139
|
count?: number;
|
|
96
140
|
total?: number;
|
|
97
141
|
};
|
|
142
|
+
/**
|
|
143
|
+
* Sends the request and reads the response by `type`. Throws when `baseUrl` (the option, else the
|
|
144
|
+
* service's) is empty. A 401 on a path that can recover triggers `refresh` and, if it refreshed,
|
|
145
|
+
* one retry with the new token. Any other non-2xx response, or a retry that still fails, throws
|
|
146
|
+
* the error read from the response.
|
|
147
|
+
*/
|
|
98
148
|
doRequest<TResult = unknown>(opts: IGetRequestPropsResult & {
|
|
149
|
+
/** @default this.baseUrl */
|
|
99
150
|
baseUrl?: string;
|
|
100
151
|
query?: any;
|
|
101
152
|
type: TRequestType;
|
|
@@ -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, } from '../../common/index.js';
|
|
5
|
+
import { App, HeaderConstants, 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
|
/**
|
|
@@ -12,12 +12,13 @@ import { BaseService } from './base.js';
|
|
|
12
12
|
export class DefaultNetworkRequestService extends BaseService {
|
|
13
13
|
constructor(opts) {
|
|
14
14
|
super({ scope: DefaultNetworkRequestService.name });
|
|
15
|
-
const { name, baseUrl = '', headers = {}, noAuthPaths, noAuthPathRegex, authRecovery } = opts;
|
|
15
|
+
const { name, baseUrl = '', headers = {}, noAuthPaths, noAuthPathRegex, authRecovery, authService, } = opts;
|
|
16
16
|
this.headers = NetworkHeaders.toRecord({ headers });
|
|
17
17
|
this.noAuthPaths = noAuthPaths;
|
|
18
18
|
this.noAuthPathRegexes = this.compileNoAuthPathRegex({ input: noAuthPathRegex });
|
|
19
19
|
this.baseUrl = baseUrl;
|
|
20
20
|
this.authRecovery = authRecovery;
|
|
21
|
+
this.authService = authService;
|
|
21
22
|
this.recovery = new AuthRecovery({
|
|
22
23
|
logger: this.logger,
|
|
23
24
|
getRefreshTokenPath: () => this.authRecovery?.refreshTokenPath,
|
|
@@ -72,16 +73,62 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
72
73
|
}
|
|
73
74
|
/**
|
|
74
75
|
* Refreshes the session after a 401. `true`: refreshed, the request is retried; `false`: this
|
|
75
|
-
* service has no refresh
|
|
76
|
+
* service has no refresh; a throw: the refresh failed and `onAuthFailure` runs. The default posts
|
|
77
|
+
* to `authRecovery.refreshTokenPath` with no body and saves the new token through `authService`,
|
|
78
|
+
* keeping the stored identity and roles; with no path or no `authService` it answers `false`.
|
|
76
79
|
*/
|
|
77
|
-
refreshToken() {
|
|
78
|
-
|
|
80
|
+
async refreshToken() {
|
|
81
|
+
const path = this.authRecovery?.refreshTokenPath;
|
|
82
|
+
if (!path || !this.authService) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const response = await this.doRequest({
|
|
86
|
+
paths: [path],
|
|
87
|
+
method: RequestMethods.POST,
|
|
88
|
+
type: RequestTypes.SEND,
|
|
89
|
+
headers: this.getRequestHeader({ resource: path }),
|
|
90
|
+
});
|
|
91
|
+
const user = this.authService.getUser();
|
|
92
|
+
// Signed out while the request was in flight: saving the token would start a session with no user.
|
|
93
|
+
if (user.userId === undefined) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
this.authService.saveAuth({
|
|
97
|
+
userId: user.userId,
|
|
98
|
+
username: user.username,
|
|
99
|
+
referenceId: user.referenceId,
|
|
100
|
+
provider: user.provider,
|
|
101
|
+
token: this.readRefreshedToken({ data: response.data }),
|
|
102
|
+
});
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The new token of a refresh response: `{ token: { value, type } }` or `{ token: '<jwt>' }`. A
|
|
107
|
+
* refresh keeps the same user, so nothing else is read. Override it for another shape. No token is
|
|
108
|
+
* a throw.
|
|
109
|
+
*/
|
|
110
|
+
readRefreshedToken(opts) {
|
|
111
|
+
const body = typeof opts.data === 'object' && opts.data !== null ? opts.data : {};
|
|
112
|
+
const { token } = body;
|
|
113
|
+
let record = {};
|
|
114
|
+
if (typeof token === 'string') {
|
|
115
|
+
record = { value: token };
|
|
116
|
+
}
|
|
117
|
+
else if (typeof token === 'object' && token !== null) {
|
|
118
|
+
record = token;
|
|
119
|
+
}
|
|
120
|
+
const value = 'value' in record ? record.value : undefined;
|
|
121
|
+
const type = 'type' in record ? record.type : undefined;
|
|
122
|
+
if (typeof value !== 'string' || !value) {
|
|
123
|
+
throw getError({ message: '[refreshToken] The refresh response carries no token' });
|
|
124
|
+
}
|
|
125
|
+
return { value, type: typeof type === 'string' ? type : 'Bearer' };
|
|
79
126
|
}
|
|
80
127
|
/** Runs once when `refreshToken` threw. The place to send the user back to sign-in. */
|
|
81
128
|
onAuthFailure() {
|
|
82
129
|
return Promise.resolve();
|
|
83
130
|
}
|
|
84
|
-
/** The tracing id of a request, sent in `x-request-
|
|
131
|
+
/** The tracing id of a request, sent in `x-request-id`. Default `<application name>_<uuid>`. */
|
|
85
132
|
buildRequestTracingId(opts) {
|
|
86
133
|
return `${opts.applicationInfo.name}_${UuidV4Generator.generate()}`;
|
|
87
134
|
}
|
|
@@ -142,6 +189,10 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
142
189
|
errorRootKey: ResponseReader.ERROR_ROOT_KEY,
|
|
143
190
|
};
|
|
144
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* The `token` (`<type> <value>`, `Bearer` when the type is empty) and `provider` of the stored
|
|
194
|
+
* token. Throws an error with status 401 when there is no token.
|
|
195
|
+
*/
|
|
145
196
|
getRequestAuthorizationHeader() {
|
|
146
197
|
const authToken = this.resolveAuthToken();
|
|
147
198
|
if (!authToken?.value) {
|
|
@@ -159,6 +210,7 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
159
210
|
setHeaders(opts) {
|
|
160
211
|
this.headers = NetworkHeaders.merge({ sources: [this.headers, opts.headers] });
|
|
161
212
|
}
|
|
213
|
+
/** Removes the headers set by name, in any case. */
|
|
162
214
|
removeHeaders(opts) {
|
|
163
215
|
if (!opts.keys?.length) {
|
|
164
216
|
return;
|
|
@@ -191,6 +243,10 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
191
243
|
[HeaderConstants.REQUEST_TRACING_ID]: this.buildRequestTracingId({ applicationInfo }),
|
|
192
244
|
};
|
|
193
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* The session headers, plus `authorization` and `x-auth-provider` unless `resource` is a no-auth
|
|
248
|
+
* path. Throws a 401 error for any other resource when there is no token.
|
|
249
|
+
*/
|
|
194
250
|
getRequestHeader(opts) {
|
|
195
251
|
const { resource } = opts;
|
|
196
252
|
const defaultHeaders = this.getSessionHeaders();
|
|
@@ -209,6 +265,10 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
209
265
|
encodeBody(opts) {
|
|
210
266
|
return RequestBodyEncoder.encode(opts);
|
|
211
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* The headers (session, auth, tracing, `x-request-count`, the `extra` header, and the content type
|
|
270
|
+
* of the encoded body) and the encoded body of a request. Throws as `getRequestHeader` does.
|
|
271
|
+
*/
|
|
212
272
|
getRequestProps(params) {
|
|
213
273
|
const { bodyType, body, resource, restDataProviderOptions, applicationInfo, requestCountData = RequestCountData.DATA_ONLY, extra, } = params;
|
|
214
274
|
const extraHeader = HttpExtraRequest.toHeader({ extra });
|
|
@@ -234,6 +294,12 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
234
294
|
convertResponse(opts) {
|
|
235
295
|
return ResponseReader.convert(opts);
|
|
236
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* Sends the request and reads the response by `type`. Throws when `baseUrl` (the option, else the
|
|
299
|
+
* service's) is empty. A 401 on a path that can recover triggers `refresh` and, if it refreshed,
|
|
300
|
+
* one retry with the new token. Any other non-2xx response, or a retry that still fails, throws
|
|
301
|
+
* the error read from the response.
|
|
302
|
+
*/
|
|
237
303
|
async doRequest(opts) {
|
|
238
304
|
const { baseUrl = this.baseUrl, type, method, paths, body, headers, query, requestCountData, } = opts;
|
|
239
305
|
if (!baseUrl) {
|
|
@@ -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
|