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