@molecule/app-svelte 3.5.1 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +287 -76
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/live-store.d.ts +0 -8
- package/dist/hooks/live-store.d.ts.map +1 -1
- package/dist/hooks/live-store.js +0 -13
- package/dist/hooks/maybe-readable.d.ts +36 -0
- package/dist/hooks/maybe-readable.d.ts.map +1 -0
- package/dist/hooks/maybe-readable.js +36 -0
- package/dist/hooks/useAIModels.d.ts +8 -2
- package/dist/hooks/useAIModels.d.ts.map +1 -1
- package/dist/hooks/useAIModels.js +44 -17
- package/dist/hooks/useAuth.d.ts +10 -4
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +39 -13
- package/dist/hooks/useForm.d.ts +13 -5
- package/dist/hooks/useForm.d.ts.map +1 -1
- package/dist/hooks/useForm.js +90 -58
- package/dist/hooks/useHttp.d.ts +28 -22
- package/dist/hooks/useHttp.d.ts.map +1 -1
- package/dist/hooks/useHttp.js +67 -96
- package/dist/hooks/useOAuth.d.ts +23 -7
- package/dist/hooks/useOAuth.d.ts.map +1 -1
- package/dist/hooks/useOAuth.js +51 -18
- package/dist/hooks/useOptimisticToggle.d.ts +67 -0
- package/dist/hooks/useOptimisticToggle.d.ts.map +1 -0
- package/dist/hooks/useOptimisticToggle.js +52 -0
- package/dist/hooks/useOptimisticValue.d.ts +11 -4
- package/dist/hooks/useOptimisticValue.d.ts.map +1 -1
- package/dist/hooks/useOptimisticValue.js +40 -49
- package/dist/hooks/usePush.d.ts +7 -3
- package/dist/hooks/usePush.d.ts.map +1 -1
- package/dist/hooks/usePush.js +26 -6
- package/dist/hooks/useRouter.d.ts +13 -7
- package/dist/hooks/useRouter.d.ts.map +1 -1
- package/dist/hooks/useRouter.js +39 -18
- package/dist/hooks/useStorage.d.ts +22 -9
- package/dist/hooks/useStorage.d.ts.map +1 -1
- package/dist/hooks/useStorage.js +146 -69
- package/dist/hooks/useStore.d.ts +14 -7
- package/dist/hooks/useStore.d.ts.map +1 -1
- package/dist/hooks/useStore.js +46 -9
- package/dist/hooks/useTranslation.d.ts +6 -2
- package/dist/hooks/useTranslation.d.ts.map +1 -1
- package/dist/hooks/useTranslation.js +11 -6
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/link/Link.svelte +33 -0
- package/dist/link/Link.svelte.d.ts +12 -0
- package/dist/link/Link.svelte.d.ts.map +1 -0
- package/dist/link/index.d.ts +23 -0
- package/dist/link/index.d.ts.map +1 -0
- package/dist/link/index.js +22 -0
- package/dist/link/types.d.ts +26 -0
- package/dist/link/types.d.ts.map +1 -0
- package/dist/link/types.js +6 -0
- package/dist/stores/chat.d.ts +4 -3
- package/dist/stores/chat.d.ts.map +1 -1
- package/dist/stores/chat.js +14 -15
- package/package.json +5 -5
package/dist/hooks/useForm.js
CHANGED
|
@@ -4,10 +4,9 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
import { onDestroy } from 'svelte';
|
|
7
|
-
import { derived, writable } from 'svelte/store';
|
|
7
|
+
import { derived, get, writable } from 'svelte/store';
|
|
8
8
|
import { createForm as createBondedForm, formValuesEqual, snapshotFormValue, } from '@molecule/app-forms';
|
|
9
|
-
|
|
10
|
-
let formHandoff = null;
|
|
9
|
+
import { toReadable } from './maybe-readable.js';
|
|
11
10
|
/**
|
|
12
11
|
* Hook for form state management, React's `useForm` in Svelte's idiom: call it
|
|
13
12
|
* during component init (no provider needed — the controller comes from the
|
|
@@ -21,8 +20,15 @@ let formHandoff = null;
|
|
|
21
20
|
* take the loaded values. A changed `resetKey` starts a fresh form; fresh
|
|
22
21
|
* `defaultValues` without one leave the drafts alone.
|
|
23
22
|
*
|
|
24
|
-
*
|
|
25
|
-
* `
|
|
23
|
+
* The options may be a plain value or a readable store of one (a store holding
|
|
24
|
+
* `undefined` means the options were omitted): setting the store re-runs the
|
|
25
|
+
* work React's re-render does, on the same controller — a changed `values`
|
|
26
|
+
* object is applied (dirty fields kept by default) and a changed `resetKey`
|
|
27
|
+
* replaces the controller with a fresh one. Edits, touched fields and pending
|
|
28
|
+
* submits survive every other change.
|
|
29
|
+
*
|
|
30
|
+
* @param options - Form options (or a store of them). `defaultValues`, `mode`
|
|
31
|
+
* and the form-level `validate` are forwarded to the provider.
|
|
26
32
|
* @returns Form state stores and methods
|
|
27
33
|
* @example
|
|
28
34
|
* ```svelte
|
|
@@ -40,64 +46,85 @@ let formHandoff = null;
|
|
|
40
46
|
* ```
|
|
41
47
|
*/
|
|
42
48
|
export function useForm(options = {}) {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const form = sameForm ? adopted.form : createForm(formOptions);
|
|
51
|
-
const lane = sameForm
|
|
52
|
-
? adopted
|
|
53
|
-
: { dead: false, form, resetKey, lastExternal: null, stateTuples: new Map() };
|
|
54
|
-
lane.resetKey = resetKey;
|
|
55
|
-
if (sameForm)
|
|
56
|
-
lane.dead = false;
|
|
57
|
-
// React's mounted flag: false once the host is gone with no replacement.
|
|
58
|
-
let mounted = !lane.dead;
|
|
49
|
+
const optionsStore = toReadable(options);
|
|
50
|
+
const readOptions = () => get(optionsStore) ?? {};
|
|
51
|
+
const initial = readOptions();
|
|
52
|
+
// React's mounted flag: false once the component is gone. The controller
|
|
53
|
+
// itself stays as React leaves it: subscribed-to-nothing, never destroyed
|
|
54
|
+
// mid-flight.
|
|
55
|
+
let mounted = true;
|
|
59
56
|
onDestroy(() => {
|
|
60
57
|
mounted = false;
|
|
61
|
-
lane.dead = true;
|
|
62
|
-
// Offer the lane to a mount in the same turn (React's re-render); the
|
|
63
|
-
// timer clears it when no replacement adopted, so a mount after a settled
|
|
64
|
-
// teardown starts fresh. The controller itself stays as React leaves it:
|
|
65
|
-
// subscribed-to-nothing, never destroyed mid-flight.
|
|
66
|
-
formHandoff = lane;
|
|
67
|
-
const offered = lane;
|
|
68
|
-
setTimeout(() => {
|
|
69
|
-
if (formHandoff === offered)
|
|
70
|
-
formHandoff = null;
|
|
71
|
-
}, 0);
|
|
72
58
|
});
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
lane.lastExternal = null;
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
if (lane.lastExternal !== null && formValuesEqual(lane.lastExternal.values, values))
|
|
83
|
-
return;
|
|
84
|
-
lane.lastExternal = { values: snapshotFormValue(values) };
|
|
85
|
-
form.updateValues(values, { keepDirtyValues });
|
|
59
|
+
// The controller (React's formRef): created once, and replaced only when the
|
|
60
|
+
// resetKey changes by Object.is. Every method below reads the current one at
|
|
61
|
+
// call time, as React's re-created callbacks read theirs.
|
|
62
|
+
const createFor = (opts) => {
|
|
63
|
+
const { createForm = createBondedForm, resetKey: _resetKey, ...formOptions } = opts;
|
|
64
|
+
return createForm(formOptions);
|
|
86
65
|
};
|
|
87
|
-
|
|
66
|
+
let form = createFor(initial);
|
|
67
|
+
let formResetKey = initial.resetKey;
|
|
88
68
|
// Subscribed state (the controller rebuilds its state object only on change),
|
|
89
69
|
// read the way React's cached useSyncExternalStore snapshot is: one read at
|
|
90
70
|
// creation, one re-read right after subscribing, one per notification. The
|
|
91
71
|
// mirror is what asState's visible value reads (React's stateSnapshots).
|
|
92
72
|
let currentState = form.getState();
|
|
93
73
|
const state = writable(currentState);
|
|
94
|
-
|
|
95
|
-
|
|
74
|
+
let stopState = null;
|
|
75
|
+
const attachState = (controller) => {
|
|
76
|
+
stopState?.();
|
|
77
|
+
stopState = controller.subscribe(() => {
|
|
78
|
+
currentState = controller.getState();
|
|
79
|
+
state.set(currentState);
|
|
80
|
+
});
|
|
81
|
+
currentState = controller.getState();
|
|
96
82
|
state.set(currentState);
|
|
97
83
|
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
84
|
+
attachState(form);
|
|
85
|
+
onDestroy(() => {
|
|
86
|
+
stopState?.();
|
|
87
|
+
});
|
|
88
|
+
// The last external `values` applied, compared by content (React's ref): it
|
|
89
|
+
// survives an argument change; a reset may change the controller baseline
|
|
90
|
+
// without making an unchanged record external again.
|
|
91
|
+
let lastExternal = null;
|
|
92
|
+
const applyExternalValues = (opts) => {
|
|
93
|
+
const values = opts.values;
|
|
94
|
+
if (values === undefined) {
|
|
95
|
+
lastExternal = null;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (lastExternal !== null &&
|
|
99
|
+
lastExternal.form === form &&
|
|
100
|
+
formValuesEqual(lastExternal.values, values)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
lastExternal = { form, values: snapshotFormValue(values) };
|
|
104
|
+
form.updateValues(values, { keepDirtyValues: opts.resetOptions?.keepDirtyValues !== false });
|
|
105
|
+
};
|
|
106
|
+
applyExternalValues(initial);
|
|
107
|
+
// asState's [edit, fill] tuples, per controller (React's per-form Map): a
|
|
108
|
+
// replaced controller starts a fresh map, so tuples are rebuilt for it.
|
|
109
|
+
let stateTuples = new Map();
|
|
110
|
+
// What a changed options source does, on the same component: a changed
|
|
111
|
+
// resetKey replaces the controller (fresh defaults, fresh tuples, external
|
|
112
|
+
// values re-applied); otherwise a changed `values` is applied with the
|
|
113
|
+
// latest keepDirtyValues. The store calls back at once with the options the
|
|
114
|
+
// form was created with, which changes nothing.
|
|
115
|
+
const stopOptions = optionsStore.subscribe((next) => {
|
|
116
|
+
const opts = (next ?? {});
|
|
117
|
+
if (!Object.is(opts.resetKey, formResetKey)) {
|
|
118
|
+
form = createFor(opts);
|
|
119
|
+
formResetKey = opts.resetKey;
|
|
120
|
+
stateTuples = new Map();
|
|
121
|
+
attachState(form);
|
|
122
|
+
applyExternalValues(opts);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
applyExternalValues(opts);
|
|
126
|
+
});
|
|
127
|
+
onDestroy(stopOptions);
|
|
101
128
|
// Field methods
|
|
102
129
|
const register = (name, registerOptions) => form.register({ ...registerOptions, name: name });
|
|
103
130
|
const getValue = (name) => form.getValue(name);
|
|
@@ -122,7 +149,7 @@ export function useForm(options = {}) {
|
|
|
122
149
|
// The value the caller sees comes from the subscribed snapshot, not a
|
|
123
150
|
// fresh read: a fill that lands between renders must not jump ahead.
|
|
124
151
|
const visible = name === undefined ? currentState.values : currentState.values[name];
|
|
125
|
-
const existing =
|
|
152
|
+
const existing = stateTuples.get(name);
|
|
126
153
|
if (existing)
|
|
127
154
|
return [visible, ...existing];
|
|
128
155
|
const edit = (value) => {
|
|
@@ -143,15 +170,20 @@ export function useForm(options = {}) {
|
|
|
143
170
|
const next = typeof value === 'function'
|
|
144
171
|
? value(read())
|
|
145
172
|
: value;
|
|
146
|
-
form.updateValues(name === undefined ? next : { [name]: next }, { keepDirtyValues: true });
|
|
173
|
+
form.updateValues(name === undefined ? next : { [name]: next }, { keepDirtyValues: true, keepOmittedValues: name !== undefined });
|
|
147
174
|
};
|
|
148
|
-
|
|
175
|
+
stateTuples.set(name, [edit, fill]);
|
|
149
176
|
return [visible, edit, fill];
|
|
150
177
|
};
|
|
151
|
-
const handleSubmit = (onSubmit, onError) =>
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
178
|
+
const handleSubmit = (onSubmit, onError) => {
|
|
179
|
+
// The controller this runner was created for (React's per-render form): a
|
|
180
|
+
// submit runner from before a resetKey change does not call its handler.
|
|
181
|
+
const boundForm = form;
|
|
182
|
+
return form.handleSubmit((values) => {
|
|
183
|
+
if (mounted && form === boundForm)
|
|
184
|
+
return onSubmit(values);
|
|
185
|
+
}, onError);
|
|
186
|
+
};
|
|
155
187
|
return {
|
|
156
188
|
formState: { subscribe: state.subscribe },
|
|
157
189
|
isValid: derived(state, (current) => current.isValid),
|
package/dist/hooks/useHttp.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { type Readable } from 'svelte/store';
|
|
7
7
|
import type { RequestConfig } from '@molecule/app-http';
|
|
8
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
8
9
|
/** State for async HTTP operations. */
|
|
9
10
|
export interface UseHttpState<T> {
|
|
10
11
|
data: T | null;
|
|
@@ -54,17 +55,22 @@ export interface UseHttpOptions<T> extends RequestConfig {
|
|
|
54
55
|
* HTTP requests with state management, React's `useHttp` in Svelte's idiom:
|
|
55
56
|
* call it during component init under `setMoleculeContext({ http })`.
|
|
56
57
|
*
|
|
58
|
+
* Every argument may be a plain value or a readable store of one (a store
|
|
59
|
+
* holding `undefined` means the argument was omitted): `method`, `url` and the
|
|
60
|
+
* request options are read at the moment `execute` runs, so setting the `url`
|
|
61
|
+
* store retargets the next `execute` on the same component, the way React's
|
|
62
|
+
* re-render does. `immediate` is read on mount only — its request runs once per
|
|
63
|
+
* component even when the arguments change later.
|
|
64
|
+
*
|
|
57
65
|
* The newest request owns the displayed state: an older response answering
|
|
58
66
|
* after a newer `execute` or `reset` cannot touch it (callbacks still report
|
|
59
67
|
* every request's own outcome). A failure resolves `null` — it never rejects
|
|
60
68
|
* to the caller. `reset` takes ownership too, so a request it superseded
|
|
61
|
-
* cannot repopulate the state.
|
|
62
|
-
* options are read at call time. `immediate` runs its request once per host,
|
|
63
|
-
* on mount only, even when the host re-mounts the hook with new arguments.
|
|
69
|
+
* cannot repopulate the state.
|
|
64
70
|
*
|
|
65
|
-
* @param method - HTTP method.
|
|
66
|
-
* @param url - Request URL.
|
|
67
|
-
* @param options - Request options, read at call time.
|
|
71
|
+
* @param method - HTTP method (or a store of it).
|
|
72
|
+
* @param url - Request URL (or a store of it).
|
|
73
|
+
* @param options - Request options, read at call time (or a store of them).
|
|
68
74
|
* @returns Request state as stores and the controls.
|
|
69
75
|
* @example
|
|
70
76
|
* ```svelte
|
|
@@ -78,45 +84,45 @@ export interface UseHttpOptions<T> extends RequestConfig {
|
|
|
78
84
|
* {:else}<UserList users={$data} />{/if}
|
|
79
85
|
* ```
|
|
80
86
|
*/
|
|
81
|
-
export declare function useHttp<T>(method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
|
|
87
|
+
export declare function useHttp<T>(method: MaybeReadable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'>, url: MaybeReadable<string>, options?: MaybeReadable<UseHttpOptions<T> | undefined>): UseHttpResult<T>;
|
|
82
88
|
/**
|
|
83
89
|
* GET requests, React's `useGet` in Svelte's idiom.
|
|
84
90
|
*
|
|
85
|
-
* @param url - Request URL to fetch from.
|
|
86
|
-
* @param options - HTTP request options including callbacks and request config.
|
|
91
|
+
* @param url - Request URL to fetch from (or a store of it).
|
|
92
|
+
* @param options - HTTP request options including callbacks and request config (or a store of them).
|
|
87
93
|
* @returns Request state as stores and the controls.
|
|
88
94
|
*/
|
|
89
|
-
export declare function useGet<T>(url: string
|
|
95
|
+
export declare function useGet<T>(url: MaybeReadable<string>, options?: MaybeReadable<UseHttpOptions<T> | undefined>): UseHttpResult<T>;
|
|
90
96
|
/**
|
|
91
97
|
* POST requests, React's `usePost` in Svelte's idiom.
|
|
92
98
|
*
|
|
93
|
-
* @param url - Request URL to post to.
|
|
94
|
-
* @param options - HTTP request options including body data and callbacks.
|
|
99
|
+
* @param url - Request URL to post to (or a store of it).
|
|
100
|
+
* @param options - HTTP request options including body data and callbacks (or a store of them).
|
|
95
101
|
* @returns Request state as stores and the controls.
|
|
96
102
|
*/
|
|
97
|
-
export declare function usePost<T>(url: string
|
|
103
|
+
export declare function usePost<T>(url: MaybeReadable<string>, options?: MaybeReadable<UseHttpOptions<T> | undefined>): UseHttpResult<T>;
|
|
98
104
|
/**
|
|
99
105
|
* PUT requests, React's `usePut` in Svelte's idiom.
|
|
100
106
|
*
|
|
101
|
-
* @param url - Request URL to send the PUT request to.
|
|
102
|
-
* @param options - HTTP request options including body
|
|
107
|
+
* @param url - Request URL to send the PUT request to (or a store of it).
|
|
108
|
+
* @param options - HTTP request options including the replacement body (or a store of them).
|
|
103
109
|
* @returns Request state as stores and the controls.
|
|
104
110
|
*/
|
|
105
|
-
export declare function usePut<T>(url: string
|
|
111
|
+
export declare function usePut<T>(url: MaybeReadable<string>, options?: MaybeReadable<UseHttpOptions<T> | undefined>): UseHttpResult<T>;
|
|
106
112
|
/**
|
|
107
113
|
* PATCH requests, React's `usePatch` in Svelte's idiom.
|
|
108
114
|
*
|
|
109
|
-
* @param url - Request URL to send the
|
|
110
|
-
* @param options - HTTP request options including partial body
|
|
115
|
+
* @param url - Request URL to send the partial body to (or a store of it).
|
|
116
|
+
* @param options - HTTP request options including the partial body (or a store of them).
|
|
111
117
|
* @returns Request state as stores and the controls.
|
|
112
118
|
*/
|
|
113
|
-
export declare function usePatch<T>(url: string
|
|
119
|
+
export declare function usePatch<T>(url: MaybeReadable<string>, options?: MaybeReadable<UseHttpOptions<T> | undefined>): UseHttpResult<T>;
|
|
114
120
|
/**
|
|
115
121
|
* DELETE requests, React's `useDelete` in Svelte's idiom.
|
|
116
122
|
*
|
|
117
|
-
* @param url - Request URL of the resource to delete.
|
|
118
|
-
* @param options - HTTP request options including callbacks.
|
|
123
|
+
* @param url - Request URL of the resource to delete (or a store of it).
|
|
124
|
+
* @param options - HTTP request options including callbacks (or a store of them).
|
|
119
125
|
* @returns Request state as stores and the controls.
|
|
120
126
|
*/
|
|
121
|
-
export declare function useDelete<T>(url: string
|
|
127
|
+
export declare function useDelete<T>(url: MaybeReadable<string>, options?: MaybeReadable<UseHttpOptions<T> | undefined>): UseHttpResult<T>;
|
|
122
128
|
//# sourceMappingURL=useHttp.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHttp.d.ts","sourceRoot":"","sources":["../../src/hooks/useHttp.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,
|
|
1
|
+
{"version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,aAAa,CAAC,CAAC,CAAC,CAElB"}
|
package/dist/hooks/useHttp.js
CHANGED
|
@@ -4,27 +4,31 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
import { onDestroy } from 'svelte';
|
|
7
|
-
import { writable } from 'svelte/store';
|
|
7
|
+
import { get, writable } from 'svelte/store';
|
|
8
8
|
import { t } from '@molecule/app-i18n';
|
|
9
9
|
import { useHttpClient } from '../context.js';
|
|
10
10
|
import { fieldStore } from './live-store.js';
|
|
11
|
-
|
|
12
|
-
let handoff = null;
|
|
11
|
+
import { toReadable } from './maybe-readable.js';
|
|
13
12
|
/**
|
|
14
13
|
* HTTP requests with state management, React's `useHttp` in Svelte's idiom:
|
|
15
14
|
* call it during component init under `setMoleculeContext({ http })`.
|
|
16
15
|
*
|
|
16
|
+
* Every argument may be a plain value or a readable store of one (a store
|
|
17
|
+
* holding `undefined` means the argument was omitted): `method`, `url` and the
|
|
18
|
+
* request options are read at the moment `execute` runs, so setting the `url`
|
|
19
|
+
* store retargets the next `execute` on the same component, the way React's
|
|
20
|
+
* re-render does. `immediate` is read on mount only — its request runs once per
|
|
21
|
+
* component even when the arguments change later.
|
|
22
|
+
*
|
|
17
23
|
* The newest request owns the displayed state: an older response answering
|
|
18
24
|
* after a newer `execute` or `reset` cannot touch it (callbacks still report
|
|
19
25
|
* every request's own outcome). A failure resolves `null` — it never rejects
|
|
20
26
|
* to the caller. `reset` takes ownership too, so a request it superseded
|
|
21
|
-
* cannot repopulate the state.
|
|
22
|
-
* options are read at call time. `immediate` runs its request once per host,
|
|
23
|
-
* on mount only, even when the host re-mounts the hook with new arguments.
|
|
27
|
+
* cannot repopulate the state.
|
|
24
28
|
*
|
|
25
|
-
* @param method - HTTP method.
|
|
26
|
-
* @param url - Request URL.
|
|
27
|
-
* @param options - Request options, read at call time.
|
|
29
|
+
* @param method - HTTP method (or a store of it).
|
|
30
|
+
* @param url - Request URL (or a store of it).
|
|
31
|
+
* @param options - Request options, read at call time (or a store of them).
|
|
28
32
|
* @returns Request state as stores and the controls.
|
|
29
33
|
* @example
|
|
30
34
|
* ```svelte
|
|
@@ -40,110 +44,78 @@ let handoff = null;
|
|
|
40
44
|
*/
|
|
41
45
|
export function useHttp(method, url, options) {
|
|
42
46
|
const client = useHttpClient();
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
if (adopted)
|
|
57
|
-
adopted.dead = false;
|
|
58
|
-
// The displayed state starts from the lane's last published state (what
|
|
59
|
-
// React's single instance kept), or from the initial one; the lane mirrors
|
|
60
|
-
// exactly what the store shows, so a replacing mount reads the same values.
|
|
61
|
-
const initialState = {
|
|
62
|
-
data: adopted ? lane.state.data : null,
|
|
63
|
-
loading: adopted ? lane.state.loading : immediate,
|
|
64
|
-
error: adopted ? lane.state.error : null,
|
|
65
|
-
};
|
|
66
|
-
const state = writable(initialState);
|
|
67
|
-
lane.state = { ...initialState };
|
|
68
|
-
lane.sink = {
|
|
69
|
-
patch: (next) => {
|
|
70
|
-
if (next.data !== undefined) {
|
|
71
|
-
lane.state.data = next.data;
|
|
72
|
-
state.update((current) => ({ ...current, data: next.data }));
|
|
73
|
-
}
|
|
74
|
-
// Read once into locals: the narrowed type is lost inside the updater
|
|
75
|
-
// closure (`state.update` runs later).
|
|
76
|
-
const loading = next.loading;
|
|
77
|
-
if (loading !== undefined) {
|
|
78
|
-
lane.state.loading = loading;
|
|
79
|
-
state.update((current) => ({ ...current, loading }));
|
|
80
|
-
}
|
|
81
|
-
const error = next.error;
|
|
82
|
-
if (error !== undefined) {
|
|
83
|
-
lane.state.error = error;
|
|
84
|
-
state.update((current) => ({ ...current, error }));
|
|
85
|
-
}
|
|
86
|
-
},
|
|
47
|
+
const methodStore = toReadable(method);
|
|
48
|
+
const urlStore = toReadable(url);
|
|
49
|
+
const optionsStore = toReadable(options);
|
|
50
|
+
const immediate = get(optionsStore)?.immediate ?? false;
|
|
51
|
+
const state = writable({
|
|
52
|
+
data: null,
|
|
53
|
+
loading: immediate,
|
|
54
|
+
error: null,
|
|
55
|
+
});
|
|
56
|
+
/** Applies one state patch; `undefined` fields are left alone. */
|
|
57
|
+
const patch = (next) => {
|
|
58
|
+
state.update((current) => ({ ...current, ...next }));
|
|
87
59
|
};
|
|
60
|
+
// React's mounted flag: false once the component is gone, so a late answer
|
|
61
|
+
// settles quietly (no state write, no callback) as it does there.
|
|
62
|
+
let mounted = true;
|
|
88
63
|
onDestroy(() => {
|
|
89
|
-
|
|
90
|
-
lane.dead = true;
|
|
91
|
-
// Offer the lane to a mount in the same turn (React's re-render); the
|
|
92
|
-
// timer clears it when no replacement adopted, so a mount after a settled
|
|
93
|
-
// teardown starts fresh.
|
|
94
|
-
handoff = lane;
|
|
95
|
-
const offered = lane;
|
|
96
|
-
setTimeout(() => {
|
|
97
|
-
if (handoff === offered)
|
|
98
|
-
handoff = null;
|
|
99
|
-
}, 0);
|
|
64
|
+
mounted = false;
|
|
100
65
|
});
|
|
66
|
+
// The newest request owns the state. Without this, an older request that
|
|
67
|
+
// answers after a newer one (a slow first load, then a refetch) overwrote the
|
|
68
|
+
// newer data and cleared `loading` while the newer request was still in flight.
|
|
69
|
+
// `reset` takes ownership too, so a request it superseded cannot repopulate.
|
|
70
|
+
let latestRequest = 0;
|
|
101
71
|
const execute = async () => {
|
|
102
|
-
if (
|
|
72
|
+
if (!mounted)
|
|
103
73
|
return null;
|
|
104
74
|
// Options describe the request being sent, read at call time; method/url/
|
|
105
75
|
// client decide WHICH request execute performs.
|
|
106
|
-
const
|
|
107
|
-
const
|
|
76
|
+
const currentMethod = get(methodStore);
|
|
77
|
+
const currentUrl = get(urlStore);
|
|
78
|
+
const { immediate: _immediate, onSuccess, onError, ...requestConfig } = get(optionsStore) ?? {};
|
|
79
|
+
const request = ++latestRequest;
|
|
108
80
|
// Callbacks report THIS request's outcome (a submit's onSuccess still
|
|
109
81
|
// runs); only the newest request may change what the hook shows.
|
|
110
|
-
const ownsState = () =>
|
|
111
|
-
|
|
82
|
+
const ownsState = () => mounted && request === latestRequest;
|
|
83
|
+
patch({ loading: true, error: null });
|
|
112
84
|
try {
|
|
113
85
|
let response;
|
|
114
|
-
switch (
|
|
86
|
+
switch (currentMethod) {
|
|
115
87
|
case 'GET':
|
|
116
|
-
response = await client.get(
|
|
88
|
+
response = await client.get(currentUrl, requestConfig);
|
|
117
89
|
break;
|
|
118
90
|
case 'POST':
|
|
119
|
-
response = await client.post(
|
|
91
|
+
response = await client.post(currentUrl, requestConfig.data, requestConfig);
|
|
120
92
|
break;
|
|
121
93
|
case 'PUT':
|
|
122
|
-
response = await client.put(
|
|
94
|
+
response = await client.put(currentUrl, requestConfig.data, requestConfig);
|
|
123
95
|
break;
|
|
124
96
|
case 'PATCH':
|
|
125
|
-
response = await client.patch(
|
|
97
|
+
response = await client.patch(currentUrl, requestConfig.data, requestConfig);
|
|
126
98
|
break;
|
|
127
99
|
case 'DELETE':
|
|
128
|
-
response = await client.delete(
|
|
100
|
+
response = await client.delete(currentUrl, requestConfig);
|
|
129
101
|
break;
|
|
130
102
|
default:
|
|
131
|
-
throw new Error(t('svelte.error.unsupportedMethod', { method }, {
|
|
103
|
+
throw new Error(t('svelte.error.unsupportedMethod', { method: currentMethod }, {
|
|
132
104
|
defaultValue: 'Unsupported method: {{method}}',
|
|
133
105
|
}));
|
|
134
106
|
}
|
|
135
|
-
if (
|
|
107
|
+
if (mounted) {
|
|
136
108
|
if (ownsState())
|
|
137
|
-
|
|
109
|
+
patch({ data: response.data, loading: false, error: null });
|
|
138
110
|
onSuccess?.(response.data);
|
|
139
111
|
}
|
|
140
112
|
return response.data;
|
|
141
113
|
}
|
|
142
114
|
catch (error) {
|
|
143
115
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
144
|
-
if (
|
|
116
|
+
if (mounted) {
|
|
145
117
|
if (ownsState())
|
|
146
|
-
|
|
118
|
+
patch({ data: null, loading: false, error: err });
|
|
147
119
|
onError?.(err);
|
|
148
120
|
}
|
|
149
121
|
return null;
|
|
@@ -151,13 +123,12 @@ export function useHttp(method, url, options) {
|
|
|
151
123
|
};
|
|
152
124
|
const reset = () => {
|
|
153
125
|
// reset takes ownership: a request it superseded cannot repopulate.
|
|
154
|
-
|
|
155
|
-
|
|
126
|
+
latestRequest++;
|
|
127
|
+
patch({ data: null, loading: false, error: null });
|
|
156
128
|
};
|
|
157
|
-
// Execute immediately if requested — mount only, once per
|
|
158
|
-
// historical contract.
|
|
159
|
-
if (immediate
|
|
160
|
-
lane.ranImmediate = true;
|
|
129
|
+
// Execute immediately if requested — mount only, once per component, the
|
|
130
|
+
// option's historical contract: an argument change never repeats it.
|
|
131
|
+
if (immediate) {
|
|
161
132
|
void execute();
|
|
162
133
|
}
|
|
163
134
|
return {
|
|
@@ -171,8 +142,8 @@ export function useHttp(method, url, options) {
|
|
|
171
142
|
/**
|
|
172
143
|
* GET requests, React's `useGet` in Svelte's idiom.
|
|
173
144
|
*
|
|
174
|
-
* @param url - Request URL to fetch from.
|
|
175
|
-
* @param options - HTTP request options including callbacks and request config.
|
|
145
|
+
* @param url - Request URL to fetch from (or a store of it).
|
|
146
|
+
* @param options - HTTP request options including callbacks and request config (or a store of them).
|
|
176
147
|
* @returns Request state as stores and the controls.
|
|
177
148
|
*/
|
|
178
149
|
export function useGet(url, options) {
|
|
@@ -181,8 +152,8 @@ export function useGet(url, options) {
|
|
|
181
152
|
/**
|
|
182
153
|
* POST requests, React's `usePost` in Svelte's idiom.
|
|
183
154
|
*
|
|
184
|
-
* @param url - Request URL to post to.
|
|
185
|
-
* @param options - HTTP request options including body data and callbacks.
|
|
155
|
+
* @param url - Request URL to post to (or a store of it).
|
|
156
|
+
* @param options - HTTP request options including body data and callbacks (or a store of them).
|
|
186
157
|
* @returns Request state as stores and the controls.
|
|
187
158
|
*/
|
|
188
159
|
export function usePost(url, options) {
|
|
@@ -191,8 +162,8 @@ export function usePost(url, options) {
|
|
|
191
162
|
/**
|
|
192
163
|
* PUT requests, React's `usePut` in Svelte's idiom.
|
|
193
164
|
*
|
|
194
|
-
* @param url - Request URL to send the PUT request to.
|
|
195
|
-
* @param options - HTTP request options including body
|
|
165
|
+
* @param url - Request URL to send the PUT request to (or a store of it).
|
|
166
|
+
* @param options - HTTP request options including the replacement body (or a store of them).
|
|
196
167
|
* @returns Request state as stores and the controls.
|
|
197
168
|
*/
|
|
198
169
|
export function usePut(url, options) {
|
|
@@ -201,8 +172,8 @@ export function usePut(url, options) {
|
|
|
201
172
|
/**
|
|
202
173
|
* PATCH requests, React's `usePatch` in Svelte's idiom.
|
|
203
174
|
*
|
|
204
|
-
* @param url - Request URL to send the
|
|
205
|
-
* @param options - HTTP request options including partial body
|
|
175
|
+
* @param url - Request URL to send the partial body to (or a store of it).
|
|
176
|
+
* @param options - HTTP request options including the partial body (or a store of them).
|
|
206
177
|
* @returns Request state as stores and the controls.
|
|
207
178
|
*/
|
|
208
179
|
export function usePatch(url, options) {
|
|
@@ -211,8 +182,8 @@ export function usePatch(url, options) {
|
|
|
211
182
|
/**
|
|
212
183
|
* DELETE requests, React's `useDelete` in Svelte's idiom.
|
|
213
184
|
*
|
|
214
|
-
* @param url - Request URL of the resource to delete.
|
|
215
|
-
* @param options - HTTP request options including callbacks.
|
|
185
|
+
* @param url - Request URL of the resource to delete (or a store of it).
|
|
186
|
+
* @param options - HTTP request options including callbacks (or a store of them).
|
|
216
187
|
* @returns Request state as stores and the controls.
|
|
217
188
|
*/
|
|
218
189
|
export function useDelete(url, options) {
|
package/dist/hooks/useOAuth.d.ts
CHANGED
|
@@ -27,12 +27,19 @@
|
|
|
27
27
|
*
|
|
28
28
|
* @module
|
|
29
29
|
*/
|
|
30
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
30
31
|
/**
|
|
31
32
|
* Return type for useOAuth hook.
|
|
32
33
|
*/
|
|
33
34
|
export interface UseOAuthReturn {
|
|
34
|
-
/**
|
|
35
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The configured provider names (React's `string[]`), read live: each read
|
|
37
|
+
* returns the list the current config holds, and a read in markup or
|
|
38
|
+
* `$derived` re-runs when a config store changes. Read it off the result
|
|
39
|
+
* (`oauth.providers`); a destructured copy keeps the list of the moment it
|
|
40
|
+
* was taken.
|
|
41
|
+
*/
|
|
42
|
+
readonly providers: string[];
|
|
36
43
|
/** Builds the provider's initiation URL (provider encoded as one path segment). */
|
|
37
44
|
getOAuthUrl: (provider: string) => string;
|
|
38
45
|
/** Full-page redirect to the provider (default). The opener page navigates away. */
|
|
@@ -78,19 +85,28 @@ export interface UseOAuthConfig {
|
|
|
78
85
|
* when the callback is running inside a popup we opened, relaying the result
|
|
79
86
|
* to the opener instead.
|
|
80
87
|
*
|
|
81
|
-
*
|
|
88
|
+
* The config may be a plain value or a readable store of one (a store holding
|
|
89
|
+
* `undefined` means no config): every field — the provider list, the URL
|
|
90
|
+
* pieces, the callbacks, the `navigate` option — is read at the moment it is
|
|
91
|
+
* used, so setting the store retargets the next `getOAuthUrl`/`redirect`/popup
|
|
92
|
+
* flow on the same component, the way React's re-render does. `providers` is
|
|
93
|
+
* React's `string[]`, read live (see {@link UseOAuthReturn.providers}). The
|
|
94
|
+
* callback exchange, when it finds a `code` in the URL, runs once with the
|
|
95
|
+
* config the hook was called with.
|
|
96
|
+
*
|
|
97
|
+
* @param config - Optional OAuth configuration override (or a store of it).
|
|
82
98
|
* @returns OAuth helpers: providers, getOAuthUrl, redirect, and loginViaPopup.
|
|
83
99
|
* @example
|
|
84
100
|
* ```svelte
|
|
85
101
|
* <script>
|
|
86
102
|
* import { useOAuth } from '@molecule/app-svelte'
|
|
87
|
-
* const
|
|
103
|
+
* const oauth = useOAuth()
|
|
88
104
|
* </script>
|
|
89
105
|
*
|
|
90
|
-
* {#each providers as provider}
|
|
91
|
-
* <button onclick={() => loginViaPopup(provider)}>Login with {provider}</button>
|
|
106
|
+
* {#each oauth.providers as provider}
|
|
107
|
+
* <button onclick={() => oauth.loginViaPopup(provider)}>Login with {provider}</button>
|
|
92
108
|
* {/each}
|
|
93
109
|
* ```
|
|
94
110
|
*/
|
|
95
|
-
export declare function useOAuth(config?: UseOAuthConfig): UseOAuthReturn;
|
|
111
|
+
export declare function useOAuth(config?: MaybeReadable<UseOAuthConfig | undefined>): UseOAuthReturn;
|
|
96
112
|
//# sourceMappingURL=useOAuth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useOAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useOAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;
|
|
1
|
+
{"version":3,"file":"useOAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useOAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AASH,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAAqB,CAAA;AAiCpE;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAA;IAC5B,mFAAmF;IACnF,WAAW,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAA;IACzC,oFAAoF;IACpF,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;IACpC;;;;;OAKG;IACH,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;CAC1C;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,0EAA0E;IAC1E,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,2DAA2D;IAC3D,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,kFAAkF;IAClF,aAAa,CAAC,EAAE,M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