@molecule/app-bonds-default 0.0.0-stage → 0.0.1-stage
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/LICENSE +202 -0
- package/README.md +456 -2
- package/dist/auth.d.ts +156 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +258 -0
- package/dist/index.d.ts +113 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +112 -0
- package/dist/optional/charts-chartjs.d.ts +15 -0
- package/dist/optional/charts-chartjs.d.ts.map +1 -0
- package/dist/optional/charts-chartjs.js +20 -0
- package/dist/optional/client-search-minisearch.d.ts +12 -0
- package/dist/optional/client-search-minisearch.d.ts.map +1 -0
- package/dist/optional/client-search-minisearch.js +17 -0
- package/dist/optional/code-editor-monaco.d.ts +12 -0
- package/dist/optional/code-editor-monaco.d.ts.map +1 -0
- package/dist/optional/code-editor-monaco.js +17 -0
- package/dist/optional/command-palette-cmdk.d.ts +12 -0
- package/dist/optional/command-palette-cmdk.d.ts.map +1 -0
- package/dist/optional/command-palette-cmdk.js +17 -0
- package/dist/optional/drag-drop-dndkit.d.ts +12 -0
- package/dist/optional/drag-drop-dndkit.d.ts.map +1 -0
- package/dist/optional/drag-drop-dndkit.js +17 -0
- package/dist/optional/keyboard-shortcuts-hotkeys.d.ts +12 -0
- package/dist/optional/keyboard-shortcuts-hotkeys.d.ts.map +1 -0
- package/dist/optional/keyboard-shortcuts-hotkeys.js +17 -0
- package/dist/optional/maps-leaflet.d.ts +19 -0
- package/dist/optional/maps-leaflet.d.ts.map +1 -0
- package/dist/optional/maps-leaflet.js +24 -0
- package/dist/optional/realtime-socketio.d.ts +24 -0
- package/dist/optional/realtime-socketio.d.ts.map +1 -0
- package/dist/optional/realtime-socketio.js +29 -0
- package/dist/optional/video-hls.d.ts +12 -0
- package/dist/optional/video-hls.d.ts.map +1 -0
- package/dist/optional/video-hls.js +17 -0
- package/dist/optional/virtual-scroll-tanstack.d.ts +12 -0
- package/dist/optional/virtual-scroll-tanstack.d.ts.map +1 -0
- package/dist/optional/virtual-scroll-tanstack.js +17 -0
- package/dist/setup.d.ts +53 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +97 -0
- package/package.json +174 -4
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { AuthClient, AuthClientConfigOptions, AuthProfileConstraint, RegisterExtraShape, UserProfile } from '@molecule/app-auth';
|
|
2
|
+
import type { HttpClient } from '@molecule/app-http';
|
|
3
|
+
/**
|
|
4
|
+
* Builds the default fetch-based HTTP client + wires it into
|
|
5
|
+
* `@molecule/app-http`. Replaces the per-app `bonds/http-default.ts`
|
|
6
|
+
* shipped by ~52 fleet apps.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // bonds/http-default.ts
|
|
11
|
+
* import { createDefaultHttpClient } from '@molecule/app-bonds-default'
|
|
12
|
+
* import { API_BASE_URL } from '../config.js'
|
|
13
|
+
*
|
|
14
|
+
* export const { httpClient, setupHttpDefault } = createDefaultHttpClient(API_BASE_URL)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function createDefaultHttpClient(baseURL: string): {
|
|
18
|
+
httpClient: HttpClient;
|
|
19
|
+
setupHttpDefault: () => void;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Variant of `createDefaultHttpClient` that adds a request interceptor
|
|
23
|
+
* injecting the JWT bearer token from the auth client AND (optionally)
|
|
24
|
+
* stripping a leading `/api/` from request URLs.
|
|
25
|
+
*
|
|
26
|
+
* Replaces ~20 per-app `bonds/http-default.ts` files that hand-roll
|
|
27
|
+
* this same wiring. Pass `stripApiPrefix: true` when the app uses
|
|
28
|
+
* `baseURL: '/api'` to handle pages that pass `/api/`-prefixed paths
|
|
29
|
+
* (would otherwise resolve to `/api/api/...` and 404). Pass `authScheme`
|
|
30
|
+
* when the API reads another scheme than `Bearer`: it is an option of THIS
|
|
31
|
+
* constructor (`createDefaultHttpClientWithAuthBearer({ authScheme: 'Token', … })`,
|
|
32
|
+
* or `createFetchClient({ baseURL, authScheme: 'Token' })` directly), never a
|
|
33
|
+
* property of the client it returns. The client is built with the same scheme, so
|
|
34
|
+
* a token the client also holds — the one a default auth bond's token sync gives
|
|
35
|
+
* it — is sent with that scheme too, not as `Bearer`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createDefaultHttpClientWithAuthBearer(opts: {
|
|
38
|
+
baseURL: string;
|
|
39
|
+
withCredentials?: boolean;
|
|
40
|
+
stripApiPrefix?: boolean;
|
|
41
|
+
getToken: () => string | null | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The scheme of the `Authorization` header the token from `getToken()` is sent
|
|
44
|
+
* with: `Bearer` (default), or what the API expects, e.g. `Token` for an API
|
|
45
|
+
* that reads `Authorization: Token <jwt>`. Pass it to this constructor — the
|
|
46
|
+
* same option `@molecule/app-http`'s client config calls `authScheme`, and not
|
|
47
|
+
* a property of the client that comes back.
|
|
48
|
+
*/
|
|
49
|
+
authScheme?: string;
|
|
50
|
+
}): {
|
|
51
|
+
httpClient: HttpClient;
|
|
52
|
+
setupHttpDefault: () => void;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Builds the default JWT auth client + wires it into `@molecule/app-auth`.
|
|
56
|
+
* Apps pass their own `authConfig` (which lives in `src/config.ts`).
|
|
57
|
+
*
|
|
58
|
+
* `TUser` is the profile the app's pages read, and may be any JSON object type —
|
|
59
|
+
* including a profile with NO `id` (`{ username, email, bio, image }`), as in the
|
|
60
|
+
* factory it wraps. Never add a synthetic `id` to satisfy a type: nothing in the
|
|
61
|
+
* client reads it, and the fabricated value then travels into storage, `getUser()`
|
|
62
|
+
* and every page. Omit the type parameter and the default is `UserProfile`, whose
|
|
63
|
+
* `id: string` stays required — the profile is never inferred from `authConfig`
|
|
64
|
+
* (not even from an annotated config variable), so an untyped wrapper call keeps
|
|
65
|
+
* the default contract.
|
|
66
|
+
*
|
|
67
|
+
* Type the config with the SAME type (`const authConfig: AuthClientConfig<MyUser> = { … }`)
|
|
68
|
+
* so its `mapAuthResponse` is checked against `MyUser`, and name that type at the
|
|
69
|
+
* call (`createDefaultAuthClient<MyUser>(authConfig)`): a mapper that omits a
|
|
70
|
+
* required custom field then fails at that call instead of shipping a signed-in
|
|
71
|
+
* page that reads `undefined`. An annotated config alone does not supply the type
|
|
72
|
+
* argument — that is a 1.8 caller shape this major changes (see
|
|
73
|
+
* `AuthClientConfig`'s migration note in `@molecule/app-auth`).
|
|
74
|
+
*
|
|
75
|
+
* `TRegister` is the app's own extra registration fields (for example
|
|
76
|
+
* `{ username: string }`), passed on to `@molecule/app-auth`'s factory so
|
|
77
|
+
* `authClient.register({ username, email, password })` type-checks with no cast.
|
|
78
|
+
* See the module guidance for the lifecycle and API adaptations already included.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* // bonds/auth-default.ts
|
|
83
|
+
* import { createDefaultAuthClient } from '@molecule/app-bonds-default'
|
|
84
|
+
* import { authConfig } from '../config.js'
|
|
85
|
+
*
|
|
86
|
+
* export const { authClient, setupAuthDefault } =
|
|
87
|
+
* createDefaultAuthClient(authConfig)
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function createDefaultAuthClient<TUser extends AuthProfileConstraint<TUser> = UserProfile, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(authConfig: NoInfer<AuthClientConfigOptions<TUser>>): {
|
|
91
|
+
authClient: AuthClient<TUser, TRegister>;
|
|
92
|
+
setupAuthDefault: () => void;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Variant of `createDefaultAuthClient` that also keeps the bonded
|
|
96
|
+
* `@molecule/app-http` client's bearer token in sync with the auth client's session.
|
|
97
|
+
* Required by apps whose http client must carry the JWT on every request — without
|
|
98
|
+
* this wiring, authed endpoints return 401 after page reloads or token refresh.
|
|
99
|
+
*
|
|
100
|
+
* The sync is a LIVE read: `@molecule/app-http`'s `setAuthTokenSource` registers the auth
|
|
101
|
+
* client's token, and every client the app bonds, before or after this setup, reads it on
|
|
102
|
+
* each request — so the order of the `setup*()` calls in `bonds/index.ts` does not matter
|
|
103
|
+
* and never needs changing for auth. A session adopted with no
|
|
104
|
+
* auth event (`useAuth().setSession()`, an OAuth callback's `setAccessToken()`, the same
|
|
105
|
+
* profile under a new token) is therefore sent by the next request, not the previous
|
|
106
|
+
* account's bearer. A transport without `setAuthTokenResolver` is followed through a request
|
|
107
|
+
* interceptor (see `setAuthTokenSource` for which request first carries the change), and
|
|
108
|
+
* auth events still push the token to it.
|
|
109
|
+
*
|
|
110
|
+
* A cookie session (no body token) resolves to `null`, so the request carries NO
|
|
111
|
+
* `Authorization` header — a kept bearer would answer as the PRIOR session.
|
|
112
|
+
* Requests also wait for the session restore, so a page that fetches viewer data on mount
|
|
113
|
+
* cannot race it (see {@link registerAuthGate}).
|
|
114
|
+
*
|
|
115
|
+
* The profile type is `createDefaultAuthClient`'s: a JSON object type of named
|
|
116
|
+
* fields (see `AuthProfileShape`), including an app profile with no `id`.
|
|
117
|
+
*/
|
|
118
|
+
export declare function createDefaultAuthClientWithHttpSync<TUser extends AuthProfileConstraint<TUser> = UserProfile, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(authConfig: NoInfer<AuthClientConfigOptions<TUser>>): {
|
|
119
|
+
authClient: AuthClient<TUser, TRegister>;
|
|
120
|
+
setupAuthDefault: () => void;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Most aggressive variant — bonds a fetch-based HTTP client with the
|
|
124
|
+
* given `baseURL` (so molecule pkg `useGet('/path')` calls hit
|
|
125
|
+
* `${baseURL}/path` instead of the SPA's catch-all route), AND keeps
|
|
126
|
+
* its bearer token in sync with the auth client's session, read on every
|
|
127
|
+
* request: a session adopted with no auth event (`setSession()`,
|
|
128
|
+
* `setAccessToken()`) is sent by the next request. The token uses the auth
|
|
129
|
+
* config's `authScheme` (default `Bearer`); a client bonded after this setup
|
|
130
|
+
* gets the same source and auth events.
|
|
131
|
+
*
|
|
132
|
+
* The auth client restores a stored token AND user SYNCHRONOUSLY, so the first
|
|
133
|
+
* render is the signed-in UI — do not gate a protected page (or a provider of
|
|
134
|
+
* yours) on a current-user request. This bond registers `whenRestored()` as the
|
|
135
|
+
* HTTP gate, so requests still wait for the background restore/verification to
|
|
136
|
+
* settle; only `useAuth().status === 'loading'` means "no decision yet"
|
|
137
|
+
* (a page that waits on its own `/me` pays a round trip on every navigation).
|
|
138
|
+
*
|
|
139
|
+
* Used by apps where molecule packages render pricing / billing /
|
|
140
|
+
* other authed JSON-fetching screens that need both behaviors.
|
|
141
|
+
*
|
|
142
|
+
* Requests are also held until the session restore settles (see
|
|
143
|
+
* {@link registerAuthGate}), so a page that fetches viewer-dependent data on
|
|
144
|
+
* mount cannot race it and answer for an anonymous viewer.
|
|
145
|
+
*
|
|
146
|
+
* The profile type is `createDefaultAuthClient`'s: a JSON object type of named
|
|
147
|
+
* fields (see `AuthProfileShape`), including an app profile with no `id`.
|
|
148
|
+
*/
|
|
149
|
+
export declare function createDefaultAuthClientWithFetchClient<TUser extends AuthProfileConstraint<TUser> = UserProfile, TRegister extends RegisterExtraShape<TRegister> = Record<never, never>>(authConfig: NoInfer<AuthClientConfigOptions<TUser>>, fetchClientOptions: {
|
|
150
|
+
baseURL: string;
|
|
151
|
+
withCredentials?: boolean;
|
|
152
|
+
}): {
|
|
153
|
+
authClient: AuthClient<TUser, TRegister>;
|
|
154
|
+
setupAuthDefault: () => void;
|
|
155
|
+
};
|
|
156
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,uBAAuB,EAEvB,qBAAqB,EACrB,kBAAkB,EAClB,WAAW,EACZ,MAAM,oBAAoB,CAAA;AAE3B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAsCpD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG;IACxD,UAAU,EAAE,UAAU,CAAA;IACtB,gBAAgB,EAAE,MAAM,IAAI,CAAA;CAC7B,CAMA;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qCAAqC,CAAC,IAAI,EAAE;IAC1D,OAAO,EAAE,MAAM,CAAA;IACf,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,QAAQ,EAAE,MAAM,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;IACzC;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,GAAG;IACF,UAAU,EAAE,UAAU,CAAA;IACtB,gBAAgB,EAAE,MAAM,IAAI,CAAA;CAC7B,CAuBA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,SAAS,qBAAqB,CAAC,KAAK,CAAC,GAAG,WAAW,EACxD,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EAEtE,UAAU,EAAE,OAAO,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC,GAClD;IACD,UAAU,EAAE,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IACxC,gBAAgB,EAAE,MAAM,IAAI,CAAA;CAC7B,CAMA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mCAAmC,CACjD,KAAK,SAAS,qBAAqB,CAAC,KAAK,CAAC,GAAG,WAAW,EACxD,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EAEtE,UAAU,EAAE,OAAO,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC,GAClD;IACD,UAAU,EAAE,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IACxC,gBAAgB,EAAE,MAAM,IAAI,CAAA;CAC7B,CAkCA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,sCAAsC,CACpD,KAAK,SAAS,qBAAqB,CAAC,KAAK,CAAC,GAAG,WAAW,EACxD,SAAS,SAAS,kBAAkB,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EAEtE,UAAU,EAAE,OAAO,CAAC,uBAAuB,CAAC,KAAK,CAAC,CAAC,EACnD,kBAAkB,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,OAAO,CAAA;CAAE,GACjE;IACD,UAAU,EAAE,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IACxC,gBAAgB,EAAE,MAAM,IAAI,CAAA;CAC7B,CAmCA"}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { createJWTAuthClient, setClient } from '@molecule/app-auth';
|
|
2
|
+
import { createFetchClient, getClient as getHttpClient, setAuthGate, setAuthTokenSource as setHttpAuthTokenSource, setClient as setHttpClient, } from '@molecule/app-http';
|
|
3
|
+
/**
|
|
4
|
+
* Holds every HTTP request until this auth client's session restore has settled.
|
|
5
|
+
*
|
|
6
|
+
* Without it a page effect that fires on mount (fetching account settings, a
|
|
7
|
+
* dashboard, or a saved-item state) sends its request before `initialize()` has
|
|
8
|
+
* restored the session, so it leaves with no `Authorization` header and the server
|
|
9
|
+
* answers for an anonymous viewer. Localhost usually hides it — the restore wins
|
|
10
|
+
* the race against the first effect there — and the deployed URL exposes it, where a
|
|
11
|
+
* saved item reads as unsaved because the request lacked its session.
|
|
12
|
+
*
|
|
13
|
+
* `whenRestored()` resolves immediately when no restore is in flight, so a request
|
|
14
|
+
* made after boot never waits at all; it holds only while the restore is actually
|
|
15
|
+
* running. A provider without `whenRestored` (a bond that restores nothing) leaves
|
|
16
|
+
* requests ungated.
|
|
17
|
+
*
|
|
18
|
+
* The restore this gate waits for does NOT go through the gated client — both default auth
|
|
19
|
+
* bonds' `createJWTAuthClient` restores with `fetch` — so the scaffold's own wiring cannot
|
|
20
|
+
* deadlock. An app that registers its own gate over a restore made through the bonded client
|
|
21
|
+
* must exempt that request with `skipAuthGate: true`
|
|
22
|
+
* (`setAuthGate(() => store.whenSettled())` + `http.get(currentUserEndpoint, { skipAuthGate: true })`
|
|
23
|
+
* in the store); otherwise the gate holds the request it is waiting for and every request it
|
|
24
|
+
* holds stalls until the gate times out. See `RequestConfig.skipAuthGate` in `@molecule/app-http`.
|
|
25
|
+
*
|
|
26
|
+
* @param authClient - The bonded auth client.
|
|
27
|
+
*/
|
|
28
|
+
function registerAuthGate(authClient) {
|
|
29
|
+
setAuthGate(() => authClient.whenRestored?.() ?? Promise.resolve());
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds the default fetch-based HTTP client + wires it into
|
|
33
|
+
* `@molecule/app-http`. Replaces the per-app `bonds/http-default.ts`
|
|
34
|
+
* shipped by ~52 fleet apps.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* // bonds/http-default.ts
|
|
39
|
+
* import { createDefaultHttpClient } from '@molecule/app-bonds-default'
|
|
40
|
+
* import { API_BASE_URL } from '../config.js'
|
|
41
|
+
*
|
|
42
|
+
* export const { httpClient, setupHttpDefault } = createDefaultHttpClient(API_BASE_URL)
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function createDefaultHttpClient(baseURL) {
|
|
46
|
+
const httpClient = createFetchClient({ baseURL });
|
|
47
|
+
return {
|
|
48
|
+
httpClient,
|
|
49
|
+
setupHttpDefault: () => setHttpClient(httpClient),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Variant of `createDefaultHttpClient` that adds a request interceptor
|
|
54
|
+
* injecting the JWT bearer token from the auth client AND (optionally)
|
|
55
|
+
* stripping a leading `/api/` from request URLs.
|
|
56
|
+
*
|
|
57
|
+
* Replaces ~20 per-app `bonds/http-default.ts` files that hand-roll
|
|
58
|
+
* this same wiring. Pass `stripApiPrefix: true` when the app uses
|
|
59
|
+
* `baseURL: '/api'` to handle pages that pass `/api/`-prefixed paths
|
|
60
|
+
* (would otherwise resolve to `/api/api/...` and 404). Pass `authScheme`
|
|
61
|
+
* when the API reads another scheme than `Bearer`: it is an option of THIS
|
|
62
|
+
* constructor (`createDefaultHttpClientWithAuthBearer({ authScheme: 'Token', … })`,
|
|
63
|
+
* or `createFetchClient({ baseURL, authScheme: 'Token' })` directly), never a
|
|
64
|
+
* property of the client it returns. The client is built with the same scheme, so
|
|
65
|
+
* a token the client also holds — the one a default auth bond's token sync gives
|
|
66
|
+
* it — is sent with that scheme too, not as `Bearer`.
|
|
67
|
+
*/
|
|
68
|
+
export function createDefaultHttpClientWithAuthBearer(opts) {
|
|
69
|
+
const authScheme = opts.authScheme ?? 'Bearer';
|
|
70
|
+
const httpClient = createFetchClient({
|
|
71
|
+
baseURL: opts.baseURL,
|
|
72
|
+
withCredentials: opts.withCredentials,
|
|
73
|
+
// The client's own token (a default auth bond's token sync) overrides the interceptor's
|
|
74
|
+
// header, so it must carry the same scheme.
|
|
75
|
+
authScheme,
|
|
76
|
+
});
|
|
77
|
+
httpClient.addRequestInterceptor((config) => {
|
|
78
|
+
if (opts.stripApiPrefix && typeof config.url === 'string' && config.url.startsWith('/api/')) {
|
|
79
|
+
config.url = config.url.replace(/^\/api\//, '/');
|
|
80
|
+
}
|
|
81
|
+
const token = opts.getToken();
|
|
82
|
+
if (token) {
|
|
83
|
+
config.headers = { ...config.headers, Authorization: `${authScheme} ${token}` };
|
|
84
|
+
}
|
|
85
|
+
return config;
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
httpClient,
|
|
89
|
+
setupHttpDefault: () => setHttpClient(httpClient),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Builds the default JWT auth client + wires it into `@molecule/app-auth`.
|
|
94
|
+
* Apps pass their own `authConfig` (which lives in `src/config.ts`).
|
|
95
|
+
*
|
|
96
|
+
* `TUser` is the profile the app's pages read, and may be any JSON object type —
|
|
97
|
+
* including a profile with NO `id` (`{ username, email, bio, image }`), as in the
|
|
98
|
+
* factory it wraps. Never add a synthetic `id` to satisfy a type: nothing in the
|
|
99
|
+
* client reads it, and the fabricated value then travels into storage, `getUser()`
|
|
100
|
+
* and every page. Omit the type parameter and the default is `UserProfile`, whose
|
|
101
|
+
* `id: string` stays required — the profile is never inferred from `authConfig`
|
|
102
|
+
* (not even from an annotated config variable), so an untyped wrapper call keeps
|
|
103
|
+
* the default contract.
|
|
104
|
+
*
|
|
105
|
+
* Type the config with the SAME type (`const authConfig: AuthClientConfig<MyUser> = { … }`)
|
|
106
|
+
* so its `mapAuthResponse` is checked against `MyUser`, and name that type at the
|
|
107
|
+
* call (`createDefaultAuthClient<MyUser>(authConfig)`): a mapper that omits a
|
|
108
|
+
* required custom field then fails at that call instead of shipping a signed-in
|
|
109
|
+
* page that reads `undefined`. An annotated config alone does not supply the type
|
|
110
|
+
* argument — that is a 1.8 caller shape this major changes (see
|
|
111
|
+
* `AuthClientConfig`'s migration note in `@molecule/app-auth`).
|
|
112
|
+
*
|
|
113
|
+
* `TRegister` is the app's own extra registration fields (for example
|
|
114
|
+
* `{ username: string }`), passed on to `@molecule/app-auth`'s factory so
|
|
115
|
+
* `authClient.register({ username, email, password })` type-checks with no cast.
|
|
116
|
+
* See the module guidance for the lifecycle and API adaptations already included.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* // bonds/auth-default.ts
|
|
121
|
+
* import { createDefaultAuthClient } from '@molecule/app-bonds-default'
|
|
122
|
+
* import { authConfig } from '../config.js'
|
|
123
|
+
*
|
|
124
|
+
* export const { authClient, setupAuthDefault } =
|
|
125
|
+
* createDefaultAuthClient(authConfig)
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export function createDefaultAuthClient(authConfig) {
|
|
129
|
+
const authClient = createJWTAuthClient(authConfig);
|
|
130
|
+
return {
|
|
131
|
+
authClient,
|
|
132
|
+
setupAuthDefault: () => setClient(authClient),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Variant of `createDefaultAuthClient` that also keeps the bonded
|
|
137
|
+
* `@molecule/app-http` client's bearer token in sync with the auth client's session.
|
|
138
|
+
* Required by apps whose http client must carry the JWT on every request — without
|
|
139
|
+
* this wiring, authed endpoints return 401 after page reloads or token refresh.
|
|
140
|
+
*
|
|
141
|
+
* The sync is a LIVE read: `@molecule/app-http`'s `setAuthTokenSource` registers the auth
|
|
142
|
+
* client's token, and every client the app bonds, before or after this setup, reads it on
|
|
143
|
+
* each request — so the order of the `setup*()` calls in `bonds/index.ts` does not matter
|
|
144
|
+
* and never needs changing for auth. A session adopted with no
|
|
145
|
+
* auth event (`useAuth().setSession()`, an OAuth callback's `setAccessToken()`, the same
|
|
146
|
+
* profile under a new token) is therefore sent by the next request, not the previous
|
|
147
|
+
* account's bearer. A transport without `setAuthTokenResolver` is followed through a request
|
|
148
|
+
* interceptor (see `setAuthTokenSource` for which request first carries the change), and
|
|
149
|
+
* auth events still push the token to it.
|
|
150
|
+
*
|
|
151
|
+
* A cookie session (no body token) resolves to `null`, so the request carries NO
|
|
152
|
+
* `Authorization` header — a kept bearer would answer as the PRIOR session.
|
|
153
|
+
* Requests also wait for the session restore, so a page that fetches viewer data on mount
|
|
154
|
+
* cannot race it (see {@link registerAuthGate}).
|
|
155
|
+
*
|
|
156
|
+
* The profile type is `createDefaultAuthClient`'s: a JSON object type of named
|
|
157
|
+
* fields (see `AuthProfileShape`), including an app profile with no `id`.
|
|
158
|
+
*/
|
|
159
|
+
export function createDefaultAuthClientWithHttpSync(authConfig) {
|
|
160
|
+
const authClient = createJWTAuthClient(authConfig);
|
|
161
|
+
return {
|
|
162
|
+
authClient,
|
|
163
|
+
setupAuthDefault: () => {
|
|
164
|
+
setClient(authClient);
|
|
165
|
+
// Requests made while the session is still restoring wait for it, so a page
|
|
166
|
+
// effect that fetches viewer-dependent data on mount cannot race the restore.
|
|
167
|
+
registerAuthGate(authClient);
|
|
168
|
+
// Resolve the HTTP client per sync, never once here. `setClient` runs this
|
|
169
|
+
// source, so the token reaches the client the scaffold bonds later as well as
|
|
170
|
+
// one already bonded; a client kept from this moment would be the throwaway
|
|
171
|
+
// fallback the later `setHttpDefault()` replaces. The client reads the source
|
|
172
|
+
// itself on every request, so a session adopted by `setSession()` (no auth
|
|
173
|
+
// event) or re-adopted for the same profile (no state change) is sent at once.
|
|
174
|
+
setHttpAuthTokenSource(() => authClient.getAccessToken());
|
|
175
|
+
authClient.addEventListener((event) => {
|
|
176
|
+
if (event.type === 'login' || event.type === 'register' || event.type === 'refresh') {
|
|
177
|
+
// Mirror the auth client's token EXACTLY — including null. Cookie-session
|
|
178
|
+
// APIs issue no body token (the credential is the httpOnly cookie), and
|
|
179
|
+
// keeping a previously-set bearer here would pin the http client to the
|
|
180
|
+
// PRIOR session (e.g. a guest's) — the server prefers the Authorization
|
|
181
|
+
// header over the fresh cookie, so every request 401s as the old user.
|
|
182
|
+
//
|
|
183
|
+
// A client that implements `setAuthTokenResolver` already resolves the source
|
|
184
|
+
// per request; this push is what carries the same change to a transport that
|
|
185
|
+
// does not (and keeps `getAuthToken()` current there).
|
|
186
|
+
getHttpClient().setAuthToken(authClient.getAccessToken());
|
|
187
|
+
}
|
|
188
|
+
else if (event.type === 'logout') {
|
|
189
|
+
getHttpClient().setAuthToken(null);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Most aggressive variant — bonds a fetch-based HTTP client with the
|
|
197
|
+
* given `baseURL` (so molecule pkg `useGet('/path')` calls hit
|
|
198
|
+
* `${baseURL}/path` instead of the SPA's catch-all route), AND keeps
|
|
199
|
+
* its bearer token in sync with the auth client's session, read on every
|
|
200
|
+
* request: a session adopted with no auth event (`setSession()`,
|
|
201
|
+
* `setAccessToken()`) is sent by the next request. The token uses the auth
|
|
202
|
+
* config's `authScheme` (default `Bearer`); a client bonded after this setup
|
|
203
|
+
* gets the same source and auth events.
|
|
204
|
+
*
|
|
205
|
+
* The auth client restores a stored token AND user SYNCHRONOUSLY, so the first
|
|
206
|
+
* render is the signed-in UI — do not gate a protected page (or a provider of
|
|
207
|
+
* yours) on a current-user request. This bond registers `whenRestored()` as the
|
|
208
|
+
* HTTP gate, so requests still wait for the background restore/verification to
|
|
209
|
+
* settle; only `useAuth().status === 'loading'` means "no decision yet"
|
|
210
|
+
* (a page that waits on its own `/me` pays a round trip on every navigation).
|
|
211
|
+
*
|
|
212
|
+
* Used by apps where molecule packages render pricing / billing /
|
|
213
|
+
* other authed JSON-fetching screens that need both behaviors.
|
|
214
|
+
*
|
|
215
|
+
* Requests are also held until the session restore settles (see
|
|
216
|
+
* {@link registerAuthGate}), so a page that fetches viewer-dependent data on
|
|
217
|
+
* mount cannot race it and answer for an anonymous viewer.
|
|
218
|
+
*
|
|
219
|
+
* The profile type is `createDefaultAuthClient`'s: a JSON object type of named
|
|
220
|
+
* fields (see `AuthProfileShape`), including an app profile with no `id`.
|
|
221
|
+
*/
|
|
222
|
+
export function createDefaultAuthClientWithFetchClient(authConfig, fetchClientOptions) {
|
|
223
|
+
const authClient = createJWTAuthClient(authConfig);
|
|
224
|
+
return {
|
|
225
|
+
authClient,
|
|
226
|
+
setupAuthDefault: () => {
|
|
227
|
+
const httpClient = createFetchClient({
|
|
228
|
+
baseURL: fetchClientOptions.baseURL,
|
|
229
|
+
withCredentials: fetchClientOptions.withCredentials ?? true,
|
|
230
|
+
// The scheme the auth config names for this token (`Authorization: Token <jwt>`).
|
|
231
|
+
authScheme: authConfig.authScheme,
|
|
232
|
+
});
|
|
233
|
+
// Registered BEFORE the bond: `setHttpClient` applies the restored session's
|
|
234
|
+
// token, and the same source reaches any client an app bonds later instead of
|
|
235
|
+
// this one. A client captured at setup cannot track later replacements. The
|
|
236
|
+
// client resolves the source on every request, so a session adopted by
|
|
237
|
+
// `setSession()` or `setAccessToken()` after this point is sent immediately.
|
|
238
|
+
setHttpAuthTokenSource(() => authClient.getAccessToken());
|
|
239
|
+
setHttpClient(httpClient);
|
|
240
|
+
setClient(authClient);
|
|
241
|
+
registerAuthGate(authClient);
|
|
242
|
+
authClient.addEventListener((event) => {
|
|
243
|
+
// The client bonded NOW, resolved per event: an app that bonds its own client after
|
|
244
|
+
// this setup sends through that one, never through the client created above.
|
|
245
|
+
if (event.type === 'login' || event.type === 'register' || event.type === 'refresh') {
|
|
246
|
+
// Mirror the token exactly, including null — see the identical note in
|
|
247
|
+
// createDefaultAuthClientWithHttpSync (stale bearer = stuck prior session).
|
|
248
|
+
// The per-request read of the source above is the guarantee; this push keeps
|
|
249
|
+
// `getAuthToken()` current on a transport without `setAuthTokenResolver`.
|
|
250
|
+
getHttpClient().setAuthToken(authClient.getAccessToken());
|
|
251
|
+
}
|
|
252
|
+
else if (event.type === 'logout') {
|
|
253
|
+
getHttpClient().setAuthToken(null);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@molecule/app-bonds-default` — the framework-neutral half of the default
|
|
3
|
+
* app-side bond wirings, shared unchanged by the per-framework packages
|
|
4
|
+
* (`@molecule/app-bonds-default-react`, `-vue`, `-solid`, `-svelte`,
|
|
5
|
+
* `-angular`), which re-export everything here and add their routing setup,
|
|
6
|
+
* `setupAllDefaultBonds()` and `bootstrapApp()`. Switching framework means
|
|
7
|
+
* changing the package name in the imports — every export below keeps its name.
|
|
8
|
+
*
|
|
9
|
+
* - The seven universal neutral setups — `setupAppStateSimple` (the vanilla
|
|
10
|
+
* `simpleProvider`, so `createStore()` works from the first render — the
|
|
11
|
+
* documented `createStore()` + `useStore()` pattern needs a provider bonded
|
|
12
|
+
* and nothing else in a scaffold wires one), `setupAppFontsArimo`,
|
|
13
|
+
* `setupAppStorageLocalstorage`, `setupAppStylingTailwind` (registers the
|
|
14
|
+
* tailwind-merge class merger), `setupAppThemeCssVariables` (light + dark via
|
|
15
|
+
* `getDefaultThemeProvider()`), `setupAppUiTailwind` ClassMap,
|
|
16
|
+
* `setupAppIconsMolecule`. Routing is framework-specific and lives in the
|
|
17
|
+
* per-framework packages.
|
|
18
|
+
* - **Optional provider wirings live behind subpaths**, one module per pair:
|
|
19
|
+
* `@molecule/app-bonds-default/optional/<pair>.js` — `realtime-socketio`,
|
|
20
|
+
* `keyboard-shortcuts-hotkeys`, `command-palette-cmdk`, `client-search-minisearch`, `code-editor-monaco`,
|
|
21
|
+
* `virtual-scroll-tanstack`, `drag-drop-dndkit`, `charts-chartjs`,
|
|
22
|
+
* `maps-leaflet`, `video-hls`. Import ONLY the ones the app installs.
|
|
23
|
+
* - Auth/http factories — `createDefaultAuthClient(authConfig)` returns
|
|
24
|
+
* `{ authClient, setupAuthDefault }`; the `...WithHttpSync` /
|
|
25
|
+
* `...WithFetchClient` variants also keep the bonded http client's bearer
|
|
26
|
+
* token in sync with auth events. All three accept any JSON object profile
|
|
27
|
+
* (`createDefaultAuthClient<SessionUser>(config)` for an API whose user has no
|
|
28
|
+
* `id`) and fall back to `UserProfile` — with its `id: string` — when no type
|
|
29
|
+
* argument is given. The profile is never inferred from `authConfig`, so an
|
|
30
|
+
* untyped call keeps the default contract.
|
|
31
|
+
*
|
|
32
|
+
* **What this client already handles**
|
|
33
|
+
* - `getAccessToken()` returns a stored token during restore; only 401/403 ends
|
|
34
|
+
* the session, while network/5xx failures retain it.
|
|
35
|
+
* - `tokenKey`/`storagePrefix`, `authScheme`, `mapAuthResponse`,
|
|
36
|
+
* `currentUserEndpoint`, and `profileEndpoint`/`profileUpdateMethod` adapt the API.
|
|
37
|
+
* - `logout()` settles local sign-out; `refresh()` uses a stored refresh token.
|
|
38
|
+
*
|
|
39
|
+
* This factory adds no render gate: `AuthProvider` renders children immediately.
|
|
40
|
+
* The HTTP-sync variants gate requests during restore, not rendering.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // bonds/index.ts — any framework
|
|
45
|
+
* import { createDefaultAuthClientWithHttpSync } from '@molecule/app-bonds-default'
|
|
46
|
+
* // Optional providers come from their own subpath — see @remarks.
|
|
47
|
+
* import { setupAppCodeEditorMonaco } from '@molecule/app-bonds-default/optional/code-editor-monaco.js'
|
|
48
|
+
* import { authConfig } from '../config.js'
|
|
49
|
+
*
|
|
50
|
+
* export const { authClient, setupAuthDefault } =
|
|
51
|
+
* createDefaultAuthClientWithHttpSync(authConfig)
|
|
52
|
+
*
|
|
53
|
+
* // Async so optional async bonds are AWAITED before the first render.
|
|
54
|
+
* export const setupProviders = async () => {
|
|
55
|
+
* setupAllDefaultBonds() // from @molecule/app-bonds-default-<framework>
|
|
56
|
+
* setupAuthDefault()
|
|
57
|
+
* await setupAppCodeEditorMonaco() // only if the app uses the code editor
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example Adapt a nested token and `Token` header without another session store.
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { createDefaultAuthClient } from '@molecule/app-bonds-default'
|
|
64
|
+
*
|
|
65
|
+
* const { authClient, setupAuthDefault } = createDefaultAuthClient({
|
|
66
|
+
* tokenKey: 'app.jwt',
|
|
67
|
+
* authScheme: 'Token',
|
|
68
|
+
* currentUserEndpoint: '/user',
|
|
69
|
+
* profileEndpoint: '/user',
|
|
70
|
+
* profileUpdateMethod: 'PUT',
|
|
71
|
+
* mapAuthResponse: (data) => {
|
|
72
|
+
* const { token, ...user } = (data as { user: { token: string; id: string } }).user
|
|
73
|
+
* return { accessToken: token, user }
|
|
74
|
+
* },
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* - **Optional setups are NOT exported from the package root.** Import each from
|
|
80
|
+
* its own subpath — `@molecule/app-bonds-default/optional/maps-leaflet.js`,
|
|
81
|
+
* not `from '@molecule/app-bonds-default'`. A bundler must RESOLVE every
|
|
82
|
+
* `import()` in a module it pulls into the graph, before tree-shaking can drop
|
|
83
|
+
* anything, so while these lived in the barrel every app inherited all 18
|
|
84
|
+
* optional providers and any app that had not installed all of them failed to
|
|
85
|
+
* build with `Rolldown failed to resolve import "@molecule/app-maps"`. Only
|
|
86
|
+
* import a subpath whose provider pair the app actually installs.
|
|
87
|
+
* - `setupAllDefaultBonds()` is per-framework (it also wires that framework's
|
|
88
|
+
* routing bond) and is exported by `@molecule/app-bonds-default-<framework>`,
|
|
89
|
+
* not here. The optional bonds are NOT part of it either way: they ship as
|
|
90
|
+
* separate ASYNC setups — `setupAppRealtimeSocketio`,
|
|
91
|
+
* `setupAppKeyboardShortcutsHotkeys`, `setupAppCommandPaletteCmdk`,
|
|
92
|
+
* `setupAppCodeEditorMonaco`, `setupAppVirtualScrollTanstack`,
|
|
93
|
+
* `setupAppDragDropDndkit`, `setupAppChartsChartjs`, `setupAppMapsLeaflet`,
|
|
94
|
+
* `setupAppVideoHls` — and MUST be awaited inside `setupProviders`
|
|
95
|
+
* (make it async). `bootstrapApp` awaits `setupProviders()` before mounting
|
|
96
|
+
* so bonded providers exist by a component's first effect; a fire-and-forget
|
|
97
|
+
* async setup races the mount and intermittently loses.
|
|
98
|
+
* - Plain `createDefaultAuthClient` does NOT attach the JWT to the bonded
|
|
99
|
+
* http client. If molecule packages call authed `/api` endpoints, use
|
|
100
|
+
* `createDefaultAuthClientWithHttpSync` (or `...WithFetchClient` to also
|
|
101
|
+
* bond a fetch client with a `baseURL`) — otherwise those endpoints return
|
|
102
|
+
* 401 after a page reload or token refresh.
|
|
103
|
+
* - `getDefaultThemeProvider()` constructs lazily because the CSS-variables
|
|
104
|
+
* theme provider touches `localStorage` at construction — importing this
|
|
105
|
+
* package is SSR/test-safe, but only CALL it in a DOM environment. Apps
|
|
106
|
+
* with custom themes build their own provider and skip
|
|
107
|
+
* `setupAppThemeCssVariables()`.
|
|
108
|
+
*
|
|
109
|
+
* @module
|
|
110
|
+
*/
|
|
111
|
+
export * from './auth.js';
|
|
112
|
+
export * from './setup.js';
|
|
113
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6GG;AAEH,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@molecule/app-bonds-default` — the framework-neutral half of the default
|
|
3
|
+
* app-side bond wirings, shared unchanged by the per-framework packages
|
|
4
|
+
* (`@molecule/app-bonds-default-react`, `-vue`, `-solid`, `-svelte`,
|
|
5
|
+
* `-angular`), which re-export everything here and add their routing setup,
|
|
6
|
+
* `setupAllDefaultBonds()` and `bootstrapApp()`. Switching framework means
|
|
7
|
+
* changing the package name in the imports — every export below keeps its name.
|
|
8
|
+
*
|
|
9
|
+
* - The seven universal neutral setups — `setupAppStateSimple` (the vanilla
|
|
10
|
+
* `simpleProvider`, so `createStore()` works from the first render — the
|
|
11
|
+
* documented `createStore()` + `useStore()` pattern needs a provider bonded
|
|
12
|
+
* and nothing else in a scaffold wires one), `setupAppFontsArimo`,
|
|
13
|
+
* `setupAppStorageLocalstorage`, `setupAppStylingTailwind` (registers the
|
|
14
|
+
* tailwind-merge class merger), `setupAppThemeCssVariables` (light + dark via
|
|
15
|
+
* `getDefaultThemeProvider()`), `setupAppUiTailwind` ClassMap,
|
|
16
|
+
* `setupAppIconsMolecule`. Routing is framework-specific and lives in the
|
|
17
|
+
* per-framework packages.
|
|
18
|
+
* - **Optional provider wirings live behind subpaths**, one module per pair:
|
|
19
|
+
* `@molecule/app-bonds-default/optional/<pair>.js` — `realtime-socketio`,
|
|
20
|
+
* `keyboard-shortcuts-hotkeys`, `command-palette-cmdk`, `client-search-minisearch`, `code-editor-monaco`,
|
|
21
|
+
* `virtual-scroll-tanstack`, `drag-drop-dndkit`, `charts-chartjs`,
|
|
22
|
+
* `maps-leaflet`, `video-hls`. Import ONLY the ones the app installs.
|
|
23
|
+
* - Auth/http factories — `createDefaultAuthClient(authConfig)` returns
|
|
24
|
+
* `{ authClient, setupAuthDefault }`; the `...WithHttpSync` /
|
|
25
|
+
* `...WithFetchClient` variants also keep the bonded http client's bearer
|
|
26
|
+
* token in sync with auth events. All three accept any JSON object profile
|
|
27
|
+
* (`createDefaultAuthClient<SessionUser>(config)` for an API whose user has no
|
|
28
|
+
* `id`) and fall back to `UserProfile` — with its `id: string` — when no type
|
|
29
|
+
* argument is given. The profile is never inferred from `authConfig`, so an
|
|
30
|
+
* untyped call keeps the default contract.
|
|
31
|
+
*
|
|
32
|
+
* **What this client already handles**
|
|
33
|
+
* - `getAccessToken()` returns a stored token during restore; only 401/403 ends
|
|
34
|
+
* the session, while network/5xx failures retain it.
|
|
35
|
+
* - `tokenKey`/`storagePrefix`, `authScheme`, `mapAuthResponse`,
|
|
36
|
+
* `currentUserEndpoint`, and `profileEndpoint`/`profileUpdateMethod` adapt the API.
|
|
37
|
+
* - `logout()` settles local sign-out; `refresh()` uses a stored refresh token.
|
|
38
|
+
*
|
|
39
|
+
* This factory adds no render gate: `AuthProvider` renders children immediately.
|
|
40
|
+
* The HTTP-sync variants gate requests during restore, not rendering.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // bonds/index.ts — any framework
|
|
45
|
+
* import { createDefaultAuthClientWithHttpSync } from '@molecule/app-bonds-default'
|
|
46
|
+
* // Optional providers come from their own subpath — see @remarks.
|
|
47
|
+
* import { setupAppCodeEditorMonaco } from '@molecule/app-bonds-default/optional/code-editor-monaco.js'
|
|
48
|
+
* import { authConfig } from '../config.js'
|
|
49
|
+
*
|
|
50
|
+
* export const { authClient, setupAuthDefault } =
|
|
51
|
+
* createDefaultAuthClientWithHttpSync(authConfig)
|
|
52
|
+
*
|
|
53
|
+
* // Async so optional async bonds are AWAITED before the first render.
|
|
54
|
+
* export const setupProviders = async () => {
|
|
55
|
+
* setupAllDefaultBonds() // from @molecule/app-bonds-default-<framework>
|
|
56
|
+
* setupAuthDefault()
|
|
57
|
+
* await setupAppCodeEditorMonaco() // only if the app uses the code editor
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example Adapt a nested token and `Token` header without another session store.
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { createDefaultAuthClient } from '@molecule/app-bonds-default'
|
|
64
|
+
*
|
|
65
|
+
* const { authClient, setupAuthDefault } = createDefaultAuthClient({
|
|
66
|
+
* tokenKey: 'app.jwt',
|
|
67
|
+
* authScheme: 'Token',
|
|
68
|
+
* currentUserEndpoint: '/user',
|
|
69
|
+
* profileEndpoint: '/user',
|
|
70
|
+
* profileUpdateMethod: 'PUT',
|
|
71
|
+
* mapAuthResponse: (data) => {
|
|
72
|
+
* const { token, ...user } = (data as { user: { token: string; id: string } }).user
|
|
73
|
+
* return { accessToken: token, user }
|
|
74
|
+
* },
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* - **Optional setups are NOT exported from the package root.** Import each from
|
|
80
|
+
* its own subpath — `@molecule/app-bonds-default/optional/maps-leaflet.js`,
|
|
81
|
+
* not `from '@molecule/app-bonds-default'`. A bundler must RESOLVE every
|
|
82
|
+
* `import()` in a module it pulls into the graph, before tree-shaking can drop
|
|
83
|
+
* anything, so while these lived in the barrel every app inherited all 18
|
|
84
|
+
* optional providers and any app that had not installed all of them failed to
|
|
85
|
+
* build with `Rolldown failed to resolve import "@molecule/app-maps"`. Only
|
|
86
|
+
* import a subpath whose provider pair the app actually installs.
|
|
87
|
+
* - `setupAllDefaultBonds()` is per-framework (it also wires that framework's
|
|
88
|
+
* routing bond) and is exported by `@molecule/app-bonds-default-<framework>`,
|
|
89
|
+
* not here. The optional bonds are NOT part of it either way: they ship as
|
|
90
|
+
* separate ASYNC setups — `setupAppRealtimeSocketio`,
|
|
91
|
+
* `setupAppKeyboardShortcutsHotkeys`, `setupAppCommandPaletteCmdk`,
|
|
92
|
+
* `setupAppCodeEditorMonaco`, `setupAppVirtualScrollTanstack`,
|
|
93
|
+
* `setupAppDragDropDndkit`, `setupAppChartsChartjs`, `setupAppMapsLeaflet`,
|
|
94
|
+
* `setupAppVideoHls` — and MUST be awaited inside `setupProviders`
|
|
95
|
+
* (make it async). `bootstrapApp` awaits `setupProviders()` before mounting
|
|
96
|
+
* so bonded providers exist by a component's first effect; a fire-and-forget
|
|
97
|
+
* async setup races the mount and intermittently loses.
|
|
98
|
+
* - Plain `createDefaultAuthClient` does NOT attach the JWT to the bonded
|
|
99
|
+
* http client. If molecule packages call authed `/api` endpoints, use
|
|
100
|
+
* `createDefaultAuthClientWithHttpSync` (or `...WithFetchClient` to also
|
|
101
|
+
* bond a fetch client with a `baseURL`) — otherwise those endpoints return
|
|
102
|
+
* 401 after a page reload or token refresh.
|
|
103
|
+
* - `getDefaultThemeProvider()` constructs lazily because the CSS-variables
|
|
104
|
+
* theme provider touches `localStorage` at construction — importing this
|
|
105
|
+
* package is SSR/test-safe, but only CALL it in a DOM environment. Apps
|
|
106
|
+
* with custom themes build their own provider and skip
|
|
107
|
+
* `setupAppThemeCssVariables()`.
|
|
108
|
+
*
|
|
109
|
+
* @module
|
|
110
|
+
*/
|
|
111
|
+
export * from './auth.js';
|
|
112
|
+
export * from './setup.js';
|