enlace 0.0.1-beta.1 → 0.0.1-beta.11
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 +836 -0
- package/dist/hook/index.d.mts +178 -0
- package/dist/hook/index.d.ts +178 -0
- package/dist/{next/hook → hook}/index.js +278 -167
- package/dist/{next/hook → hook}/index.mjs +279 -185
- package/dist/index.d.mts +59 -68
- package/dist/index.d.ts +59 -68
- package/dist/index.js +53 -327
- package/dist/index.mjs +55 -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
|
@@ -17,107 +17,20 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
21
|
|
|
23
|
-
// src/
|
|
24
|
-
var
|
|
25
|
-
__export(
|
|
26
|
-
|
|
22
|
+
// src/hook/index.ts
|
|
23
|
+
var hook_exports = {};
|
|
24
|
+
__export(hook_exports, {
|
|
25
|
+
HTTP_METHODS: () => HTTP_METHODS,
|
|
26
|
+
createEnlaceHookNext: () => createEnlaceHookNext,
|
|
27
|
+
createEnlaceHookReact: () => createEnlaceHookReact
|
|
27
28
|
});
|
|
28
|
-
module.exports = __toCommonJS(
|
|
29
|
+
module.exports = __toCommonJS(hook_exports);
|
|
29
30
|
|
|
30
|
-
// src/
|
|
31
|
-
var next_exports = {};
|
|
32
|
-
__export(next_exports, {
|
|
33
|
-
createEnlace: () => createEnlace
|
|
34
|
-
});
|
|
35
|
-
var import_enlace_core2 = require("enlace-core");
|
|
36
|
-
|
|
37
|
-
// src/next/fetch.ts
|
|
31
|
+
// src/react/createEnlaceHookReact.ts
|
|
38
32
|
var import_enlace_core = require("enlace-core");
|
|
39
33
|
|
|
40
|
-
// src/utils/generateTags.ts
|
|
41
|
-
function generateTags(path) {
|
|
42
|
-
return path.map((_, i) => path.slice(0, i + 1).join("/"));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// src/next/fetch.ts
|
|
46
|
-
async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
|
|
47
|
-
const {
|
|
48
|
-
autoGenerateTags = true,
|
|
49
|
-
autoRevalidateTags = true,
|
|
50
|
-
revalidator,
|
|
51
|
-
headers: defaultHeaders,
|
|
52
|
-
...restOptions
|
|
53
|
-
} = combinedOptions;
|
|
54
|
-
const url = (0, import_enlace_core.buildUrl)(baseUrl, path, requestOptions?.query);
|
|
55
|
-
let headers = (0, import_enlace_core.mergeHeaders)(defaultHeaders, requestOptions?.headers);
|
|
56
|
-
const isGet = method === "GET";
|
|
57
|
-
const autoTags = generateTags(path);
|
|
58
|
-
const fetchOptions = {
|
|
59
|
-
...restOptions,
|
|
60
|
-
method
|
|
61
|
-
};
|
|
62
|
-
if (requestOptions?.cache) {
|
|
63
|
-
fetchOptions.cache = requestOptions.cache;
|
|
64
|
-
}
|
|
65
|
-
if (isGet) {
|
|
66
|
-
const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
|
|
67
|
-
const nextFetchOptions = {};
|
|
68
|
-
if (tags) {
|
|
69
|
-
nextFetchOptions.tags = tags;
|
|
70
|
-
}
|
|
71
|
-
if (requestOptions?.revalidate !== void 0) {
|
|
72
|
-
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
73
|
-
}
|
|
74
|
-
fetchOptions.next = nextFetchOptions;
|
|
75
|
-
}
|
|
76
|
-
if (headers) {
|
|
77
|
-
fetchOptions.headers = headers;
|
|
78
|
-
}
|
|
79
|
-
if (requestOptions?.body !== void 0) {
|
|
80
|
-
if ((0, import_enlace_core.isJsonBody)(requestOptions.body)) {
|
|
81
|
-
fetchOptions.body = JSON.stringify(requestOptions.body);
|
|
82
|
-
headers = (0, import_enlace_core.mergeHeaders)(headers, { "Content-Type": "application/json" });
|
|
83
|
-
if (headers) {
|
|
84
|
-
fetchOptions.headers = headers;
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
fetchOptions.body = requestOptions.body;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
const response = await fetch(url, fetchOptions);
|
|
91
|
-
const contentType = response.headers.get("content-type");
|
|
92
|
-
const isJson = contentType?.includes("application/json");
|
|
93
|
-
if (response.ok) {
|
|
94
|
-
if (!isGet && !requestOptions?.skipRevalidator) {
|
|
95
|
-
const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
|
|
96
|
-
const revalidatePaths = requestOptions?.revalidatePaths ?? [];
|
|
97
|
-
if (revalidateTags.length || revalidatePaths.length) {
|
|
98
|
-
revalidator?.(revalidateTags, revalidatePaths);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return {
|
|
102
|
-
ok: true,
|
|
103
|
-
status: response.status,
|
|
104
|
-
data: isJson ? await response.json() : response
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
return {
|
|
108
|
-
ok: false,
|
|
109
|
-
status: response.status,
|
|
110
|
-
error: isJson ? await response.json() : response
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// src/next/index.ts
|
|
115
|
-
__reExport(next_exports, require("enlace-core"));
|
|
116
|
-
function createEnlace(baseUrl, defaultOptions = {}, nextOptions = {}) {
|
|
117
|
-
const combinedOptions = { ...defaultOptions, ...nextOptions };
|
|
118
|
-
return (0, import_enlace_core2.createProxyHandler)(baseUrl, combinedOptions, [], executeNextFetch);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
34
|
// src/react/useQueryMode.ts
|
|
122
35
|
var import_react = require("react");
|
|
123
36
|
|
|
@@ -125,25 +38,24 @@ var import_react = require("react");
|
|
|
125
38
|
var initialState = {
|
|
126
39
|
loading: false,
|
|
127
40
|
fetching: false,
|
|
128
|
-
ok: void 0,
|
|
129
41
|
data: void 0,
|
|
130
42
|
error: void 0
|
|
131
43
|
};
|
|
132
44
|
function hookReducer(state, action) {
|
|
133
45
|
switch (action.type) {
|
|
134
46
|
case "RESET":
|
|
135
|
-
return action.state;
|
|
47
|
+
return action.state ?? initialState;
|
|
136
48
|
case "FETCH_START":
|
|
137
49
|
return {
|
|
138
50
|
...state,
|
|
139
51
|
loading: state.data === void 0,
|
|
140
|
-
fetching: true
|
|
52
|
+
fetching: true,
|
|
53
|
+
error: void 0
|
|
141
54
|
};
|
|
142
55
|
case "FETCH_SUCCESS":
|
|
143
56
|
return {
|
|
144
57
|
loading: false,
|
|
145
58
|
fetching: false,
|
|
146
|
-
ok: true,
|
|
147
59
|
data: action.data,
|
|
148
60
|
error: void 0
|
|
149
61
|
};
|
|
@@ -151,7 +63,6 @@ function hookReducer(state, action) {
|
|
|
151
63
|
return {
|
|
152
64
|
loading: false,
|
|
153
65
|
fetching: false,
|
|
154
|
-
ok: false,
|
|
155
66
|
data: void 0,
|
|
156
67
|
error: action.error
|
|
157
68
|
};
|
|
@@ -162,13 +73,27 @@ function hookReducer(state, action) {
|
|
|
162
73
|
}
|
|
163
74
|
}
|
|
164
75
|
|
|
76
|
+
// src/utils/generateTags.ts
|
|
77
|
+
function generateTags(path) {
|
|
78
|
+
return path.map((_, i) => path.slice(0, i + 1).join("/"));
|
|
79
|
+
}
|
|
80
|
+
|
|
165
81
|
// src/utils/sortObjectKeys.ts
|
|
166
|
-
function sortObjectKeys(obj) {
|
|
82
|
+
function sortObjectKeys(obj, seen = /* @__PURE__ */ new WeakSet()) {
|
|
167
83
|
if (obj === null || typeof obj !== "object") return obj;
|
|
168
|
-
if (
|
|
84
|
+
if (seen.has(obj)) {
|
|
85
|
+
return "[Circular]";
|
|
86
|
+
}
|
|
87
|
+
seen.add(obj);
|
|
88
|
+
if (Array.isArray(obj)) {
|
|
89
|
+
return obj.map((item) => sortObjectKeys(item, seen));
|
|
90
|
+
}
|
|
169
91
|
return Object.keys(obj).sort().reduce(
|
|
170
92
|
(sorted, key) => {
|
|
171
|
-
sorted[key] = sortObjectKeys(
|
|
93
|
+
sorted[key] = sortObjectKeys(
|
|
94
|
+
obj[key],
|
|
95
|
+
seen
|
|
96
|
+
);
|
|
172
97
|
return sorted;
|
|
173
98
|
},
|
|
174
99
|
{}
|
|
@@ -192,7 +117,7 @@ function getCache(key) {
|
|
|
192
117
|
function setCache(key, entry) {
|
|
193
118
|
const existing = cache.get(key);
|
|
194
119
|
if (existing) {
|
|
195
|
-
if ("
|
|
120
|
+
if ("data" in entry || "error" in entry) {
|
|
196
121
|
delete existing.promise;
|
|
197
122
|
}
|
|
198
123
|
Object.assign(existing, entry);
|
|
@@ -201,7 +126,6 @@ function setCache(key, entry) {
|
|
|
201
126
|
cache.set(key, {
|
|
202
127
|
data: void 0,
|
|
203
128
|
error: void 0,
|
|
204
|
-
ok: void 0,
|
|
205
129
|
timestamp: 0,
|
|
206
130
|
tags: [],
|
|
207
131
|
subscribers: /* @__PURE__ */ new Set(),
|
|
@@ -215,7 +139,6 @@ function subscribeCache(key, callback) {
|
|
|
215
139
|
cache.set(key, {
|
|
216
140
|
data: void 0,
|
|
217
141
|
error: void 0,
|
|
218
|
-
ok: void 0,
|
|
219
142
|
timestamp: 0,
|
|
220
143
|
tags: [],
|
|
221
144
|
subscribers: /* @__PURE__ */ new Set()
|
|
@@ -236,11 +159,9 @@ function clearCacheByTags(tags) {
|
|
|
236
159
|
cache.forEach((entry) => {
|
|
237
160
|
const hasMatch = entry.tags.some((tag) => tags.includes(tag));
|
|
238
161
|
if (hasMatch) {
|
|
239
|
-
entry.data = void 0;
|
|
240
|
-
entry.error = void 0;
|
|
241
|
-
entry.ok = void 0;
|
|
242
162
|
entry.timestamp = 0;
|
|
243
163
|
delete entry.promise;
|
|
164
|
+
entry.subscribers.forEach((cb) => cb());
|
|
244
165
|
}
|
|
245
166
|
});
|
|
246
167
|
}
|
|
@@ -257,35 +178,58 @@ function onRevalidate(callback) {
|
|
|
257
178
|
}
|
|
258
179
|
|
|
259
180
|
// src/react/useQueryMode.ts
|
|
181
|
+
function resolvePath(path, pathParams) {
|
|
182
|
+
if (!pathParams) return path;
|
|
183
|
+
return path.map((segment) => {
|
|
184
|
+
if (segment.startsWith(":")) {
|
|
185
|
+
const paramName = segment.slice(1);
|
|
186
|
+
const value = pathParams[paramName];
|
|
187
|
+
if (value === void 0) {
|
|
188
|
+
throw new Error(`Missing path parameter: ${paramName}`);
|
|
189
|
+
}
|
|
190
|
+
return String(value);
|
|
191
|
+
}
|
|
192
|
+
return segment;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
260
195
|
function useQueryMode(api, trackedCall, options) {
|
|
261
|
-
const { autoGenerateTags, staleTime } = options;
|
|
196
|
+
const { autoGenerateTags, staleTime, enabled } = options;
|
|
262
197
|
const queryKey = createQueryKey(trackedCall);
|
|
263
198
|
const requestOptions = trackedCall.options;
|
|
264
|
-
const
|
|
199
|
+
const resolvedPath = resolvePath(
|
|
200
|
+
trackedCall.path,
|
|
201
|
+
requestOptions?.pathParams
|
|
202
|
+
);
|
|
203
|
+
const queryTags = requestOptions?.tags ?? (autoGenerateTags ? generateTags(resolvedPath) : []);
|
|
265
204
|
const getCacheState = (includeNeedsFetch = false) => {
|
|
266
205
|
const cached = getCache(queryKey);
|
|
267
206
|
const hasCachedData = cached?.data !== void 0;
|
|
268
207
|
const isFetching = !!cached?.promise;
|
|
269
|
-
const
|
|
208
|
+
const stale = isStale(queryKey, staleTime);
|
|
209
|
+
const needsFetch = includeNeedsFetch && (!hasCachedData || stale);
|
|
270
210
|
return {
|
|
271
211
|
loading: !hasCachedData && (isFetching || needsFetch),
|
|
272
212
|
fetching: isFetching || needsFetch,
|
|
273
|
-
ok: cached?.ok,
|
|
274
213
|
data: cached?.data,
|
|
275
214
|
error: cached?.error
|
|
276
215
|
};
|
|
277
216
|
};
|
|
278
|
-
const [state, dispatch] = (0, import_react.useReducer)(
|
|
217
|
+
const [state, dispatch] = (0, import_react.useReducer)(
|
|
218
|
+
hookReducer,
|
|
219
|
+
null,
|
|
220
|
+
() => getCacheState(true)
|
|
221
|
+
);
|
|
279
222
|
const mountedRef = (0, import_react.useRef)(true);
|
|
280
223
|
const fetchRef = (0, import_react.useRef)(null);
|
|
281
224
|
(0, import_react.useEffect)(() => {
|
|
282
225
|
mountedRef.current = true;
|
|
226
|
+
if (!enabled) {
|
|
227
|
+
dispatch({ type: "RESET" });
|
|
228
|
+
return () => {
|
|
229
|
+
mountedRef.current = false;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
283
232
|
dispatch({ type: "RESET", state: getCacheState(true) });
|
|
284
|
-
const unsubscribe = subscribeCache(queryKey, () => {
|
|
285
|
-
if (mountedRef.current) {
|
|
286
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
233
|
const doFetch = () => {
|
|
290
234
|
const cached2 = getCache(queryKey);
|
|
291
235
|
if (cached2?.promise) {
|
|
@@ -293,16 +237,24 @@ function useQueryMode(api, trackedCall, options) {
|
|
|
293
237
|
}
|
|
294
238
|
dispatch({ type: "FETCH_START" });
|
|
295
239
|
let current = api;
|
|
296
|
-
for (const segment of
|
|
240
|
+
for (const segment of resolvedPath) {
|
|
297
241
|
current = current[segment];
|
|
298
242
|
}
|
|
299
243
|
const method = current[trackedCall.method];
|
|
300
244
|
const fetchPromise = method(trackedCall.options).then((res) => {
|
|
301
245
|
if (mountedRef.current) {
|
|
302
246
|
setCache(queryKey, {
|
|
303
|
-
data: res.
|
|
304
|
-
error: res.
|
|
305
|
-
|
|
247
|
+
data: res.error ? void 0 : res.data,
|
|
248
|
+
error: res.error,
|
|
249
|
+
timestamp: Date.now(),
|
|
250
|
+
tags: queryTags
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}).catch((err) => {
|
|
254
|
+
if (mountedRef.current) {
|
|
255
|
+
setCache(queryKey, {
|
|
256
|
+
data: void 0,
|
|
257
|
+
error: err,
|
|
306
258
|
timestamp: Date.now(),
|
|
307
259
|
tags: queryTags
|
|
308
260
|
});
|
|
@@ -320,12 +272,17 @@ function useQueryMode(api, trackedCall, options) {
|
|
|
320
272
|
} else {
|
|
321
273
|
doFetch();
|
|
322
274
|
}
|
|
275
|
+
const unsubscribe = subscribeCache(queryKey, () => {
|
|
276
|
+
if (mountedRef.current) {
|
|
277
|
+
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
278
|
+
}
|
|
279
|
+
});
|
|
323
280
|
return () => {
|
|
324
281
|
mountedRef.current = false;
|
|
325
282
|
fetchRef.current = null;
|
|
326
283
|
unsubscribe();
|
|
327
284
|
};
|
|
328
|
-
}, [queryKey]);
|
|
285
|
+
}, [queryKey, enabled]);
|
|
329
286
|
(0, import_react.useEffect)(() => {
|
|
330
287
|
if (queryTags.length === 0) return;
|
|
331
288
|
return onRevalidate((invalidatedTags) => {
|
|
@@ -338,25 +295,90 @@ function useQueryMode(api, trackedCall, options) {
|
|
|
338
295
|
return state;
|
|
339
296
|
}
|
|
340
297
|
|
|
298
|
+
// src/react/types.ts
|
|
299
|
+
var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
|
|
300
|
+
|
|
301
|
+
// src/react/trackingProxy.ts
|
|
302
|
+
function createTrackingProxy(onTrack) {
|
|
303
|
+
const createProxy = (path = []) => {
|
|
304
|
+
return new Proxy(() => {
|
|
305
|
+
}, {
|
|
306
|
+
get(_, prop) {
|
|
307
|
+
if (HTTP_METHODS.includes(prop)) {
|
|
308
|
+
const methodFn = (options) => {
|
|
309
|
+
onTrack({
|
|
310
|
+
trackedCall: { path, method: prop, options },
|
|
311
|
+
selectorPath: null,
|
|
312
|
+
selectorMethod: null
|
|
313
|
+
});
|
|
314
|
+
return Promise.resolve({ status: 200, data: void 0, error: void 0 });
|
|
315
|
+
};
|
|
316
|
+
onTrack({
|
|
317
|
+
trackedCall: null,
|
|
318
|
+
selectorPath: path,
|
|
319
|
+
selectorMethod: prop
|
|
320
|
+
});
|
|
321
|
+
return methodFn;
|
|
322
|
+
}
|
|
323
|
+
return createProxy([...path, prop]);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
return createProxy();
|
|
328
|
+
}
|
|
329
|
+
|
|
341
330
|
// src/react/useSelectorMode.ts
|
|
342
331
|
var import_react2 = require("react");
|
|
343
|
-
function
|
|
332
|
+
function resolvePath2(path, pathParams) {
|
|
333
|
+
if (!pathParams) return path;
|
|
334
|
+
return path.map((segment) => {
|
|
335
|
+
if (segment.startsWith(":")) {
|
|
336
|
+
const paramName = segment.slice(1);
|
|
337
|
+
const value = pathParams[paramName];
|
|
338
|
+
if (value === void 0) {
|
|
339
|
+
throw new Error(`Missing path parameter: ${paramName}`);
|
|
340
|
+
}
|
|
341
|
+
return String(value);
|
|
342
|
+
}
|
|
343
|
+
return segment;
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function hasPathParams(path) {
|
|
347
|
+
return path.some((segment) => segment.startsWith(":"));
|
|
348
|
+
}
|
|
349
|
+
function useSelectorMode(config) {
|
|
350
|
+
const { method, api, path, methodName, autoRevalidateTags } = config;
|
|
344
351
|
const [state, dispatch] = (0, import_react2.useReducer)(hookReducer, initialState);
|
|
345
352
|
const methodRef = (0, import_react2.useRef)(method);
|
|
353
|
+
const apiRef = (0, import_react2.useRef)(api);
|
|
346
354
|
const triggerRef = (0, import_react2.useRef)(null);
|
|
347
355
|
const pathRef = (0, import_react2.useRef)(path);
|
|
356
|
+
const methodNameRef = (0, import_react2.useRef)(methodName);
|
|
348
357
|
const autoRevalidateRef = (0, import_react2.useRef)(autoRevalidateTags);
|
|
349
358
|
methodRef.current = method;
|
|
359
|
+
apiRef.current = api;
|
|
350
360
|
pathRef.current = path;
|
|
361
|
+
methodNameRef.current = methodName;
|
|
351
362
|
autoRevalidateRef.current = autoRevalidateTags;
|
|
352
363
|
if (!triggerRef.current) {
|
|
353
364
|
triggerRef.current = (async (...args) => {
|
|
354
365
|
dispatch({ type: "FETCH_START" });
|
|
355
|
-
const
|
|
356
|
-
|
|
366
|
+
const options = args[0];
|
|
367
|
+
const resolvedPath = resolvePath2(pathRef.current, options?.pathParams);
|
|
368
|
+
let res;
|
|
369
|
+
if (hasPathParams(pathRef.current)) {
|
|
370
|
+
let current = apiRef.current;
|
|
371
|
+
for (const segment of resolvedPath) {
|
|
372
|
+
current = current[segment];
|
|
373
|
+
}
|
|
374
|
+
const resolvedMethod = current[methodNameRef.current];
|
|
375
|
+
res = await resolvedMethod(...args);
|
|
376
|
+
} else {
|
|
377
|
+
res = await methodRef.current(...args);
|
|
378
|
+
}
|
|
379
|
+
if (!res.error) {
|
|
357
380
|
dispatch({ type: "FETCH_SUCCESS", data: res.data });
|
|
358
|
-
const
|
|
359
|
-
const tagsToInvalidate = options?.revalidateTags ?? (autoRevalidateRef.current ? generateTags(pathRef.current) : []);
|
|
381
|
+
const tagsToInvalidate = options?.revalidateTags ?? (autoRevalidateRef.current ? generateTags(resolvedPath) : []);
|
|
360
382
|
if (tagsToInvalidate.length > 0) {
|
|
361
383
|
invalidateTags(tagsToInvalidate);
|
|
362
384
|
}
|
|
@@ -372,71 +394,160 @@ function useSelectorMode(method, path, autoRevalidateTags) {
|
|
|
372
394
|
};
|
|
373
395
|
}
|
|
374
396
|
|
|
375
|
-
// src/react/
|
|
376
|
-
|
|
397
|
+
// src/react/createEnlaceHookReact.ts
|
|
398
|
+
function createEnlaceHookReact(baseUrl, defaultOptions = {}, hookOptions = {}) {
|
|
399
|
+
const {
|
|
400
|
+
autoGenerateTags = true,
|
|
401
|
+
autoRevalidateTags = true,
|
|
402
|
+
staleTime = 0,
|
|
403
|
+
onSuccess,
|
|
404
|
+
onError
|
|
405
|
+
} = hookOptions;
|
|
406
|
+
const api = (0, import_enlace_core.createEnlace)(baseUrl, defaultOptions, {
|
|
407
|
+
onSuccess,
|
|
408
|
+
onError
|
|
409
|
+
});
|
|
410
|
+
function useEnlaceHook(selectorOrQuery, queryOptions) {
|
|
411
|
+
let trackingResult = {
|
|
412
|
+
trackedCall: null,
|
|
413
|
+
selectorPath: null,
|
|
414
|
+
selectorMethod: null
|
|
415
|
+
};
|
|
416
|
+
const trackingProxy = createTrackingProxy((result2) => {
|
|
417
|
+
trackingResult = result2;
|
|
418
|
+
});
|
|
419
|
+
const result = selectorOrQuery(trackingProxy);
|
|
420
|
+
if (typeof result === "function") {
|
|
421
|
+
const actualResult = selectorOrQuery(api);
|
|
422
|
+
return useSelectorMode({
|
|
423
|
+
method: actualResult,
|
|
424
|
+
api,
|
|
425
|
+
path: trackingResult.selectorPath ?? [],
|
|
426
|
+
methodName: trackingResult.selectorMethod ?? "",
|
|
427
|
+
autoRevalidateTags
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
if (!trackingResult.trackedCall) {
|
|
431
|
+
throw new Error(
|
|
432
|
+
"useAPI query mode requires calling an HTTP method (get, post, etc.). Did you mean to use selector mode? Example: useAPI((api) => api.posts.get())"
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
return useQueryMode(
|
|
436
|
+
api,
|
|
437
|
+
trackingResult.trackedCall,
|
|
438
|
+
{ autoGenerateTags, staleTime, enabled: queryOptions?.enabled ?? true }
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
return useEnlaceHook;
|
|
442
|
+
}
|
|
377
443
|
|
|
378
|
-
// src/
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
444
|
+
// src/next/index.ts
|
|
445
|
+
var import_enlace_core3 = require("enlace-core");
|
|
446
|
+
|
|
447
|
+
// src/next/fetch.ts
|
|
448
|
+
var import_enlace_core2 = require("enlace-core");
|
|
449
|
+
async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
|
|
450
|
+
const {
|
|
451
|
+
autoGenerateTags = true,
|
|
452
|
+
autoRevalidateTags = true,
|
|
453
|
+
skipServerRevalidation = false,
|
|
454
|
+
serverRevalidator,
|
|
455
|
+
onSuccess,
|
|
456
|
+
...coreOptions
|
|
457
|
+
} = combinedOptions;
|
|
458
|
+
const isGet = method === "GET";
|
|
459
|
+
const autoTags = generateTags(path);
|
|
460
|
+
const nextOnSuccess = (payload) => {
|
|
461
|
+
if (!isGet) {
|
|
462
|
+
const shouldRevalidateServer = requestOptions?.serverRevalidate ?? !skipServerRevalidation;
|
|
463
|
+
if (shouldRevalidateServer) {
|
|
464
|
+
const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
|
|
465
|
+
const revalidatePaths = requestOptions?.revalidatePaths ?? [];
|
|
466
|
+
if (revalidateTags.length || revalidatePaths.length) {
|
|
467
|
+
serverRevalidator?.(revalidateTags, revalidatePaths);
|
|
399
468
|
}
|
|
400
|
-
return createProxy([...path, prop]);
|
|
401
469
|
}
|
|
402
|
-
}
|
|
470
|
+
}
|
|
471
|
+
onSuccess?.(payload);
|
|
403
472
|
};
|
|
404
|
-
|
|
473
|
+
const nextRequestOptions = { ...requestOptions };
|
|
474
|
+
if (isGet) {
|
|
475
|
+
const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
|
|
476
|
+
const nextFetchOptions = {};
|
|
477
|
+
if (tags) {
|
|
478
|
+
nextFetchOptions.tags = tags;
|
|
479
|
+
}
|
|
480
|
+
if (requestOptions?.revalidate !== void 0) {
|
|
481
|
+
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
482
|
+
}
|
|
483
|
+
nextRequestOptions.next = nextFetchOptions;
|
|
484
|
+
}
|
|
485
|
+
return (0, import_enlace_core2.executeFetch)(
|
|
486
|
+
baseUrl,
|
|
487
|
+
path,
|
|
488
|
+
method,
|
|
489
|
+
{ ...coreOptions, onSuccess: nextOnSuccess },
|
|
490
|
+
nextRequestOptions
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// src/next/index.ts
|
|
495
|
+
function createEnlaceNext(baseUrl, defaultOptions = {}, nextOptions = {}) {
|
|
496
|
+
const combinedOptions = { ...defaultOptions, ...nextOptions };
|
|
497
|
+
return (0, import_enlace_core3.createProxyHandler)(
|
|
498
|
+
baseUrl,
|
|
499
|
+
combinedOptions,
|
|
500
|
+
[],
|
|
501
|
+
executeNextFetch
|
|
502
|
+
);
|
|
405
503
|
}
|
|
406
504
|
|
|
407
|
-
// src/next/
|
|
408
|
-
function
|
|
505
|
+
// src/next/createEnlaceHookNext.ts
|
|
506
|
+
function createEnlaceHookNext(baseUrl, defaultOptions = {}, hookOptions = {}) {
|
|
409
507
|
const {
|
|
410
508
|
autoGenerateTags = true,
|
|
411
509
|
autoRevalidateTags = true,
|
|
412
510
|
staleTime = 0,
|
|
413
511
|
...nextOptions
|
|
414
512
|
} = hookOptions;
|
|
415
|
-
const api =
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
513
|
+
const api = createEnlaceNext(
|
|
514
|
+
baseUrl,
|
|
515
|
+
defaultOptions,
|
|
516
|
+
{
|
|
517
|
+
autoGenerateTags,
|
|
518
|
+
autoRevalidateTags,
|
|
519
|
+
...nextOptions
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
function useEnlaceHook(selectorOrQuery, queryOptions) {
|
|
421
523
|
let trackedCall = null;
|
|
422
524
|
let selectorPath = null;
|
|
525
|
+
let selectorMethod = null;
|
|
423
526
|
const trackingProxy = createTrackingProxy((result2) => {
|
|
424
527
|
trackedCall = result2.trackedCall;
|
|
425
528
|
selectorPath = result2.selectorPath;
|
|
529
|
+
selectorMethod = result2.selectorMethod;
|
|
426
530
|
});
|
|
427
531
|
const result = selectorOrQuery(trackingProxy);
|
|
428
532
|
if (typeof result === "function") {
|
|
429
533
|
const actualResult = selectorOrQuery(api);
|
|
430
|
-
return useSelectorMode(
|
|
431
|
-
actualResult,
|
|
432
|
-
|
|
534
|
+
return useSelectorMode({
|
|
535
|
+
method: actualResult,
|
|
536
|
+
api,
|
|
537
|
+
path: selectorPath ?? [],
|
|
538
|
+
methodName: selectorMethod ?? "",
|
|
433
539
|
autoRevalidateTags
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
if (!trackedCall) {
|
|
543
|
+
throw new Error(
|
|
544
|
+
"useAPI query mode requires calling an HTTP method (get, post, etc.). Did you mean to use selector mode? Example: useAPI((api) => api.posts.get())"
|
|
434
545
|
);
|
|
435
546
|
}
|
|
436
547
|
return useQueryMode(
|
|
437
548
|
api,
|
|
438
549
|
trackedCall,
|
|
439
|
-
{ autoGenerateTags, staleTime }
|
|
550
|
+
{ autoGenerateTags, staleTime, enabled: queryOptions?.enabled ?? true }
|
|
440
551
|
);
|
|
441
552
|
}
|
|
442
553
|
return useEnlaceHook;
|