enlace 0.0.1-beta.2 → 0.0.1-beta.20
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 +648 -85
- package/dist/hook/index.d.mts +220 -0
- package/dist/hook/index.d.ts +220 -0
- package/dist/{next/hook → hook}/index.js +334 -169
- package/dist/hook/index.mjs +590 -0
- package/dist/index.d.mts +74 -69
- package/dist/index.d.ts +74 -69
- package/dist/index.js +65 -314
- package/dist/index.mjs +67 -314
- package/package.json +17 -12
- package/dist/next/hook/index.d.mts +0 -124
- package/dist/next/hook/index.d.ts +0 -124
- package/dist/next/hook/index.mjs +0 -444
- package/dist/next/index.d.mts +0 -74
- package/dist/next/index.d.ts +0 -74
- package/dist/next/index.js +0 -111
- package/dist/next/index.mjs +0 -95
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceResponse, CoreRequestOptionsBase, WildcardClient, EnlaceClient, EnlaceOptions, EnlaceCallbacks } from 'enlace-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-request options for React hooks.
|
|
5
|
+
* Extends CoreRequestOptionsBase which provides conditional params support.
|
|
6
|
+
* The params option is only available when accessing dynamic URL segments (via _ or index access).
|
|
7
|
+
*/
|
|
8
|
+
type ReactRequestOptionsBase = CoreRequestOptionsBase & {
|
|
9
|
+
/**
|
|
10
|
+
* Cache tags for caching (GET requests only)
|
|
11
|
+
* This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
|
|
12
|
+
* But can be manually specified to override auto-generation.
|
|
13
|
+
* */
|
|
14
|
+
tags?: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Additional cache tags to merge with auto-generated tags.
|
|
17
|
+
* Use this when you want to extend (not replace) the auto-generated tags.
|
|
18
|
+
* @example
|
|
19
|
+
* api.posts.$get({ additionalTags: ['custom-tag'] })
|
|
20
|
+
* // If autoGenerateTags produces ['posts'], final tags: ['posts', 'custom-tag']
|
|
21
|
+
*/
|
|
22
|
+
additionalTags?: string[];
|
|
23
|
+
/** Tags to invalidate after mutation (triggers refetch in matching queries) */
|
|
24
|
+
revalidateTags?: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Additional revalidation tags to merge with auto-generated tags.
|
|
27
|
+
* Use this when you want to extend (not replace) the auto-generated revalidation tags.
|
|
28
|
+
* @example
|
|
29
|
+
* api.posts.$post({ body: {...}, additionalRevalidateTags: ['other-tag'] })
|
|
30
|
+
* // If autoRevalidateTags produces ['posts'], final tags: ['posts', 'other-tag']
|
|
31
|
+
*/
|
|
32
|
+
additionalRevalidateTags?: string[];
|
|
33
|
+
};
|
|
34
|
+
/** Runtime request options that includes all possible properties */
|
|
35
|
+
type AnyReactRequestOptions = ReactRequestOptionsBase & {
|
|
36
|
+
params?: Record<string, string | number>;
|
|
37
|
+
};
|
|
38
|
+
/** Polling interval value: milliseconds to wait, or false to stop polling */
|
|
39
|
+
type PollingIntervalValue = number | false;
|
|
40
|
+
/** Function that determines polling interval based on current data/error state */
|
|
41
|
+
type PollingIntervalFn<TData, TError> = (data: TData | undefined, error: TError | undefined) => PollingIntervalValue;
|
|
42
|
+
/** Polling interval option: static value or dynamic function */
|
|
43
|
+
type PollingInterval<TData = unknown, TError = unknown> = PollingIntervalValue | PollingIntervalFn<TData, TError>;
|
|
44
|
+
/** Options for query mode hooks */
|
|
45
|
+
type UseEnlaceQueryOptions<TData = unknown, TError = unknown> = {
|
|
46
|
+
/**
|
|
47
|
+
* Whether the query should execute.
|
|
48
|
+
* Set to false to skip fetching (useful when ID is "new" or undefined).
|
|
49
|
+
* @default true
|
|
50
|
+
*/
|
|
51
|
+
enabled?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Polling interval in milliseconds, or a function that returns the interval.
|
|
54
|
+
* When set, the query will refetch after this interval AFTER the previous request completes.
|
|
55
|
+
* Uses sequential polling (setTimeout after fetch completes), not interval-based.
|
|
56
|
+
*
|
|
57
|
+
* Can be:
|
|
58
|
+
* - `number`: Fixed interval in milliseconds
|
|
59
|
+
* - `false`: Disable polling
|
|
60
|
+
* - `(data, error) => number | false`: Dynamic interval based on response
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Fixed interval
|
|
64
|
+
* { pollingInterval: 5000 }
|
|
65
|
+
*
|
|
66
|
+
* // Conditional polling based on data
|
|
67
|
+
* { pollingInterval: (order) => order?.status === 'pending' ? 2000 : false }
|
|
68
|
+
*
|
|
69
|
+
* @default undefined (no polling)
|
|
70
|
+
*/
|
|
71
|
+
pollingInterval?: PollingInterval<TData, TError>;
|
|
72
|
+
};
|
|
73
|
+
type ApiClient<TSchema, TDefaultError = unknown, TOptions = ReactRequestOptionsBase> = unknown extends TSchema ? WildcardClient<TOptions> : EnlaceClient<TSchema, TDefaultError, TOptions>;
|
|
74
|
+
type QueryFn<TSchema, TData, TError, TDefaultError = unknown, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TDefaultError, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
|
|
75
|
+
type SelectorFn<TSchema, TMethod, TDefaultError = unknown, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TDefaultError, TOptions>) => TMethod;
|
|
76
|
+
type HookState = {
|
|
77
|
+
loading: boolean;
|
|
78
|
+
fetching: boolean;
|
|
79
|
+
data: unknown;
|
|
80
|
+
error: unknown;
|
|
81
|
+
};
|
|
82
|
+
type TrackedCall = {
|
|
83
|
+
path: string[];
|
|
84
|
+
method: string;
|
|
85
|
+
options: unknown;
|
|
86
|
+
};
|
|
87
|
+
declare const HTTP_METHODS: readonly ["$get", "$post", "$put", "$patch", "$delete"];
|
|
88
|
+
type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
|
|
89
|
+
type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
|
|
90
|
+
/** Discriminated union for hook response state - enables type narrowing via error check */
|
|
91
|
+
type HookResponseState<TData, TError> = {
|
|
92
|
+
data: TData;
|
|
93
|
+
error?: undefined;
|
|
94
|
+
} | {
|
|
95
|
+
data?: undefined;
|
|
96
|
+
error: TError;
|
|
97
|
+
};
|
|
98
|
+
/** Result when hook is called with query function (auto-fetch mode) */
|
|
99
|
+
type UseEnlaceQueryResult<TData, TError> = {
|
|
100
|
+
loading: boolean;
|
|
101
|
+
fetching: boolean;
|
|
102
|
+
} & HookResponseState<TData, TError>;
|
|
103
|
+
/** Result when hook is called with method selector (trigger mode) */
|
|
104
|
+
type UseEnlaceSelectorResult<TMethod> = {
|
|
105
|
+
trigger: TMethod;
|
|
106
|
+
loading: boolean;
|
|
107
|
+
fetching: boolean;
|
|
108
|
+
} & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
|
|
109
|
+
/** Options for enlaceHookReact factory */
|
|
110
|
+
type EnlaceHookOptions = {
|
|
111
|
+
/**
|
|
112
|
+
* Auto-generate cache tags from URL path for GET requests.
|
|
113
|
+
* e.g., `/posts/1` generates tags `['posts', 'posts/1']`
|
|
114
|
+
* @default true
|
|
115
|
+
*/
|
|
116
|
+
autoGenerateTags?: boolean;
|
|
117
|
+
/** Auto-revalidate generated tags after successful mutations. @default true */
|
|
118
|
+
autoRevalidateTags?: boolean;
|
|
119
|
+
/** Time in ms before cached data is considered stale. @default 0 (always stale) */
|
|
120
|
+
staleTime?: number;
|
|
121
|
+
/** Callback called on successful API responses */
|
|
122
|
+
onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
|
|
123
|
+
/** Callback called on error responses (HTTP errors or network failures) */
|
|
124
|
+
onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
|
|
125
|
+
};
|
|
126
|
+
/** Hook type returned by enlaceHookReact */
|
|
127
|
+
type EnlaceHook<TSchema, TDefaultError = unknown> = {
|
|
128
|
+
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod, TDefaultError>): UseEnlaceSelectorResult<TMethod>;
|
|
129
|
+
<TData, TError>(queryFn: QueryFn<TSchema, TData, TError, TDefaultError>, options?: UseEnlaceQueryOptions<TData, TError>): UseEnlaceQueryResult<TData, TError>;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates a React hook for making API calls.
|
|
134
|
+
* Called at module level to create a reusable hook.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* const useAPI = enlaceHookReact<ApiSchema>('https://api.com');
|
|
138
|
+
*
|
|
139
|
+
* // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
|
|
140
|
+
* const { loading, data, error } = useAPI((api) => api.posts.$get({ query: { userId } }));
|
|
141
|
+
*
|
|
142
|
+
* // Selector mode - typed trigger for lazy calls
|
|
143
|
+
* const { trigger, loading, data, error } = useAPI((api) => api.posts.$delete);
|
|
144
|
+
* onClick={() => trigger({ body: { id: 1 } })}
|
|
145
|
+
*/
|
|
146
|
+
declare function enlaceHookReact<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: EnlaceHookOptions): EnlaceHook<TSchema, TDefaultError>;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Handler function called after successful mutations to trigger server-side revalidation.
|
|
150
|
+
* @param tags - Cache tags to revalidate
|
|
151
|
+
* @param paths - URL paths to revalidate
|
|
152
|
+
*/
|
|
153
|
+
type ServerRevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
|
|
154
|
+
/** Next.js-specific options (third argument for enlaceNext) */
|
|
155
|
+
type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
|
|
156
|
+
/**
|
|
157
|
+
* Handler called after successful mutations to trigger server-side revalidation.
|
|
158
|
+
* Receives auto-generated or manually specified tags and paths.
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* enlaceNext("http://localhost:3000/api/", {}, {
|
|
162
|
+
* serverRevalidator: (tags, paths) => revalidateServerAction(tags, paths)
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
serverRevalidator?: ServerRevalidateHandler;
|
|
167
|
+
/**
|
|
168
|
+
* Skip server-side revalidation by default for all mutations.
|
|
169
|
+
* Individual requests can override with serverRevalidate: true.
|
|
170
|
+
* Useful for CSR-heavy apps where server cache invalidation is rarely needed.
|
|
171
|
+
* @default false
|
|
172
|
+
*/
|
|
173
|
+
skipServerRevalidation?: boolean;
|
|
174
|
+
};
|
|
175
|
+
/** Next.js hook options (third argument for enlaceHookNext) - extends React's EnlaceHookOptions */
|
|
176
|
+
type NextHookOptions = EnlaceHookOptions & Pick<NextOptions, "serverRevalidator" | "skipServerRevalidation">;
|
|
177
|
+
/** Per-request options for Next.js fetch - extends React's base options */
|
|
178
|
+
type NextRequestOptionsBase = ReactRequestOptionsBase & {
|
|
179
|
+
/** Time in seconds to revalidate, or false to disable */
|
|
180
|
+
revalidate?: number | false;
|
|
181
|
+
/**
|
|
182
|
+
* URL paths to revalidate after mutation.
|
|
183
|
+
* Passed to the serverRevalidator handler.
|
|
184
|
+
*/
|
|
185
|
+
revalidatePaths?: string[];
|
|
186
|
+
/**
|
|
187
|
+
* Control server-side revalidation for this specific request.
|
|
188
|
+
* - true: Force server revalidation
|
|
189
|
+
* - false: Skip server revalidation
|
|
190
|
+
* When undefined, follows the global skipServerRevalidation setting.
|
|
191
|
+
*/
|
|
192
|
+
serverRevalidate?: boolean;
|
|
193
|
+
};
|
|
194
|
+
type NextQueryFn<TSchema, TData, TError, TDefaultError = unknown> = QueryFn<TSchema, TData, TError, TDefaultError, NextRequestOptionsBase>;
|
|
195
|
+
type NextSelectorFn<TSchema, TMethod, TDefaultError = unknown> = SelectorFn<TSchema, TMethod, TDefaultError, NextRequestOptionsBase>;
|
|
196
|
+
/** Hook type returned by enlaceHookNext */
|
|
197
|
+
type NextEnlaceHook<TSchema, TDefaultError = unknown> = {
|
|
198
|
+
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: NextSelectorFn<TSchema, TMethod, TDefaultError>): UseEnlaceSelectorResult<TMethod>;
|
|
199
|
+
<TData, TError>(queryFn: NextQueryFn<TSchema, TData, TError, TDefaultError>, options?: UseEnlaceQueryOptions<TData, TError>): UseEnlaceQueryResult<TData, TError>;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Creates a React hook for making API calls in Next.js Client Components.
|
|
204
|
+
* Uses Next.js-specific features like serverRevalidator for server-side cache invalidation.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* const useAPI = enlaceHookNext<ApiSchema>('https://api.com', {}, {
|
|
208
|
+
* serverRevalidator: (tags) => revalidateTagsAction(tags),
|
|
209
|
+
* staleTime: 5000,
|
|
210
|
+
* });
|
|
211
|
+
*
|
|
212
|
+
* // Query mode - auto-fetch
|
|
213
|
+
* const { loading, data, error } = useAPI((api) => api.posts.$get());
|
|
214
|
+
*
|
|
215
|
+
* // Selector mode - trigger for mutations
|
|
216
|
+
* const { trigger } = useAPI((api) => api.posts.$delete);
|
|
217
|
+
*/
|
|
218
|
+
declare function enlaceHookNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: NextHookOptions): NextEnlaceHook<TSchema, TDefaultError>;
|
|
219
|
+
|
|
220
|
+
export { type AnyReactRequestOptions, type ApiClient, type EnlaceHook, type EnlaceHookOptions, HTTP_METHODS, type HookState, type NextEnlaceHook, type NextHookOptions, type PollingInterval, type PollingIntervalFn, type PollingIntervalValue, type QueryFn, type ReactRequestOptionsBase, type SelectorFn, type TrackedCall, type UseEnlaceQueryOptions, type UseEnlaceQueryResult, type UseEnlaceSelectorResult, enlaceHookNext, enlaceHookReact };
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceResponse, CoreRequestOptionsBase, WildcardClient, EnlaceClient, EnlaceOptions, EnlaceCallbacks } from 'enlace-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-request options for React hooks.
|
|
5
|
+
* Extends CoreRequestOptionsBase which provides conditional params support.
|
|
6
|
+
* The params option is only available when accessing dynamic URL segments (via _ or index access).
|
|
7
|
+
*/
|
|
8
|
+
type ReactRequestOptionsBase = CoreRequestOptionsBase & {
|
|
9
|
+
/**
|
|
10
|
+
* Cache tags for caching (GET requests only)
|
|
11
|
+
* This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
|
|
12
|
+
* But can be manually specified to override auto-generation.
|
|
13
|
+
* */
|
|
14
|
+
tags?: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Additional cache tags to merge with auto-generated tags.
|
|
17
|
+
* Use this when you want to extend (not replace) the auto-generated tags.
|
|
18
|
+
* @example
|
|
19
|
+
* api.posts.$get({ additionalTags: ['custom-tag'] })
|
|
20
|
+
* // If autoGenerateTags produces ['posts'], final tags: ['posts', 'custom-tag']
|
|
21
|
+
*/
|
|
22
|
+
additionalTags?: string[];
|
|
23
|
+
/** Tags to invalidate after mutation (triggers refetch in matching queries) */
|
|
24
|
+
revalidateTags?: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Additional revalidation tags to merge with auto-generated tags.
|
|
27
|
+
* Use this when you want to extend (not replace) the auto-generated revalidation tags.
|
|
28
|
+
* @example
|
|
29
|
+
* api.posts.$post({ body: {...}, additionalRevalidateTags: ['other-tag'] })
|
|
30
|
+
* // If autoRevalidateTags produces ['posts'], final tags: ['posts', 'other-tag']
|
|
31
|
+
*/
|
|
32
|
+
additionalRevalidateTags?: string[];
|
|
33
|
+
};
|
|
34
|
+
/** Runtime request options that includes all possible properties */
|
|
35
|
+
type AnyReactRequestOptions = ReactRequestOptionsBase & {
|
|
36
|
+
params?: Record<string, string | number>;
|
|
37
|
+
};
|
|
38
|
+
/** Polling interval value: milliseconds to wait, or false to stop polling */
|
|
39
|
+
type PollingIntervalValue = number | false;
|
|
40
|
+
/** Function that determines polling interval based on current data/error state */
|
|
41
|
+
type PollingIntervalFn<TData, TError> = (data: TData | undefined, error: TError | undefined) => PollingIntervalValue;
|
|
42
|
+
/** Polling interval option: static value or dynamic function */
|
|
43
|
+
type PollingInterval<TData = unknown, TError = unknown> = PollingIntervalValue | PollingIntervalFn<TData, TError>;
|
|
44
|
+
/** Options for query mode hooks */
|
|
45
|
+
type UseEnlaceQueryOptions<TData = unknown, TError = unknown> = {
|
|
46
|
+
/**
|
|
47
|
+
* Whether the query should execute.
|
|
48
|
+
* Set to false to skip fetching (useful when ID is "new" or undefined).
|
|
49
|
+
* @default true
|
|
50
|
+
*/
|
|
51
|
+
enabled?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Polling interval in milliseconds, or a function that returns the interval.
|
|
54
|
+
* When set, the query will refetch after this interval AFTER the previous request completes.
|
|
55
|
+
* Uses sequential polling (setTimeout after fetch completes), not interval-based.
|
|
56
|
+
*
|
|
57
|
+
* Can be:
|
|
58
|
+
* - `number`: Fixed interval in milliseconds
|
|
59
|
+
* - `false`: Disable polling
|
|
60
|
+
* - `(data, error) => number | false`: Dynamic interval based on response
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Fixed interval
|
|
64
|
+
* { pollingInterval: 5000 }
|
|
65
|
+
*
|
|
66
|
+
* // Conditional polling based on data
|
|
67
|
+
* { pollingInterval: (order) => order?.status === 'pending' ? 2000 : false }
|
|
68
|
+
*
|
|
69
|
+
* @default undefined (no polling)
|
|
70
|
+
*/
|
|
71
|
+
pollingInterval?: PollingInterval<TData, TError>;
|
|
72
|
+
};
|
|
73
|
+
type ApiClient<TSchema, TDefaultError = unknown, TOptions = ReactRequestOptionsBase> = unknown extends TSchema ? WildcardClient<TOptions> : EnlaceClient<TSchema, TDefaultError, TOptions>;
|
|
74
|
+
type QueryFn<TSchema, TData, TError, TDefaultError = unknown, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TDefaultError, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
|
|
75
|
+
type SelectorFn<TSchema, TMethod, TDefaultError = unknown, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TDefaultError, TOptions>) => TMethod;
|
|
76
|
+
type HookState = {
|
|
77
|
+
loading: boolean;
|
|
78
|
+
fetching: boolean;
|
|
79
|
+
data: unknown;
|
|
80
|
+
error: unknown;
|
|
81
|
+
};
|
|
82
|
+
type TrackedCall = {
|
|
83
|
+
path: string[];
|
|
84
|
+
method: string;
|
|
85
|
+
options: unknown;
|
|
86
|
+
};
|
|
87
|
+
declare const HTTP_METHODS: readonly ["$get", "$post", "$put", "$patch", "$delete"];
|
|
88
|
+
type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
|
|
89
|
+
type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
|
|
90
|
+
/** Discriminated union for hook response state - enables type narrowing via error check */
|
|
91
|
+
type HookResponseState<TData, TError> = {
|
|
92
|
+
data: TData;
|
|
93
|
+
error?: undefined;
|
|
94
|
+
} | {
|
|
95
|
+
data?: undefined;
|
|
96
|
+
error: TError;
|
|
97
|
+
};
|
|
98
|
+
/** Result when hook is called with query function (auto-fetch mode) */
|
|
99
|
+
type UseEnlaceQueryResult<TData, TError> = {
|
|
100
|
+
loading: boolean;
|
|
101
|
+
fetching: boolean;
|
|
102
|
+
} & HookResponseState<TData, TError>;
|
|
103
|
+
/** Result when hook is called with method selector (trigger mode) */
|
|
104
|
+
type UseEnlaceSelectorResult<TMethod> = {
|
|
105
|
+
trigger: TMethod;
|
|
106
|
+
loading: boolean;
|
|
107
|
+
fetching: boolean;
|
|
108
|
+
} & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
|
|
109
|
+
/** Options for enlaceHookReact factory */
|
|
110
|
+
type EnlaceHookOptions = {
|
|
111
|
+
/**
|
|
112
|
+
* Auto-generate cache tags from URL path for GET requests.
|
|
113
|
+
* e.g., `/posts/1` generates tags `['posts', 'posts/1']`
|
|
114
|
+
* @default true
|
|
115
|
+
*/
|
|
116
|
+
autoGenerateTags?: boolean;
|
|
117
|
+
/** Auto-revalidate generated tags after successful mutations. @default true */
|
|
118
|
+
autoRevalidateTags?: boolean;
|
|
119
|
+
/** Time in ms before cached data is considered stale. @default 0 (always stale) */
|
|
120
|
+
staleTime?: number;
|
|
121
|
+
/** Callback called on successful API responses */
|
|
122
|
+
onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
|
|
123
|
+
/** Callback called on error responses (HTTP errors or network failures) */
|
|
124
|
+
onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
|
|
125
|
+
};
|
|
126
|
+
/** Hook type returned by enlaceHookReact */
|
|
127
|
+
type EnlaceHook<TSchema, TDefaultError = unknown> = {
|
|
128
|
+
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod, TDefaultError>): UseEnlaceSelectorResult<TMethod>;
|
|
129
|
+
<TData, TError>(queryFn: QueryFn<TSchema, TData, TError, TDefaultError>, options?: UseEnlaceQueryOptions<TData, TError>): UseEnlaceQueryResult<TData, TError>;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates a React hook for making API calls.
|
|
134
|
+
* Called at module level to create a reusable hook.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* const useAPI = enlaceHookReact<ApiSchema>('https://api.com');
|
|
138
|
+
*
|
|
139
|
+
* // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
|
|
140
|
+
* const { loading, data, error } = useAPI((api) => api.posts.$get({ query: { userId } }));
|
|
141
|
+
*
|
|
142
|
+
* // Selector mode - typed trigger for lazy calls
|
|
143
|
+
* const { trigger, loading, data, error } = useAPI((api) => api.posts.$delete);
|
|
144
|
+
* onClick={() => trigger({ body: { id: 1 } })}
|
|
145
|
+
*/
|
|
146
|
+
declare function enlaceHookReact<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: EnlaceHookOptions): EnlaceHook<TSchema, TDefaultError>;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Handler function called after successful mutations to trigger server-side revalidation.
|
|
150
|
+
* @param tags - Cache tags to revalidate
|
|
151
|
+
* @param paths - URL paths to revalidate
|
|
152
|
+
*/
|
|
153
|
+
type ServerRevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
|
|
154
|
+
/** Next.js-specific options (third argument for enlaceNext) */
|
|
155
|
+
type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
|
|
156
|
+
/**
|
|
157
|
+
* Handler called after successful mutations to trigger server-side revalidation.
|
|
158
|
+
* Receives auto-generated or manually specified tags and paths.
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* enlaceNext("http://localhost:3000/api/", {}, {
|
|
162
|
+
* serverRevalidator: (tags, paths) => revalidateServerAction(tags, paths)
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
serverRevalidator?: ServerRevalidateHandler;
|
|
167
|
+
/**
|
|
168
|
+
* Skip server-side revalidation by default for all mutations.
|
|
169
|
+
* Individual requests can override with serverRevalidate: true.
|
|
170
|
+
* Useful for CSR-heavy apps where server cache invalidation is rarely needed.
|
|
171
|
+
* @default false
|
|
172
|
+
*/
|
|
173
|
+
skipServerRevalidation?: boolean;
|
|
174
|
+
};
|
|
175
|
+
/** Next.js hook options (third argument for enlaceHookNext) - extends React's EnlaceHookOptions */
|
|
176
|
+
type NextHookOptions = EnlaceHookOptions & Pick<NextOptions, "serverRevalidator" | "skipServerRevalidation">;
|
|
177
|
+
/** Per-request options for Next.js fetch - extends React's base options */
|
|
178
|
+
type NextRequestOptionsBase = ReactRequestOptionsBase & {
|
|
179
|
+
/** Time in seconds to revalidate, or false to disable */
|
|
180
|
+
revalidate?: number | false;
|
|
181
|
+
/**
|
|
182
|
+
* URL paths to revalidate after mutation.
|
|
183
|
+
* Passed to the serverRevalidator handler.
|
|
184
|
+
*/
|
|
185
|
+
revalidatePaths?: string[];
|
|
186
|
+
/**
|
|
187
|
+
* Control server-side revalidation for this specific request.
|
|
188
|
+
* - true: Force server revalidation
|
|
189
|
+
* - false: Skip server revalidation
|
|
190
|
+
* When undefined, follows the global skipServerRevalidation setting.
|
|
191
|
+
*/
|
|
192
|
+
serverRevalidate?: boolean;
|
|
193
|
+
};
|
|
194
|
+
type NextQueryFn<TSchema, TData, TError, TDefaultError = unknown> = QueryFn<TSchema, TData, TError, TDefaultError, NextRequestOptionsBase>;
|
|
195
|
+
type NextSelectorFn<TSchema, TMethod, TDefaultError = unknown> = SelectorFn<TSchema, TMethod, TDefaultError, NextRequestOptionsBase>;
|
|
196
|
+
/** Hook type returned by enlaceHookNext */
|
|
197
|
+
type NextEnlaceHook<TSchema, TDefaultError = unknown> = {
|
|
198
|
+
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: NextSelectorFn<TSchema, TMethod, TDefaultError>): UseEnlaceSelectorResult<TMethod>;
|
|
199
|
+
<TData, TError>(queryFn: NextQueryFn<TSchema, TData, TError, TDefaultError>, options?: UseEnlaceQueryOptions<TData, TError>): UseEnlaceQueryResult<TData, TError>;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Creates a React hook for making API calls in Next.js Client Components.
|
|
204
|
+
* Uses Next.js-specific features like serverRevalidator for server-side cache invalidation.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* const useAPI = enlaceHookNext<ApiSchema>('https://api.com', {}, {
|
|
208
|
+
* serverRevalidator: (tags) => revalidateTagsAction(tags),
|
|
209
|
+
* staleTime: 5000,
|
|
210
|
+
* });
|
|
211
|
+
*
|
|
212
|
+
* // Query mode - auto-fetch
|
|
213
|
+
* const { loading, data, error } = useAPI((api) => api.posts.$get());
|
|
214
|
+
*
|
|
215
|
+
* // Selector mode - trigger for mutations
|
|
216
|
+
* const { trigger } = useAPI((api) => api.posts.$delete);
|
|
217
|
+
*/
|
|
218
|
+
declare function enlaceHookNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: NextHookOptions): NextEnlaceHook<TSchema, TDefaultError>;
|
|
219
|
+
|
|
220
|
+
export { type AnyReactRequestOptions, type ApiClient, type EnlaceHook, type EnlaceHookOptions, HTTP_METHODS, type HookState, type NextEnlaceHook, type NextHookOptions, type PollingInterval, type PollingIntervalFn, type PollingIntervalValue, type QueryFn, type ReactRequestOptionsBase, type SelectorFn, type TrackedCall, type UseEnlaceQueryOptions, type UseEnlaceQueryResult, type UseEnlaceSelectorResult, enlaceHookNext, enlaceHookReact };
|