enlace 0.0.1-beta.1 → 0.0.1-beta.10
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/LICENSE +21 -0
- package/README.md +728 -0
- package/dist/hook/index.d.mts +173 -0
- package/dist/hook/index.d.ts +173 -0
- package/dist/{next/hook → hook}/index.js +231 -158
- package/dist/{next/hook → hook}/index.mjs +232 -178
- package/dist/index.d.mts +54 -68
- package/dist/index.d.ts +54 -68
- package/dist/index.js +49 -327
- package/dist/index.mjs +51 -327
- package/package.json +6 -11
- package/dist/next/hook/index.d.mts +0 -124
- package/dist/next/hook/index.d.ts +0 -124
- 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
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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 */
|
|
@@ -11,51 +11,16 @@ type ReactRequestOptionsBase = {
|
|
|
11
11
|
tags?: string[];
|
|
12
12
|
/** Tags to invalidate after mutation (triggers refetch in matching queries) */
|
|
13
13
|
revalidateTags?: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Path parameters for dynamic URL segments.
|
|
16
|
+
* Used to replace :paramName placeholders in the URL path.
|
|
17
|
+
* @example
|
|
18
|
+
* // With path api.products[':id'].delete
|
|
19
|
+
* trigger({ pathParams: { id: '123' } }) // → DELETE /products/123
|
|
20
|
+
*/
|
|
21
|
+
pathParams?: Record<string, string | number>;
|
|
14
22
|
};
|
|
15
|
-
|
|
16
|
-
type QueryFn<TSchema, TData, TError, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
|
|
17
|
-
type SelectorFn<TSchema, TMethod, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => TMethod;
|
|
18
|
-
type HookState = {
|
|
19
|
-
loading: boolean;
|
|
20
|
-
fetching: boolean;
|
|
21
|
-
ok: boolean | undefined;
|
|
22
|
-
data: unknown;
|
|
23
|
-
error: unknown;
|
|
24
|
-
};
|
|
25
|
-
type TrackedCall = {
|
|
26
|
-
path: string[];
|
|
27
|
-
method: string;
|
|
28
|
-
options: unknown;
|
|
29
|
-
};
|
|
30
|
-
declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete"];
|
|
31
|
-
type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
|
|
32
|
-
type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
|
|
33
|
-
/** Discriminated union for hook response state - enables type narrowing on ok check */
|
|
34
|
-
type HookResponseState<TData, TError> = {
|
|
35
|
-
ok: undefined;
|
|
36
|
-
data: undefined;
|
|
37
|
-
error: undefined;
|
|
38
|
-
} | {
|
|
39
|
-
ok: true;
|
|
40
|
-
data: TData;
|
|
41
|
-
error: undefined;
|
|
42
|
-
} | {
|
|
43
|
-
ok: false;
|
|
44
|
-
data: undefined;
|
|
45
|
-
error: TError;
|
|
46
|
-
};
|
|
47
|
-
/** Result when hook is called with query function (auto-fetch mode) */
|
|
48
|
-
type UseEnlaceQueryResult<TData, TError> = {
|
|
49
|
-
loading: boolean;
|
|
50
|
-
fetching: boolean;
|
|
51
|
-
} & HookResponseState<TData, TError>;
|
|
52
|
-
/** Result when hook is called with method selector (trigger mode) */
|
|
53
|
-
type UseEnlaceSelectorResult<TMethod> = {
|
|
54
|
-
trigger: TMethod;
|
|
55
|
-
loading: boolean;
|
|
56
|
-
fetching: boolean;
|
|
57
|
-
} & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
|
|
58
|
-
|
|
23
|
+
/** Options for createEnlaceHookReact factory */
|
|
59
24
|
type EnlaceHookOptions = {
|
|
60
25
|
/**
|
|
61
26
|
* Auto-generate cache tags from URL path for GET requests.
|
|
@@ -67,31 +32,52 @@ type EnlaceHookOptions = {
|
|
|
67
32
|
autoRevalidateTags?: boolean;
|
|
68
33
|
/** Time in ms before cached data is considered stale. @default 0 (always stale) */
|
|
69
34
|
staleTime?: number;
|
|
35
|
+
/** Callback called on successful API responses */
|
|
36
|
+
onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
|
|
37
|
+
/** Callback called on error responses (HTTP errors or network failures) */
|
|
38
|
+
onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
|
|
70
39
|
};
|
|
71
|
-
|
|
72
|
-
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod>): UseEnlaceSelectorResult<TMethod>;
|
|
73
|
-
<TData, TError>(queryFn: QueryFn<TSchema, TData, TError>): UseEnlaceQueryResult<TData, TError>;
|
|
74
|
-
};
|
|
40
|
+
|
|
75
41
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* const useAPI = createEnlaceHook<ApiSchema>('https://api.com');
|
|
81
|
-
*
|
|
82
|
-
* // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
|
|
83
|
-
* const { loading, data, error } = useAPI((api) => api.posts.get({ query: { userId } }));
|
|
84
|
-
*
|
|
85
|
-
* // Selector mode - typed trigger for lazy calls
|
|
86
|
-
* const { trigger, loading, data, error } = useAPI((api) => api.posts.delete);
|
|
87
|
-
* 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
|
|
88
45
|
*/
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
type
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
};
|
|
94
80
|
|
|
95
|
-
declare function
|
|
81
|
+
declare function createEnlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
|
|
96
82
|
|
|
97
|
-
export { type
|
|
83
|
+
export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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 */
|
|
@@ -11,51 +11,16 @@ type ReactRequestOptionsBase = {
|
|
|
11
11
|
tags?: string[];
|
|
12
12
|
/** Tags to invalidate after mutation (triggers refetch in matching queries) */
|
|
13
13
|
revalidateTags?: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Path parameters for dynamic URL segments.
|
|
16
|
+
* Used to replace :paramName placeholders in the URL path.
|
|
17
|
+
* @example
|
|
18
|
+
* // With path api.products[':id'].delete
|
|
19
|
+
* trigger({ pathParams: { id: '123' } }) // → DELETE /products/123
|
|
20
|
+
*/
|
|
21
|
+
pathParams?: Record<string, string | number>;
|
|
14
22
|
};
|
|
15
|
-
|
|
16
|
-
type QueryFn<TSchema, TData, TError, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
|
|
17
|
-
type SelectorFn<TSchema, TMethod, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => TMethod;
|
|
18
|
-
type HookState = {
|
|
19
|
-
loading: boolean;
|
|
20
|
-
fetching: boolean;
|
|
21
|
-
ok: boolean | undefined;
|
|
22
|
-
data: unknown;
|
|
23
|
-
error: unknown;
|
|
24
|
-
};
|
|
25
|
-
type TrackedCall = {
|
|
26
|
-
path: string[];
|
|
27
|
-
method: string;
|
|
28
|
-
options: unknown;
|
|
29
|
-
};
|
|
30
|
-
declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete"];
|
|
31
|
-
type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
|
|
32
|
-
type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
|
|
33
|
-
/** Discriminated union for hook response state - enables type narrowing on ok check */
|
|
34
|
-
type HookResponseState<TData, TError> = {
|
|
35
|
-
ok: undefined;
|
|
36
|
-
data: undefined;
|
|
37
|
-
error: undefined;
|
|
38
|
-
} | {
|
|
39
|
-
ok: true;
|
|
40
|
-
data: TData;
|
|
41
|
-
error: undefined;
|
|
42
|
-
} | {
|
|
43
|
-
ok: false;
|
|
44
|
-
data: undefined;
|
|
45
|
-
error: TError;
|
|
46
|
-
};
|
|
47
|
-
/** Result when hook is called with query function (auto-fetch mode) */
|
|
48
|
-
type UseEnlaceQueryResult<TData, TError> = {
|
|
49
|
-
loading: boolean;
|
|
50
|
-
fetching: boolean;
|
|
51
|
-
} & HookResponseState<TData, TError>;
|
|
52
|
-
/** Result when hook is called with method selector (trigger mode) */
|
|
53
|
-
type UseEnlaceSelectorResult<TMethod> = {
|
|
54
|
-
trigger: TMethod;
|
|
55
|
-
loading: boolean;
|
|
56
|
-
fetching: boolean;
|
|
57
|
-
} & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
|
|
58
|
-
|
|
23
|
+
/** Options for createEnlaceHookReact factory */
|
|
59
24
|
type EnlaceHookOptions = {
|
|
60
25
|
/**
|
|
61
26
|
* Auto-generate cache tags from URL path for GET requests.
|
|
@@ -67,31 +32,52 @@ type EnlaceHookOptions = {
|
|
|
67
32
|
autoRevalidateTags?: boolean;
|
|
68
33
|
/** Time in ms before cached data is considered stale. @default 0 (always stale) */
|
|
69
34
|
staleTime?: number;
|
|
35
|
+
/** Callback called on successful API responses */
|
|
36
|
+
onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
|
|
37
|
+
/** Callback called on error responses (HTTP errors or network failures) */
|
|
38
|
+
onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
|
|
70
39
|
};
|
|
71
|
-
|
|
72
|
-
<TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod>): UseEnlaceSelectorResult<TMethod>;
|
|
73
|
-
<TData, TError>(queryFn: QueryFn<TSchema, TData, TError>): UseEnlaceQueryResult<TData, TError>;
|
|
74
|
-
};
|
|
40
|
+
|
|
75
41
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* const useAPI = createEnlaceHook<ApiSchema>('https://api.com');
|
|
81
|
-
*
|
|
82
|
-
* // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
|
|
83
|
-
* const { loading, data, error } = useAPI((api) => api.posts.get({ query: { userId } }));
|
|
84
|
-
*
|
|
85
|
-
* // Selector mode - typed trigger for lazy calls
|
|
86
|
-
* const { trigger, loading, data, error } = useAPI((api) => api.posts.delete);
|
|
87
|
-
* 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
|
|
88
45
|
*/
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
type
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
};
|
|
94
80
|
|
|
95
|
-
declare function
|
|
81
|
+
declare function createEnlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
|
|
96
82
|
|
|
97
|
-
export { type
|
|
83
|
+
export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };
|
package/dist/index.js
CHANGED
|
@@ -21,349 +21,71 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// src/index.ts
|
|
22
22
|
var src_exports = {};
|
|
23
23
|
__export(src_exports, {
|
|
24
|
-
|
|
25
|
-
clearCache: () => clearCache,
|
|
26
|
-
createEnlaceHook: () => createEnlaceHook,
|
|
27
|
-
invalidateTags: () => invalidateTags,
|
|
28
|
-
onRevalidate: () => onRevalidate
|
|
24
|
+
createEnlaceNext: () => createEnlaceNext
|
|
29
25
|
});
|
|
30
26
|
module.exports = __toCommonJS(src_exports);
|
|
31
27
|
__reExport(src_exports, require("enlace-core"), module.exports);
|
|
32
28
|
|
|
33
|
-
// src/
|
|
34
|
-
var
|
|
35
|
-
|
|
36
|
-
// src/react/useQueryMode.ts
|
|
37
|
-
var import_react = require("react");
|
|
29
|
+
// src/next/index.ts
|
|
30
|
+
var import_enlace_core2 = require("enlace-core");
|
|
38
31
|
|
|
39
|
-
// src/
|
|
40
|
-
var
|
|
41
|
-
loading: false,
|
|
42
|
-
fetching: false,
|
|
43
|
-
ok: void 0,
|
|
44
|
-
data: void 0,
|
|
45
|
-
error: void 0
|
|
46
|
-
};
|
|
47
|
-
function hookReducer(state, action) {
|
|
48
|
-
switch (action.type) {
|
|
49
|
-
case "RESET":
|
|
50
|
-
return action.state;
|
|
51
|
-
case "FETCH_START":
|
|
52
|
-
return {
|
|
53
|
-
...state,
|
|
54
|
-
loading: state.data === void 0,
|
|
55
|
-
fetching: true
|
|
56
|
-
};
|
|
57
|
-
case "FETCH_SUCCESS":
|
|
58
|
-
return {
|
|
59
|
-
loading: false,
|
|
60
|
-
fetching: false,
|
|
61
|
-
ok: true,
|
|
62
|
-
data: action.data,
|
|
63
|
-
error: void 0
|
|
64
|
-
};
|
|
65
|
-
case "FETCH_ERROR":
|
|
66
|
-
return {
|
|
67
|
-
loading: false,
|
|
68
|
-
fetching: false,
|
|
69
|
-
ok: false,
|
|
70
|
-
data: void 0,
|
|
71
|
-
error: action.error
|
|
72
|
-
};
|
|
73
|
-
case "SYNC_CACHE":
|
|
74
|
-
return action.state;
|
|
75
|
-
default:
|
|
76
|
-
return state;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
32
|
+
// src/next/fetch.ts
|
|
33
|
+
var import_enlace_core = require("enlace-core");
|
|
79
34
|
|
|
80
35
|
// src/utils/generateTags.ts
|
|
81
36
|
function generateTags(path) {
|
|
82
37
|
return path.map((_, i) => path.slice(0, i + 1).join("/"));
|
|
83
38
|
}
|
|
84
39
|
|
|
85
|
-
// src/
|
|
86
|
-
function
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
path: tracked.path,
|
|
104
|
-
method: tracked.method,
|
|
105
|
-
options: tracked.options
|
|
106
|
-
})
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
function getCache(key) {
|
|
110
|
-
return cache.get(key);
|
|
111
|
-
}
|
|
112
|
-
function setCache(key, entry) {
|
|
113
|
-
const existing = cache.get(key);
|
|
114
|
-
if (existing) {
|
|
115
|
-
if ("ok" in entry) {
|
|
116
|
-
delete existing.promise;
|
|
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
|
+
}
|
|
117
58
|
}
|
|
118
|
-
|
|
119
|
-
existing.subscribers.forEach((cb) => cb());
|
|
120
|
-
} else {
|
|
121
|
-
cache.set(key, {
|
|
122
|
-
data: void 0,
|
|
123
|
-
error: void 0,
|
|
124
|
-
ok: void 0,
|
|
125
|
-
timestamp: 0,
|
|
126
|
-
tags: [],
|
|
127
|
-
subscribers: /* @__PURE__ */ new Set(),
|
|
128
|
-
...entry
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
function subscribeCache(key, callback) {
|
|
133
|
-
let entry = cache.get(key);
|
|
134
|
-
if (!entry) {
|
|
135
|
-
cache.set(key, {
|
|
136
|
-
data: void 0,
|
|
137
|
-
error: void 0,
|
|
138
|
-
ok: void 0,
|
|
139
|
-
timestamp: 0,
|
|
140
|
-
tags: [],
|
|
141
|
-
subscribers: /* @__PURE__ */ new Set()
|
|
142
|
-
});
|
|
143
|
-
entry = cache.get(key);
|
|
144
|
-
}
|
|
145
|
-
entry.subscribers.add(callback);
|
|
146
|
-
return () => {
|
|
147
|
-
entry.subscribers.delete(callback);
|
|
59
|
+
onSuccess?.(payload);
|
|
148
60
|
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
function clearCache(key) {
|
|
156
|
-
if (key) {
|
|
157
|
-
cache.delete(key);
|
|
158
|
-
} else {
|
|
159
|
-
cache.clear();
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
function clearCacheByTags(tags) {
|
|
163
|
-
cache.forEach((entry) => {
|
|
164
|
-
const hasMatch = entry.tags.some((tag) => tags.includes(tag));
|
|
165
|
-
if (hasMatch) {
|
|
166
|
-
entry.data = void 0;
|
|
167
|
-
entry.error = void 0;
|
|
168
|
-
entry.ok = void 0;
|
|
169
|
-
entry.timestamp = 0;
|
|
170
|
-
delete entry.promise;
|
|
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;
|
|
171
67
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
// src/react/revalidator.ts
|
|
176
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
177
|
-
function invalidateTags(tags) {
|
|
178
|
-
clearCacheByTags(tags);
|
|
179
|
-
listeners.forEach((listener) => listener(tags));
|
|
180
|
-
}
|
|
181
|
-
function onRevalidate(callback) {
|
|
182
|
-
listeners.add(callback);
|
|
183
|
-
return () => listeners.delete(callback);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// src/react/useQueryMode.ts
|
|
187
|
-
function useQueryMode(api, trackedCall, options) {
|
|
188
|
-
const { autoGenerateTags, staleTime } = options;
|
|
189
|
-
const queryKey = createQueryKey(trackedCall);
|
|
190
|
-
const requestOptions = trackedCall.options;
|
|
191
|
-
const queryTags = requestOptions?.tags ?? (autoGenerateTags ? generateTags(trackedCall.path) : []);
|
|
192
|
-
const getCacheState = (includeNeedsFetch = false) => {
|
|
193
|
-
const cached = getCache(queryKey);
|
|
194
|
-
const hasCachedData = cached?.data !== void 0;
|
|
195
|
-
const isFetching = !!cached?.promise;
|
|
196
|
-
const needsFetch = includeNeedsFetch && (!hasCachedData || isStale(queryKey, staleTime));
|
|
197
|
-
return {
|
|
198
|
-
loading: !hasCachedData && (isFetching || needsFetch),
|
|
199
|
-
fetching: isFetching || needsFetch,
|
|
200
|
-
ok: cached?.ok,
|
|
201
|
-
data: cached?.data,
|
|
202
|
-
error: cached?.error
|
|
203
|
-
};
|
|
204
|
-
};
|
|
205
|
-
const [state, dispatch] = (0, import_react.useReducer)(hookReducer, null, () => getCacheState(true));
|
|
206
|
-
const mountedRef = (0, import_react.useRef)(true);
|
|
207
|
-
const fetchRef = (0, import_react.useRef)(null);
|
|
208
|
-
(0, import_react.useEffect)(() => {
|
|
209
|
-
mountedRef.current = true;
|
|
210
|
-
dispatch({ type: "RESET", state: getCacheState(true) });
|
|
211
|
-
const unsubscribe = subscribeCache(queryKey, () => {
|
|
212
|
-
if (mountedRef.current) {
|
|
213
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
const doFetch = () => {
|
|
217
|
-
const cached2 = getCache(queryKey);
|
|
218
|
-
if (cached2?.promise) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
dispatch({ type: "FETCH_START" });
|
|
222
|
-
let current = api;
|
|
223
|
-
for (const segment of trackedCall.path) {
|
|
224
|
-
current = current[segment];
|
|
225
|
-
}
|
|
226
|
-
const method = current[trackedCall.method];
|
|
227
|
-
const fetchPromise = method(trackedCall.options).then((res) => {
|
|
228
|
-
if (mountedRef.current) {
|
|
229
|
-
setCache(queryKey, {
|
|
230
|
-
data: res.ok ? res.data : void 0,
|
|
231
|
-
error: res.ok ? void 0 : res.error,
|
|
232
|
-
ok: res.ok,
|
|
233
|
-
timestamp: Date.now(),
|
|
234
|
-
tags: queryTags
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
setCache(queryKey, {
|
|
239
|
-
promise: fetchPromise,
|
|
240
|
-
tags: queryTags
|
|
241
|
-
});
|
|
242
|
-
};
|
|
243
|
-
fetchRef.current = doFetch;
|
|
244
|
-
const cached = getCache(queryKey);
|
|
245
|
-
if (cached?.data !== void 0 && !isStale(queryKey, staleTime)) {
|
|
246
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
247
|
-
} else {
|
|
248
|
-
doFetch();
|
|
68
|
+
if (requestOptions?.revalidate !== void 0) {
|
|
69
|
+
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
249
70
|
}
|
|
250
|
-
|
|
251
|
-
mountedRef.current = false;
|
|
252
|
-
fetchRef.current = null;
|
|
253
|
-
unsubscribe();
|
|
254
|
-
};
|
|
255
|
-
}, [queryKey]);
|
|
256
|
-
(0, import_react.useEffect)(() => {
|
|
257
|
-
if (queryTags.length === 0) return;
|
|
258
|
-
return onRevalidate((invalidatedTags) => {
|
|
259
|
-
const hasMatch = invalidatedTags.some((tag) => queryTags.includes(tag));
|
|
260
|
-
if (hasMatch && mountedRef.current && fetchRef.current) {
|
|
261
|
-
fetchRef.current();
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
}, [JSON.stringify(queryTags)]);
|
|
265
|
-
return state;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// src/react/types.ts
|
|
269
|
-
var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
|
|
270
|
-
|
|
271
|
-
// src/react/trackingProxy.ts
|
|
272
|
-
function createTrackingProxy(onTrack) {
|
|
273
|
-
const createProxy = (path = []) => {
|
|
274
|
-
return new Proxy(() => {
|
|
275
|
-
}, {
|
|
276
|
-
get(_, prop) {
|
|
277
|
-
if (HTTP_METHODS.includes(prop)) {
|
|
278
|
-
const methodFn = (options) => {
|
|
279
|
-
onTrack({
|
|
280
|
-
trackedCall: { path, method: prop, options },
|
|
281
|
-
selectorPath: null,
|
|
282
|
-
selectorMethod: null
|
|
283
|
-
});
|
|
284
|
-
return Promise.resolve({ ok: true, data: void 0 });
|
|
285
|
-
};
|
|
286
|
-
onTrack({
|
|
287
|
-
trackedCall: null,
|
|
288
|
-
selectorPath: path,
|
|
289
|
-
selectorMethod: prop
|
|
290
|
-
});
|
|
291
|
-
return methodFn;
|
|
292
|
-
}
|
|
293
|
-
return createProxy([...path, prop]);
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
return createProxy();
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// src/react/useSelectorMode.ts
|
|
301
|
-
var import_react2 = require("react");
|
|
302
|
-
function useSelectorMode(method, path, autoRevalidateTags) {
|
|
303
|
-
const [state, dispatch] = (0, import_react2.useReducer)(hookReducer, initialState);
|
|
304
|
-
const methodRef = (0, import_react2.useRef)(method);
|
|
305
|
-
const triggerRef = (0, import_react2.useRef)(null);
|
|
306
|
-
const pathRef = (0, import_react2.useRef)(path);
|
|
307
|
-
const autoRevalidateRef = (0, import_react2.useRef)(autoRevalidateTags);
|
|
308
|
-
methodRef.current = method;
|
|
309
|
-
pathRef.current = path;
|
|
310
|
-
autoRevalidateRef.current = autoRevalidateTags;
|
|
311
|
-
if (!triggerRef.current) {
|
|
312
|
-
triggerRef.current = (async (...args) => {
|
|
313
|
-
dispatch({ type: "FETCH_START" });
|
|
314
|
-
const res = await methodRef.current(...args);
|
|
315
|
-
if (res.ok) {
|
|
316
|
-
dispatch({ type: "FETCH_SUCCESS", data: res.data });
|
|
317
|
-
const options = args[0];
|
|
318
|
-
const tagsToInvalidate = options?.revalidateTags ?? (autoRevalidateRef.current ? generateTags(pathRef.current) : []);
|
|
319
|
-
if (tagsToInvalidate.length > 0) {
|
|
320
|
-
invalidateTags(tagsToInvalidate);
|
|
321
|
-
}
|
|
322
|
-
} else {
|
|
323
|
-
dispatch({ type: "FETCH_ERROR", error: res.error });
|
|
324
|
-
}
|
|
325
|
-
return res;
|
|
326
|
-
});
|
|
71
|
+
nextRequestOptions.next = nextFetchOptions;
|
|
327
72
|
}
|
|
328
|
-
return
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
73
|
+
return (0, import_enlace_core.executeFetch)(
|
|
74
|
+
baseUrl,
|
|
75
|
+
path,
|
|
76
|
+
method,
|
|
77
|
+
{ ...coreOptions, onSuccess: nextOnSuccess },
|
|
78
|
+
nextRequestOptions
|
|
79
|
+
);
|
|
332
80
|
}
|
|
333
81
|
|
|
334
|
-
// src/
|
|
335
|
-
function
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
let trackingResult = {
|
|
344
|
-
trackedCall: null,
|
|
345
|
-
selectorPath: null,
|
|
346
|
-
selectorMethod: null
|
|
347
|
-
};
|
|
348
|
-
const trackingProxy = createTrackingProxy((result2) => {
|
|
349
|
-
trackingResult = result2;
|
|
350
|
-
});
|
|
351
|
-
const result = selectorOrQuery(
|
|
352
|
-
trackingProxy
|
|
353
|
-
);
|
|
354
|
-
if (typeof result === "function") {
|
|
355
|
-
const actualResult = selectorOrQuery(api);
|
|
356
|
-
return useSelectorMode(
|
|
357
|
-
actualResult,
|
|
358
|
-
trackingResult.selectorPath ?? [],
|
|
359
|
-
autoRevalidateTags
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
return useQueryMode(
|
|
363
|
-
api,
|
|
364
|
-
trackingResult.trackedCall,
|
|
365
|
-
{ autoGenerateTags, staleTime }
|
|
366
|
-
);
|
|
367
|
-
}
|
|
368
|
-
return useEnlaceHook;
|
|
82
|
+
// src/next/index.ts
|
|
83
|
+
function createEnlaceNext(baseUrl, defaultOptions = {}, nextOptions = {}) {
|
|
84
|
+
const combinedOptions = { ...defaultOptions, ...nextOptions };
|
|
85
|
+
return (0, import_enlace_core2.createProxyHandler)(
|
|
86
|
+
baseUrl,
|
|
87
|
+
combinedOptions,
|
|
88
|
+
[],
|
|
89
|
+
executeNextFetch
|
|
90
|
+
);
|
|
369
91
|
}
|