@bquery/bquery 1.7.0 → 1.8.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 +760 -716
- package/dist/{a11y-C5QOVvRn.js → a11y-DVBCy09c.js} +3 -3
- package/dist/a11y-DVBCy09c.js.map +1 -0
- package/dist/a11y.es.mjs +1 -1
- package/dist/component/library.d.ts.map +1 -1
- package/dist/{component-CuuTijA6.js → component-L3-JfOFz.js} +5 -5
- package/dist/component-L3-JfOFz.js.map +1 -0
- package/dist/component.es.mjs +1 -1
- package/dist/{config-BW35FKuA.js → config-DhT9auRm.js} +1 -1
- package/dist/{config-BW35FKuA.js.map → config-DhT9auRm.js.map} +1 -1
- package/dist/{constraints-3lV9yyBw.js → constraints-D5RHQLmP.js} +1 -1
- package/dist/constraints-D5RHQLmP.js.map +1 -0
- package/dist/core/collection.d.ts +86 -0
- package/dist/core/collection.d.ts.map +1 -1
- package/dist/core/element.d.ts +28 -0
- package/dist/core/element.d.ts.map +1 -1
- package/dist/core/shared.d.ts +6 -0
- package/dist/core/shared.d.ts.map +1 -1
- package/dist/core-DdtZHzsS.js +168 -0
- package/dist/core-DdtZHzsS.js.map +1 -0
- package/dist/{core-Cjl7GUu8.js → core-EMYSLzaT.js} +289 -259
- package/dist/core-EMYSLzaT.js.map +1 -0
- package/dist/core.es.mjs +48 -47
- package/dist/{custom-directives-7wAShnnd.js → custom-directives-Dr4C5lVV.js} +1 -1
- package/dist/custom-directives-Dr4C5lVV.js.map +1 -0
- package/dist/{devtools-D2fQLhDN.js → devtools-BhB2iDPT.js} +2 -2
- package/dist/devtools-BhB2iDPT.js.map +1 -0
- package/dist/devtools.es.mjs +1 -1
- package/dist/{dnd-B8EgyzaI.js → dnd-NwZBYh4l.js} +1 -1
- package/dist/dnd-NwZBYh4l.js.map +1 -0
- package/dist/dnd.es.mjs +1 -1
- package/dist/{env-NeVmr4Gf.js → env-CTdvLaH2.js} +1 -1
- package/dist/env-CTdvLaH2.js.map +1 -0
- package/dist/forms/create-form.d.ts.map +1 -1
- package/dist/forms/index.d.ts +3 -2
- package/dist/forms/index.d.ts.map +1 -1
- package/dist/forms/types.d.ts +46 -0
- package/dist/forms/types.d.ts.map +1 -1
- package/dist/forms/use-field.d.ts +34 -0
- package/dist/forms/use-field.d.ts.map +1 -0
- package/dist/forms/validators.d.ts +25 -0
- package/dist/forms/validators.d.ts.map +1 -1
- package/dist/forms-UcRHsYxC.js +227 -0
- package/dist/forms-UcRHsYxC.js.map +1 -0
- package/dist/forms.es.mjs +14 -12
- package/dist/full.d.ts +17 -26
- package/dist/full.d.ts.map +1 -1
- package/dist/full.es.mjs +206 -181
- package/dist/full.iife.js +33 -33
- package/dist/full.iife.js.map +1 -1
- package/dist/full.umd.js +33 -33
- package/dist/full.umd.js.map +1 -1
- package/dist/function-Cybd57JV.js +33 -0
- package/dist/function-Cybd57JV.js.map +1 -0
- package/dist/{i18n-BnnhTFOS.js → i18n-kuF6Ekj6.js} +3 -3
- package/dist/i18n-kuF6Ekj6.js.map +1 -0
- package/dist/i18n.es.mjs +1 -1
- package/dist/index.es.mjs +251 -228
- package/dist/media/breakpoints.d.ts.map +1 -1
- package/dist/media/types.d.ts +2 -2
- package/dist/media/types.d.ts.map +1 -1
- package/dist/{media-Di2Ta22s.js → media-i-fB5WxI.js} +3 -3
- package/dist/media-i-fB5WxI.js.map +1 -0
- package/dist/media.es.mjs +1 -1
- package/dist/{motion-qPj_TYGv.js → motion-BJsAuULb.js} +2 -2
- package/dist/motion-BJsAuULb.js.map +1 -0
- package/dist/motion.es.mjs +1 -1
- package/dist/{mount-SM07RUa6.js → mount-B4Y8bk8Z.js} +5 -5
- package/dist/mount-B4Y8bk8Z.js.map +1 -0
- package/dist/{platform-CPbCprb6.js → platform-Dw2gE3zI.js} +3 -3
- package/dist/{platform-CPbCprb6.js.map → platform-Dw2gE3zI.js.map} +1 -1
- package/dist/platform.es.mjs +2 -2
- package/dist/plugin/registry.d.ts.map +1 -1
- package/dist/{plugin-cPoOHFLY.js → plugin-C2WuC8SF.js} +20 -18
- package/dist/plugin-C2WuC8SF.js.map +1 -0
- package/dist/plugin.es.mjs +1 -1
- package/dist/reactive/async-data.d.ts +28 -3
- package/dist/reactive/async-data.d.ts.map +1 -1
- package/dist/reactive/computed.d.ts +3 -0
- package/dist/reactive/computed.d.ts.map +1 -1
- package/dist/reactive/effect.d.ts +3 -0
- package/dist/reactive/effect.d.ts.map +1 -1
- package/dist/reactive/http.d.ts +194 -0
- package/dist/reactive/http.d.ts.map +1 -0
- package/dist/reactive/index.d.ts +2 -2
- package/dist/reactive/index.d.ts.map +1 -1
- package/dist/reactive/pagination.d.ts +126 -0
- package/dist/reactive/pagination.d.ts.map +1 -0
- package/dist/reactive/polling.d.ts +55 -0
- package/dist/reactive/polling.d.ts.map +1 -0
- package/dist/reactive/readonly.d.ts +20 -1
- package/dist/reactive/readonly.d.ts.map +1 -1
- package/dist/reactive/rest.d.ts +293 -0
- package/dist/reactive/rest.d.ts.map +1 -0
- package/dist/reactive/scope.d.ts +140 -0
- package/dist/reactive/scope.d.ts.map +1 -0
- package/dist/reactive/signal.d.ts +16 -2
- package/dist/reactive/signal.d.ts.map +1 -1
- package/dist/reactive/to-value.d.ts +57 -0
- package/dist/reactive/to-value.d.ts.map +1 -0
- package/dist/reactive/websocket.d.ts +285 -0
- package/dist/reactive/websocket.d.ts.map +1 -0
- package/dist/reactive-DwkhUJfP.js +1148 -0
- package/dist/reactive-DwkhUJfP.js.map +1 -0
- package/dist/reactive.es.mjs +38 -19
- package/dist/{registry-CWf368tT.js → registry-B08iilIh.js} +1 -1
- package/dist/{registry-CWf368tT.js.map → registry-B08iilIh.js.map} +1 -1
- package/dist/router/constraints.d.ts.map +1 -1
- package/dist/router/index.d.ts +1 -1
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/state.d.ts +25 -2
- package/dist/router/state.d.ts.map +1 -1
- package/dist/router-CQikC9Ed.js +492 -0
- package/dist/router-CQikC9Ed.js.map +1 -0
- package/dist/router.es.mjs +9 -8
- package/dist/ssr/hydrate.d.ts.map +1 -1
- package/dist/{ssr-B2qd_WBB.js → ssr-_dAcGdzu.js} +4 -4
- package/dist/ssr-_dAcGdzu.js.map +1 -0
- package/dist/ssr.es.mjs +1 -1
- package/dist/store/persisted.d.ts.map +1 -1
- package/dist/{store-DWpyH6p5.js → store-Cb3gPRve.js} +7 -7
- package/dist/store-Cb3gPRve.js.map +1 -0
- package/dist/store.es.mjs +2 -2
- package/dist/storybook.es.mjs.map +1 -1
- package/dist/{testing-CsqjNUyy.js → testing-C5Sjfsna.js} +8 -8
- package/dist/testing-C5Sjfsna.js.map +1 -0
- package/dist/testing.es.mjs +1 -1
- package/dist/{type-guards-Do9DWgNp.js → type-guards-BMX2c0LP.js} +1 -1
- package/dist/{type-guards-Do9DWgNp.js.map → type-guards-BMX2c0LP.js.map} +1 -1
- package/dist/untrack-D0fnO5k2.js +36 -0
- package/dist/untrack-D0fnO5k2.js.map +1 -0
- package/dist/view/custom-directives.d.ts.map +1 -1
- package/dist/view.es.mjs +4 -4
- package/package.json +177 -177
- package/src/a11y/announce.ts +131 -131
- package/src/a11y/audit.ts +314 -314
- package/src/a11y/index.ts +68 -68
- package/src/a11y/media-preferences.ts +255 -255
- package/src/a11y/roving-tab-index.ts +164 -164
- package/src/a11y/skip-link.ts +255 -255
- package/src/a11y/trap-focus.ts +184 -184
- package/src/a11y/types.ts +183 -183
- package/src/component/component.ts +599 -599
- package/src/component/html.ts +153 -153
- package/src/component/index.ts +52 -52
- package/src/component/library.ts +540 -542
- package/src/component/scope.ts +212 -212
- package/src/component/types.ts +310 -310
- package/src/core/collection.ts +876 -707
- package/src/core/element.ts +1015 -981
- package/src/core/env.ts +60 -60
- package/src/core/index.ts +49 -49
- package/src/core/shared.ts +77 -62
- package/src/core/utils/index.ts +148 -148
- package/src/devtools/devtools.ts +410 -410
- package/src/devtools/index.ts +48 -48
- package/src/devtools/types.ts +104 -104
- package/src/dnd/draggable.ts +296 -296
- package/src/dnd/droppable.ts +228 -228
- package/src/dnd/index.ts +62 -62
- package/src/dnd/sortable.ts +307 -307
- package/src/dnd/types.ts +293 -293
- package/src/forms/create-form.ts +320 -278
- package/src/forms/index.ts +70 -65
- package/src/forms/types.ts +203 -154
- package/src/forms/use-field.ts +231 -0
- package/src/forms/validators.ts +294 -265
- package/src/full.ts +554 -480
- package/src/i18n/formatting.ts +67 -67
- package/src/i18n/i18n.ts +200 -200
- package/src/i18n/index.ts +67 -67
- package/src/i18n/translate.ts +182 -182
- package/src/i18n/types.ts +171 -171
- package/src/index.ts +108 -108
- package/src/media/battery.ts +116 -116
- package/src/media/breakpoints.ts +129 -131
- package/src/media/clipboard.ts +80 -80
- package/src/media/device-sensors.ts +158 -158
- package/src/media/geolocation.ts +119 -119
- package/src/media/index.ts +76 -76
- package/src/media/media-query.ts +92 -92
- package/src/media/network.ts +115 -115
- package/src/media/types.ts +177 -177
- package/src/media/viewport.ts +84 -84
- package/src/motion/index.ts +57 -57
- package/src/motion/morph.ts +151 -151
- package/src/motion/parallax.ts +120 -120
- package/src/motion/reduced-motion.ts +66 -66
- package/src/motion/types.ts +271 -271
- package/src/motion/typewriter.ts +164 -164
- package/src/plugin/index.ts +37 -37
- package/src/plugin/registry.ts +284 -269
- package/src/plugin/types.ts +137 -137
- package/src/reactive/async-data.ts +250 -29
- package/src/reactive/computed.ts +144 -130
- package/src/reactive/effect.ts +29 -6
- package/src/reactive/http.ts +790 -0
- package/src/reactive/index.ts +60 -0
- package/src/reactive/pagination.ts +317 -0
- package/src/reactive/polling.ts +179 -0
- package/src/reactive/readonly.ts +52 -8
- package/src/reactive/rest.ts +859 -0
- package/src/reactive/scope.ts +276 -0
- package/src/reactive/signal.ts +61 -1
- package/src/reactive/to-value.ts +71 -0
- package/src/reactive/websocket.ts +849 -0
- package/src/router/bq-link.ts +279 -279
- package/src/router/constraints.ts +204 -201
- package/src/router/index.ts +49 -49
- package/src/router/match.ts +312 -312
- package/src/router/path-pattern.ts +52 -52
- package/src/router/query.ts +38 -38
- package/src/router/router.ts +421 -402
- package/src/router/state.ts +51 -3
- package/src/router/types.ts +139 -139
- package/src/router/use-route.ts +68 -68
- package/src/router/utils.ts +157 -157
- package/src/security/index.ts +12 -12
- package/src/ssr/hydrate.ts +84 -82
- package/src/ssr/index.ts +70 -70
- package/src/ssr/render.ts +508 -508
- package/src/ssr/serialize.ts +296 -296
- package/src/ssr/types.ts +81 -81
- package/src/store/create-store.ts +467 -467
- package/src/store/index.ts +27 -27
- package/src/store/persisted.ts +245 -249
- package/src/store/types.ts +247 -247
- package/src/store/utils.ts +135 -135
- package/src/storybook/index.ts +480 -480
- package/src/testing/index.ts +42 -42
- package/src/testing/testing.ts +593 -593
- package/src/testing/types.ts +170 -170
- package/src/view/custom-directives.ts +28 -30
- package/src/view/evaluate.ts +292 -292
- package/src/view/process.ts +108 -108
- package/dist/a11y-C5QOVvRn.js.map +0 -1
- package/dist/component-CuuTijA6.js.map +0 -1
- package/dist/constraints-3lV9yyBw.js.map +0 -1
- package/dist/core-Cjl7GUu8.js.map +0 -1
- package/dist/core-DnlyjbF2.js +0 -112
- package/dist/core-DnlyjbF2.js.map +0 -1
- package/dist/custom-directives-7wAShnnd.js.map +0 -1
- package/dist/devtools-D2fQLhDN.js.map +0 -1
- package/dist/dnd-B8EgyzaI.js.map +0 -1
- package/dist/env-NeVmr4Gf.js.map +0 -1
- package/dist/forms-C3yovgH9.js +0 -141
- package/dist/forms-C3yovgH9.js.map +0 -1
- package/dist/i18n-BnnhTFOS.js.map +0 -1
- package/dist/media-Di2Ta22s.js.map +0 -1
- package/dist/motion-qPj_TYGv.js.map +0 -1
- package/dist/mount-SM07RUa6.js.map +0 -1
- package/dist/plugin-cPoOHFLY.js.map +0 -1
- package/dist/reactive-Cfv0RK6x.js +0 -233
- package/dist/reactive-Cfv0RK6x.js.map +0 -1
- package/dist/router-BrthaP_z.js +0 -473
- package/dist/router-BrthaP_z.js.map +0 -1
- package/dist/ssr-B2qd_WBB.js.map +0 -1
- package/dist/store-DWpyH6p5.js.map +0 -1
- package/dist/testing-CsqjNUyy.js.map +0 -1
- package/dist/untrack-DJVQQ2WM.js +0 -33
- package/dist/untrack-DJVQQ2WM.js.map +0 -1
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* REST resource composable for CRUD operations with optimistic updates,
|
|
3
|
+
* form submission, and reactive caching built on the bQuery fetch layer.
|
|
4
|
+
*
|
|
5
|
+
* @module bquery/reactive
|
|
6
|
+
*/
|
|
7
|
+
import { Signal } from './core';
|
|
8
|
+
import { type AsyncDataStatus, type UseFetchOptions } from './async-data';
|
|
9
|
+
import { type HttpClient, type HttpRequestConfig, type HttpResponse } from './http';
|
|
10
|
+
/** HTTP method shortcuts available on a resource. */
|
|
11
|
+
export interface ResourceActions<T> {
|
|
12
|
+
/** Fetch the resource (GET). */
|
|
13
|
+
fetch: () => Promise<T | undefined>;
|
|
14
|
+
/** Create a new item (POST). */
|
|
15
|
+
create: (body: Partial<T> | Record<string, unknown>) => Promise<T | undefined>;
|
|
16
|
+
/** Replace the resource (PUT). */
|
|
17
|
+
update: (body: Partial<T> | Record<string, unknown>) => Promise<T | undefined>;
|
|
18
|
+
/** Partially update the resource (PATCH). */
|
|
19
|
+
patch: (body: Partial<T> | Record<string, unknown>) => Promise<T | undefined>;
|
|
20
|
+
/** Delete the resource (DELETE). */
|
|
21
|
+
remove: () => Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Options for `useResource()`. */
|
|
24
|
+
export interface UseResourceOptions<T = unknown> extends Omit<UseFetchOptions<T>, 'method' | 'body'> {
|
|
25
|
+
/** Enable optimistic updates for mutating operations (default: false). */
|
|
26
|
+
optimistic?: boolean;
|
|
27
|
+
/** Called after any successful mutation (create / update / patch / remove). */
|
|
28
|
+
onMutationSuccess?: (data: T | undefined, action: string) => void;
|
|
29
|
+
/** Called after a failed mutation, receives the error and action name. */
|
|
30
|
+
onMutationError?: (error: Error, action: string) => void;
|
|
31
|
+
}
|
|
32
|
+
/** Return value of `useResource()`. */
|
|
33
|
+
export interface UseResourceReturn<T> {
|
|
34
|
+
/** Reactive resource data. */
|
|
35
|
+
data: Signal<T | undefined>;
|
|
36
|
+
/** Last error. */
|
|
37
|
+
error: Signal<Error | null>;
|
|
38
|
+
/** Lifecycle status for the initial fetch. */
|
|
39
|
+
status: Signal<AsyncDataStatus>;
|
|
40
|
+
/** Whether the initial fetch is pending. */
|
|
41
|
+
pending: {
|
|
42
|
+
readonly value: boolean;
|
|
43
|
+
peek(): boolean;
|
|
44
|
+
};
|
|
45
|
+
/** Whether any mutation is in progress. */
|
|
46
|
+
isMutating: {
|
|
47
|
+
readonly value: boolean;
|
|
48
|
+
peek(): boolean;
|
|
49
|
+
};
|
|
50
|
+
/** CRUD actions. */
|
|
51
|
+
actions: ResourceActions<T>;
|
|
52
|
+
/** Refresh the resource (re-GET). */
|
|
53
|
+
refresh: () => Promise<T | undefined>;
|
|
54
|
+
/** Clear data, error, and status. */
|
|
55
|
+
clear: () => void;
|
|
56
|
+
/** Dispose all reactive state and prevent future operations. */
|
|
57
|
+
dispose: () => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Reactive REST resource composable providing CRUD operations.
|
|
61
|
+
*
|
|
62
|
+
* Binds a base URL to a resource and exposes `fetch`, `create`, `update`,
|
|
63
|
+
* `patch`, and `remove` helpers with optional optimistic updates.
|
|
64
|
+
*
|
|
65
|
+
* @template T - Resource data type
|
|
66
|
+
* @param url - Resource endpoint URL or getter
|
|
67
|
+
* @param options - Fetch and resource options
|
|
68
|
+
* @returns Reactive resource state with CRUD actions
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { useResource } from '@bquery/bquery/reactive';
|
|
73
|
+
*
|
|
74
|
+
* const user = useResource<User>('/api/users/1', {
|
|
75
|
+
* baseUrl: 'https://api.example.com',
|
|
76
|
+
* optimistic: true,
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* // Read
|
|
80
|
+
* await user.actions.fetch();
|
|
81
|
+
*
|
|
82
|
+
* // Update
|
|
83
|
+
* await user.actions.patch({ name: 'Ada' });
|
|
84
|
+
*
|
|
85
|
+
* // Delete
|
|
86
|
+
* await user.actions.remove();
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare const useResource: <T = unknown>(url: string | URL | (() => string | URL), options?: UseResourceOptions<T>) => UseResourceReturn<T>;
|
|
90
|
+
/** Options for `useSubmit()`. */
|
|
91
|
+
export interface UseSubmitOptions<TResponse = unknown> extends Omit<UseFetchOptions<TResponse>, 'body' | 'immediate'> {
|
|
92
|
+
/** HTTP method (default: `'POST'`). */
|
|
93
|
+
method?: string;
|
|
94
|
+
}
|
|
95
|
+
/** Return value of `useSubmit()`. */
|
|
96
|
+
export interface UseSubmitReturn<TResponse = unknown> {
|
|
97
|
+
/** Last response data. */
|
|
98
|
+
data: Signal<TResponse | undefined>;
|
|
99
|
+
/** Last error. */
|
|
100
|
+
error: Signal<Error | null>;
|
|
101
|
+
/** Current status. */
|
|
102
|
+
status: Signal<AsyncDataStatus>;
|
|
103
|
+
/** Whether the submission is pending. */
|
|
104
|
+
pending: {
|
|
105
|
+
readonly value: boolean;
|
|
106
|
+
peek(): boolean;
|
|
107
|
+
};
|
|
108
|
+
/** Submit data to the endpoint. */
|
|
109
|
+
submit: (body: Record<string, unknown> | FormData | BodyInit) => Promise<TResponse | undefined>;
|
|
110
|
+
/** Reset state. */
|
|
111
|
+
clear: () => void;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Reactive form submission composable.
|
|
115
|
+
*
|
|
116
|
+
* Provides a `submit()` function that sends data to an endpoint with
|
|
117
|
+
* reactive status, data, and error signals.
|
|
118
|
+
*
|
|
119
|
+
* @template TResponse - Response data type
|
|
120
|
+
* @param url - Submission endpoint URL
|
|
121
|
+
* @param options - Fetch options (method defaults to POST)
|
|
122
|
+
* @returns Reactive submission state with `submit()` and `clear()`
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* import { useSubmit } from '@bquery/bquery/reactive';
|
|
127
|
+
*
|
|
128
|
+
* const form = useSubmit<{ id: number }>('/api/users', {
|
|
129
|
+
* baseUrl: 'https://api.example.com',
|
|
130
|
+
* headers: { 'x-csrf': token },
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* const result = await form.submit({ name: 'Ada', email: 'ada@example.com' });
|
|
134
|
+
* console.log(form.status.value); // 'success'
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export declare const useSubmit: <TResponse = unknown>(url: string | URL, options?: UseSubmitOptions<TResponse>) => UseSubmitReturn<TResponse>;
|
|
138
|
+
/** Typed CRUD methods for a REST endpoint. */
|
|
139
|
+
export interface RestClient<T = unknown> {
|
|
140
|
+
/** GET all items. */
|
|
141
|
+
list: (config?: HttpRequestConfig) => Promise<HttpResponse<T[]>>;
|
|
142
|
+
/** GET a single item by ID. */
|
|
143
|
+
get: (id: string | number, config?: HttpRequestConfig) => Promise<HttpResponse<T>>;
|
|
144
|
+
/** POST a new item. */
|
|
145
|
+
create: (body: Partial<T> | Record<string, unknown>, config?: HttpRequestConfig) => Promise<HttpResponse<T>>;
|
|
146
|
+
/** PUT (full replace) an item by ID. */
|
|
147
|
+
update: (id: string | number, body: Partial<T> | Record<string, unknown>, config?: HttpRequestConfig) => Promise<HttpResponse<T>>;
|
|
148
|
+
/** PATCH (partial update) an item by ID. */
|
|
149
|
+
patch: (id: string | number, body: Partial<T> | Record<string, unknown>, config?: HttpRequestConfig) => Promise<HttpResponse<T>>;
|
|
150
|
+
/** DELETE an item by ID. */
|
|
151
|
+
remove: (id: string | number, config?: HttpRequestConfig) => Promise<HttpResponse<void>>;
|
|
152
|
+
/** The underlying HttpClient instance. */
|
|
153
|
+
http: HttpClient;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Create a typed REST client for a specific API resource.
|
|
157
|
+
*
|
|
158
|
+
* Wraps `createHttp()` and maps standard CRUD operations to their
|
|
159
|
+
* conventional REST endpoints (`GET /`, `GET /:id`, `POST /`, `PUT /:id`,
|
|
160
|
+
* `PATCH /:id`, `DELETE /:id`).
|
|
161
|
+
*
|
|
162
|
+
* @template T - Resource item type
|
|
163
|
+
* @param baseUrl - Base URL of the resource (e.g. `https://api.example.com/users`)
|
|
164
|
+
* @param defaults - Default request configuration merged into every call
|
|
165
|
+
* @returns Typed REST client with `list`, `get`, `create`, `update`, `patch`, `remove`
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* import { createRestClient } from '@bquery/bquery/reactive';
|
|
170
|
+
*
|
|
171
|
+
* interface User { id: number; name: string; email: string }
|
|
172
|
+
*
|
|
173
|
+
* const users = createRestClient<User>('https://api.example.com/users', {
|
|
174
|
+
* headers: { authorization: '******' },
|
|
175
|
+
* timeout: 10_000,
|
|
176
|
+
* });
|
|
177
|
+
*
|
|
178
|
+
* const { data: allUsers } = await users.list();
|
|
179
|
+
* const { data: user } = await users.get(1);
|
|
180
|
+
* const { data: created } = await users.create({ name: 'Ada' });
|
|
181
|
+
* await users.update(1, { name: 'Ada', email: 'ada@example.com' });
|
|
182
|
+
* await users.patch(1, { email: 'new@example.com' });
|
|
183
|
+
* await users.remove(1);
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export declare const createRestClient: <T = unknown>(baseUrl: string, defaults?: HttpRequestConfig) => RestClient<T>;
|
|
187
|
+
/** Extract a unique identifier from an item. */
|
|
188
|
+
export type IdExtractor<T> = (item: T) => string | number;
|
|
189
|
+
/** Options for `useResourceList()`. */
|
|
190
|
+
export interface UseResourceListOptions<T = unknown> extends Omit<UseFetchOptions<T[]>, 'method' | 'body'> {
|
|
191
|
+
/** Extract the unique ID from each item (default: `item.id`). */
|
|
192
|
+
getId?: IdExtractor<T>;
|
|
193
|
+
/** Enable optimistic list mutations (default: false). */
|
|
194
|
+
optimistic?: boolean;
|
|
195
|
+
/** Called after a successful list mutation. */
|
|
196
|
+
onMutationSuccess?: (action: string) => void;
|
|
197
|
+
/** Called after a failed list mutation. */
|
|
198
|
+
onMutationError?: (error: Error, action: string) => void;
|
|
199
|
+
}
|
|
200
|
+
/** CRUD actions for a list resource. */
|
|
201
|
+
export interface ResourceListActions<T> {
|
|
202
|
+
/** Refresh the list (GET). */
|
|
203
|
+
fetch: () => Promise<T[] | undefined>;
|
|
204
|
+
/** Add a new item to the list (POST). */
|
|
205
|
+
add: (body: Partial<T> | Record<string, unknown>) => Promise<T | undefined>;
|
|
206
|
+
/** Update an existing item (PUT) by ID. */
|
|
207
|
+
update: (id: string | number, body: Partial<T> | Record<string, unknown>) => Promise<T | undefined>;
|
|
208
|
+
/** Partially update an existing item (PATCH) by ID. */
|
|
209
|
+
patch: (id: string | number, body: Partial<T> | Record<string, unknown>) => Promise<T | undefined>;
|
|
210
|
+
/** Remove an item from the list (DELETE) by ID. */
|
|
211
|
+
remove: (id: string | number) => Promise<void>;
|
|
212
|
+
}
|
|
213
|
+
/** Return value of `useResourceList()`. */
|
|
214
|
+
export interface UseResourceListReturn<T> {
|
|
215
|
+
/** Reactive list data. */
|
|
216
|
+
data: Signal<T[] | undefined>;
|
|
217
|
+
/** Last error. */
|
|
218
|
+
error: Signal<Error | null>;
|
|
219
|
+
/** Lifecycle status. */
|
|
220
|
+
status: Signal<AsyncDataStatus>;
|
|
221
|
+
/** Whether the list fetch is pending. */
|
|
222
|
+
pending: {
|
|
223
|
+
readonly value: boolean;
|
|
224
|
+
peek(): boolean;
|
|
225
|
+
};
|
|
226
|
+
/** Whether any mutation is in progress. */
|
|
227
|
+
isMutating: {
|
|
228
|
+
readonly value: boolean;
|
|
229
|
+
peek(): boolean;
|
|
230
|
+
};
|
|
231
|
+
/** CRUD actions. */
|
|
232
|
+
actions: ResourceListActions<T>;
|
|
233
|
+
/** Refresh the list. */
|
|
234
|
+
refresh: () => Promise<T[] | undefined>;
|
|
235
|
+
/** Clear data, error, and status. */
|
|
236
|
+
clear: () => void;
|
|
237
|
+
/** Dispose all reactive state. */
|
|
238
|
+
dispose: () => void;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Reactive list/collection CRUD composable with optimistic add, remove, and update.
|
|
242
|
+
*
|
|
243
|
+
* Fetches a list of items and provides typed CRUD helpers that update the
|
|
244
|
+
* reactive array optimistically or after server confirmation.
|
|
245
|
+
*
|
|
246
|
+
* @template T - Item type
|
|
247
|
+
* @param url - List endpoint URL or getter
|
|
248
|
+
* @param options - Fetch and list options
|
|
249
|
+
* @returns Reactive list state with CRUD actions
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* import { useResourceList } from '@bquery/bquery/reactive';
|
|
254
|
+
*
|
|
255
|
+
* interface Todo { id: number; title: string; done: boolean }
|
|
256
|
+
*
|
|
257
|
+
* const todos = useResourceList<Todo>('/api/todos', {
|
|
258
|
+
* baseUrl: 'https://api.example.com',
|
|
259
|
+
* optimistic: true,
|
|
260
|
+
* getId: (t) => t.id,
|
|
261
|
+
* });
|
|
262
|
+
*
|
|
263
|
+
* await todos.actions.add({ title: 'Buy milk', done: false });
|
|
264
|
+
* await todos.actions.patch(1, { done: true });
|
|
265
|
+
* await todos.actions.remove(1);
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
export declare const useResourceList: <T = unknown>(url: string | URL | (() => string | URL), options?: UseResourceListOptions<T>) => UseResourceListReturn<T>;
|
|
269
|
+
/**
|
|
270
|
+
* Deduplicate identical in-flight requests or operations keyed by `key`.
|
|
271
|
+
*
|
|
272
|
+
* If an operation with the same key is already in flight, reuse its promise
|
|
273
|
+
* instead of starting a new one. Once the operation completes, the entry is removed.
|
|
274
|
+
*
|
|
275
|
+
* @param key - Cache key for the in-flight operation (for HTTP, typically URL + serialized query)
|
|
276
|
+
* @param execute - The operation function to run if no duplicate is in flight
|
|
277
|
+
* @returns The shared result promise for callers using the same key concurrently
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
281
|
+
* import { deduplicateRequest, createHttp } from '@bquery/bquery/reactive';
|
|
282
|
+
*
|
|
283
|
+
* const api = createHttp({ baseUrl: 'https://api.example.com' });
|
|
284
|
+
*
|
|
285
|
+
* // Both calls share the same in-flight operation
|
|
286
|
+
* const [a, b] = await Promise.all([
|
|
287
|
+
* deduplicateRequest('/users', () => api.get('/users')),
|
|
288
|
+
* deduplicateRequest('/users', () => api.get('/users')),
|
|
289
|
+
* ]);
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
export declare function deduplicateRequest<T>(key: string, execute: () => Promise<T>): Promise<T>;
|
|
293
|
+
//# sourceMappingURL=rest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../../src/reactive/rest.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAY,KAAK,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AACpF,OAAO,EAAc,KAAK,UAAU,EAAE,KAAK,iBAAiB,EAAE,KAAK,YAAY,EAAE,MAAM,QAAQ,CAAC;AAMhG,qDAAqD;AACrD,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,gCAAgC;IAChC,KAAK,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IACpC,gCAAgC;IAChC,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAC/E,kCAAkC;IAClC,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,C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C,CAAC,CAAC,CAAC;IAChC,wBAAwB;IACxB,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC;IACxC,qCAAqC;IACrC,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,kCAAkC;IAClC,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,GAAG,OAAO,EACzC,KAAK,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,MAAM,GAAG,GAAG,CAAC,EACxC,UAAS,sBAAsB,CAAC,CAAC,CAAM,KACtC,qBAAqB,CAAC,CAAC,CA8PzB,CAAC;AASF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAUxF"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive effect scopes for grouped disposal.
|
|
3
|
+
*
|
|
4
|
+
* An `EffectScope` collects all effects, computed values, and watches created
|
|
5
|
+
* inside its `run()` callback so they can be disposed together with a single
|
|
6
|
+
* `stop()` call. Scopes nest — an inner scope is collected by its parent.
|
|
7
|
+
*
|
|
8
|
+
* @module bquery/reactive
|
|
9
|
+
*/
|
|
10
|
+
import type { CleanupFn } from './internals';
|
|
11
|
+
/**
|
|
12
|
+
* A scope that collects reactive resources for grouped disposal.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { effectScope, signal, effect, computed } from '@bquery/bquery/reactive';
|
|
17
|
+
*
|
|
18
|
+
* const scope = effectScope();
|
|
19
|
+
*
|
|
20
|
+
* scope.run(() => {
|
|
21
|
+
* const count = signal(0);
|
|
22
|
+
* effect(() => console.log(count.value));
|
|
23
|
+
* const doubled = computed(() => count.value * 2);
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* scope.stop(); // All effects and computed values disposed
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export interface EffectScope {
|
|
30
|
+
/** Whether the scope has not yet been stopped. */
|
|
31
|
+
readonly active: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Executes `fn` inside this scope, collecting any reactive resources
|
|
34
|
+
* (effects, computed values, watches, nested scopes) created during the call.
|
|
35
|
+
*
|
|
36
|
+
* `run()` is synchronous-only. Do not pass an async function or a function
|
|
37
|
+
* that returns a Promise — resources created after an `await` cannot be
|
|
38
|
+
* collected reliably.
|
|
39
|
+
*
|
|
40
|
+
* @template T - Return type of the provided function
|
|
41
|
+
* @param fn - Function to run inside the scope
|
|
42
|
+
* @returns The return value of `fn`
|
|
43
|
+
* @throws {Error} If the scope has already been stopped
|
|
44
|
+
*/
|
|
45
|
+
run<T>(fn: () => T): T;
|
|
46
|
+
/**
|
|
47
|
+
* Disposes all collected resources and marks the scope as inactive.
|
|
48
|
+
* Calling `stop()` on an already-stopped scope is a safe no-op.
|
|
49
|
+
*/
|
|
50
|
+
stop(): void;
|
|
51
|
+
}
|
|
52
|
+
/** @internal */
|
|
53
|
+
export declare const hasScopeDisposer: (scope: EffectScope | undefined) => scope is EffectScope & {
|
|
54
|
+
_addDisposer(fn: CleanupFn): void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Returns the currently active scope, or `undefined` if none.
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export declare const getActiveScope: () => EffectScope | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a new effect scope for grouped disposal of reactive resources.
|
|
63
|
+
*
|
|
64
|
+
* All `effect()`, `computed()`, `watch()`, and nested `effectScope()` calls
|
|
65
|
+
* made inside `scope.run(fn)` are automatically collected. Calling
|
|
66
|
+
* `scope.stop()` disposes them all at once.
|
|
67
|
+
*
|
|
68
|
+
* `run()` is synchronous-only. Create the scope outside async flows when
|
|
69
|
+
* needed, but keep the callback itself synchronous so cleanup registration
|
|
70
|
+
* stays deterministic.
|
|
71
|
+
*
|
|
72
|
+
* @returns A new {@link EffectScope}
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { effectScope, signal, effect, onScopeDispose } from '@bquery/bquery/reactive';
|
|
77
|
+
*
|
|
78
|
+
* const scope = effectScope();
|
|
79
|
+
*
|
|
80
|
+
* scope.run(() => {
|
|
81
|
+
* const count = signal(0);
|
|
82
|
+
*
|
|
83
|
+
* effect(() => console.log(count.value));
|
|
84
|
+
*
|
|
85
|
+
* onScopeDispose(() => {
|
|
86
|
+
* console.log('Custom cleanup');
|
|
87
|
+
* });
|
|
88
|
+
* });
|
|
89
|
+
*
|
|
90
|
+
* scope.stop(); // logs "Custom cleanup", all effects stopped
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare const effectScope: () => EffectScope;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the currently active {@link EffectScope}, or `undefined` if
|
|
96
|
+
* code is not running inside any scope's `run()` callback.
|
|
97
|
+
*
|
|
98
|
+
* @returns The active scope, or `undefined`
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { effectScope, getCurrentScope } from '@bquery/bquery/reactive';
|
|
103
|
+
*
|
|
104
|
+
* const scope = effectScope();
|
|
105
|
+
* scope.run(() => {
|
|
106
|
+
* console.log(getCurrentScope() !== undefined); // true
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* console.log(getCurrentScope()); // undefined
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare const getCurrentScope: () => EffectScope | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Registers a cleanup callback on the currently active scope.
|
|
115
|
+
*
|
|
116
|
+
* The callback runs when the scope is stopped. This is useful for
|
|
117
|
+
* registering arbitrary cleanup (e.g. event listeners, timers)
|
|
118
|
+
* alongside effects and computed values.
|
|
119
|
+
*
|
|
120
|
+
* @param fn - Cleanup function to run when the scope stops
|
|
121
|
+
* @throws {Error} If called outside an active scope
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* import { effectScope, onScopeDispose } from '@bquery/bquery/reactive';
|
|
126
|
+
*
|
|
127
|
+
* const scope = effectScope();
|
|
128
|
+
*
|
|
129
|
+
* scope.run(() => {
|
|
130
|
+
* const controller = new AbortController();
|
|
131
|
+
* fetch('/api/data', { signal: controller.signal });
|
|
132
|
+
*
|
|
133
|
+
* onScopeDispose(() => controller.abort());
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* scope.stop(); // abort() is called
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare const onScopeDispose: (fn: CleanupFn) => void;
|
|
140
|
+
//# sourceMappingURL=scope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../../src/reactive/scope.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAM7C;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,WAAW;IAC1B,kDAAkD;IAClD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAEzB;;;;;;;;;;;;OAYG;IACH,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAEvB;;;OAGG;IACH,IAAI,IAAI,IAAI,CAAC;CACd;AAcD,gBAAgB;AAChB,eAAO,MAAM,gBAAgB,GAC3B,OAAO,WAAW,GAAG,SAAS,KAC7B,KAAK,IAAI,WAAW,GAAG;IAAE,YAAY,CAAC,EAAE,EAAE,SAAS,GAAG,IAAI,CAAA;CACW,CAAC;AA2BzE;;;GAGG;AACH,eAAO,MAAM,cAAc,QAAO,WAAW,GAAG,SAQ/C,CAAC;AA8DF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,WAAW,QAAO,WAU9B,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,eAAe,QAAO,WAAW,GAAG,SAA6B,CAAC;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,cAAc,GAAI,IAAI,SAAS,KAAG,IAQ9C,CAAC"}
|
|
@@ -8,14 +8,28 @@ export { createUseFetch, useAsyncData, useFetch } from './async-data';
|
|
|
8
8
|
export { Computed, computed } from './computed';
|
|
9
9
|
export { Signal, signal } from './core';
|
|
10
10
|
export { effect } from './effect';
|
|
11
|
+
export { createHttp, createRequestQueue, http, HttpError } from './http';
|
|
11
12
|
export { linkedSignal } from './linked';
|
|
13
|
+
export { useInfiniteFetch, usePaginatedFetch } from './pagination';
|
|
12
14
|
export { persistedSignal } from './persisted';
|
|
15
|
+
export { usePolling } from './polling';
|
|
13
16
|
export { readonly } from './readonly';
|
|
17
|
+
export { createRestClient, deduplicateRequest, useResource, useResourceList, useSubmit, } from './rest';
|
|
18
|
+
export { effectScope, getCurrentScope, onScopeDispose } from './scope';
|
|
14
19
|
export { isComputed, isSignal } from './type-guards';
|
|
20
|
+
export { toValue } from './to-value';
|
|
15
21
|
export { untrack } from './untrack';
|
|
16
22
|
export { watch } from './watch';
|
|
23
|
+
export { useEventSource, useWebSocket, useWebSocketChannel } from './websocket';
|
|
17
24
|
export type { CleanupFn, Observer } from './internals';
|
|
18
|
-
export type { AsyncDataState, AsyncDataStatus, AsyncWatchSource, FetchInput, UseAsyncDataOptions, UseFetchOptions, } from './async-data';
|
|
25
|
+
export type { AsyncDataState, AsyncDataStatus, AsyncWatchSource, FetchInput, UseAsyncDataOptions, UseFetchOptions, UseFetchRetryConfig, } from './async-data';
|
|
26
|
+
export type { HttpClient, HttpProgressEvent, HttpRequestConfig, HttpResponse, Interceptor, InterceptorManager, RequestQueue, RequestQueueOptions, RetryConfig, } from './http';
|
|
27
|
+
export type { InfiniteState, PaginatedState, UseInfiniteFetchOptions, UsePaginatedFetchOptions, } from './pagination';
|
|
28
|
+
export type { PollingState, UsePollingOptions } from './polling';
|
|
29
|
+
export type { IdExtractor, ResourceListActions, RestClient, UseResourceListOptions, UseResourceListReturn, UseResourceOptions, UseResourceReturn, UseSubmitOptions, UseSubmitReturn, } from './rest';
|
|
30
|
+
export type { EffectScope } from './scope';
|
|
19
31
|
export type { LinkedSignal } from './linked';
|
|
20
|
-
export type {
|
|
32
|
+
export type { MaybeSignal } from './to-value';
|
|
33
|
+
export type { ReadonlySignal, ReadonlySignalHandle } from './readonly';
|
|
34
|
+
export type { ChannelMessage, ChannelSubscription, EventSourceStatus, UseEventSourceOptions, UseEventSourceReturn, UseWebSocketChannelOptions, UseWebSocketChannelReturn, UseWebSocketOptions, UseWebSocketReturn, WebSocketHeartbeatConfig, WebSocketReconnectConfig, WebSocketSerializer, WebSocketStatus, } from './websocket';
|
|
21
35
|
//# sourceMappingURL=signal.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../../src/reactive/signal.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,O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@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility to unwrap reactive or plain values.
|
|
3
|
+
*/
|
|
4
|
+
import { Computed } from './computed';
|
|
5
|
+
import { Signal } from './core';
|
|
6
|
+
import { readonly } from './readonly';
|
|
7
|
+
/**
|
|
8
|
+
* A value that may be a raw value, a Signal, a `readonly()` wrapper, or a Computed.
|
|
9
|
+
*
|
|
10
|
+
* Useful for APIs that accept both reactive and plain inputs.
|
|
11
|
+
*
|
|
12
|
+
* Readonly wrappers are limited to the values returned by {@link readonly}. This keeps
|
|
13
|
+
* the type aligned with runtime behavior, where arbitrary structural `{ value, peek }`
|
|
14
|
+
* objects are intentionally returned unchanged.
|
|
15
|
+
*
|
|
16
|
+
* @template T - The underlying value type
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* function useTitle(title: MaybeSignal<string>) {
|
|
21
|
+
* document.title = toValue(title);
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* useTitle('Hello'); // plain string
|
|
25
|
+
* useTitle(signal('Hello')); // reactive signal
|
|
26
|
+
* useTitle(computed(() => 'Hi')); // computed value
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export type MaybeSignal<T> = T | Signal<T> | ReturnType<typeof readonly<T>> | Computed<T>;
|
|
30
|
+
/**
|
|
31
|
+
* Extracts the current value from a Signal, a bQuery `readonly()` wrapper, a
|
|
32
|
+
* Computed, or returns the raw value as-is. This eliminates repetitive
|
|
33
|
+
* `isSignal(x) ? x.value : x` patterns throughout user code.
|
|
34
|
+
*
|
|
35
|
+
* Reading a Signal or Computed via `toValue()` uses `.value`, so the
|
|
36
|
+
* read **does** participate in reactive tracking when called inside
|
|
37
|
+
* an effect or computed.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The underlying value type
|
|
40
|
+
* @param source - A plain value, Signal, bQuery readonly wrapper, or Computed
|
|
41
|
+
* @returns The unwrapped value
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { signal, computed, toValue } from '@bquery/bquery/reactive';
|
|
46
|
+
*
|
|
47
|
+
* const count = signal(5);
|
|
48
|
+
* const doubled = computed(() => count.value * 2);
|
|
49
|
+
*
|
|
50
|
+
* toValue(42); // 42
|
|
51
|
+
* toValue(count); // 5
|
|
52
|
+
* toValue(doubled); // 10
|
|
53
|
+
* toValue(null); // null
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare const toValue: <T>(source: MaybeSignal<T>) => T;
|
|
57
|
+
//# sourceMappingURL=to-value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-value.d.ts","sourceRoot":"","sources":["../../src/reactive/to-value.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAoB,MAAM,YAAY,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;AAE1F;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,EAAE,QAAQ,WAAW,CAAC,CAAC,CAAC,KAAG,CAYnD,CAAC"}
|