@spoosh/core 0.12.1 → 0.13.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/dist/index.d.mts +340 -18
- package/dist/index.d.ts +340 -18
- package/dist/index.js +93 -25
- package/dist/index.mjs +93 -25
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -172,11 +172,6 @@ interface BuiltInEvents {
|
|
|
172
172
|
invalidate: string[];
|
|
173
173
|
refetchAll: void;
|
|
174
174
|
}
|
|
175
|
-
/**
|
|
176
|
-
* Resolves event payload type. Built-in events get their specific type,
|
|
177
|
-
* custom events get `unknown` (or explicit type parameter).
|
|
178
|
-
*/
|
|
179
|
-
type EventPayload<E extends string> = E extends keyof BuiltInEvents ? BuiltInEvents[E] : unknown;
|
|
180
175
|
type EventEmitter = {
|
|
181
176
|
/**
|
|
182
177
|
* Subscribe to an event. Built-in events have type-safe payloads.
|
|
@@ -192,7 +187,8 @@ type EventEmitter = {
|
|
|
192
187
|
* eventEmitter.on<MyPayload>("my-event", (payload) => { ... });
|
|
193
188
|
* ```
|
|
194
189
|
*/
|
|
195
|
-
on<E extends
|
|
190
|
+
on<E extends keyof BuiltInEvents>(event: E, callback: EventCallback<BuiltInEvents[E]>): () => void;
|
|
191
|
+
on<T = unknown>(event: string, callback: EventCallback<T>): () => void;
|
|
196
192
|
/**
|
|
197
193
|
* Emit an event. Built-in events have type-safe payloads.
|
|
198
194
|
*
|
|
@@ -205,7 +201,8 @@ type EventEmitter = {
|
|
|
205
201
|
* eventEmitter.emit("my-event", myPayload);
|
|
206
202
|
* ```
|
|
207
203
|
*/
|
|
208
|
-
emit<E extends
|
|
204
|
+
emit<E extends keyof BuiltInEvents>(event: E, payload: BuiltInEvents[E]): void;
|
|
205
|
+
emit<T = unknown>(event: string, payload: T): void;
|
|
209
206
|
off: (event: string, callback: EventCallback) => void;
|
|
210
207
|
clear: () => void;
|
|
211
208
|
};
|
|
@@ -247,6 +244,190 @@ type StateManager = {
|
|
|
247
244
|
};
|
|
248
245
|
declare function createStateManager(): StateManager;
|
|
249
246
|
|
|
247
|
+
/**
|
|
248
|
+
* Devtool-related types for tracing and debugging.
|
|
249
|
+
* These types are used by the devtool plugin and plugins that emit trace events.
|
|
250
|
+
*/
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Stage of plugin execution for tracing.
|
|
254
|
+
*/
|
|
255
|
+
type TraceStage = "return" | "log" | "skip" | "fetch";
|
|
256
|
+
/**
|
|
257
|
+
* Color hint for devtools visualization.
|
|
258
|
+
*/
|
|
259
|
+
type TraceColor = "success" | "warning" | "error" | "info" | "muted";
|
|
260
|
+
/**
|
|
261
|
+
* Structured trace event emitted by plugins.
|
|
262
|
+
* Plugins self-report what they did and why.
|
|
263
|
+
*/
|
|
264
|
+
type TraceEvent = {
|
|
265
|
+
/** Plugin name */
|
|
266
|
+
plugin: string;
|
|
267
|
+
/** Execution stage */
|
|
268
|
+
stage: TraceStage;
|
|
269
|
+
/** Human-readable explanation of what happened */
|
|
270
|
+
reason?: string;
|
|
271
|
+
/** Color hint for devtools (success=green, warning=yellow, error=red, info=blue) */
|
|
272
|
+
color?: TraceColor;
|
|
273
|
+
/** Before/after diff with optional label */
|
|
274
|
+
diff?: {
|
|
275
|
+
before: unknown;
|
|
276
|
+
after: unknown;
|
|
277
|
+
label?: string;
|
|
278
|
+
};
|
|
279
|
+
/** Structured information to display (e.g., invalidated tags, cache keys) */
|
|
280
|
+
info?: Array<{
|
|
281
|
+
label?: string;
|
|
282
|
+
value: unknown;
|
|
283
|
+
}>;
|
|
284
|
+
};
|
|
285
|
+
/**
|
|
286
|
+
* Trace API available to plugins via ctx.trace.
|
|
287
|
+
* Plugins emit structured events; devtools renders them.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* middleware: async (ctx, next) => {
|
|
292
|
+
* const cached = getCache(ctx.queryKey);
|
|
293
|
+
* if (cached) {
|
|
294
|
+
* ctx.trace?.step({
|
|
295
|
+
* plugin: "cache",
|
|
296
|
+
* stage: "skip",
|
|
297
|
+
* meta: { reason: "Cache hit (TTL valid)" }
|
|
298
|
+
* });
|
|
299
|
+
* return cached;
|
|
300
|
+
* }
|
|
301
|
+
*
|
|
302
|
+
* ctx.trace?.step({
|
|
303
|
+
* plugin: "cache",
|
|
304
|
+
* stage: "before",
|
|
305
|
+
* intent: "read",
|
|
306
|
+
* });
|
|
307
|
+
*
|
|
308
|
+
* const result = await next();
|
|
309
|
+
*
|
|
310
|
+
* ctx.trace?.step({
|
|
311
|
+
* plugin: "cache",
|
|
312
|
+
* stage: "after",
|
|
313
|
+
* meta: {
|
|
314
|
+
* reason: "Stored in cache",
|
|
315
|
+
* diff: { before: null, after: result.data }
|
|
316
|
+
* }
|
|
317
|
+
* });
|
|
318
|
+
*
|
|
319
|
+
* return result;
|
|
320
|
+
* }
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
type Trace = {
|
|
324
|
+
/**
|
|
325
|
+
* Emit a trace event. Lazy evaluation - only computed when devtools is active.
|
|
326
|
+
*
|
|
327
|
+
* @param event - Trace event or function that returns trace event (for lazy evaluation)
|
|
328
|
+
*/
|
|
329
|
+
step: (event: TraceEvent | (() => TraceEvent)) => void;
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* Listener for trace events emitted by plugins.
|
|
333
|
+
*/
|
|
334
|
+
type TraceListener = (event: TraceEvent & {
|
|
335
|
+
queryKey: string;
|
|
336
|
+
timestamp: number;
|
|
337
|
+
}) => void;
|
|
338
|
+
/**
|
|
339
|
+
* Standalone event not tied to a request lifecycle.
|
|
340
|
+
* Used for polling, debounce, gc, and other background activities.
|
|
341
|
+
*/
|
|
342
|
+
type StandaloneEvent = {
|
|
343
|
+
/** Plugin name */
|
|
344
|
+
plugin: string;
|
|
345
|
+
/** Human-readable message */
|
|
346
|
+
message: string;
|
|
347
|
+
/** Color hint for devtools */
|
|
348
|
+
color?: TraceColor;
|
|
349
|
+
/** Related query key (for filtering) */
|
|
350
|
+
queryKey?: string;
|
|
351
|
+
/** Additional metadata */
|
|
352
|
+
meta?: Record<string, unknown>;
|
|
353
|
+
/** Timestamp when event occurred */
|
|
354
|
+
timestamp: number;
|
|
355
|
+
};
|
|
356
|
+
type EventListener = (event: StandaloneEvent) => void;
|
|
357
|
+
type TraceInfo = {
|
|
358
|
+
label?: string;
|
|
359
|
+
value: unknown;
|
|
360
|
+
};
|
|
361
|
+
type TraceOptions = {
|
|
362
|
+
color?: TraceColor;
|
|
363
|
+
diff?: {
|
|
364
|
+
before: unknown;
|
|
365
|
+
after: unknown;
|
|
366
|
+
label?: string;
|
|
367
|
+
};
|
|
368
|
+
info?: TraceInfo[];
|
|
369
|
+
};
|
|
370
|
+
type EventOptions = {
|
|
371
|
+
color?: TraceColor;
|
|
372
|
+
/** Query key this event relates to (for filtering) */
|
|
373
|
+
queryKey?: string;
|
|
374
|
+
/** Additional metadata to display */
|
|
375
|
+
meta?: Record<string, unknown>;
|
|
376
|
+
};
|
|
377
|
+
/**
|
|
378
|
+
* Request-bound tracer API for plugins.
|
|
379
|
+
* Created via `context.tracer?.(pluginName)`.
|
|
380
|
+
* Automatically bound to the request's queryKey for accurate devtool tracing.
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```ts
|
|
384
|
+
* const t = context.tracer?.("my-plugin");
|
|
385
|
+
* t?.return("Cache hit", { color: "success" });
|
|
386
|
+
* t?.log("Transformed", { color: "info", diff: { before, after } });
|
|
387
|
+
* t?.skip("Nothing to do", { color: "muted" });
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
interface RequestTracer {
|
|
391
|
+
/** Returned early without calling next() */
|
|
392
|
+
return(msg: string, options?: TraceOptions): void;
|
|
393
|
+
/** Did something (any activity worth noting) */
|
|
394
|
+
log(msg: string, options?: TraceOptions): void;
|
|
395
|
+
/** Nothing to do, passed through */
|
|
396
|
+
skip(msg: string, options?: TraceOptions): void;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Event emitted after all afterResponse hooks complete.
|
|
400
|
+
* Used by devtools to capture meta snapshots.
|
|
401
|
+
*/
|
|
402
|
+
interface RequestCompleteEvent {
|
|
403
|
+
context: PluginContext;
|
|
404
|
+
queryKey: string;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Internal events used by core and devtools. Not for public use.
|
|
408
|
+
* @internal
|
|
409
|
+
*/
|
|
410
|
+
interface DevtoolEvents {
|
|
411
|
+
"spoosh:devtool-event": StandaloneEvent;
|
|
412
|
+
"spoosh:request-complete": RequestCompleteEvent;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Event tracer API for standalone events not tied to a request lifecycle.
|
|
416
|
+
* Created via `context.eventTracer?.(pluginName)`.
|
|
417
|
+
* Use for async callbacks like polling, debounce completion, gc, etc.
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```ts
|
|
421
|
+
* const et = context.eventTracer?.("my-plugin");
|
|
422
|
+
* et?.emit("Poll triggered", { queryKey, color: "success" });
|
|
423
|
+
* et?.emit("GC cleaned 5 entries", { color: "info", meta: { count: 5 } });
|
|
424
|
+
* ```
|
|
425
|
+
*/
|
|
426
|
+
interface EventTracer {
|
|
427
|
+
/** Emit a standalone event not tied to a request */
|
|
428
|
+
emit(msg: string, options?: EventOptions): void;
|
|
429
|
+
}
|
|
430
|
+
|
|
250
431
|
type OperationType = "read" | "write" | "infiniteRead";
|
|
251
432
|
type LifecyclePhase = "onMount" | "onUnmount" | "onUpdate";
|
|
252
433
|
type OperationState<TData = unknown, TError = unknown> = {
|
|
@@ -269,7 +450,23 @@ type CacheEntry<TData = unknown, TError = unknown> = {
|
|
|
269
450
|
type PluginRequestOptions = Omit<AnyRequestOptions, "headers" | "cache"> & {
|
|
270
451
|
headers: Record<string, string>;
|
|
271
452
|
};
|
|
272
|
-
|
|
453
|
+
/**
|
|
454
|
+
* Registry for extending PluginContext with custom properties.
|
|
455
|
+
* Third-party plugins can extend this interface via declaration merging.
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```ts
|
|
459
|
+
* // In your plugin's types file:
|
|
460
|
+
* declare module '@spoosh/core' {
|
|
461
|
+
* interface PluginContextExtensions {
|
|
462
|
+
* myCustomProperty?: MyCustomType;
|
|
463
|
+
* }
|
|
464
|
+
* }
|
|
465
|
+
* ```
|
|
466
|
+
*/
|
|
467
|
+
interface PluginContextExtensions {
|
|
468
|
+
}
|
|
469
|
+
type PluginContextBase = {
|
|
273
470
|
readonly operationType: OperationType;
|
|
274
471
|
readonly path: string;
|
|
275
472
|
readonly method: HttpMethod;
|
|
@@ -292,9 +489,40 @@ type PluginContext = {
|
|
|
292
489
|
pluginOptions?: unknown;
|
|
293
490
|
/** Force a network request even if cached data exists. Used by plugins to communicate intent. */
|
|
294
491
|
forceRefetch?: boolean;
|
|
492
|
+
/**
|
|
493
|
+
* Creates a request-bound tracer for devtools debugging.
|
|
494
|
+
* Automatically bound to this request's queryKey.
|
|
495
|
+
* Only available when devtools plugin is active.
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* ```ts
|
|
499
|
+
* const t = ctx.tracer?.("my-plugin");
|
|
500
|
+
* t?.return("Cache hit", { color: "success" });
|
|
501
|
+
* t?.log("Processing", { color: "info" });
|
|
502
|
+
* t?.skip("Nothing to do", { color: "muted" });
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
tracer?: (plugin: string) => RequestTracer;
|
|
506
|
+
/**
|
|
507
|
+
* Creates an event tracer for standalone events not tied to a request lifecycle.
|
|
508
|
+
* Use for async callbacks like polling, debounce completion, gc, etc.
|
|
509
|
+
* Only available when devtools plugin is active.
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* ```ts
|
|
513
|
+
* const et = ctx.eventTracer?.("my-plugin");
|
|
514
|
+
* et?.emit("Poll triggered", { queryKey, color: "success" });
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
517
|
+
eventTracer?: (plugin: string) => EventTracer;
|
|
295
518
|
};
|
|
519
|
+
/**
|
|
520
|
+
* Plugin context with extensions from third-party plugins.
|
|
521
|
+
* Plugins can extend this via PluginContextExtensions declaration merging.
|
|
522
|
+
*/
|
|
523
|
+
type PluginContext = PluginContextBase & PluginContextExtensions;
|
|
296
524
|
/** Input type for creating PluginContext (without injected properties) */
|
|
297
|
-
type PluginContextInput = Omit<PluginContext, "plugins">;
|
|
525
|
+
type PluginContextInput = Omit<PluginContext, "plugins" | "tracer" | "eventTracer">;
|
|
298
526
|
/**
|
|
299
527
|
* Middleware function that wraps the fetch flow.
|
|
300
528
|
* Plugins use this for full control over request/response handling.
|
|
@@ -364,6 +592,7 @@ type PluginTypeConfig = {
|
|
|
364
592
|
readOptions?: object;
|
|
365
593
|
writeOptions?: object;
|
|
366
594
|
infiniteReadOptions?: object;
|
|
595
|
+
writeTriggerOptions?: object;
|
|
367
596
|
readResult?: object;
|
|
368
597
|
writeResult?: object;
|
|
369
598
|
instanceApi?: object;
|
|
@@ -419,9 +648,36 @@ interface SpooshPlugin<T extends PluginTypeConfig = PluginTypeConfig> {
|
|
|
419
648
|
lifecycle?: PluginLifecycle;
|
|
420
649
|
/** Expose functions/variables for other plugins to access via `context.plugins.get(name)` */
|
|
421
650
|
exports?: (context: PluginContext) => object;
|
|
651
|
+
/**
|
|
652
|
+
* One-time initialization when the Spoosh instance is created.
|
|
653
|
+
* Use for setting up timers, event listeners, or other side effects.
|
|
654
|
+
* Runs before instanceApi is called.
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* ```ts
|
|
658
|
+
* setup: ({ stateManager, eventEmitter, pluginExecutor }) => {
|
|
659
|
+
* // Set up interval timer
|
|
660
|
+
* const intervalId = setInterval(() => {
|
|
661
|
+
* // periodic cleanup
|
|
662
|
+
* }, 60000);
|
|
663
|
+
*
|
|
664
|
+
* // Register context enhancer
|
|
665
|
+
* pluginExecutor.registerContextEnhancer((context) => {
|
|
666
|
+
* context.myProperty = myValue;
|
|
667
|
+
* });
|
|
668
|
+
*
|
|
669
|
+
* // Set up event listener
|
|
670
|
+
* eventEmitter.on("invalidate", (tags) => {
|
|
671
|
+
* // handle invalidation
|
|
672
|
+
* });
|
|
673
|
+
* }
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
setup?: (context: SetupContext) => void;
|
|
422
677
|
/**
|
|
423
678
|
* Expose functions/properties on the framework adapter return value (e.g., create).
|
|
424
679
|
* Unlike `exports`, these are accessible directly from the instance, not just within plugin context.
|
|
680
|
+
* Should be pure - use `setup` for side effects like timers or event listeners.
|
|
425
681
|
*
|
|
426
682
|
* @example
|
|
427
683
|
* ```ts
|
|
@@ -625,6 +881,12 @@ type RefetchEvent = {
|
|
|
625
881
|
type InstancePluginExecutor = {
|
|
626
882
|
executeMiddleware: <TData, TError>(operationType: OperationType, context: PluginContext, coreFetch: () => Promise<SpooshResponse<any, any>>) => Promise<SpooshResponse<TData, TError>>;
|
|
627
883
|
createContext: (input: PluginContextInput) => PluginContext;
|
|
884
|
+
getPlugins: () => readonly SpooshPlugin[];
|
|
885
|
+
/**
|
|
886
|
+
* Register a function to enhance every PluginContext during creation.
|
|
887
|
+
* Call this during plugin setup to inject properties into all request contexts.
|
|
888
|
+
*/
|
|
889
|
+
registerContextEnhancer: (enhancer: (context: PluginContext) => void) => void;
|
|
628
890
|
};
|
|
629
891
|
/**
|
|
630
892
|
* Context provided to plugin's instanceApi function.
|
|
@@ -635,6 +897,25 @@ type InstanceApiContext<TApi = unknown> = {
|
|
|
635
897
|
stateManager: StateManager;
|
|
636
898
|
eventEmitter: EventEmitter;
|
|
637
899
|
pluginExecutor: InstancePluginExecutor;
|
|
900
|
+
/**
|
|
901
|
+
* Creates an event tracer for standalone events.
|
|
902
|
+
* Only available when devtools plugin is active.
|
|
903
|
+
*/
|
|
904
|
+
eventTracer?: (plugin: string) => EventTracer;
|
|
905
|
+
};
|
|
906
|
+
/**
|
|
907
|
+
* Context provided to plugin's setup function.
|
|
908
|
+
* Used for one-time initialization when the Spoosh instance is created.
|
|
909
|
+
*/
|
|
910
|
+
type SetupContext = {
|
|
911
|
+
stateManager: StateManager;
|
|
912
|
+
eventEmitter: EventEmitter;
|
|
913
|
+
pluginExecutor: InstancePluginExecutor;
|
|
914
|
+
/**
|
|
915
|
+
* Creates an event tracer for standalone events.
|
|
916
|
+
* Only available when devtools plugin is active.
|
|
917
|
+
*/
|
|
918
|
+
eventTracer?: (plugin: string) => EventTracer;
|
|
638
919
|
};
|
|
639
920
|
|
|
640
921
|
type PluginExecutor = {
|
|
@@ -644,8 +925,13 @@ type PluginExecutor = {
|
|
|
644
925
|
executeUpdateLifecycle: (operationType: OperationType, context: PluginContext, previousContext: PluginContext) => Promise<void>;
|
|
645
926
|
executeMiddleware: (operationType: OperationType, context: PluginContext, coreFetch: () => Promise<SpooshResponse<any, any>>) => Promise<SpooshResponse<any, any>>;
|
|
646
927
|
getPlugins: () => readonly SpooshPlugin[];
|
|
647
|
-
/** Creates a full PluginContext with plugins accessor
|
|
928
|
+
/** Creates a full PluginContext with plugins accessor */
|
|
648
929
|
createContext: (input: PluginContextInput) => PluginContext;
|
|
930
|
+
/**
|
|
931
|
+
* Register a function to enhance every PluginContext during creation.
|
|
932
|
+
* Call this during plugin setup to inject properties into all request contexts.
|
|
933
|
+
*/
|
|
934
|
+
registerContextEnhancer: (enhancer: (context: PluginContext) => void) => void;
|
|
649
935
|
};
|
|
650
936
|
declare function createPluginExecutor(initialPlugins?: SpooshPlugin[]): PluginExecutor;
|
|
651
937
|
|
|
@@ -734,6 +1020,9 @@ type ExtractWriteOptions<T> = T extends SpooshPlugin<infer Types> ? Types extend
|
|
|
734
1020
|
type ExtractInfiniteReadOptions<T> = T extends SpooshPlugin<infer Types> ? Types extends {
|
|
735
1021
|
infiniteReadOptions: infer I;
|
|
736
1022
|
} ? I : object : object;
|
|
1023
|
+
type ExtractWriteTriggerOptions<T> = T extends SpooshPlugin<infer Types> ? Types extends {
|
|
1024
|
+
writeTriggerOptions: infer W;
|
|
1025
|
+
} ? W : object : object;
|
|
737
1026
|
type ExtractReadResult<T> = T extends SpooshPlugin<infer Types> ? Types extends {
|
|
738
1027
|
readResult: infer R;
|
|
739
1028
|
} ? R : object : object;
|
|
@@ -748,6 +1037,7 @@ type MergePluginOptions<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>
|
|
|
748
1037
|
read: UnionToIntersection<ExtractReadOptions<TPlugins[number]>>;
|
|
749
1038
|
write: UnionToIntersection<ExtractWriteOptions<TPlugins[number]>>;
|
|
750
1039
|
infiniteRead: UnionToIntersection<ExtractInfiniteReadOptions<TPlugins[number]>>;
|
|
1040
|
+
writeTrigger: UnionToIntersection<ExtractWriteTriggerOptions<TPlugins[number]>>;
|
|
751
1041
|
};
|
|
752
1042
|
type MergePluginResults<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
753
1043
|
read: UnionToIntersection<ExtractReadResult<TPlugins[number]>>;
|
|
@@ -1123,6 +1413,22 @@ type WritePathMethods<TSchema, TPath extends string, TDefaultError> = FindMatchi
|
|
|
1123
1413
|
* Used by useWrite and injectWrite hooks.
|
|
1124
1414
|
*/
|
|
1125
1415
|
type WriteClient<TSchema, TDefaultError = unknown> = <TPath extends WritePaths<TSchema> | (string & {})>(path: TPath) => HasWriteMethod<TSchema, TPath> extends true ? WritePathMethods<TSchema, TPath, TDefaultError> : never;
|
|
1416
|
+
/**
|
|
1417
|
+
* Method function type for write selectors - accepts no arguments.
|
|
1418
|
+
* All input (body, query, params) is passed to trigger() instead.
|
|
1419
|
+
*/
|
|
1420
|
+
type WriteSelectorMethodFn<TMethodConfig, TDefaultError, TUserPath extends string> = () => Promise<MethodResponse<TMethodConfig, TDefaultError, TUserPath>>;
|
|
1421
|
+
/**
|
|
1422
|
+
* Write selector path methods - methods accept no arguments.
|
|
1423
|
+
*/
|
|
1424
|
+
type WriteSelectorPathMethods<TSchema, TPath extends string, TDefaultError> = FindMatchingKey<TSchema, TPath> extends infer TKey ? TKey extends keyof TSchema ? Simplify<{
|
|
1425
|
+
[M in WriteMethod as M extends keyof TSchema[TKey] ? M : never]: M extends keyof TSchema[TKey] ? WriteSelectorMethodFn<TSchema[TKey][M], TDefaultError, TPath> : never;
|
|
1426
|
+
}> : never : never;
|
|
1427
|
+
/**
|
|
1428
|
+
* Write selector client - methods accept no arguments.
|
|
1429
|
+
* Used by useWrite for selecting endpoints. All input goes to trigger().
|
|
1430
|
+
*/
|
|
1431
|
+
type WriteSelectorClient<TSchema, TDefaultError = unknown> = <TPath extends WritePaths<TSchema> | (string & {})>(path: TPath) => HasWriteMethod<TSchema, TPath> extends true ? WriteSelectorPathMethods<TSchema, TPath, TDefaultError> : never;
|
|
1126
1432
|
|
|
1127
1433
|
type PluginArray = readonly SpooshPlugin<PluginTypeConfig>[];
|
|
1128
1434
|
interface SpooshConfig<TPlugins extends PluginArray = PluginArray> {
|
|
@@ -1156,7 +1462,7 @@ type SpooshInstance<TSchema = unknown, TDefaultError = unknown, TPlugins extends
|
|
|
1156
1462
|
* @example Basic usage
|
|
1157
1463
|
* ```ts
|
|
1158
1464
|
* const spoosh = new Spoosh<ApiSchema, Error>('/api')
|
|
1159
|
-
* .use([cachePlugin(),
|
|
1465
|
+
* .use([cachePlugin(), devtool()]);
|
|
1160
1466
|
*
|
|
1161
1467
|
* const { api } = client;
|
|
1162
1468
|
* const response = await api("posts").GET();
|
|
@@ -1180,7 +1486,7 @@ type SpooshInstance<TSchema = unknown, TDefaultError = unknown, TPlugins extends
|
|
|
1180
1486
|
*
|
|
1181
1487
|
* // In component
|
|
1182
1488
|
* const { data } = useRead((api) => api("posts").GET());
|
|
1183
|
-
* const { trigger } = useWrite((api) => api("posts").POST);
|
|
1489
|
+
* const { trigger } = useWrite((api) => api("posts").POST());
|
|
1184
1490
|
* ```
|
|
1185
1491
|
*
|
|
1186
1492
|
* @since 0.1.0
|
|
@@ -1217,12 +1523,12 @@ declare class Spoosh<TSchema = unknown, TError = unknown, TPlugins extends Plugi
|
|
|
1217
1523
|
/**
|
|
1218
1524
|
* Adds plugins to the Spoosh instance.
|
|
1219
1525
|
*
|
|
1220
|
-
* Returns a
|
|
1221
|
-
*
|
|
1526
|
+
* Returns a configured Spoosh instance with the specified plugins.
|
|
1527
|
+
* Can only be called once - the returned instance does not have `.use()`.
|
|
1222
1528
|
*
|
|
1223
1529
|
* @template TNewPlugins - The const tuple type of the new plugins array
|
|
1224
1530
|
* @param plugins - Array of plugin instances to use
|
|
1225
|
-
* @returns A
|
|
1531
|
+
* @returns A configured Spoosh instance (without `.use()` method)
|
|
1226
1532
|
*
|
|
1227
1533
|
* ```ts
|
|
1228
1534
|
* const spoosh = new Spoosh<Schema, Error>('/api').use([
|
|
@@ -1232,7 +1538,7 @@ declare class Spoosh<TSchema = unknown, TError = unknown, TPlugins extends Plugi
|
|
|
1232
1538
|
* ]);
|
|
1233
1539
|
* ```
|
|
1234
1540
|
*/
|
|
1235
|
-
use<const TNewPlugins extends PluginArray>(plugins: TNewPlugins): Spoosh<TSchema, TError, TNewPlugins>;
|
|
1541
|
+
use<const TNewPlugins extends PluginArray>(plugins: TNewPlugins): Omit<Spoosh<TSchema, TError, TNewPlugins>, "use">;
|
|
1236
1542
|
/**
|
|
1237
1543
|
* Cached instance of the underlying SpooshInstance.
|
|
1238
1544
|
* Created lazily on first property access.
|
|
@@ -1446,6 +1752,22 @@ declare function resolvePath(path: string[], params: Record<string, string | num
|
|
|
1446
1752
|
declare const isNetworkError: (err: unknown) => boolean;
|
|
1447
1753
|
declare const isAbortError: (err: unknown) => boolean;
|
|
1448
1754
|
|
|
1755
|
+
declare function clone<T>(value: T, seen?: WeakMap<WeakKey, any>): T;
|
|
1756
|
+
|
|
1757
|
+
/**
|
|
1758
|
+
* Creates a request-bound tracer for a plugin.
|
|
1759
|
+
* Use for middleware, afterResponse, and lifecycle hooks.
|
|
1760
|
+
*
|
|
1761
|
+
* @example
|
|
1762
|
+
* ```ts
|
|
1763
|
+
* const t = createTracer("spoosh:cache", context.trace);
|
|
1764
|
+
* t.return("Cache hit", { color: "success" });
|
|
1765
|
+
* t.log("Cached response", { color: "info", diff: { before, after } });
|
|
1766
|
+
* t.skip("No query params", { color: "muted" });
|
|
1767
|
+
* ```
|
|
1768
|
+
*/
|
|
1769
|
+
declare function createTracer(plugin: string, trace: Trace | undefined): RequestTracer;
|
|
1770
|
+
|
|
1449
1771
|
type ProxyHandlerConfig<TOptions = SpooshOptions> = {
|
|
1450
1772
|
baseUrl: string;
|
|
1451
1773
|
defaultOptions: TOptions;
|
|
@@ -1571,7 +1893,7 @@ declare function extractPathFromSelector(fn: unknown): string;
|
|
|
1571
1893
|
* @example
|
|
1572
1894
|
* ```ts
|
|
1573
1895
|
* const proxy = createSelectorProxy<ApiSchema>();
|
|
1574
|
-
* const method = extractMethodFromSelector(proxy("posts").POST);
|
|
1896
|
+
* const method = extractMethodFromSelector(proxy("posts").POST());
|
|
1575
1897
|
* // method = 'POST'
|
|
1576
1898
|
* ```
|
|
1577
1899
|
*/
|
|
@@ -1680,4 +2002,4 @@ type CreateInfiniteReadOptions<TData, TItem, TError, TRequest> = {
|
|
|
1680
2002
|
};
|
|
1681
2003
|
declare function createInfiniteReadController<TData, TItem, TError, TRequest extends InfiniteRequestOptions = InfiniteRequestOptions>(options: CreateInfiniteReadOptions<TData, TItem, TError, TRequest>): InfiniteReadController<TData, TItem, TError>;
|
|
1682
2004
|
|
|
1683
|
-
export { type AnyRequestOptions, type ApiSchema, type BuiltInEvents, type CacheEntry, type CacheEntryWithKey, type CapturedCall, type ComputeRequestOptions, type CoreRequestOptionsBase, type CreateInfiniteReadOptions, type CreateOperationOptions, type DataAwareCallback, type DataAwareTransform, type EventEmitter, type ExtractBody$1 as ExtractBody, type ExtractData, type ExtractError, type ExtractMethodOptions, type ExtractParamNames, type ExtractQuery$1 as ExtractQuery, type FetchDirection, type FetchExecutor, type FindMatchingKey, HTTP_METHODS, type HasParams, type HasReadMethod, type HasWriteMethod, type HeadersInitOrGetter, type HttpMethod, type HttpMethodKey, type InfiniteReadController, type InfiniteReadState, type InfiniteRequestOptions, type InstanceApiContext, type InstanceApiResolvers, type InstancePluginExecutor, type LifecyclePhase, type MergePluginInstanceApi, type MergePluginOptions, type MergePluginResults, type MethodOptionsMap, type OperationController, type OperationState, type OperationType, type PageContext, type PluginAccessor, type PluginArray, type PluginContext, type PluginContextInput, type PluginExecutor, type PluginExportsRegistry, type PluginFactory, type PluginHandler, type PluginLifecycle, type PluginMiddleware, type PluginRegistry, type PluginRequestOptions, type PluginResolvers, type PluginResponseHandler, type PluginResultResolvers, type PluginTypeConfig, type PluginUpdateHandler, type ReadClient, type ReadPaths, type ReadSchemaHelper, type RefetchEvent, type RequestOptions$1 as RequestOptions, type ResolveInstanceApi, type ResolveResultTypes, type ResolveSchemaTypes, type ResolveTypes, type ResolverContext, type SchemaPaths, type SelectedEndpoint, type SelectorFunction, type SelectorResult, type Simplify, Spoosh, type SpooshBody, type SpooshClient, type SpooshConfig, type SpooshInstance, type SpooshOptions, type SpooshOptionsInput, type SpooshPlugin, type SpooshResponse, type SpooshSchema, type StateManager, type StripPrefix, type TagMode, type TagOptions, type Transport, type TransportOption, type TransportOptionsMap, type TransportResponse, type WriteClient, type WriteMethod, type WritePaths, type WriteSchemaHelper, __DEV__, buildUrl, containsFile, createClient, createEventEmitter, createInfiniteReadController, createInitialState, createOperationController, createPluginExecutor, createPluginRegistry, createProxyHandler, createSelectorProxy, createStateManager, executeFetch, extractMethodFromSelector, extractPathFromSelector, fetchTransport, form, generateTags, getContentType, isAbortError, isJsonBody, isNetworkError, isSpooshBody, json, mergeHeaders, objectToFormData, objectToUrlEncoded, resolveHeadersToRecord, resolvePath, resolveRequestBody, resolveTags, setHeaders, sortObjectKeys, urlencoded, xhrTransport };
|
|
2005
|
+
export { type AnyRequestOptions, type ApiSchema, type BuiltInEvents, type CacheEntry, type CacheEntryWithKey, type CapturedCall, type ComputeRequestOptions, type CoreRequestOptionsBase, type CreateInfiniteReadOptions, type CreateOperationOptions, type DataAwareCallback, type DataAwareTransform, type DevtoolEvents, type EventEmitter, type EventListener, type EventOptions, type EventTracer, type ExtractBody$1 as ExtractBody, type ExtractData, type ExtractError, type ExtractMethodOptions, type ExtractParamNames, type ExtractQuery$1 as ExtractQuery, type FetchDirection, type FetchExecutor, type FindMatchingKey, HTTP_METHODS, type HasParams, type HasReadMethod, type HasWriteMethod, type HeadersInitOrGetter, type HttpMethod, type HttpMethodKey, type InfiniteReadController, type InfiniteReadState, type InfiniteRequestOptions, type InstanceApiContext, type InstanceApiResolvers, type InstancePluginExecutor, type LifecyclePhase, type MergePluginInstanceApi, type MergePluginOptions, type MergePluginResults, type MethodOptionsMap, type OperationController, type OperationState, type OperationType, type PageContext, type PluginAccessor, type PluginArray, type PluginContext, type PluginContextBase, type PluginContextExtensions, type PluginContextInput, type PluginExecutor, type PluginExportsRegistry, type PluginFactory, type PluginHandler, type PluginLifecycle, type PluginMiddleware, type PluginRegistry, type PluginRequestOptions, type PluginResolvers, type PluginResponseHandler, type PluginResultResolvers, type PluginTypeConfig, type PluginUpdateHandler, type ReadClient, type ReadPaths, type ReadSchemaHelper, type RefetchEvent, type RequestCompleteEvent, type RequestOptions$1 as RequestOptions, type RequestTracer, type ResolveInstanceApi, type ResolveResultTypes, type ResolveSchemaTypes, type ResolveTypes, type ResolverContext, type SchemaPaths, type SelectedEndpoint, type SelectorFunction, type SelectorResult, type SetupContext, type Simplify, Spoosh, type SpooshBody, type SpooshClient, type SpooshConfig, type SpooshInstance, type SpooshOptions, type SpooshOptionsInput, type SpooshPlugin, type SpooshResponse, type SpooshSchema, type StandaloneEvent, type StateManager, type StripPrefix, type TagMode, type TagOptions, type Trace, type TraceColor, type TraceEvent, type TraceInfo, type TraceListener, type TraceOptions, type TraceStage, type Transport, type TransportOption, type TransportOptionsMap, type TransportResponse, type WriteClient, type WriteMethod, type WritePaths, type WriteSchemaHelper, type WriteSelectorClient, __DEV__, buildUrl, clone, containsFile, createClient, createEventEmitter, createInfiniteReadController, createInitialState, createOperationController, createPluginExecutor, createPluginRegistry, createProxyHandler, createSelectorProxy, createStateManager, createTracer, executeFetch, extractMethodFromSelector, extractPathFromSelector, fetchTransport, form, generateTags, getContentType, isAbortError, isJsonBody, isNetworkError, isSpooshBody, json, mergeHeaders, objectToFormData, objectToUrlEncoded, resolveHeadersToRecord, resolvePath, resolveRequestBody, resolveTags, setHeaders, sortObjectKeys, urlencoded, xhrTransport };
|