enlace 0.0.1-beta.5 → 0.0.1-beta.7
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 +26 -21
- package/dist/{next/hook → hook}/index.d.mts +43 -9
- package/dist/{next/hook → hook}/index.d.ts +43 -9
- package/dist/{next/hook → hook}/index.js +141 -103
- package/dist/{next/hook → hook}/index.mjs +142 -121
- package/dist/index.d.mts +41 -78
- package/dist/index.d.ts +41 -78
- package/dist/index.js +48 -391
- package/dist/index.mjs +52 -391
- package/package.json +6 -11
- package/dist/next/index.d.mts +0 -133
- package/dist/next/index.d.ts +0 -133
- package/dist/next/index.js +0 -89
- package/dist/next/index.mjs +0 -71
|
@@ -1,96 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
"use client";
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
-
|
|
21
|
-
// src/next/index.ts
|
|
22
|
-
var next_exports = {};
|
|
23
|
-
__export(next_exports, {
|
|
24
|
-
createEnlace: () => createEnlace
|
|
25
|
-
});
|
|
26
|
-
import {
|
|
27
|
-
createProxyHandler
|
|
28
|
-
} from "enlace-core";
|
|
29
|
-
|
|
30
|
-
// src/next/fetch.ts
|
|
31
|
-
import {
|
|
32
|
-
executeFetch
|
|
33
|
-
} from "enlace-core";
|
|
34
|
-
|
|
35
|
-
// src/utils/generateTags.ts
|
|
36
|
-
function generateTags(path) {
|
|
37
|
-
return path.map((_, i) => path.slice(0, i + 1).join("/"));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// src/next/fetch.ts
|
|
41
|
-
async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
|
|
42
|
-
const {
|
|
43
|
-
autoGenerateTags = true,
|
|
44
|
-
autoRevalidateTags = true,
|
|
45
|
-
revalidator,
|
|
46
|
-
onSuccess,
|
|
47
|
-
...coreOptions
|
|
48
|
-
} = combinedOptions;
|
|
49
|
-
const isGet = method === "GET";
|
|
50
|
-
const autoTags = generateTags(path);
|
|
51
|
-
const nextOnSuccess = (payload) => {
|
|
52
|
-
if (!isGet && !requestOptions?.skipRevalidator) {
|
|
53
|
-
const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
|
|
54
|
-
const revalidatePaths = requestOptions?.revalidatePaths ?? [];
|
|
55
|
-
if (revalidateTags.length || revalidatePaths.length) {
|
|
56
|
-
revalidator?.(revalidateTags, revalidatePaths);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
onSuccess?.(payload);
|
|
60
|
-
};
|
|
61
|
-
const nextRequestOptions = { ...requestOptions };
|
|
62
|
-
if (isGet) {
|
|
63
|
-
const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
|
|
64
|
-
const nextFetchOptions = {};
|
|
65
|
-
if (tags) {
|
|
66
|
-
nextFetchOptions.tags = tags;
|
|
67
|
-
}
|
|
68
|
-
if (requestOptions?.revalidate !== void 0) {
|
|
69
|
-
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
70
|
-
}
|
|
71
|
-
nextRequestOptions.next = nextFetchOptions;
|
|
72
|
-
}
|
|
73
|
-
return executeFetch(
|
|
74
|
-
baseUrl,
|
|
75
|
-
path,
|
|
76
|
-
method,
|
|
77
|
-
{ ...coreOptions, onSuccess: nextOnSuccess },
|
|
78
|
-
nextRequestOptions
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
3
|
|
|
82
|
-
// src/
|
|
83
|
-
|
|
84
|
-
import * as enlace_core_star from "enlace-core";
|
|
85
|
-
function createEnlace(baseUrl, defaultOptions = {}, nextOptions = {}) {
|
|
86
|
-
const combinedOptions = { ...defaultOptions, ...nextOptions };
|
|
87
|
-
return createProxyHandler(
|
|
88
|
-
baseUrl,
|
|
89
|
-
combinedOptions,
|
|
90
|
-
[],
|
|
91
|
-
executeNextFetch
|
|
92
|
-
);
|
|
93
|
-
}
|
|
4
|
+
// src/react/createEnlaceHookReact.ts
|
|
5
|
+
import { createEnlace } from "enlace-core";
|
|
94
6
|
|
|
95
7
|
// src/react/useQueryMode.ts
|
|
96
8
|
import { useRef, useReducer, useEffect } from "react";
|
|
@@ -136,6 +48,11 @@ function hookReducer(state, action) {
|
|
|
136
48
|
}
|
|
137
49
|
}
|
|
138
50
|
|
|
51
|
+
// src/utils/generateTags.ts
|
|
52
|
+
function generateTags(path) {
|
|
53
|
+
return path.map((_, i) => path.slice(0, i + 1).join("/"));
|
|
54
|
+
}
|
|
55
|
+
|
|
139
56
|
// src/utils/sortObjectKeys.ts
|
|
140
57
|
function sortObjectKeys(obj) {
|
|
141
58
|
if (obj === null || typeof obj !== "object") return obj;
|
|
@@ -340,6 +257,38 @@ function useQueryMode(api, trackedCall, options) {
|
|
|
340
257
|
return state;
|
|
341
258
|
}
|
|
342
259
|
|
|
260
|
+
// src/react/types.ts
|
|
261
|
+
var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
|
|
262
|
+
|
|
263
|
+
// src/react/trackingProxy.ts
|
|
264
|
+
function createTrackingProxy(onTrack) {
|
|
265
|
+
const createProxy = (path = []) => {
|
|
266
|
+
return new Proxy(() => {
|
|
267
|
+
}, {
|
|
268
|
+
get(_, prop) {
|
|
269
|
+
if (HTTP_METHODS.includes(prop)) {
|
|
270
|
+
const methodFn = (options) => {
|
|
271
|
+
onTrack({
|
|
272
|
+
trackedCall: { path, method: prop, options },
|
|
273
|
+
selectorPath: null,
|
|
274
|
+
selectorMethod: null
|
|
275
|
+
});
|
|
276
|
+
return Promise.resolve({ ok: true, data: void 0 });
|
|
277
|
+
};
|
|
278
|
+
onTrack({
|
|
279
|
+
trackedCall: null,
|
|
280
|
+
selectorPath: path,
|
|
281
|
+
selectorMethod: prop
|
|
282
|
+
});
|
|
283
|
+
return methodFn;
|
|
284
|
+
}
|
|
285
|
+
return createProxy([...path, prop]);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
return createProxy();
|
|
290
|
+
}
|
|
291
|
+
|
|
343
292
|
// src/react/useSelectorMode.ts
|
|
344
293
|
import { useRef as useRef2, useReducer as useReducer2 } from "react";
|
|
345
294
|
function resolvePath2(path, pathParams) {
|
|
@@ -407,47 +356,117 @@ function useSelectorMode(config) {
|
|
|
407
356
|
};
|
|
408
357
|
}
|
|
409
358
|
|
|
410
|
-
// src/react/
|
|
411
|
-
|
|
359
|
+
// src/react/createEnlaceHookReact.ts
|
|
360
|
+
function createEnlaceHookReact(baseUrl, defaultOptions = {}, hookOptions = {}) {
|
|
361
|
+
const {
|
|
362
|
+
autoGenerateTags = true,
|
|
363
|
+
autoRevalidateTags = true,
|
|
364
|
+
staleTime = 0,
|
|
365
|
+
onSuccess,
|
|
366
|
+
onError
|
|
367
|
+
} = hookOptions;
|
|
368
|
+
const api = createEnlace(baseUrl, defaultOptions, { onSuccess, onError });
|
|
369
|
+
function useEnlaceHook(selectorOrQuery, queryOptions) {
|
|
370
|
+
let trackingResult = {
|
|
371
|
+
trackedCall: null,
|
|
372
|
+
selectorPath: null,
|
|
373
|
+
selectorMethod: null
|
|
374
|
+
};
|
|
375
|
+
const trackingProxy = createTrackingProxy((result2) => {
|
|
376
|
+
trackingResult = result2;
|
|
377
|
+
});
|
|
378
|
+
const result = selectorOrQuery(
|
|
379
|
+
trackingProxy
|
|
380
|
+
);
|
|
381
|
+
if (typeof result === "function") {
|
|
382
|
+
const actualResult = selectorOrQuery(api);
|
|
383
|
+
return useSelectorMode({
|
|
384
|
+
method: actualResult,
|
|
385
|
+
api,
|
|
386
|
+
path: trackingResult.selectorPath ?? [],
|
|
387
|
+
methodName: trackingResult.selectorMethod ?? "",
|
|
388
|
+
autoRevalidateTags
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
return useQueryMode(
|
|
392
|
+
api,
|
|
393
|
+
trackingResult.trackedCall,
|
|
394
|
+
{ autoGenerateTags, staleTime, enabled: queryOptions?.enabled ?? true }
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
return useEnlaceHook;
|
|
398
|
+
}
|
|
412
399
|
|
|
413
|
-
// src/
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
400
|
+
// src/next/index.ts
|
|
401
|
+
import {
|
|
402
|
+
createProxyHandler
|
|
403
|
+
} from "enlace-core";
|
|
404
|
+
|
|
405
|
+
// src/next/fetch.ts
|
|
406
|
+
import {
|
|
407
|
+
executeFetch
|
|
408
|
+
} from "enlace-core";
|
|
409
|
+
async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
|
|
410
|
+
const {
|
|
411
|
+
autoGenerateTags = true,
|
|
412
|
+
autoRevalidateTags = true,
|
|
413
|
+
revalidator,
|
|
414
|
+
onSuccess,
|
|
415
|
+
...coreOptions
|
|
416
|
+
} = combinedOptions;
|
|
417
|
+
const isGet = method === "GET";
|
|
418
|
+
const autoTags = generateTags(path);
|
|
419
|
+
const nextOnSuccess = (payload) => {
|
|
420
|
+
if (!isGet && !requestOptions?.skipRevalidator) {
|
|
421
|
+
const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
|
|
422
|
+
const revalidatePaths = requestOptions?.revalidatePaths ?? [];
|
|
423
|
+
if (revalidateTags.length || revalidatePaths.length) {
|
|
424
|
+
revalidator?.(revalidateTags, revalidatePaths);
|
|
436
425
|
}
|
|
437
|
-
}
|
|
426
|
+
}
|
|
427
|
+
onSuccess?.(payload);
|
|
438
428
|
};
|
|
439
|
-
|
|
429
|
+
const nextRequestOptions = { ...requestOptions };
|
|
430
|
+
if (isGet) {
|
|
431
|
+
const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
|
|
432
|
+
const nextFetchOptions = {};
|
|
433
|
+
if (tags) {
|
|
434
|
+
nextFetchOptions.tags = tags;
|
|
435
|
+
}
|
|
436
|
+
if (requestOptions?.revalidate !== void 0) {
|
|
437
|
+
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
438
|
+
}
|
|
439
|
+
nextRequestOptions.next = nextFetchOptions;
|
|
440
|
+
}
|
|
441
|
+
return executeFetch(
|
|
442
|
+
baseUrl,
|
|
443
|
+
path,
|
|
444
|
+
method,
|
|
445
|
+
{ ...coreOptions, onSuccess: nextOnSuccess },
|
|
446
|
+
nextRequestOptions
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// src/next/index.ts
|
|
451
|
+
function createEnlaceNext(baseUrl, defaultOptions = {}, nextOptions = {}) {
|
|
452
|
+
const combinedOptions = { ...defaultOptions, ...nextOptions };
|
|
453
|
+
return createProxyHandler(
|
|
454
|
+
baseUrl,
|
|
455
|
+
combinedOptions,
|
|
456
|
+
[],
|
|
457
|
+
executeNextFetch
|
|
458
|
+
);
|
|
440
459
|
}
|
|
441
460
|
|
|
442
|
-
// src/next/
|
|
443
|
-
function
|
|
461
|
+
// src/next/createEnlaceHookNext.ts
|
|
462
|
+
function createEnlaceHookNext(baseUrl, defaultOptions = {}, hookOptions = {}) {
|
|
444
463
|
const {
|
|
445
464
|
autoGenerateTags = true,
|
|
446
465
|
autoRevalidateTags = true,
|
|
447
466
|
staleTime = 0,
|
|
448
467
|
...nextOptions
|
|
449
468
|
} = hookOptions;
|
|
450
|
-
const api =
|
|
469
|
+
const api = createEnlaceNext(baseUrl, defaultOptions, {
|
|
451
470
|
autoGenerateTags,
|
|
452
471
|
autoRevalidateTags,
|
|
453
472
|
...nextOptions
|
|
@@ -481,5 +500,7 @@ function createEnlaceHook(baseUrl, defaultOptions = {}, hookOptions = {}) {
|
|
|
481
500
|
return useEnlaceHook;
|
|
482
501
|
}
|
|
483
502
|
export {
|
|
484
|
-
|
|
503
|
+
HTTP_METHODS,
|
|
504
|
+
createEnlaceHookNext,
|
|
505
|
+
createEnlaceHookReact
|
|
485
506
|
};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload,
|
|
1
|
+
import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
|
|
2
2
|
export * from 'enlace-core';
|
|
3
3
|
|
|
4
4
|
/** Per-request options for React hooks */
|
|
@@ -20,59 +20,7 @@ type ReactRequestOptionsBase = {
|
|
|
20
20
|
*/
|
|
21
21
|
pathParams?: Record<string, string | number>;
|
|
22
22
|
};
|
|
23
|
-
/** Options for
|
|
24
|
-
type UseEnlaceQueryOptions = {
|
|
25
|
-
/**
|
|
26
|
-
* Whether the query should execute.
|
|
27
|
-
* Set to false to skip fetching (useful when ID is "new" or undefined).
|
|
28
|
-
* @default true
|
|
29
|
-
*/
|
|
30
|
-
enabled?: boolean;
|
|
31
|
-
};
|
|
32
|
-
type ApiClient<TSchema, TOptions = ReactRequestOptionsBase> = unknown extends TSchema ? WildcardClient<TOptions> : EnlaceClient<TSchema, TOptions>;
|
|
33
|
-
type QueryFn<TSchema, TData, TError, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
|
|
34
|
-
type SelectorFn<TSchema, TMethod, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => TMethod;
|
|
35
|
-
type HookState = {
|
|
36
|
-
loading: boolean;
|
|
37
|
-
fetching: boolean;
|
|
38
|
-
ok: boolean | undefined;
|
|
39
|
-
data: unknown;
|
|
40
|
-
error: unknown;
|
|
41
|
-
};
|
|
42
|
-
type TrackedCall = {
|
|
43
|
-
path: string[];
|
|
44
|
-
method: string;
|
|
45
|
-
options: unknown;
|
|
46
|
-
};
|
|
47
|
-
declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete"];
|
|
48
|
-
type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
|
|
49
|
-
type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
|
|
50
|
-
/** Discriminated union for hook response state - enables type narrowing on ok check */
|
|
51
|
-
type HookResponseState<TData, TError> = {
|
|
52
|
-
ok: undefined;
|
|
53
|
-
data: undefined;
|
|
54
|
-
error: undefined;
|
|
55
|
-
} | {
|
|
56
|
-
ok: true;
|
|
57
|
-
data: TData;
|
|
58
|
-
error: undefined;
|
|
59
|
-
} | {
|
|
60
|
-
ok: false;
|
|
61
|
-
data: undefined;
|
|
62
|
-
error: TError;
|
|
63
|
-
};
|
|
64
|
-
/** Result when hook is called with query function (auto-fetch mode) */
|
|
65
|
-
type UseEnlaceQueryResult<TData, TError> = {
|
|
66
|
-
loading: boolean;
|
|
67
|
-
fetching: boolean;
|
|
68
|
-
} & HookResponseState<TData, TError>;
|
|
69
|
-
/** Result when hook is called with method selector (trigger mode) */
|
|
70
|
-
type UseEnlaceSelectorResult<TMethod> = {
|
|
71
|
-
trigger: TMethod;
|
|
72
|
-
loading: boolean;
|
|
73
|
-
fetching: boolean;
|
|
74
|
-
} & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
|
|
75
|
-
/** Options for createEnlaceHook factory */
|
|
23
|
+
/** Options for createEnlaceHookReact factory */
|
|
76
24
|
type EnlaceHookOptions = {
|
|
77
25
|
/**
|
|
78
26
|
* Auto-generate cache tags from URL path for GET requests.
|
|
@@ -89,32 +37,47 @@ type EnlaceHookOptions = {
|
|
|
89
37
|
/** Callback called on error responses (HTTP errors or network failures) */
|
|
90
38
|
onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
|
|
91
39
|
};
|
|
92
|
-
/** Hook type returned by createEnlaceHook */
|
|
93
|
-
type EnlaceHook<TSchema> = {
|
|
94
|
-
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod>): UseEnlaceSelectorResult<TMethod>;
|
|
95
|
-
<TData, TError>(queryFn: QueryFn<TSchema, TData, TError>, options?: UseEnlaceQueryOptions): UseEnlaceQueryResult<TData, TError>;
|
|
96
|
-
};
|
|
97
40
|
|
|
98
41
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* const useAPI = createEnlaceHook<ApiSchema>('https://api.com');
|
|
104
|
-
*
|
|
105
|
-
* // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
|
|
106
|
-
* const { loading, data, error } = useAPI((api) => api.posts.get({ query: { userId } }));
|
|
107
|
-
*
|
|
108
|
-
* // Selector mode - typed trigger for lazy calls
|
|
109
|
-
* const { trigger, loading, data, error } = useAPI((api) => api.posts.delete);
|
|
110
|
-
* onClick={() => trigger({ body: { id: 1 } })}
|
|
42
|
+
* Handler function called after successful mutations to trigger server-side revalidation.
|
|
43
|
+
* @param tags - Cache tags to revalidate
|
|
44
|
+
* @param paths - URL paths to revalidate
|
|
111
45
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
type
|
|
115
|
-
|
|
116
|
-
|
|
46
|
+
type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
|
|
47
|
+
/** Next.js-specific options (third argument for createEnlaceNext) */
|
|
48
|
+
type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
|
|
49
|
+
/**
|
|
50
|
+
* Handler called after successful mutations to trigger server-side revalidation.
|
|
51
|
+
* Receives auto-generated or manually specified tags and paths.
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* createEnlaceNext("http://localhost:3000/api/", {}, {
|
|
55
|
+
* revalidator: (tags, paths) => revalidateServerAction(tags, paths)
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
revalidator?: RevalidateHandler;
|
|
60
|
+
};
|
|
61
|
+
/** Per-request options for Next.js fetch - extends React's base options */
|
|
62
|
+
type NextRequestOptionsBase = ReactRequestOptionsBase & {
|
|
63
|
+
/** Time in seconds to revalidate, or false to disable */
|
|
64
|
+
revalidate?: number | false;
|
|
65
|
+
/**
|
|
66
|
+
* URL paths to revalidate after mutation
|
|
67
|
+
* This doesn't do anything on the client by itself - it's passed to the revalidator handler.
|
|
68
|
+
* You must implement the revalidation logic in the revalidator.
|
|
69
|
+
*/
|
|
70
|
+
revalidatePaths?: string[];
|
|
71
|
+
/**
|
|
72
|
+
* Skip server-side revalidation for this request.
|
|
73
|
+
* Useful when autoRevalidateTags is enabled but you want to opt-out for specific mutations.
|
|
74
|
+
* You can still pass empty [] to revalidateTags to skip triggering revalidation.
|
|
75
|
+
* But this flag can be used if you want to revalidate client-side and skip server-side entirely.
|
|
76
|
+
* Eg. you don't fetch any data on server component and you might want to skip the overhead of revalidation.
|
|
77
|
+
*/
|
|
78
|
+
skipRevalidator?: boolean;
|
|
79
|
+
};
|
|
117
80
|
|
|
118
|
-
declare function
|
|
81
|
+
declare function createEnlaceNext<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
|
|
119
82
|
|
|
120
|
-
export { type
|
|
83
|
+
export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload,
|
|
1
|
+
import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
|
|
2
2
|
export * from 'enlace-core';
|
|
3
3
|
|
|
4
4
|
/** Per-request options for React hooks */
|
|
@@ -20,59 +20,7 @@ type ReactRequestOptionsBase = {
|
|
|
20
20
|
*/
|
|
21
21
|
pathParams?: Record<string, string | number>;
|
|
22
22
|
};
|
|
23
|
-
/** Options for
|
|
24
|
-
type UseEnlaceQueryOptions = {
|
|
25
|
-
/**
|
|
26
|
-
* Whether the query should execute.
|
|
27
|
-
* Set to false to skip fetching (useful when ID is "new" or undefined).
|
|
28
|
-
* @default true
|
|
29
|
-
*/
|
|
30
|
-
enabled?: boolean;
|
|
31
|
-
};
|
|
32
|
-
type ApiClient<TSchema, TOptions = ReactRequestOptionsBase> = unknown extends TSchema ? WildcardClient<TOptions> : EnlaceClient<TSchema, TOptions>;
|
|
33
|
-
type QueryFn<TSchema, TData, TError, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
|
|
34
|
-
type SelectorFn<TSchema, TMethod, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => TMethod;
|
|
35
|
-
type HookState = {
|
|
36
|
-
loading: boolean;
|
|
37
|
-
fetching: boolean;
|
|
38
|
-
ok: boolean | undefined;
|
|
39
|
-
data: unknown;
|
|
40
|
-
error: unknown;
|
|
41
|
-
};
|
|
42
|
-
type TrackedCall = {
|
|
43
|
-
path: string[];
|
|
44
|
-
method: string;
|
|
45
|
-
options: unknown;
|
|
46
|
-
};
|
|
47
|
-
declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete"];
|
|
48
|
-
type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
|
|
49
|
-
type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
|
|
50
|
-
/** Discriminated union for hook response state - enables type narrowing on ok check */
|
|
51
|
-
type HookResponseState<TData, TError> = {
|
|
52
|
-
ok: undefined;
|
|
53
|
-
data: undefined;
|
|
54
|
-
error: undefined;
|
|
55
|
-
} | {
|
|
56
|
-
ok: true;
|
|
57
|
-
data: TData;
|
|
58
|
-
error: undefined;
|
|
59
|
-
} | {
|
|
60
|
-
ok: false;
|
|
61
|
-
data: undefined;
|
|
62
|
-
error: TError;
|
|
63
|
-
};
|
|
64
|
-
/** Result when hook is called with query function (auto-fetch mode) */
|
|
65
|
-
type UseEnlaceQueryResult<TData, TError> = {
|
|
66
|
-
loading: boolean;
|
|
67
|
-
fetching: boolean;
|
|
68
|
-
} & HookResponseState<TData, TError>;
|
|
69
|
-
/** Result when hook is called with method selector (trigger mode) */
|
|
70
|
-
type UseEnlaceSelectorResult<TMethod> = {
|
|
71
|
-
trigger: TMethod;
|
|
72
|
-
loading: boolean;
|
|
73
|
-
fetching: boolean;
|
|
74
|
-
} & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
|
|
75
|
-
/** Options for createEnlaceHook factory */
|
|
23
|
+
/** Options for createEnlaceHookReact factory */
|
|
76
24
|
type EnlaceHookOptions = {
|
|
77
25
|
/**
|
|
78
26
|
* Auto-generate cache tags from URL path for GET requests.
|
|
@@ -89,32 +37,47 @@ type EnlaceHookOptions = {
|
|
|
89
37
|
/** Callback called on error responses (HTTP errors or network failures) */
|
|
90
38
|
onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
|
|
91
39
|
};
|
|
92
|
-
/** Hook type returned by createEnlaceHook */
|
|
93
|
-
type EnlaceHook<TSchema> = {
|
|
94
|
-
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod>): UseEnlaceSelectorResult<TMethod>;
|
|
95
|
-
<TData, TError>(queryFn: QueryFn<TSchema, TData, TError>, options?: UseEnlaceQueryOptions): UseEnlaceQueryResult<TData, TError>;
|
|
96
|
-
};
|
|
97
40
|
|
|
98
41
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* const useAPI = createEnlaceHook<ApiSchema>('https://api.com');
|
|
104
|
-
*
|
|
105
|
-
* // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
|
|
106
|
-
* const { loading, data, error } = useAPI((api) => api.posts.get({ query: { userId } }));
|
|
107
|
-
*
|
|
108
|
-
* // Selector mode - typed trigger for lazy calls
|
|
109
|
-
* const { trigger, loading, data, error } = useAPI((api) => api.posts.delete);
|
|
110
|
-
* onClick={() => trigger({ body: { id: 1 } })}
|
|
42
|
+
* Handler function called after successful mutations to trigger server-side revalidation.
|
|
43
|
+
* @param tags - Cache tags to revalidate
|
|
44
|
+
* @param paths - URL paths to revalidate
|
|
111
45
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
type
|
|
115
|
-
|
|
116
|
-
|
|
46
|
+
type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
|
|
47
|
+
/** Next.js-specific options (third argument for createEnlaceNext) */
|
|
48
|
+
type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
|
|
49
|
+
/**
|
|
50
|
+
* Handler called after successful mutations to trigger server-side revalidation.
|
|
51
|
+
* Receives auto-generated or manually specified tags and paths.
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* createEnlaceNext("http://localhost:3000/api/", {}, {
|
|
55
|
+
* revalidator: (tags, paths) => revalidateServerAction(tags, paths)
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
revalidator?: RevalidateHandler;
|
|
60
|
+
};
|
|
61
|
+
/** Per-request options for Next.js fetch - extends React's base options */
|
|
62
|
+
type NextRequestOptionsBase = ReactRequestOptionsBase & {
|
|
63
|
+
/** Time in seconds to revalidate, or false to disable */
|
|
64
|
+
revalidate?: number | false;
|
|
65
|
+
/**
|
|
66
|
+
* URL paths to revalidate after mutation
|
|
67
|
+
* This doesn't do anything on the client by itself - it's passed to the revalidator handler.
|
|
68
|
+
* You must implement the revalidation logic in the revalidator.
|
|
69
|
+
*/
|
|
70
|
+
revalidatePaths?: string[];
|
|
71
|
+
/**
|
|
72
|
+
* Skip server-side revalidation for this request.
|
|
73
|
+
* Useful when autoRevalidateTags is enabled but you want to opt-out for specific mutations.
|
|
74
|
+
* You can still pass empty [] to revalidateTags to skip triggering revalidation.
|
|
75
|
+
* But this flag can be used if you want to revalidate client-side and skip server-side entirely.
|
|
76
|
+
* Eg. you don't fetch any data on server component and you might want to skip the overhead of revalidation.
|
|
77
|
+
*/
|
|
78
|
+
skipRevalidator?: boolean;
|
|
79
|
+
};
|
|
117
80
|
|
|
118
|
-
declare function
|
|
81
|
+
declare function createEnlaceNext<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
|
|
119
82
|
|
|
120
|
-
export { type
|
|
83
|
+
export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };
|