@spoosh/react 0.5.0 → 0.5.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 +58 -53
- package/dist/index.d.ts +58 -53
- package/dist/index.js +24 -15
- package/dist/index.mjs +24 -15
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ReadClient, TagMode, SpooshResponse, WriteClient, SpooshPlugin, PluginTypeConfig, SpooshOptions, MergePluginResults, StateManager, EventEmitter, PluginExecutor, PluginArray, MergePluginOptions, ResolverContext, ResolveResultTypes, MergePluginInstanceApi } from '@spoosh/core';
|
|
1
|
+
import { ReadClient, TagMode, SpooshResponse, WriteClient, SpooshPlugin, PluginTypeConfig, SpooshOptions, MergePluginResults, StateManager, EventEmitter, PluginExecutor, PluginArray, ResolveTypes, MergePluginOptions, ResolverContext, ResolveResultTypes, MergePluginInstanceApi } from '@spoosh/core';
|
|
3
2
|
|
|
4
3
|
type PluginHooksConfig<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
5
4
|
baseUrl: string;
|
|
@@ -22,15 +21,14 @@ type BaseReadOptions = {
|
|
|
22
21
|
*/
|
|
23
22
|
tags?: TagMode | (TagModeInArray | (string & {}))[];
|
|
24
23
|
};
|
|
25
|
-
|
|
26
24
|
/**
|
|
27
25
|
* Result returned by `useRead` hook.
|
|
28
26
|
*
|
|
29
27
|
* @template TData - The response data type
|
|
30
28
|
* @template TError - The error type
|
|
31
|
-
* @template
|
|
29
|
+
* @template TMeta - Plugin-provided metadata fields
|
|
32
30
|
*/
|
|
33
|
-
type BaseReadResult<TData, TError,
|
|
31
|
+
type BaseReadResult<TData, TError, TMeta = Record<string, unknown>> = {
|
|
34
32
|
/** True during the initial load (no data yet) */
|
|
35
33
|
loading: boolean;
|
|
36
34
|
/** True during any fetch operation */
|
|
@@ -40,7 +38,7 @@ type BaseReadResult<TData, TError, TPluginResult = Record<string, unknown>> = {
|
|
|
40
38
|
/** Error from the last failed request */
|
|
41
39
|
error: TError | undefined;
|
|
42
40
|
/** Plugin-provided metadata */
|
|
43
|
-
meta:
|
|
41
|
+
meta: TMeta;
|
|
44
42
|
/** Abort the current fetch operation */
|
|
45
43
|
abort: () => void;
|
|
46
44
|
/** Manually trigger a refetch */
|
|
@@ -52,9 +50,9 @@ type BaseReadResult<TData, TError, TPluginResult = Record<string, unknown>> = {
|
|
|
52
50
|
* @template TData - The response data type
|
|
53
51
|
* @template TError - The error type
|
|
54
52
|
* @template TOptions - The trigger options type
|
|
55
|
-
* @template
|
|
53
|
+
* @template TMeta - Plugin-provided metadata fields
|
|
56
54
|
*/
|
|
57
|
-
type BaseWriteResult<TData, TError, TOptions,
|
|
55
|
+
type BaseWriteResult<TData, TError, TOptions, TMeta = Record<string, unknown>> = {
|
|
58
56
|
/** Execute the mutation with optional options */
|
|
59
57
|
trigger: (options?: TOptions) => Promise<SpooshResponse<TData, TError>>;
|
|
60
58
|
/** True while the mutation is in progress */
|
|
@@ -64,7 +62,7 @@ type BaseWriteResult<TData, TError, TOptions, TPluginResult = Record<string, unk
|
|
|
64
62
|
/** Error from the last failed request */
|
|
65
63
|
error: TError | undefined;
|
|
66
64
|
/** Plugin-provided metadata */
|
|
67
|
-
meta:
|
|
65
|
+
meta: TMeta;
|
|
68
66
|
/** Reset the state to initial values */
|
|
69
67
|
reset: () => void;
|
|
70
68
|
/** Abort the current mutation */
|
|
@@ -89,8 +87,8 @@ type InputFields<TQuery, TBody, TParamNames extends string> = OptionalQueryField
|
|
|
89
87
|
type WriteResponseInputFields<TQuery, TBody, TParamNames extends string> = [TQuery, TBody, TParamNames] extends [never, never, never] ? object : {
|
|
90
88
|
input: InputFields<TQuery, TBody, TParamNames> | undefined;
|
|
91
89
|
};
|
|
92
|
-
type UseReadResult<TData, TError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseReadResult<TData, TError> & MergePluginResults<TPlugins>["read"];
|
|
93
|
-
type UseWriteResult<TData, TError, TOptions, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseWriteResult<TData, TError, TOptions> & MergePluginResults<TPlugins>["write"];
|
|
90
|
+
type UseReadResult<TData, TError, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseReadResult<TData, TError, TMeta> & MergePluginResults<TPlugins>["read"];
|
|
91
|
+
type UseWriteResult<TData, TError, TOptions, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseWriteResult<TData, TError, TOptions, TMeta> & MergePluginResults<TPlugins>["write"];
|
|
94
92
|
type ReadApiClient<TSchema, TDefaultError> = ReadClient<TSchema, TDefaultError>;
|
|
95
93
|
type WriteApiClient<TSchema, TDefaultError> = WriteClient<TSchema, TDefaultError>;
|
|
96
94
|
type SuccessResponse<T> = Extract<T, {
|
|
@@ -250,16 +248,21 @@ type UseInfiniteReadResult<TData, TError, TItem, TPlugins extends readonly Spoos
|
|
|
250
248
|
type InfiniteReadApiClient<TSchema, TDefaultError> = ReadClient<TSchema, TDefaultError>;
|
|
251
249
|
|
|
252
250
|
type InferError<T, TDefaultError> = [T] extends [unknown] ? TDefaultError : T;
|
|
253
|
-
type ExtractParamsRecord<T> = ExtractResponseParamNames<T> extends never ? never : Record<ExtractResponseParamNames<T>, string | number>;
|
|
254
|
-
type ReadResolverContext<TSchema, TReadFn, TDefaultError> = ResolverContext<TSchema, ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractParamsRecord<TReadFn>>;
|
|
255
|
-
type ResolvedReadOptions<TSchema, TPlugins extends PluginArray, TReadFn, TDefaultError> = _spoosh_core.ResolveTypes<MergePluginOptions<TPlugins>["read"], ReadResolverContext<TSchema, TReadFn, TDefaultError>>;
|
|
256
251
|
type WriteResolverContext<TSchema, TMethod, TDefaultError> = ResolverContext<TSchema, ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>;
|
|
257
|
-
type ResolvedWriteOptions<TSchema, TPlugins extends PluginArray, TMethod, TDefaultError> =
|
|
258
|
-
type UseReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> =
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
252
|
+
type ResolvedWriteOptions<TSchema, TPlugins extends PluginArray, TMethod, TDefaultError> = ResolveTypes<MergePluginOptions<TPlugins>["write"], WriteResolverContext<TSchema, TMethod, TDefaultError>>;
|
|
253
|
+
type UseReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
254
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
255
|
+
data?: unknown;
|
|
256
|
+
error?: unknown;
|
|
257
|
+
}>, TReadOpts>(readFn: TReadFn, readOptions: TReadOpts & BaseReadOptions & ResolveTypes<MergePluginOptions<TPlugins>["read"], ResolverContext<TSchema, ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn> extends never ? never : Record<ExtractResponseParamNames<TReadFn>, string | number>>>): BaseReadResult<ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, ResolveResultTypes<MergePluginResults<TPlugins>["read"], TReadOpts>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
258
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
259
|
+
data?: unknown;
|
|
260
|
+
error?: unknown;
|
|
261
|
+
}>>(readFn: TReadFn): BaseReadResult<ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, MergePluginResults<TPlugins>["read"]> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
262
|
+
};
|
|
263
|
+
type UseWriteFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
264
|
+
<TMethod extends (...args: never) => Promise<SpooshResponse<unknown, unknown>>>(writeFn: (api: WriteApiClient<TSchema, TDefaultError>) => TMethod): BaseWriteResult<ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, Parameters<TMethod>[0] & ResolvedWriteOptions<TSchema, TPlugins, TMethod, TDefaultError>, MergePluginResults<TPlugins>["write"]> & WriteResponseInputFields<ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
265
|
+
};
|
|
263
266
|
type InfiniteReadResolverContext<TSchema, TData, TError, TRequest> = ResolverContext<TSchema, TData, TError, TRequest extends {
|
|
264
267
|
query: infer Q;
|
|
265
268
|
} ? Q : never, TRequest extends {
|
|
@@ -267,7 +270,7 @@ type InfiniteReadResolverContext<TSchema, TData, TError, TRequest> = ResolverCon
|
|
|
267
270
|
} ? B : never, TRequest extends {
|
|
268
271
|
params: infer P;
|
|
269
272
|
} ? P : never>;
|
|
270
|
-
type ResolvedInfiniteReadOptions<TSchema, TPlugins extends PluginArray, TData, TError, TRequest> =
|
|
273
|
+
type ResolvedInfiniteReadOptions<TSchema, TPlugins extends PluginArray, TData, TError, TRequest> = ResolveTypes<MergePluginOptions<TPlugins>["infiniteRead"], InfiniteReadResolverContext<TSchema, TData, TError, TRequest>>;
|
|
271
274
|
type UseInfiniteReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> = <TData, TItem, TError = TDefaultError, TRequest extends AnyInfiniteRequestOptions = AnyInfiniteRequestOptions>(readFn: (api: InfiniteReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<TData, TError>>, readOptions: BaseInfiniteReadOptions<TData, TItem, TRequest> & ResolvedInfiniteReadOptions<TSchema, TPlugins, TData, TError, TRequest>) => BaseInfiniteReadResult<TData, TError, TItem, MergePluginResults<TPlugins>["read"]>;
|
|
272
275
|
/**
|
|
273
276
|
* Spoosh React hooks interface containing useRead, useWrite, and useInfiniteRead.
|
|
@@ -375,42 +378,44 @@ type SpooshInstanceShape<TApi, TSchema, TDefaultError, TPlugins> = {
|
|
|
375
378
|
*/
|
|
376
379
|
declare function createReactSpoosh<TSchema, TDefaultError, TPlugins extends PluginArray, TApi>(instance: SpooshInstanceShape<TApi, TSchema, TDefaultError, TPlugins>): SpooshReactHooks<TDefaultError, TSchema, TPlugins>;
|
|
377
380
|
|
|
378
|
-
declare function createUseRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">):
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
readOptions: infer
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
381
|
+
declare function createUseRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): {
|
|
382
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
383
|
+
data?: unknown;
|
|
384
|
+
error?: unknown;
|
|
385
|
+
}>, TReadOpts>(readFn: TReadFn, readOptions: TReadOpts & BaseReadOptions & ResolveTypes<((TPlugins[number] extends infer T ? T extends TPlugins[number] ? T extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
386
|
+
readOptions: infer R;
|
|
387
|
+
} ? R : object : object : never : never) extends infer T_1 ? T_1 extends (TPlugins[number] extends infer T_2 ? T_2 extends TPlugins[number] ? T_2 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
388
|
+
readOptions: infer R;
|
|
389
|
+
} ? R : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, TReadFn extends (...args: unknown[]) => Promise<{
|
|
390
|
+
data: infer D;
|
|
391
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
392
|
+
error: infer E;
|
|
393
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
394
|
+
error: infer E;
|
|
395
|
+
}> ? E : unknown, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn> extends never ? never : Record<ExtractResponseParamNames<TReadFn>, string | number>>>): BaseReadResult<TReadFn extends (...args: unknown[]) => Promise<{
|
|
396
|
+
data: infer D;
|
|
397
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
398
|
+
error: infer E;
|
|
399
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
400
|
+
error: infer E;
|
|
401
|
+
}> ? E : unknown, ResolveResultTypes<MergePluginResults<TPlugins>["read"], TReadOpts>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
402
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
403
|
+
data?: unknown;
|
|
404
|
+
error?: unknown;
|
|
405
|
+
}>>(readFn: TReadFn): BaseReadResult<TReadFn extends (...args: unknown[]) => Promise<{
|
|
406
|
+
data: infer D;
|
|
407
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
408
|
+
error: infer E;
|
|
409
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
410
|
+
error: infer E;
|
|
411
|
+
}> ? E : unknown, MergePluginResults<TPlugins>["read"]> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
412
|
+
};
|
|
400
413
|
|
|
401
|
-
declare function createUseWrite<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TMethod extends (...args: never[]) => Promise<SpooshResponse<unknown, unknown>>>(writeFn: (api: WriteApiClient<TSchema, TDefaultError>) => TMethod) => BaseWriteResult<ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodOptions<TMethod> &
|
|
414
|
+
declare function createUseWrite<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TMethod extends (...args: never[]) => Promise<SpooshResponse<unknown, unknown>>>(writeFn: (api: WriteApiClient<TSchema, TDefaultError>) => TMethod) => BaseWriteResult<ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodOptions<TMethod> & ResolveTypes<((TPlugins[number] extends infer T ? T extends TPlugins[number] ? T extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
402
415
|
writeOptions: infer W;
|
|
403
416
|
} ? W : object : object : never : never) extends infer T_1 ? T_1 extends (TPlugins[number] extends infer T_2 ? T_2 extends TPlugins[number] ? T_2 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
404
417
|
writeOptions: infer W;
|
|
405
|
-
} ? W : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>>,
|
|
406
|
-
writeResult: infer W_1;
|
|
407
|
-
} ? W_1 : object : object : never : never) extends infer T_4 ? T_4 extends (TPlugins[number] extends infer T_5 ? T_5 extends TPlugins[number] ? T_5 extends SpooshPlugin<infer Types_1 extends PluginTypeConfig> ? Types_1 extends {
|
|
408
|
-
writeResult: infer W_1;
|
|
409
|
-
} ? W_1 : object : object : never : never) ? T_4 extends unknown ? (x: T_4) => void : never : never : never) extends (x: infer I) => void ? I : never, ExtractMethodOptions<TMethod> & _spoosh_core.ResolveTypes<((TPlugins[number] extends infer T_6 ? T_6 extends TPlugins[number] ? T_6 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
410
|
-
writeOptions: infer W;
|
|
411
|
-
} ? W : object : object : never : never) extends infer T_7 ? T_7 extends (TPlugins[number] extends infer T_8 ? T_8 extends TPlugins[number] ? T_8 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
412
|
-
writeOptions: infer W;
|
|
413
|
-
} ? W : object : object : never : never) ? T_7 extends unknown ? (x: T_7) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>>>> & WriteResponseInputFields<ExtractResponseQuery<TMethod>, ExtractResponseBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
418
|
+
} ? W : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>>, MergePluginResults<TPlugins>["write"]> & WriteResponseInputFields<ExtractResponseQuery<TMethod>, ExtractResponseBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
414
419
|
|
|
415
420
|
declare function createUseInfiniteRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TData, TItem, TError = TDefaultError, TRequest extends AnyInfiniteRequestOptions = AnyInfiniteRequestOptions>(readFn: (api: InfiniteReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<TData, TError>>, readOptions: BaseInfiniteReadOptions<TData, TItem, TRequest> & (((TPlugins[number] extends infer T_3 ? T_3 extends TPlugins[number] ? T_3 extends SpooshPlugin<infer Types_1 extends PluginTypeConfig> ? Types_1 extends {
|
|
416
421
|
infiniteReadOptions: infer I_1;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ReadClient, TagMode, SpooshResponse, WriteClient, SpooshPlugin, PluginTypeConfig, SpooshOptions, MergePluginResults, StateManager, EventEmitter, PluginExecutor, PluginArray, MergePluginOptions, ResolverContext, ResolveResultTypes, MergePluginInstanceApi } from '@spoosh/core';
|
|
1
|
+
import { ReadClient, TagMode, SpooshResponse, WriteClient, SpooshPlugin, PluginTypeConfig, SpooshOptions, MergePluginResults, StateManager, EventEmitter, PluginExecutor, PluginArray, ResolveTypes, MergePluginOptions, ResolverContext, ResolveResultTypes, MergePluginInstanceApi } from '@spoosh/core';
|
|
3
2
|
|
|
4
3
|
type PluginHooksConfig<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
5
4
|
baseUrl: string;
|
|
@@ -22,15 +21,14 @@ type BaseReadOptions = {
|
|
|
22
21
|
*/
|
|
23
22
|
tags?: TagMode | (TagModeInArray | (string & {}))[];
|
|
24
23
|
};
|
|
25
|
-
|
|
26
24
|
/**
|
|
27
25
|
* Result returned by `useRead` hook.
|
|
28
26
|
*
|
|
29
27
|
* @template TData - The response data type
|
|
30
28
|
* @template TError - The error type
|
|
31
|
-
* @template
|
|
29
|
+
* @template TMeta - Plugin-provided metadata fields
|
|
32
30
|
*/
|
|
33
|
-
type BaseReadResult<TData, TError,
|
|
31
|
+
type BaseReadResult<TData, TError, TMeta = Record<string, unknown>> = {
|
|
34
32
|
/** True during the initial load (no data yet) */
|
|
35
33
|
loading: boolean;
|
|
36
34
|
/** True during any fetch operation */
|
|
@@ -40,7 +38,7 @@ type BaseReadResult<TData, TError, TPluginResult = Record<string, unknown>> = {
|
|
|
40
38
|
/** Error from the last failed request */
|
|
41
39
|
error: TError | undefined;
|
|
42
40
|
/** Plugin-provided metadata */
|
|
43
|
-
meta:
|
|
41
|
+
meta: TMeta;
|
|
44
42
|
/** Abort the current fetch operation */
|
|
45
43
|
abort: () => void;
|
|
46
44
|
/** Manually trigger a refetch */
|
|
@@ -52,9 +50,9 @@ type BaseReadResult<TData, TError, TPluginResult = Record<string, unknown>> = {
|
|
|
52
50
|
* @template TData - The response data type
|
|
53
51
|
* @template TError - The error type
|
|
54
52
|
* @template TOptions - The trigger options type
|
|
55
|
-
* @template
|
|
53
|
+
* @template TMeta - Plugin-provided metadata fields
|
|
56
54
|
*/
|
|
57
|
-
type BaseWriteResult<TData, TError, TOptions,
|
|
55
|
+
type BaseWriteResult<TData, TError, TOptions, TMeta = Record<string, unknown>> = {
|
|
58
56
|
/** Execute the mutation with optional options */
|
|
59
57
|
trigger: (options?: TOptions) => Promise<SpooshResponse<TData, TError>>;
|
|
60
58
|
/** True while the mutation is in progress */
|
|
@@ -64,7 +62,7 @@ type BaseWriteResult<TData, TError, TOptions, TPluginResult = Record<string, unk
|
|
|
64
62
|
/** Error from the last failed request */
|
|
65
63
|
error: TError | undefined;
|
|
66
64
|
/** Plugin-provided metadata */
|
|
67
|
-
meta:
|
|
65
|
+
meta: TMeta;
|
|
68
66
|
/** Reset the state to initial values */
|
|
69
67
|
reset: () => void;
|
|
70
68
|
/** Abort the current mutation */
|
|
@@ -89,8 +87,8 @@ type InputFields<TQuery, TBody, TParamNames extends string> = OptionalQueryField
|
|
|
89
87
|
type WriteResponseInputFields<TQuery, TBody, TParamNames extends string> = [TQuery, TBody, TParamNames] extends [never, never, never] ? object : {
|
|
90
88
|
input: InputFields<TQuery, TBody, TParamNames> | undefined;
|
|
91
89
|
};
|
|
92
|
-
type UseReadResult<TData, TError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseReadResult<TData, TError> & MergePluginResults<TPlugins>["read"];
|
|
93
|
-
type UseWriteResult<TData, TError, TOptions, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseWriteResult<TData, TError, TOptions> & MergePluginResults<TPlugins>["write"];
|
|
90
|
+
type UseReadResult<TData, TError, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseReadResult<TData, TError, TMeta> & MergePluginResults<TPlugins>["read"];
|
|
91
|
+
type UseWriteResult<TData, TError, TOptions, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseWriteResult<TData, TError, TOptions, TMeta> & MergePluginResults<TPlugins>["write"];
|
|
94
92
|
type ReadApiClient<TSchema, TDefaultError> = ReadClient<TSchema, TDefaultError>;
|
|
95
93
|
type WriteApiClient<TSchema, TDefaultError> = WriteClient<TSchema, TDefaultError>;
|
|
96
94
|
type SuccessResponse<T> = Extract<T, {
|
|
@@ -250,16 +248,21 @@ type UseInfiniteReadResult<TData, TError, TItem, TPlugins extends readonly Spoos
|
|
|
250
248
|
type InfiniteReadApiClient<TSchema, TDefaultError> = ReadClient<TSchema, TDefaultError>;
|
|
251
249
|
|
|
252
250
|
type InferError<T, TDefaultError> = [T] extends [unknown] ? TDefaultError : T;
|
|
253
|
-
type ExtractParamsRecord<T> = ExtractResponseParamNames<T> extends never ? never : Record<ExtractResponseParamNames<T>, string | number>;
|
|
254
|
-
type ReadResolverContext<TSchema, TReadFn, TDefaultError> = ResolverContext<TSchema, ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractParamsRecord<TReadFn>>;
|
|
255
|
-
type ResolvedReadOptions<TSchema, TPlugins extends PluginArray, TReadFn, TDefaultError> = _spoosh_core.ResolveTypes<MergePluginOptions<TPlugins>["read"], ReadResolverContext<TSchema, TReadFn, TDefaultError>>;
|
|
256
251
|
type WriteResolverContext<TSchema, TMethod, TDefaultError> = ResolverContext<TSchema, ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>;
|
|
257
|
-
type ResolvedWriteOptions<TSchema, TPlugins extends PluginArray, TMethod, TDefaultError> =
|
|
258
|
-
type UseReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> =
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
252
|
+
type ResolvedWriteOptions<TSchema, TPlugins extends PluginArray, TMethod, TDefaultError> = ResolveTypes<MergePluginOptions<TPlugins>["write"], WriteResolverContext<TSchema, TMethod, TDefaultError>>;
|
|
253
|
+
type UseReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
254
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
255
|
+
data?: unknown;
|
|
256
|
+
error?: unknown;
|
|
257
|
+
}>, TReadOpts>(readFn: TReadFn, readOptions: TReadOpts & BaseReadOptions & ResolveTypes<MergePluginOptions<TPlugins>["read"], ResolverContext<TSchema, ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn> extends never ? never : Record<ExtractResponseParamNames<TReadFn>, string | number>>>): BaseReadResult<ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, ResolveResultTypes<MergePluginResults<TPlugins>["read"], TReadOpts>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
258
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
259
|
+
data?: unknown;
|
|
260
|
+
error?: unknown;
|
|
261
|
+
}>>(readFn: TReadFn): BaseReadResult<ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, MergePluginResults<TPlugins>["read"]> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
262
|
+
};
|
|
263
|
+
type UseWriteFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
264
|
+
<TMethod extends (...args: never) => Promise<SpooshResponse<unknown, unknown>>>(writeFn: (api: WriteApiClient<TSchema, TDefaultError>) => TMethod): BaseWriteResult<ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, Parameters<TMethod>[0] & ResolvedWriteOptions<TSchema, TPlugins, TMethod, TDefaultError>, MergePluginResults<TPlugins>["write"]> & WriteResponseInputFields<ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
265
|
+
};
|
|
263
266
|
type InfiniteReadResolverContext<TSchema, TData, TError, TRequest> = ResolverContext<TSchema, TData, TError, TRequest extends {
|
|
264
267
|
query: infer Q;
|
|
265
268
|
} ? Q : never, TRequest extends {
|
|
@@ -267,7 +270,7 @@ type InfiniteReadResolverContext<TSchema, TData, TError, TRequest> = ResolverCon
|
|
|
267
270
|
} ? B : never, TRequest extends {
|
|
268
271
|
params: infer P;
|
|
269
272
|
} ? P : never>;
|
|
270
|
-
type ResolvedInfiniteReadOptions<TSchema, TPlugins extends PluginArray, TData, TError, TRequest> =
|
|
273
|
+
type ResolvedInfiniteReadOptions<TSchema, TPlugins extends PluginArray, TData, TError, TRequest> = ResolveTypes<MergePluginOptions<TPlugins>["infiniteRead"], InfiniteReadResolverContext<TSchema, TData, TError, TRequest>>;
|
|
271
274
|
type UseInfiniteReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> = <TData, TItem, TError = TDefaultError, TRequest extends AnyInfiniteRequestOptions = AnyInfiniteRequestOptions>(readFn: (api: InfiniteReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<TData, TError>>, readOptions: BaseInfiniteReadOptions<TData, TItem, TRequest> & ResolvedInfiniteReadOptions<TSchema, TPlugins, TData, TError, TRequest>) => BaseInfiniteReadResult<TData, TError, TItem, MergePluginResults<TPlugins>["read"]>;
|
|
272
275
|
/**
|
|
273
276
|
* Spoosh React hooks interface containing useRead, useWrite, and useInfiniteRead.
|
|
@@ -375,42 +378,44 @@ type SpooshInstanceShape<TApi, TSchema, TDefaultError, TPlugins> = {
|
|
|
375
378
|
*/
|
|
376
379
|
declare function createReactSpoosh<TSchema, TDefaultError, TPlugins extends PluginArray, TApi>(instance: SpooshInstanceShape<TApi, TSchema, TDefaultError, TPlugins>): SpooshReactHooks<TDefaultError, TSchema, TPlugins>;
|
|
377
380
|
|
|
378
|
-
declare function createUseRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">):
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
readOptions: infer
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
381
|
+
declare function createUseRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): {
|
|
382
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
383
|
+
data?: unknown;
|
|
384
|
+
error?: unknown;
|
|
385
|
+
}>, TReadOpts>(readFn: TReadFn, readOptions: TReadOpts & BaseReadOptions & ResolveTypes<((TPlugins[number] extends infer T ? T extends TPlugins[number] ? T extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
386
|
+
readOptions: infer R;
|
|
387
|
+
} ? R : object : object : never : never) extends infer T_1 ? T_1 extends (TPlugins[number] extends infer T_2 ? T_2 extends TPlugins[number] ? T_2 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
388
|
+
readOptions: infer R;
|
|
389
|
+
} ? R : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, TReadFn extends (...args: unknown[]) => Promise<{
|
|
390
|
+
data: infer D;
|
|
391
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
392
|
+
error: infer E;
|
|
393
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
394
|
+
error: infer E;
|
|
395
|
+
}> ? E : unknown, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn> extends never ? never : Record<ExtractResponseParamNames<TReadFn>, string | number>>>): BaseReadResult<TReadFn extends (...args: unknown[]) => Promise<{
|
|
396
|
+
data: infer D;
|
|
397
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
398
|
+
error: infer E;
|
|
399
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
400
|
+
error: infer E;
|
|
401
|
+
}> ? E : unknown, ResolveResultTypes<MergePluginResults<TPlugins>["read"], TReadOpts>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
402
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
403
|
+
data?: unknown;
|
|
404
|
+
error?: unknown;
|
|
405
|
+
}>>(readFn: TReadFn): BaseReadResult<TReadFn extends (...args: unknown[]) => Promise<{
|
|
406
|
+
data: infer D;
|
|
407
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
408
|
+
error: infer E;
|
|
409
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
410
|
+
error: infer E;
|
|
411
|
+
}> ? E : unknown, MergePluginResults<TPlugins>["read"]> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
412
|
+
};
|
|
400
413
|
|
|
401
|
-
declare function createUseWrite<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TMethod extends (...args: never[]) => Promise<SpooshResponse<unknown, unknown>>>(writeFn: (api: WriteApiClient<TSchema, TDefaultError>) => TMethod) => BaseWriteResult<ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodOptions<TMethod> &
|
|
414
|
+
declare function createUseWrite<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TMethod extends (...args: never[]) => Promise<SpooshResponse<unknown, unknown>>>(writeFn: (api: WriteApiClient<TSchema, TDefaultError>) => TMethod) => BaseWriteResult<ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodOptions<TMethod> & ResolveTypes<((TPlugins[number] extends infer T ? T extends TPlugins[number] ? T extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
402
415
|
writeOptions: infer W;
|
|
403
416
|
} ? W : object : object : never : never) extends infer T_1 ? T_1 extends (TPlugins[number] extends infer T_2 ? T_2 extends TPlugins[number] ? T_2 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
404
417
|
writeOptions: infer W;
|
|
405
|
-
} ? W : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>>,
|
|
406
|
-
writeResult: infer W_1;
|
|
407
|
-
} ? W_1 : object : object : never : never) extends infer T_4 ? T_4 extends (TPlugins[number] extends infer T_5 ? T_5 extends TPlugins[number] ? T_5 extends SpooshPlugin<infer Types_1 extends PluginTypeConfig> ? Types_1 extends {
|
|
408
|
-
writeResult: infer W_1;
|
|
409
|
-
} ? W_1 : object : object : never : never) ? T_4 extends unknown ? (x: T_4) => void : never : never : never) extends (x: infer I) => void ? I : never, ExtractMethodOptions<TMethod> & _spoosh_core.ResolveTypes<((TPlugins[number] extends infer T_6 ? T_6 extends TPlugins[number] ? T_6 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
410
|
-
writeOptions: infer W;
|
|
411
|
-
} ? W : object : object : never : never) extends infer T_7 ? T_7 extends (TPlugins[number] extends infer T_8 ? T_8 extends TPlugins[number] ? T_8 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
412
|
-
writeOptions: infer W;
|
|
413
|
-
} ? W : object : object : never : never) ? T_7 extends unknown ? (x: T_7) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>>>> & WriteResponseInputFields<ExtractResponseQuery<TMethod>, ExtractResponseBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
418
|
+
} ? W : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>>, MergePluginResults<TPlugins>["write"]> & WriteResponseInputFields<ExtractResponseQuery<TMethod>, ExtractResponseBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
414
419
|
|
|
415
420
|
declare function createUseInfiniteRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TData, TItem, TError = TDefaultError, TRequest extends AnyInfiniteRequestOptions = AnyInfiniteRequestOptions>(readFn: (api: InfiniteReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<TData, TError>>, readOptions: BaseInfiniteReadOptions<TData, TItem, TRequest> & (((TPlugins[number] extends infer T_3 ? T_3 extends TPlugins[number] ? T_3 extends SpooshPlugin<infer Types_1 extends PluginTypeConfig> ? Types_1 extends {
|
|
416
421
|
infiniteReadOptions: infer I_1;
|
package/dist/index.js
CHANGED
|
@@ -34,15 +34,19 @@ var import_react = require("react");
|
|
|
34
34
|
var import_core = require("@spoosh/core");
|
|
35
35
|
function createUseRead(options) {
|
|
36
36
|
const { api, stateManager, eventEmitter, pluginExecutor } = options;
|
|
37
|
-
|
|
38
|
-
const {
|
|
37
|
+
function useRead(readFn, readOptions) {
|
|
38
|
+
const {
|
|
39
|
+
enabled = true,
|
|
40
|
+
tags,
|
|
41
|
+
...pluginOpts
|
|
42
|
+
} = readOptions ?? {};
|
|
39
43
|
const hookId = (0, import_react.useId)();
|
|
40
44
|
const selectorResultRef = (0, import_react.useRef)({
|
|
41
45
|
call: null,
|
|
42
46
|
selector: null
|
|
43
47
|
});
|
|
44
|
-
const selectorProxy = (0, import_core.createSelectorProxy)((
|
|
45
|
-
selectorResultRef.current =
|
|
48
|
+
const selectorProxy = (0, import_core.createSelectorProxy)((result) => {
|
|
49
|
+
selectorResultRef.current = result;
|
|
46
50
|
});
|
|
47
51
|
readFn(selectorProxy);
|
|
48
52
|
const capturedCall = selectorResultRef.current.call;
|
|
@@ -102,6 +106,7 @@ function createUseRead(options) {
|
|
|
102
106
|
const abortRef = (0, import_react.useRef)(controller.abort);
|
|
103
107
|
abortRef.current = controller.abort;
|
|
104
108
|
const pluginOptsKey = JSON.stringify(pluginOpts);
|
|
109
|
+
const tagsKey = JSON.stringify(tags);
|
|
105
110
|
const executeWithTracking = (0, import_react.useCallback)(
|
|
106
111
|
async (force = false) => {
|
|
107
112
|
setRequestState((prev) => ({ ...prev, isPending: true }));
|
|
@@ -157,7 +162,7 @@ function createUseRead(options) {
|
|
|
157
162
|
unsubRefetch();
|
|
158
163
|
unsubInvalidate();
|
|
159
164
|
};
|
|
160
|
-
}, [queryKey, enabled]);
|
|
165
|
+
}, [queryKey, enabled, tagsKey]);
|
|
161
166
|
(0, import_react.useEffect)(() => {
|
|
162
167
|
if (!enabled || !lifecycleRef.current.initialized) return;
|
|
163
168
|
const prevContext = controller.getContext();
|
|
@@ -186,7 +191,8 @@ function createUseRead(options) {
|
|
|
186
191
|
const hasData = state.data !== void 0;
|
|
187
192
|
const loading = requestState.isPending && !hasData;
|
|
188
193
|
const fetching = requestState.isPending;
|
|
189
|
-
|
|
194
|
+
return {
|
|
195
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
190
196
|
meta: pluginResultData,
|
|
191
197
|
...inputField,
|
|
192
198
|
data: state.data,
|
|
@@ -196,8 +202,8 @@ function createUseRead(options) {
|
|
|
196
202
|
abort,
|
|
197
203
|
refetch
|
|
198
204
|
};
|
|
199
|
-
|
|
200
|
-
|
|
205
|
+
}
|
|
206
|
+
return useRead;
|
|
201
207
|
}
|
|
202
208
|
|
|
203
209
|
// src/useWrite/index.ts
|
|
@@ -205,14 +211,14 @@ var import_react2 = require("react");
|
|
|
205
211
|
var import_core2 = require("@spoosh/core");
|
|
206
212
|
function createUseWrite(options) {
|
|
207
213
|
const { api, stateManager, pluginExecutor, eventEmitter } = options;
|
|
208
|
-
|
|
214
|
+
function useWrite(writeFn) {
|
|
209
215
|
const hookId = (0, import_react2.useId)();
|
|
210
216
|
const selectorResultRef = (0, import_react2.useRef)({
|
|
211
217
|
call: null,
|
|
212
218
|
selector: null
|
|
213
219
|
});
|
|
214
|
-
const selectorProxy = (0, import_core2.createSelectorProxy)((
|
|
215
|
-
selectorResultRef.current =
|
|
220
|
+
const selectorProxy = (0, import_core2.createSelectorProxy)((result) => {
|
|
221
|
+
selectorResultRef.current = result;
|
|
216
222
|
});
|
|
217
223
|
writeFn(selectorProxy);
|
|
218
224
|
const selectedEndpoint = selectorResultRef.current.selector;
|
|
@@ -303,8 +309,9 @@ function createUseWrite(options) {
|
|
|
303
309
|
}
|
|
304
310
|
const inputField = Object.keys(inputInner).length > 0 ? { input: inputInner } : {};
|
|
305
311
|
const loading = requestState.isPending;
|
|
306
|
-
|
|
312
|
+
return {
|
|
307
313
|
trigger,
|
|
314
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
308
315
|
meta: pluginResultData,
|
|
309
316
|
...inputField,
|
|
310
317
|
data: state.data,
|
|
@@ -312,9 +319,10 @@ function createUseWrite(options) {
|
|
|
312
319
|
loading,
|
|
313
320
|
reset,
|
|
314
321
|
abort
|
|
322
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
315
323
|
};
|
|
316
|
-
|
|
317
|
-
|
|
324
|
+
}
|
|
325
|
+
return useWrite;
|
|
318
326
|
}
|
|
319
327
|
|
|
320
328
|
// src/useInfiniteRead/index.ts
|
|
@@ -432,6 +440,7 @@ function createUseInfiniteRead(options) {
|
|
|
432
440
|
initialized: false,
|
|
433
441
|
prevContext: null
|
|
434
442
|
});
|
|
443
|
+
const tagsKey = JSON.stringify(tags);
|
|
435
444
|
(0, import_react3.useEffect)(() => {
|
|
436
445
|
return () => {
|
|
437
446
|
controllerRef.current?.controller.unmount();
|
|
@@ -456,7 +465,7 @@ function createUseInfiniteRead(options) {
|
|
|
456
465
|
return () => {
|
|
457
466
|
unsubInvalidate();
|
|
458
467
|
};
|
|
459
|
-
}, []);
|
|
468
|
+
}, [tagsKey]);
|
|
460
469
|
(0, import_react3.useEffect)(() => {
|
|
461
470
|
if (!lifecycleRef.current.initialized) return;
|
|
462
471
|
if (enabled) {
|
package/dist/index.mjs
CHANGED
|
@@ -18,15 +18,19 @@ import {
|
|
|
18
18
|
} from "@spoosh/core";
|
|
19
19
|
function createUseRead(options) {
|
|
20
20
|
const { api, stateManager, eventEmitter, pluginExecutor } = options;
|
|
21
|
-
|
|
22
|
-
const {
|
|
21
|
+
function useRead(readFn, readOptions) {
|
|
22
|
+
const {
|
|
23
|
+
enabled = true,
|
|
24
|
+
tags,
|
|
25
|
+
...pluginOpts
|
|
26
|
+
} = readOptions ?? {};
|
|
23
27
|
const hookId = useId();
|
|
24
28
|
const selectorResultRef = useRef({
|
|
25
29
|
call: null,
|
|
26
30
|
selector: null
|
|
27
31
|
});
|
|
28
|
-
const selectorProxy = createSelectorProxy((
|
|
29
|
-
selectorResultRef.current =
|
|
32
|
+
const selectorProxy = createSelectorProxy((result) => {
|
|
33
|
+
selectorResultRef.current = result;
|
|
30
34
|
});
|
|
31
35
|
readFn(selectorProxy);
|
|
32
36
|
const capturedCall = selectorResultRef.current.call;
|
|
@@ -86,6 +90,7 @@ function createUseRead(options) {
|
|
|
86
90
|
const abortRef = useRef(controller.abort);
|
|
87
91
|
abortRef.current = controller.abort;
|
|
88
92
|
const pluginOptsKey = JSON.stringify(pluginOpts);
|
|
93
|
+
const tagsKey = JSON.stringify(tags);
|
|
89
94
|
const executeWithTracking = useCallback(
|
|
90
95
|
async (force = false) => {
|
|
91
96
|
setRequestState((prev) => ({ ...prev, isPending: true }));
|
|
@@ -141,7 +146,7 @@ function createUseRead(options) {
|
|
|
141
146
|
unsubRefetch();
|
|
142
147
|
unsubInvalidate();
|
|
143
148
|
};
|
|
144
|
-
}, [queryKey, enabled]);
|
|
149
|
+
}, [queryKey, enabled, tagsKey]);
|
|
145
150
|
useEffect(() => {
|
|
146
151
|
if (!enabled || !lifecycleRef.current.initialized) return;
|
|
147
152
|
const prevContext = controller.getContext();
|
|
@@ -170,7 +175,8 @@ function createUseRead(options) {
|
|
|
170
175
|
const hasData = state.data !== void 0;
|
|
171
176
|
const loading = requestState.isPending && !hasData;
|
|
172
177
|
const fetching = requestState.isPending;
|
|
173
|
-
|
|
178
|
+
return {
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
174
180
|
meta: pluginResultData,
|
|
175
181
|
...inputField,
|
|
176
182
|
data: state.data,
|
|
@@ -180,8 +186,8 @@ function createUseRead(options) {
|
|
|
180
186
|
abort,
|
|
181
187
|
refetch
|
|
182
188
|
};
|
|
183
|
-
|
|
184
|
-
|
|
189
|
+
}
|
|
190
|
+
return useRead;
|
|
185
191
|
}
|
|
186
192
|
|
|
187
193
|
// src/useWrite/index.ts
|
|
@@ -200,14 +206,14 @@ import {
|
|
|
200
206
|
} from "@spoosh/core";
|
|
201
207
|
function createUseWrite(options) {
|
|
202
208
|
const { api, stateManager, pluginExecutor, eventEmitter } = options;
|
|
203
|
-
|
|
209
|
+
function useWrite(writeFn) {
|
|
204
210
|
const hookId = useId2();
|
|
205
211
|
const selectorResultRef = useRef2({
|
|
206
212
|
call: null,
|
|
207
213
|
selector: null
|
|
208
214
|
});
|
|
209
|
-
const selectorProxy = createSelectorProxy2((
|
|
210
|
-
selectorResultRef.current =
|
|
215
|
+
const selectorProxy = createSelectorProxy2((result) => {
|
|
216
|
+
selectorResultRef.current = result;
|
|
211
217
|
});
|
|
212
218
|
writeFn(selectorProxy);
|
|
213
219
|
const selectedEndpoint = selectorResultRef.current.selector;
|
|
@@ -298,8 +304,9 @@ function createUseWrite(options) {
|
|
|
298
304
|
}
|
|
299
305
|
const inputField = Object.keys(inputInner).length > 0 ? { input: inputInner } : {};
|
|
300
306
|
const loading = requestState.isPending;
|
|
301
|
-
|
|
307
|
+
return {
|
|
302
308
|
trigger,
|
|
309
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
303
310
|
meta: pluginResultData,
|
|
304
311
|
...inputField,
|
|
305
312
|
data: state.data,
|
|
@@ -307,9 +314,10 @@ function createUseWrite(options) {
|
|
|
307
314
|
loading,
|
|
308
315
|
reset,
|
|
309
316
|
abort
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
310
318
|
};
|
|
311
|
-
|
|
312
|
-
|
|
319
|
+
}
|
|
320
|
+
return useWrite;
|
|
313
321
|
}
|
|
314
322
|
|
|
315
323
|
// src/useInfiniteRead/index.ts
|
|
@@ -438,6 +446,7 @@ function createUseInfiniteRead(options) {
|
|
|
438
446
|
initialized: false,
|
|
439
447
|
prevContext: null
|
|
440
448
|
});
|
|
449
|
+
const tagsKey = JSON.stringify(tags);
|
|
441
450
|
useEffect2(() => {
|
|
442
451
|
return () => {
|
|
443
452
|
controllerRef.current?.controller.unmount();
|
|
@@ -462,7 +471,7 @@ function createUseInfiniteRead(options) {
|
|
|
462
471
|
return () => {
|
|
463
472
|
unsubInvalidate();
|
|
464
473
|
};
|
|
465
|
-
}, []);
|
|
474
|
+
}, [tagsKey]);
|
|
466
475
|
useEffect2(() => {
|
|
467
476
|
if (!lifecycleRef.current.initialized) return;
|
|
468
477
|
if (enabled) {
|