@spoosh/react 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -24
- package/dist/index.d.mts +165 -174
- package/dist/index.d.ts +165 -174
- package/dist/index.js +127 -175
- package/dist/index.mjs +126 -184
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @spoosh/react
|
|
2
2
|
|
|
3
|
-
React hooks for Spoosh - `useRead`, `
|
|
3
|
+
React hooks for Spoosh - `useRead`, `useWrite`, and `useInfiniteRead`.
|
|
4
4
|
|
|
5
5
|
**[Documentation](https://spoosh.dev/docs/integrations/react)** · **Requirements:** TypeScript >= 5.0, React >= 18.0
|
|
6
6
|
|
|
@@ -23,8 +23,7 @@ const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
|
|
|
23
23
|
cachePlugin({ staleTime: 5000 }),
|
|
24
24
|
]);
|
|
25
25
|
|
|
26
|
-
export const { useRead,
|
|
27
|
-
createReactSpoosh(spoosh);
|
|
26
|
+
export const { useRead, useWrite, useInfiniteRead } = createReactSpoosh(spoosh);
|
|
28
27
|
```
|
|
29
28
|
|
|
30
29
|
### useRead
|
|
@@ -63,27 +62,6 @@ const { data: user } = useRead(
|
|
|
63
62
|
);
|
|
64
63
|
```
|
|
65
64
|
|
|
66
|
-
### useLazyRead
|
|
67
|
-
|
|
68
|
-
Lazy data fetching for print/download/export scenarios. Does not auto-fetch on mount.
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
function PrintOrder() {
|
|
72
|
-
const { trigger, loading } = useLazyRead((api) => api("orders/:id").GET);
|
|
73
|
-
|
|
74
|
-
const handlePrint = async (orderId: string) => {
|
|
75
|
-
const { data } = await trigger({ params: { id: orderId } });
|
|
76
|
-
if (data) printReceipt(data);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
<button onClick={() => handlePrint("123")} disabled={loading}>
|
|
81
|
-
{loading ? "Loading..." : "Print"}
|
|
82
|
-
</button>
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
65
|
### useWrite
|
|
88
66
|
|
|
89
67
|
Trigger mutations with loading and error states.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,113 +1,5 @@
|
|
|
1
|
-
import { ReadClient, TagMode, SpooshResponse,
|
|
1
|
+
import { ReadClient, TagMode, SpooshResponse, SpooshPlugin, PluginTypeConfig, MergePluginResults, WriteClient, StateManager, EventEmitter, PluginExecutor, PluginArray, ResolveTypes, MergePluginOptions, ResolverContext, ResolveResultTypes, MergePluginInstanceApi, SpooshOptions } from '@spoosh/core';
|
|
2
2
|
|
|
3
|
-
type PluginHooksConfig<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
4
|
-
baseUrl: string;
|
|
5
|
-
defaultOptions?: SpooshOptions;
|
|
6
|
-
plugins: TPlugins;
|
|
7
|
-
};
|
|
8
|
-
type TagModeInArray = "all" | "self";
|
|
9
|
-
/**
|
|
10
|
-
* Base options for `useRead` hook.
|
|
11
|
-
*/
|
|
12
|
-
type BaseReadOptions = {
|
|
13
|
-
/** Whether to fetch automatically on mount. Default: true */
|
|
14
|
-
enabled?: boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Unified tag option
|
|
17
|
-
* - String: mode only ('all' | 'self' | 'none')
|
|
18
|
-
* - Array: custom tags only OR [mode keyword mixed with custom tags]
|
|
19
|
-
* - 'all' or 'self' can be used in arrays
|
|
20
|
-
* - 'none' should only be used as string (use `tags: 'none'` not in array)
|
|
21
|
-
*/
|
|
22
|
-
tags?: TagMode | (TagModeInArray | (string & {}))[];
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Result returned by `useRead` hook.
|
|
26
|
-
*
|
|
27
|
-
* @template TData - The response data type
|
|
28
|
-
* @template TError - The error type
|
|
29
|
-
* @template TMeta - Plugin-provided metadata fields
|
|
30
|
-
*/
|
|
31
|
-
type BaseReadResult<TData, TError, TMeta = Record<string, unknown>> = {
|
|
32
|
-
/** True during the initial load (no data yet) */
|
|
33
|
-
loading: boolean;
|
|
34
|
-
/** True during any fetch operation */
|
|
35
|
-
fetching: boolean;
|
|
36
|
-
/** Response data from the API */
|
|
37
|
-
data: TData | undefined;
|
|
38
|
-
/** Error from the last failed request */
|
|
39
|
-
error: TError | undefined;
|
|
40
|
-
/** Plugin-provided metadata */
|
|
41
|
-
meta: TMeta;
|
|
42
|
-
/** Abort the current fetch operation */
|
|
43
|
-
abort: () => void;
|
|
44
|
-
/** Manually trigger a fetch */
|
|
45
|
-
trigger: () => Promise<SpooshResponse<TData, TError>>;
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* Result returned by `useWrite` hook.
|
|
49
|
-
*
|
|
50
|
-
* @template TData - The response data type
|
|
51
|
-
* @template TError - The error type
|
|
52
|
-
* @template TOptions - The trigger options type
|
|
53
|
-
* @template TMeta - Plugin-provided metadata fields
|
|
54
|
-
*/
|
|
55
|
-
type BaseWriteResult<TData, TError, TOptions, TMeta = Record<string, unknown>> = {
|
|
56
|
-
/** Execute the mutation with optional options */
|
|
57
|
-
trigger: (options?: TOptions) => Promise<SpooshResponse<TData, TError>>;
|
|
58
|
-
/** True while the mutation is in progress */
|
|
59
|
-
loading: boolean;
|
|
60
|
-
/** Response data from the API */
|
|
61
|
-
data: TData | undefined;
|
|
62
|
-
/** Error from the last failed request */
|
|
63
|
-
error: TError | undefined;
|
|
64
|
-
/** Plugin-provided metadata */
|
|
65
|
-
meta: TMeta;
|
|
66
|
-
/** Abort the current mutation */
|
|
67
|
-
abort: () => void;
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Result returned by `useLazyRead` hook.
|
|
71
|
-
*
|
|
72
|
-
* @template TData - The response data type
|
|
73
|
-
* @template TError - The error type
|
|
74
|
-
* @template TOptions - The trigger options type
|
|
75
|
-
*/
|
|
76
|
-
type BaseLazyReadResult<TData, TError, TOptions> = {
|
|
77
|
-
/** Execute the fetch with optional options */
|
|
78
|
-
trigger: (options?: TOptions) => Promise<SpooshResponse<TData, TError>>;
|
|
79
|
-
/** True while the fetch is in progress */
|
|
80
|
-
loading: boolean;
|
|
81
|
-
/** Response data from the API */
|
|
82
|
-
data: TData | undefined;
|
|
83
|
-
/** Error from the last failed request */
|
|
84
|
-
error: TError | undefined;
|
|
85
|
-
/** Abort the current fetch */
|
|
86
|
-
abort: () => void;
|
|
87
|
-
};
|
|
88
|
-
type OptionalQueryField<TQuery> = [TQuery] extends [never] ? object : undefined extends TQuery ? {
|
|
89
|
-
query?: Exclude<TQuery, undefined>;
|
|
90
|
-
} : {
|
|
91
|
-
query: TQuery;
|
|
92
|
-
};
|
|
93
|
-
type OptionalBodyField<TBody> = [TBody] extends [never] ? object : undefined extends TBody ? {
|
|
94
|
-
body?: Exclude<TBody, undefined>;
|
|
95
|
-
} : {
|
|
96
|
-
body: TBody;
|
|
97
|
-
};
|
|
98
|
-
type OptionalParamsField<TParamNames extends string> = [TParamNames] extends [
|
|
99
|
-
never
|
|
100
|
-
] ? object : {
|
|
101
|
-
params: Record<TParamNames, string | number>;
|
|
102
|
-
};
|
|
103
|
-
type InputFields<TQuery, TBody, TParamNames extends string> = OptionalQueryField<TQuery> & OptionalBodyField<TBody> & OptionalParamsField<TParamNames>;
|
|
104
|
-
type WriteResponseInputFields<TQuery, TBody, TParamNames extends string> = [TQuery, TBody, TParamNames] extends [never, never, never] ? object : {
|
|
105
|
-
input: InputFields<TQuery, TBody, TParamNames> | undefined;
|
|
106
|
-
};
|
|
107
|
-
type UseReadResult<TData, TError, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseReadResult<TData, TError, TMeta> & MergePluginResults<TPlugins>["read"];
|
|
108
|
-
type UseWriteResult<TData, TError, TOptions, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseWriteResult<TData, TError, TOptions, TMeta> & MergePluginResults<TPlugins>["write"];
|
|
109
|
-
type ReadApiClient<TSchema, TDefaultError> = ReadClient<TSchema, TDefaultError>;
|
|
110
|
-
type WriteApiClient<TSchema, TDefaultError> = WriteClient<TSchema, TDefaultError>;
|
|
111
3
|
type SuccessResponse<T> = Extract<T, {
|
|
112
4
|
data: unknown;
|
|
113
5
|
error?: undefined;
|
|
@@ -124,14 +16,18 @@ type ExtractMethodError<T> = T extends (...args: never[]) => infer R ? ErrorResp
|
|
|
124
16
|
} ? E : unknown : unknown;
|
|
125
17
|
type ExtractMethodOptions<T> = T extends (...args: infer A) => unknown ? A[0] : never;
|
|
126
18
|
type ExtractCoreMethodOptions<T> = T extends (...args: infer A) => unknown ? A[0] extends object ? Pick<A[0], Extract<keyof A[0], "query" | "params" | "body">> : object : object;
|
|
19
|
+
type AwaitedReturnType<T> = T extends (...args: never[]) => infer R ? Awaited<R> : never;
|
|
20
|
+
type SuccessReturnType<T> = SuccessResponse<AwaitedReturnType<T>>;
|
|
21
|
+
type ExtractSuccessInput<T> = SuccessResponse<AwaitedReturnType<T>> extends {
|
|
22
|
+
input?: infer I;
|
|
23
|
+
} ? I : object;
|
|
24
|
+
type ExtractResponseRequestOptions<T> = ExtractSuccessInput<T>;
|
|
127
25
|
type ExtractMethodQuery<T> = ExtractMethodOptions<T> extends {
|
|
128
26
|
query: infer Q;
|
|
129
27
|
} ? Q : never;
|
|
130
28
|
type ExtractMethodBody<T> = ExtractMethodOptions<T> extends {
|
|
131
29
|
body: infer B;
|
|
132
30
|
} ? B : never;
|
|
133
|
-
type AwaitedReturnType<T> = T extends (...args: never[]) => infer R ? Awaited<R> : never;
|
|
134
|
-
type SuccessReturnType<T> = SuccessResponse<AwaitedReturnType<T>>;
|
|
135
31
|
type ExtractResponseQuery<T> = SuccessReturnType<T> extends {
|
|
136
32
|
input: {
|
|
137
33
|
query: infer Q;
|
|
@@ -147,6 +43,23 @@ type ExtractResponseParamNames<T> = SuccessReturnType<T> extends {
|
|
|
147
43
|
params: Record<infer K, unknown>;
|
|
148
44
|
};
|
|
149
45
|
} ? K extends string ? K : never : never;
|
|
46
|
+
|
|
47
|
+
type TagModeInArray$1 = "all" | "self";
|
|
48
|
+
/**
|
|
49
|
+
* Base options for `useRead` hook.
|
|
50
|
+
*/
|
|
51
|
+
type BaseReadOptions = {
|
|
52
|
+
/** Whether to fetch automatically on mount. Default: true */
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Unified tag option
|
|
56
|
+
* - String: mode only ('all' | 'self' | 'none')
|
|
57
|
+
* - Array: custom tags only OR [mode keyword mixed with custom tags]
|
|
58
|
+
* - 'all' or 'self' can be used in arrays
|
|
59
|
+
* - 'none' should only be used as string (use `tags: 'none'` not in array)
|
|
60
|
+
*/
|
|
61
|
+
tags?: TagMode | (TagModeInArray$1 | (string & {}))[];
|
|
62
|
+
};
|
|
150
63
|
type QueryField<TQuery> = [TQuery] extends [never] ? object : undefined extends TQuery ? {
|
|
151
64
|
query?: Exclude<TQuery, undefined>;
|
|
152
65
|
} : {
|
|
@@ -168,6 +81,111 @@ type ResponseInputFields<TQuery, TBody, TParamNames extends string> = [
|
|
|
168
81
|
] extends [never, never, never] ? object : {
|
|
169
82
|
input: ReadInputFields<TQuery, TBody, TParamNames>;
|
|
170
83
|
};
|
|
84
|
+
type TriggerAwaitedReturn<T> = T extends (...args: never[]) => infer R ? Awaited<R> : never;
|
|
85
|
+
type ExtractInputFromResponse<T> = T extends {
|
|
86
|
+
input: infer I;
|
|
87
|
+
} ? I : never;
|
|
88
|
+
type ExtractTriggerQuery<I> = I extends {
|
|
89
|
+
query: infer Q;
|
|
90
|
+
} ? {
|
|
91
|
+
query?: Q;
|
|
92
|
+
} : unknown;
|
|
93
|
+
type ExtractTriggerBody<I> = I extends {
|
|
94
|
+
body: infer B;
|
|
95
|
+
} ? {
|
|
96
|
+
body?: B;
|
|
97
|
+
} : unknown;
|
|
98
|
+
type ExtractTriggerParams<I> = I extends {
|
|
99
|
+
params: infer P;
|
|
100
|
+
} ? {
|
|
101
|
+
params?: P;
|
|
102
|
+
} : unknown;
|
|
103
|
+
type TriggerOptions<T> = ExtractInputFromResponse<TriggerAwaitedReturn<T>> extends infer I ? [I] extends [never] ? {
|
|
104
|
+
force?: boolean;
|
|
105
|
+
} : ExtractTriggerQuery<I> & ExtractTriggerBody<I> & ExtractTriggerParams<I> & {
|
|
106
|
+
/** Force refetch even if data is cached */
|
|
107
|
+
force?: boolean;
|
|
108
|
+
} : {
|
|
109
|
+
force?: boolean;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Result returned by `useRead` hook.
|
|
113
|
+
*
|
|
114
|
+
* @template TData - The response data type
|
|
115
|
+
* @template TError - The error type
|
|
116
|
+
* @template TMeta - Plugin-provided metadata fields
|
|
117
|
+
* @template TTriggerOptions - Options that can be passed to trigger()
|
|
118
|
+
*/
|
|
119
|
+
type BaseReadResult<TData, TError, TMeta = Record<string, unknown>, TTriggerOptions = {
|
|
120
|
+
force?: boolean;
|
|
121
|
+
}> = {
|
|
122
|
+
/** True during the initial load (no data yet) */
|
|
123
|
+
loading: boolean;
|
|
124
|
+
/** True during any fetch operation */
|
|
125
|
+
fetching: boolean;
|
|
126
|
+
/** Response data from the API */
|
|
127
|
+
data: TData | undefined;
|
|
128
|
+
/** Error from the last failed request */
|
|
129
|
+
error: TError | undefined;
|
|
130
|
+
/** Plugin-provided metadata */
|
|
131
|
+
meta: TMeta;
|
|
132
|
+
/** Abort the current fetch operation */
|
|
133
|
+
abort: () => void;
|
|
134
|
+
/**
|
|
135
|
+
* Manually trigger a fetch.
|
|
136
|
+
*
|
|
137
|
+
* @param options - Optional override options (query, body, params) to use for this specific request
|
|
138
|
+
*/
|
|
139
|
+
trigger: (options?: TTriggerOptions) => Promise<SpooshResponse<TData, TError>>;
|
|
140
|
+
};
|
|
141
|
+
type UseReadResult<TData, TError, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseReadResult<TData, TError, TMeta> & MergePluginResults<TPlugins>["read"];
|
|
142
|
+
type ReadApiClient<TSchema, TDefaultError> = ReadClient<TSchema, TDefaultError>;
|
|
143
|
+
|
|
144
|
+
type OptionalQueryField<TQuery> = [TQuery] extends [never] ? object : undefined extends TQuery ? {
|
|
145
|
+
query?: Exclude<TQuery, undefined>;
|
|
146
|
+
} : {
|
|
147
|
+
query: TQuery;
|
|
148
|
+
};
|
|
149
|
+
type OptionalBodyField<TBody> = [TBody] extends [never] ? object : undefined extends TBody ? {
|
|
150
|
+
body?: Exclude<TBody, undefined>;
|
|
151
|
+
} : {
|
|
152
|
+
body: TBody;
|
|
153
|
+
};
|
|
154
|
+
type OptionalParamsField<TParamNames extends string> = [TParamNames] extends [
|
|
155
|
+
never
|
|
156
|
+
] ? object : {
|
|
157
|
+
params: Record<TParamNames, string | number>;
|
|
158
|
+
};
|
|
159
|
+
type InputFields<TQuery, TBody, TParamNames extends string> = OptionalQueryField<TQuery> & OptionalBodyField<TBody> & OptionalParamsField<TParamNames>;
|
|
160
|
+
type WriteResponseInputFields<TQuery, TBody, TParamNames extends string> = [TQuery, TBody, TParamNames] extends [never, never, never] ? object : {
|
|
161
|
+
input: InputFields<TQuery, TBody, TParamNames> | undefined;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Result returned by `useWrite` hook.
|
|
165
|
+
*
|
|
166
|
+
* @template TData - The response data type
|
|
167
|
+
* @template TError - The error type
|
|
168
|
+
* @template TOptions - The trigger options type
|
|
169
|
+
* @template TMeta - Plugin-provided metadata fields
|
|
170
|
+
*/
|
|
171
|
+
type BaseWriteResult<TData, TError, TOptions, TMeta = Record<string, unknown>> = {
|
|
172
|
+
/** Execute the mutation with optional options */
|
|
173
|
+
trigger: (options?: TOptions) => Promise<SpooshResponse<TData, TError>>;
|
|
174
|
+
/** True while the mutation is in progress */
|
|
175
|
+
loading: boolean;
|
|
176
|
+
/** Response data from the API */
|
|
177
|
+
data: TData | undefined;
|
|
178
|
+
/** Error from the last failed request */
|
|
179
|
+
error: TError | undefined;
|
|
180
|
+
/** Plugin-provided metadata */
|
|
181
|
+
meta: TMeta;
|
|
182
|
+
/** Abort the current mutation */
|
|
183
|
+
abort: () => void;
|
|
184
|
+
};
|
|
185
|
+
type UseWriteResult<TData, TError, TOptions, TMeta, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = BaseWriteResult<TData, TError, TOptions, TMeta> & MergePluginResults<TPlugins>["write"];
|
|
186
|
+
type WriteApiClient<TSchema, TDefaultError> = WriteClient<TSchema, TDefaultError>;
|
|
187
|
+
|
|
188
|
+
type TagModeInArray = "all" | "self";
|
|
171
189
|
type AnyInfiniteRequestOptions = {
|
|
172
190
|
query?: Record<string, unknown>;
|
|
173
191
|
params?: Record<string, string | number>;
|
|
@@ -269,21 +287,12 @@ type InferError<T, TDefaultError> = [T] extends [unknown] ? TDefaultError : T;
|
|
|
269
287
|
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>>;
|
|
270
288
|
type ResolvedWriteOptions<TSchema, TPlugins extends PluginArray, TMethod, TDefaultError> = ResolveTypes<MergePluginOptions<TPlugins>["write"], WriteResolverContext<TSchema, TMethod, TDefaultError>>;
|
|
271
289
|
type UseReadFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
272
|
-
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<
|
|
273
|
-
|
|
274
|
-
error?: unknown;
|
|
275
|
-
}>, 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>>;
|
|
276
|
-
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<{
|
|
277
|
-
data?: unknown;
|
|
278
|
-
error?: unknown;
|
|
279
|
-
}>>(readFn: TReadFn): BaseReadResult<ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, MergePluginResults<TPlugins>["read"]> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
290
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<unknown, unknown>>, 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>, TriggerOptions<TReadFn>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
291
|
+
<TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<unknown, unknown>>>(readFn: TReadFn): BaseReadResult<ExtractMethodData<TReadFn>, InferError<ExtractMethodError<TReadFn>, TDefaultError>, MergePluginResults<TPlugins>["read"], TriggerOptions<TReadFn>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
280
292
|
};
|
|
281
293
|
type UseWriteFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
282
294
|
<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>>;
|
|
283
295
|
};
|
|
284
|
-
type UseLazyReadFn<TDefaultError, TSchema> = {
|
|
285
|
-
<TMethod extends (...args: never) => Promise<SpooshResponse<unknown, unknown>>>(readFn: (api: ReadApiClient<TSchema, TDefaultError>) => TMethod): BaseLazyReadResult<ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, ExtractCoreMethodOptions<TMethod>> & WriteResponseInputFields<ExtractResponseQuery<TMethod>, ExtractResponseBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
286
|
-
};
|
|
287
296
|
type InfiniteReadResolverContext<TSchema, TData, TError, TRequest> = ResolverContext<TSchema, TData, TError, TRequest extends {
|
|
288
297
|
query: infer Q;
|
|
289
298
|
} ? Q : never, TRequest extends {
|
|
@@ -336,23 +345,6 @@ type SpooshReactHooks<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
|
336
345
|
* ```
|
|
337
346
|
*/
|
|
338
347
|
useWrite: UseWriteFn<TDefaultError, TSchema, TPlugins>;
|
|
339
|
-
/**
|
|
340
|
-
* React hook for lazy GET requests with manual triggering (does not auto-fetch on mount).
|
|
341
|
-
*
|
|
342
|
-
* @param readFn - Function that selects the API endpoint (e.g., `(api) => api("posts").GET`)
|
|
343
|
-
* @returns Object containing `trigger`, `data`, `error`, `loading`, and `abort`
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* ```tsx
|
|
347
|
-
* const { trigger, loading, data } = useLazyRead((api) => api("posts/:id").GET);
|
|
348
|
-
*
|
|
349
|
-
* const handleClick = async (id) => {
|
|
350
|
-
* const { data, error } = await trigger({ params: { id } });
|
|
351
|
-
* if (data) console.log('Fetched:', data);
|
|
352
|
-
* };
|
|
353
|
-
* ```
|
|
354
|
-
*/
|
|
355
|
-
useLazyRead: UseLazyReadFn<TDefaultError, TSchema>;
|
|
356
348
|
/**
|
|
357
349
|
* React hook for infinite/paginated data fetching with automatic pagination control.
|
|
358
350
|
*
|
|
@@ -416,40 +408,33 @@ type SpooshInstanceShape<TApi, TSchema, TDefaultError, TPlugins> = {
|
|
|
416
408
|
*/
|
|
417
409
|
declare function createReactSpoosh<TSchema, TDefaultError, TPlugins extends PluginArray, TApi>(instance: SpooshInstanceShape<TApi, TSchema, TDefaultError, TPlugins>): SpooshReactHooks<TDefaultError, TSchema, TPlugins>;
|
|
418
410
|
|
|
419
|
-
declare function createUseRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
error: infer E;
|
|
447
|
-
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
448
|
-
error: infer E;
|
|
449
|
-
}> ? E : unknown, MergePluginResults<TPlugins>["read"]> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
450
|
-
};
|
|
451
|
-
|
|
452
|
-
declare function createUseLazyRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TMethod extends (...args: never[]) => Promise<SpooshResponse<unknown, unknown>>>(readFn: (api: ReadApiClient<TSchema, TDefaultError>) => TMethod) => BaseLazyReadResult<ExtractMethodData<TMethod>, [ExtractMethodError<TMethod>] extends [unknown] ? TDefaultError : ExtractMethodError<TMethod>, ExtractCoreMethodOptions<TMethod>> & WriteResponseInputFields<ExtractResponseQuery<TMethod>, ExtractResponseBody<TMethod>, ExtractResponseParamNames<TMethod>>;
|
|
411
|
+
declare function createUseRead<TSchema, TDefaultError, TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]>(options: Omit<SpooshInstanceShape<unknown, TSchema, TDefaultError, TPlugins>, "_types">): <TReadFn extends (api: ReadApiClient<TSchema, TDefaultError>) => Promise<SpooshResponse<unknown, unknown>>, TReadOpts extends BaseReadOptions & ResolveTypes<((TPlugins[number] extends infer T ? T extends TPlugins[number] ? T extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
412
|
+
readOptions: infer R;
|
|
413
|
+
} ? 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 {
|
|
414
|
+
readOptions: infer R;
|
|
415
|
+
} ? 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<{
|
|
416
|
+
data: infer D;
|
|
417
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
418
|
+
error: infer E;
|
|
419
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
420
|
+
error: infer E;
|
|
421
|
+
}> ? E : unknown, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn> extends never ? never : Record<ExtractResponseParamNames<TReadFn>, string | number>>> = BaseReadOptions & ResolveTypes<((TPlugins[number] extends infer T_3 ? T_3 extends TPlugins[number] ? T_3 extends SpooshPlugin<infer Types extends PluginTypeConfig> ? Types extends {
|
|
422
|
+
readOptions: infer R;
|
|
423
|
+
} ? R : 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 extends PluginTypeConfig> ? Types extends {
|
|
424
|
+
readOptions: infer R;
|
|
425
|
+
} ? R : object : object : never : never) ? T_4 extends unknown ? (x: T_4) => void : never : never : never) extends (x: infer I) => void ? I : never, ResolverContext<TSchema, TReadFn extends (...args: unknown[]) => Promise<{
|
|
426
|
+
data: infer D;
|
|
427
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
428
|
+
error: infer E;
|
|
429
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
430
|
+
error: infer E;
|
|
431
|
+
}> ? E : unknown, ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn> extends never ? never : Record<ExtractResponseParamNames<TReadFn>, string | number>>>>(readFn: TReadFn, readOptions?: TReadOpts) => BaseReadResult<TReadFn extends (...args: unknown[]) => Promise<{
|
|
432
|
+
data: infer D;
|
|
433
|
+
}> ? D : unknown, [TReadFn extends (...args: unknown[]) => Promise<{
|
|
434
|
+
error: infer E;
|
|
435
|
+
}> ? E : unknown] extends [unknown] ? TDefaultError : TReadFn extends (...args: unknown[]) => Promise<{
|
|
436
|
+
error: infer E;
|
|
437
|
+
}> ? E : unknown, ResolveResultTypes<MergePluginResults<TPlugins>["read"], TReadOpts>, TriggerOptions<TReadFn>> & ResponseInputFields<ExtractResponseQuery<TReadFn>, ExtractResponseBody<TReadFn>, ExtractResponseParamNames<TReadFn>>;
|
|
453
438
|
|
|
454
439
|
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 {
|
|
455
440
|
writeOptions: infer W;
|
|
@@ -467,4 +452,10 @@ declare function createUseInfiniteRead<TSchema, TDefaultError, TPlugins extends
|
|
|
467
452
|
readResult: infer R;
|
|
468
453
|
} ? R : object : object : never : never) ? T_1 extends unknown ? (x: T_1) => void : never : never : never) extends (x: infer I) => void ? I : never>;
|
|
469
454
|
|
|
470
|
-
|
|
455
|
+
type PluginHooksConfig<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
456
|
+
baseUrl: string;
|
|
457
|
+
defaultOptions?: SpooshOptions;
|
|
458
|
+
plugins: TPlugins;
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
export { type AnyInfiniteRequestOptions, type BaseInfiniteReadOptions, type BaseInfiniteReadResult, type BaseReadOptions, type BaseReadResult, type BaseWriteResult, type ExtractCoreMethodOptions, type ExtractMethodBody, type ExtractMethodData, type ExtractMethodError, type ExtractMethodOptions, type ExtractMethodQuery, type ExtractResponseBody, type ExtractResponseParamNames, type ExtractResponseQuery, type ExtractResponseRequestOptions, type InfiniteNextContext, type InfinitePrevContext, type InfiniteReadApiClient, type PluginHooksConfig, type ReadApiClient, type ResponseInputFields, type SpooshReactHooks, type TriggerOptions, type UseInfiniteReadResult, type UseReadResult, type UseWriteResult, type WriteApiClient, type WriteResponseInputFields, createReactSpoosh, createUseInfiniteRead, createUseRead, createUseWrite };
|