@studiolambda/query 1.0.0 → 1.1.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.
@@ -0,0 +1,263 @@
1
+ import { Cache, Caches, CacheType, ItemsCacheItem, ResolversCacheItem } from './cache';
2
+ /**
3
+ * Represents the available configuration options
4
+ * for a query instance.
5
+ */
6
+ export interface Configuration extends Options {
7
+ /**
8
+ * Determines the resolved items cache to use.
9
+ */
10
+ readonly itemsCache?: Cache<ItemsCacheItem>;
11
+ /**
12
+ * Determines the resolvers cache to use.
13
+ */
14
+ readonly resolversCache?: Cache<ResolversCacheItem>;
15
+ /**
16
+ * Stores the event system.
17
+ */
18
+ readonly events?: EventTarget;
19
+ /**
20
+ * Broadcast channel. This is useful for communicating
21
+ * between tabs and windows (browser contexts).
22
+ *
23
+ * By default it does not use any broadcast channel.
24
+ * If a broadcast channel is provided, query
25
+ * won't close automatically, therefore, the responsability
26
+ * of closing the broadcast channel is up to the user.
27
+ */
28
+ readonly broadcast?: BroadcastChannel;
29
+ }
30
+ export interface FetcherAdditional {
31
+ /**
32
+ * An abort signal to cancel pending queries.
33
+ */
34
+ readonly signal: AbortSignal;
35
+ }
36
+ /**
37
+ * The available options for query.
38
+ */
39
+ export interface Options<T = unknown> {
40
+ /**
41
+ * Determines the item deduplication interval.
42
+ * This determines how many milliseconds an item
43
+ * is considered valid.
44
+ */
45
+ readonly expiration?: ExpirationFunction<T>;
46
+ /**
47
+ * Determines the fetcher function to use.
48
+ */
49
+ readonly fetcher?: FetcherFunction<T>;
50
+ /**
51
+ * Determines if we can return a stale item.
52
+ * If `true`, it will return the previous stale item
53
+ * stored in the cache if it has expired. It will attempt
54
+ * to revalidate it in the background. If `false`, the returned
55
+ * promise will be the revalidation promise.
56
+ */
57
+ readonly stale?: boolean;
58
+ /**
59
+ * Removes the stored item if there is an error in the request.
60
+ * By default, we don't remove the item upon failure, only the resolver
61
+ * is removed from the cache.
62
+ */
63
+ readonly removeOnError?: boolean;
64
+ /**
65
+ * Determines if the result should be a fresh fetched
66
+ * instance regardless of any cached value or its expiration time.
67
+ */
68
+ readonly fresh?: boolean;
69
+ }
70
+ /**
71
+ * The mutation function type.
72
+ */
73
+ export type MutationFunction<T> = {
74
+ (previous?: T, expiresAt?: Date): T | Promise<T>;
75
+ };
76
+ /**
77
+ * The available mutation values.
78
+ */
79
+ export type MutationValue<T> = T | MutationFunction<T>;
80
+ /**
81
+ * The broadcast payload.
82
+ */
83
+ export interface BroadcastPayload {
84
+ /**
85
+ * The event name.
86
+ */
87
+ readonly event: QueryEvent;
88
+ /**
89
+ * The event detail.
90
+ */
91
+ readonly detail: unknown;
92
+ }
93
+ /**
94
+ * The function type for the broadcast subscription.
95
+ */
96
+ export type SubscribeBroadcastFunction = {
97
+ (): Unsubscriber;
98
+ };
99
+ /**
100
+ * The query function type.
101
+ */
102
+ export type QueryFunction = {
103
+ <T = unknown>(key: string, options?: Options<T>): Promise<T>;
104
+ };
105
+ export type ExpirationFunction<T = unknown> = {
106
+ (item: T): number;
107
+ };
108
+ export type FetcherFunction<T = unknown> = {
109
+ (key: string, additional: FetcherAdditional): Promise<T>;
110
+ };
111
+ /**
112
+ * The mutate function options.
113
+ */
114
+ export interface HydrateOptions<T = unknown> {
115
+ expiration?: ExpirationFunction<T>;
116
+ }
117
+ /**
118
+ * The mutate function options.
119
+ */
120
+ export interface MutateOptions<T = unknown> {
121
+ expiration?: ExpirationFunction<T>;
122
+ }
123
+ /**
124
+ * The mutate function type.
125
+ */
126
+ export type MutateFunction = {
127
+ <T = unknown>(key: string, item: MutationValue<T>, options?: MutateOptions<T>): Promise<T>;
128
+ };
129
+ /**
130
+ * The hydrate function type.
131
+ */
132
+ export type HydrateFunction = {
133
+ <T = unknown>(keys: string | string[], item: T, options?: HydrateOptions<T>): void;
134
+ };
135
+ /**
136
+ * The unsubscriber function.
137
+ */
138
+ export type Unsubscriber = () => void;
139
+ export type OnceFunction = {
140
+ <T = unknown>(key: string, event: QueryEvent): Promise<CustomEventInit<T>>;
141
+ };
142
+ export type StreamFunction = {
143
+ <T = unknown>(key: string, event: QueryEvent): AsyncGenerator<CustomEventInit<T>>;
144
+ };
145
+ export type ConfigureFunction = {
146
+ (options?: Partial<Configuration>): void;
147
+ };
148
+ /**
149
+ * Represents the methods a query
150
+ * should implement.
151
+ */
152
+ export interface Query {
153
+ /**
154
+ * Configures the current instance of query.
155
+ */
156
+ readonly configure: ConfigureFunction;
157
+ /**
158
+ * Fetches the key information using a fetcher.
159
+ * The returned promise contains the result item.
160
+ */
161
+ readonly query: QueryFunction;
162
+ /**
163
+ * Emit is able to send events to active subscribers
164
+ * with the given payload. It is a low level API
165
+ * and should be used with case.
166
+ */
167
+ readonly emit: EmitFunction;
168
+ /**
169
+ * Subscribes to a given event & key. The event handler
170
+ * does have a payload parameter that will contain relevant
171
+ * information depending on the event type.
172
+ */
173
+ readonly subscribe: SubscribeFunction;
174
+ /**
175
+ * Subscribes to the broadcast channel
176
+ * to listen for other browser context
177
+ * events and reproduce them in the current
178
+ * context.
179
+ */
180
+ readonly subscribeBroadcast: SubscribeBroadcastFunction;
181
+ /**
182
+ * Mutates the key with a given optimistic value.
183
+ * The mutated value is considered expired and will be
184
+ * replaced immediatly if a refetch happens.
185
+ */
186
+ readonly mutate: MutateFunction;
187
+ /**
188
+ * Aborts the active resolvers on each key
189
+ * by calling `.abort()` on the `AbortController`.
190
+ * The fetcher is responsible for using the
191
+ * `AbortSignal` to cancel the job.
192
+ */
193
+ readonly abort: (key?: string | string[], reason?: unknown) => void;
194
+ /**
195
+ * Forgets the given keys from the cache.
196
+ * Removes items from both, the cache and resolvers.
197
+ */
198
+ readonly forget: (keys?: string | string[] | RegExp) => Promise<void>;
199
+ /**
200
+ * Hydrates the given keys on the cache
201
+ * with the given value and expiration time.
202
+ */
203
+ readonly hydrate: HydrateFunction;
204
+ /**
205
+ * Returns the given keys for the given cache.
206
+ */
207
+ readonly keys: (cache?: CacheType) => string[];
208
+ /**
209
+ * Returns the expiration date of a given key item.
210
+ * If the item is not in the cache, it will return `undefined`.
211
+ */
212
+ readonly expiration: (key: string) => Date | undefined;
213
+ /**
214
+ * Returns the current snapshot of the given key.
215
+ * If the item is not in the items cache, it will return `undefined`.
216
+ */
217
+ readonly snapshot: <T = unknown>(key: string) => T | undefined;
218
+ /**
219
+ * Returns the first event on the given key that happens.
220
+ *
221
+ * It does so by subscribing and unsubscribing after event has
222
+ * been emitted.
223
+ */
224
+ readonly once: OnceFunction;
225
+ /**
226
+ * A generator that is able to stream events as they come in.
227
+ */
228
+ readonly stream: StreamFunction;
229
+ /**
230
+ * Returns the current cache instances in use.
231
+ */
232
+ readonly caches: () => Caches;
233
+ /**
234
+ * Returns the event system in use.
235
+ */
236
+ readonly events: () => EventTarget;
237
+ /**
238
+ * Returns the broadcast channel in use.
239
+ */
240
+ readonly broadcast: () => BroadcastChannel | undefined;
241
+ }
242
+ /**
243
+ * Available events on query.
244
+ */
245
+ export type QueryEvent = 'refetching' | 'resolved' | 'mutating' | 'mutated' | 'aborted' | 'forgotten' | 'hydrated' | 'error';
246
+ export type SubscribeListener<T> = (event: CustomEventInit<T>) => void;
247
+ /**
248
+ * The function type for the event subscription.
249
+ */
250
+ export interface SubscribeFunction {
251
+ <T = unknown>(key: string, event: 'refetching' | 'resolved' | 'mutating' | 'mutated' | 'forgotten' | 'hydrated', listener: SubscribeListener<Promise<T>>): Unsubscriber;
252
+ (key: string, event: 'error' | 'aborted', listener: SubscribeListener<unknown>): Unsubscriber;
253
+ <T = unknown>(key: string, event: QueryEvent, listener: SubscribeListener<T>): Unsubscriber;
254
+ }
255
+ /**
256
+ * The emit function to manually emit events.
257
+ */
258
+ export interface EmitFunction {
259
+ <T = unknown>(key: string, event: 'refetching' | 'resolved' | 'mutating' | 'mutated' | 'forgotten' | 'hydrated', detail: Promise<T>): void;
260
+ (key: string, event: 'error' | 'aborted', detail: unknown): void;
261
+ <T = unknown>(key: string, event: QueryEvent, detail: T): void;
262
+ }
263
+ export type TriggerFunction = undefined | (() => Promise<void>);
@@ -0,0 +1,9 @@
1
+ import { Configuration, FetcherFunction, Query } from './options';
2
+ /**
3
+ * Stores the default fetcher function.
4
+ */
5
+ export declare function defaultFetcher<T>(fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>): FetcherFunction<T>;
6
+ /**
7
+ * Creates a new query instance.
8
+ */
9
+ export declare function createQuery(instanceOptions?: Configuration): Query;
@@ -1,9 +1,6 @@
1
1
  import { ReactNode } from 'react';
2
- import { ContextOptions } from '../context';
3
- import { Configuration } from '../../query/index';
4
- type OtherProps = Configuration & ContextOptions;
5
- export interface QueryProviderProps extends OtherProps {
2
+ import { ContextValue } from '../context';
3
+ export interface QueryProviderProps extends ContextValue {
6
4
  children?: ReactNode;
7
5
  }
8
- export declare function QueryProvider({ children, clearOnForget, broadcast, ...options }: QueryProviderProps): import("react/jsx-runtime").JSX.Element;
9
- export {};
6
+ export declare function QueryProvider({ children, clearOnForget, ignoreTransitionContext, query, }: QueryProviderProps): import("react/jsx-runtime").JSX.Element;
@@ -1,10 +1,6 @@
1
- import { Query } from '../query/index';
2
- export interface ContextOptions {
1
+ import { QueryInstance } from './hooks/useQueryInstance';
2
+ export interface ContextValue extends QueryInstance {
3
3
  readonly clearOnForget?: boolean;
4
4
  readonly ignoreTransitionContext?: boolean;
5
5
  }
6
- export interface ContextValue {
7
- readonly query?: Query;
8
- readonly additional?: ContextOptions;
9
- }
10
6
  export declare const Context: import('react').Context<ContextValue>;
@@ -1,13 +1,14 @@
1
- import { ContextOptions as ContextOptions } from '../context';
1
+ import { ContextValue } from '../context';
2
2
  import { Options } from '../../query/index';
3
3
  import { QueryInstance } from './useQueryInstance';
4
4
  import { QueryActions } from './useQueryActions';
5
5
  import { Status } from './useQueryStatus';
6
- type AdditionalHooks<T> = QueryActions<T> & Status;
6
+ import { BasicResource } from './useQueryBasic';
7
+ type AdditionalHooks<T> = QueryActions<T> & BasicResource<T> & Status;
7
8
  export interface Resource<T = unknown> extends AdditionalHooks<T> {
8
9
  data: T;
9
10
  isPending: boolean;
10
11
  }
11
- export type ResourceOptions<T = unknown> = ContextOptions & Options<T> & QueryInstance;
12
+ export type ResourceOptions<T = unknown> = ContextValue & Options<T> & QueryInstance;
12
13
  export declare function useQuery<T = unknown>(key: string, options?: ResourceOptions<T>): Resource<T>;
13
14
  export {};
@@ -0,0 +1,9 @@
1
+ import { ContextValue } from '../context';
2
+ import { Options } from '../../query/index';
3
+ import { QueryInstance } from './useQueryInstance';
4
+ export interface BasicResource<T = unknown> {
5
+ data: T;
6
+ isPending: boolean;
7
+ }
8
+ export type BasicResourceOptions<T = unknown> = ContextValue & Options<T> & QueryInstance;
9
+ export declare function useQueryBasic<T = unknown>(key: string, options?: BasicResourceOptions<T>): BasicResource<T>;
@@ -1,7 +1,8 @@
1
+ import { QueryInstance } from './useQueryInstance';
1
2
  export interface Status {
2
3
  readonly expiresAt: Date;
3
4
  readonly isExpired: boolean;
4
5
  readonly isRefetching: boolean;
5
6
  readonly isMutating: boolean;
6
7
  }
7
- export declare function useQueryStatus(key: string): Status;
8
+ export declare function useQueryStatus(key: string, options?: QueryInstance): Status;
@@ -1,4 +1,5 @@
1
1
  export * from './hooks/useQuery';
2
+ export * from './hooks/useQueryBasic';
2
3
  export * from './hooks/useQueryStatus';
3
4
  export * from './hooks/useQueryInstance';
4
5
  export * from './hooks/useQueryContext';
package/package.json CHANGED
@@ -1,7 +1,19 @@
1
1
  {
2
2
  "name": "@studiolambda/query",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "license": "MIT",
5
+ "keywords": [
6
+ "stale-while-revalidate",
7
+ "swr",
8
+ "react-query",
9
+ "isomorphic",
10
+ "fetch",
11
+ "query",
12
+ "data-management",
13
+ "ui",
14
+ "vanilla",
15
+ "react"
16
+ ],
5
17
  "description": "Lightweight, isomorphic and framework agnostic asynchronous data management for modern UIs",
6
18
  "author": {
7
19
  "name": "Erik C. Forés",
@@ -43,7 +55,7 @@
43
55
  "peerDependencies": {
44
56
  "react": "^19.0.0",
45
57
  "react-dom": "^19.0.0",
46
- "solid-js": "^1.9.0"
58
+ "solid-js": "^1.9.4"
47
59
  },
48
60
  "peerDependenciesMeta": {
49
61
  "react": {
@@ -57,28 +69,28 @@
57
69
  }
58
70
  },
59
71
  "devDependencies": {
60
- "@types/node": "^22.10.2",
61
- "@types/react": "19.0.1",
62
- "@types/react-dom": "19.0.2",
63
- "@typescript-eslint/eslint-plugin": "^8.18.0",
64
- "@typescript-eslint/parser": "^8.18.0",
72
+ "@types/node": "^22.13.4",
73
+ "@types/react": "19.0.8",
74
+ "@types/react-dom": "19.0.3",
75
+ "@typescript-eslint/eslint-plugin": "^8.24.0",
76
+ "@typescript-eslint/parser": "^8.24.0",
65
77
  "@vitejs/plugin-react": "^4.3.4",
66
- "@vitest/coverage-v8": "^2.1.8",
67
- "@vitest/ui": "^2.1.8",
68
- "eslint": "^9.16.0",
78
+ "@vitest/coverage-v8": "^3.0.5",
79
+ "@vitest/ui": "^3.0.5",
80
+ "eslint": "^9.20.1",
69
81
  "eslint-plugin-react-hooks": "^5.1.0",
70
- "eslint-plugin-react-refresh": "^0.4.16",
82
+ "eslint-plugin-react-refresh": "^0.4.19",
71
83
  "eslint-plugin-solid": "^0.14.5",
72
- "globals": "^15.13.0",
73
- "happy-dom": "^15.11.7",
84
+ "globals": "^15.15.0",
85
+ "happy-dom": "^17.1.0",
74
86
  "react": "^19.0.0",
75
87
  "react-dom": "^19.0.0",
76
- "solid-js": "^1.9.3",
77
- "typescript": "^5.7.2",
78
- "typescript-eslint": "^8.18.0",
79
- "vite": "^6.0.3",
80
- "vite-plugin-dts": "^4.3.0",
81
- "vite-plugin-solid": "^2.11.0",
82
- "vitest": "^2.1.8"
88
+ "solid-js": "^1.9.4",
89
+ "typescript": "~5.7.3",
90
+ "typescript-eslint": "^8.24.0",
91
+ "vite": "^6.1.0",
92
+ "vite-plugin-dts": "^4.5.0",
93
+ "vite-plugin-solid": "^2.11.1",
94
+ "vitest": "^3.0.5"
83
95
  }
84
96
  }