enlace 0.0.1-beta.4 → 0.0.1-beta.6
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 +111 -17
- package/dist/{next/hook → hook}/index.d.mts +49 -10
- package/dist/{next/hook → hook}/index.d.ts +49 -10
- package/dist/{next/hook → hook}/index.js +158 -138
- package/dist/{next/hook → hook}/index.mjs +159 -158
- package/dist/index.d.mts +46 -77
- package/dist/index.d.ts +46 -77
- package/dist/index.js +48 -385
- package/dist/index.mjs +50 -385
- package/package.json +6 -11
- package/dist/next/index.d.mts +0 -82
- package/dist/next/index.d.ts +0 -82
- package/dist/next/index.js +0 -111
- package/dist/next/index.mjs +0 -95
package/dist/index.mjs
CHANGED
|
@@ -1,408 +1,73 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
export * from "enlace-core";
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/next/index.ts
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
createProxyHandler
|
|
7
7
|
} from "enlace-core";
|
|
8
8
|
|
|
9
|
-
// src/
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var initialState = {
|
|
14
|
-
loading: false,
|
|
15
|
-
fetching: false,
|
|
16
|
-
ok: void 0,
|
|
17
|
-
data: void 0,
|
|
18
|
-
error: void 0
|
|
19
|
-
};
|
|
20
|
-
function hookReducer(state, action) {
|
|
21
|
-
switch (action.type) {
|
|
22
|
-
case "RESET":
|
|
23
|
-
return action.state;
|
|
24
|
-
case "FETCH_START":
|
|
25
|
-
return {
|
|
26
|
-
...state,
|
|
27
|
-
loading: state.data === void 0,
|
|
28
|
-
fetching: true
|
|
29
|
-
};
|
|
30
|
-
case "FETCH_SUCCESS":
|
|
31
|
-
return {
|
|
32
|
-
loading: false,
|
|
33
|
-
fetching: false,
|
|
34
|
-
ok: true,
|
|
35
|
-
data: action.data,
|
|
36
|
-
error: void 0
|
|
37
|
-
};
|
|
38
|
-
case "FETCH_ERROR":
|
|
39
|
-
return {
|
|
40
|
-
loading: false,
|
|
41
|
-
fetching: false,
|
|
42
|
-
ok: false,
|
|
43
|
-
data: void 0,
|
|
44
|
-
error: action.error
|
|
45
|
-
};
|
|
46
|
-
case "SYNC_CACHE":
|
|
47
|
-
return action.state;
|
|
48
|
-
default:
|
|
49
|
-
return state;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
9
|
+
// src/next/fetch.ts
|
|
10
|
+
import {
|
|
11
|
+
executeFetch
|
|
12
|
+
} from "enlace-core";
|
|
52
13
|
|
|
53
14
|
// src/utils/generateTags.ts
|
|
54
15
|
function generateTags(path) {
|
|
55
16
|
return path.map((_, i) => path.slice(0, i + 1).join("/"));
|
|
56
17
|
}
|
|
57
18
|
|
|
58
|
-
// src/
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
sortObjectKeys({
|
|
76
|
-
path: tracked.path,
|
|
77
|
-
method: tracked.method,
|
|
78
|
-
options: tracked.options
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
function getCache(key) {
|
|
83
|
-
return cache.get(key);
|
|
84
|
-
}
|
|
85
|
-
function setCache(key, entry) {
|
|
86
|
-
const existing = cache.get(key);
|
|
87
|
-
if (existing) {
|
|
88
|
-
if ("ok" in entry) {
|
|
89
|
-
delete existing.promise;
|
|
90
|
-
}
|
|
91
|
-
Object.assign(existing, entry);
|
|
92
|
-
existing.subscribers.forEach((cb) => cb());
|
|
93
|
-
} else {
|
|
94
|
-
cache.set(key, {
|
|
95
|
-
data: void 0,
|
|
96
|
-
error: void 0,
|
|
97
|
-
ok: void 0,
|
|
98
|
-
timestamp: 0,
|
|
99
|
-
tags: [],
|
|
100
|
-
subscribers: /* @__PURE__ */ new Set(),
|
|
101
|
-
...entry
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
function subscribeCache(key, callback) {
|
|
106
|
-
let entry = cache.get(key);
|
|
107
|
-
if (!entry) {
|
|
108
|
-
cache.set(key, {
|
|
109
|
-
data: void 0,
|
|
110
|
-
error: void 0,
|
|
111
|
-
ok: void 0,
|
|
112
|
-
timestamp: 0,
|
|
113
|
-
tags: [],
|
|
114
|
-
subscribers: /* @__PURE__ */ new Set()
|
|
115
|
-
});
|
|
116
|
-
entry = cache.get(key);
|
|
117
|
-
}
|
|
118
|
-
entry.subscribers.add(callback);
|
|
119
|
-
return () => {
|
|
120
|
-
entry.subscribers.delete(callback);
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
function isStale(key, staleTime) {
|
|
124
|
-
const entry = cache.get(key);
|
|
125
|
-
if (!entry) return true;
|
|
126
|
-
return Date.now() - entry.timestamp > staleTime;
|
|
127
|
-
}
|
|
128
|
-
function clearCache(key) {
|
|
129
|
-
if (key) {
|
|
130
|
-
cache.delete(key);
|
|
131
|
-
} else {
|
|
132
|
-
cache.clear();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
function clearCacheByTags(tags) {
|
|
136
|
-
cache.forEach((entry) => {
|
|
137
|
-
const hasMatch = entry.tags.some((tag) => tags.includes(tag));
|
|
138
|
-
if (hasMatch) {
|
|
139
|
-
entry.data = void 0;
|
|
140
|
-
entry.error = void 0;
|
|
141
|
-
entry.ok = void 0;
|
|
142
|
-
entry.timestamp = 0;
|
|
143
|
-
delete entry.promise;
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// src/react/revalidator.ts
|
|
149
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
150
|
-
function invalidateTags(tags) {
|
|
151
|
-
clearCacheByTags(tags);
|
|
152
|
-
listeners.forEach((listener) => listener(tags));
|
|
153
|
-
}
|
|
154
|
-
function onRevalidate(callback) {
|
|
155
|
-
listeners.add(callback);
|
|
156
|
-
return () => listeners.delete(callback);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// src/react/useQueryMode.ts
|
|
160
|
-
function resolvePath(path, pathParams) {
|
|
161
|
-
if (!pathParams) return path;
|
|
162
|
-
return path.map((segment) => {
|
|
163
|
-
if (segment.startsWith(":")) {
|
|
164
|
-
const paramName = segment.slice(1);
|
|
165
|
-
const value = pathParams[paramName];
|
|
166
|
-
if (value === void 0) {
|
|
167
|
-
throw new Error(`Missing path parameter: ${paramName}`);
|
|
19
|
+
// src/next/fetch.ts
|
|
20
|
+
async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
|
|
21
|
+
const {
|
|
22
|
+
autoGenerateTags = true,
|
|
23
|
+
autoRevalidateTags = true,
|
|
24
|
+
revalidator,
|
|
25
|
+
onSuccess,
|
|
26
|
+
...coreOptions
|
|
27
|
+
} = combinedOptions;
|
|
28
|
+
const isGet = method === "GET";
|
|
29
|
+
const autoTags = generateTags(path);
|
|
30
|
+
const nextOnSuccess = (payload) => {
|
|
31
|
+
if (!isGet && !requestOptions?.skipRevalidator) {
|
|
32
|
+
const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
|
|
33
|
+
const revalidatePaths = requestOptions?.revalidatePaths ?? [];
|
|
34
|
+
if (revalidateTags.length || revalidatePaths.length) {
|
|
35
|
+
revalidator?.(revalidateTags, revalidatePaths);
|
|
168
36
|
}
|
|
169
|
-
return String(value);
|
|
170
37
|
}
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
function useQueryMode(api, trackedCall, options) {
|
|
175
|
-
const { autoGenerateTags, staleTime, enabled } = options;
|
|
176
|
-
const queryKey = createQueryKey(trackedCall);
|
|
177
|
-
const requestOptions = trackedCall.options;
|
|
178
|
-
const resolvedPath = resolvePath(trackedCall.path, requestOptions?.pathParams);
|
|
179
|
-
const queryTags = requestOptions?.tags ?? (autoGenerateTags ? generateTags(resolvedPath) : []);
|
|
180
|
-
const getCacheState = (includeNeedsFetch = false) => {
|
|
181
|
-
const cached = getCache(queryKey);
|
|
182
|
-
const hasCachedData = cached?.data !== void 0;
|
|
183
|
-
const isFetching = !!cached?.promise;
|
|
184
|
-
const needsFetch = includeNeedsFetch && (!hasCachedData || isStale(queryKey, staleTime));
|
|
185
|
-
return {
|
|
186
|
-
loading: !hasCachedData && (isFetching || needsFetch),
|
|
187
|
-
fetching: isFetching || needsFetch,
|
|
188
|
-
ok: cached?.ok,
|
|
189
|
-
data: cached?.data,
|
|
190
|
-
error: cached?.error
|
|
191
|
-
};
|
|
38
|
+
onSuccess?.(payload);
|
|
192
39
|
};
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
dispatch({
|
|
200
|
-
type: "RESET",
|
|
201
|
-
state: { loading: false, fetching: false, ok: void 0, data: void 0, error: void 0 }
|
|
202
|
-
});
|
|
203
|
-
return () => {
|
|
204
|
-
mountedRef.current = false;
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
dispatch({ type: "RESET", state: getCacheState(true) });
|
|
208
|
-
const unsubscribe = subscribeCache(queryKey, () => {
|
|
209
|
-
if (mountedRef.current) {
|
|
210
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
const doFetch = () => {
|
|
214
|
-
const cached2 = getCache(queryKey);
|
|
215
|
-
if (cached2?.promise) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
dispatch({ type: "FETCH_START" });
|
|
219
|
-
let current = api;
|
|
220
|
-
for (const segment of resolvedPath) {
|
|
221
|
-
current = current[segment];
|
|
222
|
-
}
|
|
223
|
-
const method = current[trackedCall.method];
|
|
224
|
-
const fetchPromise = method(trackedCall.options).then((res) => {
|
|
225
|
-
if (mountedRef.current) {
|
|
226
|
-
setCache(queryKey, {
|
|
227
|
-
data: res.ok ? res.data : void 0,
|
|
228
|
-
error: res.ok ? void 0 : res.error,
|
|
229
|
-
ok: res.ok,
|
|
230
|
-
timestamp: Date.now(),
|
|
231
|
-
tags: queryTags
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
setCache(queryKey, {
|
|
236
|
-
promise: fetchPromise,
|
|
237
|
-
tags: queryTags
|
|
238
|
-
});
|
|
239
|
-
};
|
|
240
|
-
fetchRef.current = doFetch;
|
|
241
|
-
const cached = getCache(queryKey);
|
|
242
|
-
if (cached?.data !== void 0 && !isStale(queryKey, staleTime)) {
|
|
243
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
244
|
-
} else {
|
|
245
|
-
doFetch();
|
|
40
|
+
const nextRequestOptions = { ...requestOptions };
|
|
41
|
+
if (isGet) {
|
|
42
|
+
const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
|
|
43
|
+
const nextFetchOptions = {};
|
|
44
|
+
if (tags) {
|
|
45
|
+
nextFetchOptions.tags = tags;
|
|
246
46
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
fetchRef.current = null;
|
|
250
|
-
unsubscribe();
|
|
251
|
-
};
|
|
252
|
-
}, [queryKey, enabled]);
|
|
253
|
-
useEffect(() => {
|
|
254
|
-
if (queryTags.length === 0) return;
|
|
255
|
-
return onRevalidate((invalidatedTags) => {
|
|
256
|
-
const hasMatch = invalidatedTags.some((tag) => queryTags.includes(tag));
|
|
257
|
-
if (hasMatch && mountedRef.current && fetchRef.current) {
|
|
258
|
-
fetchRef.current();
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
}, [JSON.stringify(queryTags)]);
|
|
262
|
-
return state;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// src/react/types.ts
|
|
266
|
-
var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
|
|
267
|
-
|
|
268
|
-
// src/react/trackingProxy.ts
|
|
269
|
-
function createTrackingProxy(onTrack) {
|
|
270
|
-
const createProxy = (path = []) => {
|
|
271
|
-
return new Proxy(() => {
|
|
272
|
-
}, {
|
|
273
|
-
get(_, prop) {
|
|
274
|
-
if (HTTP_METHODS.includes(prop)) {
|
|
275
|
-
const methodFn = (options) => {
|
|
276
|
-
onTrack({
|
|
277
|
-
trackedCall: { path, method: prop, options },
|
|
278
|
-
selectorPath: null,
|
|
279
|
-
selectorMethod: null
|
|
280
|
-
});
|
|
281
|
-
return Promise.resolve({ ok: true, data: void 0 });
|
|
282
|
-
};
|
|
283
|
-
onTrack({
|
|
284
|
-
trackedCall: null,
|
|
285
|
-
selectorPath: path,
|
|
286
|
-
selectorMethod: prop
|
|
287
|
-
});
|
|
288
|
-
return methodFn;
|
|
289
|
-
}
|
|
290
|
-
return createProxy([...path, prop]);
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
};
|
|
294
|
-
return createProxy();
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// src/react/useSelectorMode.ts
|
|
298
|
-
import { useRef as useRef2, useReducer as useReducer2 } from "react";
|
|
299
|
-
function resolvePath2(path, pathParams) {
|
|
300
|
-
if (!pathParams) return path;
|
|
301
|
-
return path.map((segment) => {
|
|
302
|
-
if (segment.startsWith(":")) {
|
|
303
|
-
const paramName = segment.slice(1);
|
|
304
|
-
const value = pathParams[paramName];
|
|
305
|
-
if (value === void 0) {
|
|
306
|
-
throw new Error(`Missing path parameter: ${paramName}`);
|
|
307
|
-
}
|
|
308
|
-
return String(value);
|
|
47
|
+
if (requestOptions?.revalidate !== void 0) {
|
|
48
|
+
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
309
49
|
}
|
|
310
|
-
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
function hasPathParams(path) {
|
|
314
|
-
return path.some((segment) => segment.startsWith(":"));
|
|
315
|
-
}
|
|
316
|
-
function useSelectorMode(config) {
|
|
317
|
-
const { method, api, path, methodName, autoRevalidateTags } = config;
|
|
318
|
-
const [state, dispatch] = useReducer2(hookReducer, initialState);
|
|
319
|
-
const methodRef = useRef2(method);
|
|
320
|
-
const apiRef = useRef2(api);
|
|
321
|
-
const triggerRef = useRef2(null);
|
|
322
|
-
const pathRef = useRef2(path);
|
|
323
|
-
const methodNameRef = useRef2(methodName);
|
|
324
|
-
const autoRevalidateRef = useRef2(autoRevalidateTags);
|
|
325
|
-
methodRef.current = method;
|
|
326
|
-
apiRef.current = api;
|
|
327
|
-
pathRef.current = path;
|
|
328
|
-
methodNameRef.current = methodName;
|
|
329
|
-
autoRevalidateRef.current = autoRevalidateTags;
|
|
330
|
-
if (!triggerRef.current) {
|
|
331
|
-
triggerRef.current = (async (...args) => {
|
|
332
|
-
dispatch({ type: "FETCH_START" });
|
|
333
|
-
const options = args[0];
|
|
334
|
-
const resolvedPath = resolvePath2(pathRef.current, options?.pathParams);
|
|
335
|
-
let res;
|
|
336
|
-
if (hasPathParams(pathRef.current)) {
|
|
337
|
-
let current = apiRef.current;
|
|
338
|
-
for (const segment of resolvedPath) {
|
|
339
|
-
current = current[segment];
|
|
340
|
-
}
|
|
341
|
-
const resolvedMethod = current[methodNameRef.current];
|
|
342
|
-
res = await resolvedMethod(...args);
|
|
343
|
-
} else {
|
|
344
|
-
res = await methodRef.current(...args);
|
|
345
|
-
}
|
|
346
|
-
if (res.ok) {
|
|
347
|
-
dispatch({ type: "FETCH_SUCCESS", data: res.data });
|
|
348
|
-
const tagsToInvalidate = options?.revalidateTags ?? (autoRevalidateRef.current ? generateTags(resolvedPath) : []);
|
|
349
|
-
if (tagsToInvalidate.length > 0) {
|
|
350
|
-
invalidateTags(tagsToInvalidate);
|
|
351
|
-
}
|
|
352
|
-
} else {
|
|
353
|
-
dispatch({ type: "FETCH_ERROR", error: res.error });
|
|
354
|
-
}
|
|
355
|
-
return res;
|
|
356
|
-
});
|
|
50
|
+
nextRequestOptions.next = nextFetchOptions;
|
|
357
51
|
}
|
|
358
|
-
return
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
52
|
+
return executeFetch(
|
|
53
|
+
baseUrl,
|
|
54
|
+
path,
|
|
55
|
+
method,
|
|
56
|
+
{ ...coreOptions, onSuccess: nextOnSuccess },
|
|
57
|
+
nextRequestOptions
|
|
58
|
+
);
|
|
362
59
|
}
|
|
363
60
|
|
|
364
|
-
// src/
|
|
365
|
-
function
|
|
366
|
-
const
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
let trackingResult = {
|
|
374
|
-
trackedCall: null,
|
|
375
|
-
selectorPath: null,
|
|
376
|
-
selectorMethod: null
|
|
377
|
-
};
|
|
378
|
-
const trackingProxy = createTrackingProxy((result2) => {
|
|
379
|
-
trackingResult = result2;
|
|
380
|
-
});
|
|
381
|
-
const result = selectorOrQuery(
|
|
382
|
-
trackingProxy
|
|
383
|
-
);
|
|
384
|
-
if (typeof result === "function") {
|
|
385
|
-
const actualResult = selectorOrQuery(api);
|
|
386
|
-
return useSelectorMode({
|
|
387
|
-
method: actualResult,
|
|
388
|
-
api,
|
|
389
|
-
path: trackingResult.selectorPath ?? [],
|
|
390
|
-
methodName: trackingResult.selectorMethod ?? "",
|
|
391
|
-
autoRevalidateTags
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
return useQueryMode(
|
|
395
|
-
api,
|
|
396
|
-
trackingResult.trackedCall,
|
|
397
|
-
{ autoGenerateTags, staleTime, enabled: queryOptions?.enabled ?? true }
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
return useEnlaceHook;
|
|
61
|
+
// src/next/index.ts
|
|
62
|
+
function createEnlaceNext(baseUrl, defaultOptions = {}, nextOptions = {}) {
|
|
63
|
+
const combinedOptions = { ...defaultOptions, ...nextOptions };
|
|
64
|
+
return createProxyHandler(
|
|
65
|
+
baseUrl,
|
|
66
|
+
combinedOptions,
|
|
67
|
+
[],
|
|
68
|
+
executeNextFetch
|
|
69
|
+
);
|
|
401
70
|
}
|
|
402
71
|
export {
|
|
403
|
-
|
|
404
|
-
clearCache,
|
|
405
|
-
createEnlaceHook,
|
|
406
|
-
invalidateTags,
|
|
407
|
-
onRevalidate
|
|
72
|
+
createEnlaceNext
|
|
408
73
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "enlace",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -11,19 +11,14 @@
|
|
|
11
11
|
"import": "./dist/index.mjs",
|
|
12
12
|
"require": "./dist/index.js"
|
|
13
13
|
},
|
|
14
|
-
"./
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"import": "./dist/
|
|
17
|
-
"require": "./dist/
|
|
18
|
-
},
|
|
19
|
-
"./next/hook": {
|
|
20
|
-
"types": "./dist/next/hook/index.d.ts",
|
|
21
|
-
"import": "./dist/next/hook/index.mjs",
|
|
22
|
-
"require": "./dist/next/hook/index.js"
|
|
14
|
+
"./hook": {
|
|
15
|
+
"types": "./dist/hook/index.d.ts",
|
|
16
|
+
"import": "./dist/hook/index.mjs",
|
|
17
|
+
"require": "./dist/hook/index.js"
|
|
23
18
|
}
|
|
24
19
|
},
|
|
25
20
|
"dependencies": {
|
|
26
|
-
"enlace-core": "0.0.1-beta.
|
|
21
|
+
"enlace-core": "0.0.1-beta.4"
|
|
27
22
|
},
|
|
28
23
|
"peerDependencies": {
|
|
29
24
|
"react": "^19"
|
package/dist/next/index.d.mts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
|
|
2
|
-
export * from 'enlace-core';
|
|
3
|
-
export { EnlaceOptions } from 'enlace-core';
|
|
4
|
-
|
|
5
|
-
/** Per-request options for React hooks */
|
|
6
|
-
type ReactRequestOptionsBase = {
|
|
7
|
-
/**
|
|
8
|
-
* Cache tags for caching (GET requests only)
|
|
9
|
-
* This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
|
|
10
|
-
* But can be manually specified to override auto-generation.
|
|
11
|
-
* */
|
|
12
|
-
tags?: string[];
|
|
13
|
-
/** Tags to invalidate after mutation (triggers refetch in matching queries) */
|
|
14
|
-
revalidateTags?: string[];
|
|
15
|
-
/**
|
|
16
|
-
* Path parameters for dynamic URL segments.
|
|
17
|
-
* Used to replace :paramName placeholders in the URL path.
|
|
18
|
-
* @example
|
|
19
|
-
* // With path api.products[':id'].delete
|
|
20
|
-
* trigger({ pathParams: { id: '123' } }) // → DELETE /products/123
|
|
21
|
-
*/
|
|
22
|
-
pathParams?: Record<string, string | number>;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
type EnlaceHookOptions = {
|
|
26
|
-
/**
|
|
27
|
-
* Auto-generate cache tags from URL path for GET requests.
|
|
28
|
-
* e.g., `/posts/1` generates tags `['posts', 'posts/1']`
|
|
29
|
-
* @default true
|
|
30
|
-
*/
|
|
31
|
-
autoGenerateTags?: boolean;
|
|
32
|
-
/** Auto-revalidate generated tags after successful mutations. @default true */
|
|
33
|
-
autoRevalidateTags?: boolean;
|
|
34
|
-
/** Time in ms before cached data is considered stale. @default 0 (always stale) */
|
|
35
|
-
staleTime?: number;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Handler function called after successful mutations to trigger server-side revalidation.
|
|
40
|
-
* @param tags - Cache tags to revalidate
|
|
41
|
-
* @param paths - URL paths to revalidate
|
|
42
|
-
*/
|
|
43
|
-
type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
|
|
44
|
-
/** Next.js-specific options (third argument for createEnlace) */
|
|
45
|
-
type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & {
|
|
46
|
-
/**
|
|
47
|
-
* Handler called after successful mutations to trigger server-side revalidation.
|
|
48
|
-
* Receives auto-generated or manually specified tags and paths.
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* createEnlace("http://localhost:3000/api/", {}, {
|
|
52
|
-
* revalidator: (tags, paths) => revalidateServerAction(tags, paths)
|
|
53
|
-
* });
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
revalidator?: RevalidateHandler;
|
|
57
|
-
};
|
|
58
|
-
/** Next.js hook options (third argument for createEnlaceHook) - extends React's EnlaceHookOptions */
|
|
59
|
-
type NextHookOptions = EnlaceHookOptions & Pick<NextOptions, "revalidator">;
|
|
60
|
-
/** Per-request options for Next.js fetch - extends React's base options */
|
|
61
|
-
type NextRequestOptionsBase = ReactRequestOptionsBase & {
|
|
62
|
-
/** Time in seconds to revalidate, or false to disable */
|
|
63
|
-
revalidate?: number | false;
|
|
64
|
-
/**
|
|
65
|
-
* URL paths to revalidate after mutation
|
|
66
|
-
* This doesn't do anything on the client by itself - it's passed to the revalidator handler.
|
|
67
|
-
* You must implement the revalidation logic in the revalidator.
|
|
68
|
-
*/
|
|
69
|
-
revalidatePaths?: string[];
|
|
70
|
-
/**
|
|
71
|
-
* Skip server-side revalidation for this request.
|
|
72
|
-
* Useful when autoRevalidateTags is enabled but you want to opt-out for specific mutations.
|
|
73
|
-
* You can still pass empty [] to revalidateTags to skip triggering revalidation.
|
|
74
|
-
* But this flag can be used if you want to revalidate client-side and skip server-side entirely.
|
|
75
|
-
* Eg. you don't fetch any data on server component and you might want to skip the overhead of revalidation.
|
|
76
|
-
*/
|
|
77
|
-
skipRevalidator?: boolean;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
|
|
81
|
-
|
|
82
|
-
export { type NextHookOptions, type NextOptions, type NextRequestOptionsBase, type RevalidateHandler, createEnlace };
|
package/dist/next/index.d.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
|
|
2
|
-
export * from 'enlace-core';
|
|
3
|
-
export { EnlaceOptions } from 'enlace-core';
|
|
4
|
-
|
|
5
|
-
/** Per-request options for React hooks */
|
|
6
|
-
type ReactRequestOptionsBase = {
|
|
7
|
-
/**
|
|
8
|
-
* Cache tags for caching (GET requests only)
|
|
9
|
-
* This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
|
|
10
|
-
* But can be manually specified to override auto-generation.
|
|
11
|
-
* */
|
|
12
|
-
tags?: string[];
|
|
13
|
-
/** Tags to invalidate after mutation (triggers refetch in matching queries) */
|
|
14
|
-
revalidateTags?: string[];
|
|
15
|
-
/**
|
|
16
|
-
* Path parameters for dynamic URL segments.
|
|
17
|
-
* Used to replace :paramName placeholders in the URL path.
|
|
18
|
-
* @example
|
|
19
|
-
* // With path api.products[':id'].delete
|
|
20
|
-
* trigger({ pathParams: { id: '123' } }) // → DELETE /products/123
|
|
21
|
-
*/
|
|
22
|
-
pathParams?: Record<string, string | number>;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
type EnlaceHookOptions = {
|
|
26
|
-
/**
|
|
27
|
-
* Auto-generate cache tags from URL path for GET requests.
|
|
28
|
-
* e.g., `/posts/1` generates tags `['posts', 'posts/1']`
|
|
29
|
-
* @default true
|
|
30
|
-
*/
|
|
31
|
-
autoGenerateTags?: boolean;
|
|
32
|
-
/** Auto-revalidate generated tags after successful mutations. @default true */
|
|
33
|
-
autoRevalidateTags?: boolean;
|
|
34
|
-
/** Time in ms before cached data is considered stale. @default 0 (always stale) */
|
|
35
|
-
staleTime?: number;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Handler function called after successful mutations to trigger server-side revalidation.
|
|
40
|
-
* @param tags - Cache tags to revalidate
|
|
41
|
-
* @param paths - URL paths to revalidate
|
|
42
|
-
*/
|
|
43
|
-
type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
|
|
44
|
-
/** Next.js-specific options (third argument for createEnlace) */
|
|
45
|
-
type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & {
|
|
46
|
-
/**
|
|
47
|
-
* Handler called after successful mutations to trigger server-side revalidation.
|
|
48
|
-
* Receives auto-generated or manually specified tags and paths.
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* createEnlace("http://localhost:3000/api/", {}, {
|
|
52
|
-
* revalidator: (tags, paths) => revalidateServerAction(tags, paths)
|
|
53
|
-
* });
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
revalidator?: RevalidateHandler;
|
|
57
|
-
};
|
|
58
|
-
/** Next.js hook options (third argument for createEnlaceHook) - extends React's EnlaceHookOptions */
|
|
59
|
-
type NextHookOptions = EnlaceHookOptions & Pick<NextOptions, "revalidator">;
|
|
60
|
-
/** Per-request options for Next.js fetch - extends React's base options */
|
|
61
|
-
type NextRequestOptionsBase = ReactRequestOptionsBase & {
|
|
62
|
-
/** Time in seconds to revalidate, or false to disable */
|
|
63
|
-
revalidate?: number | false;
|
|
64
|
-
/**
|
|
65
|
-
* URL paths to revalidate after mutation
|
|
66
|
-
* This doesn't do anything on the client by itself - it's passed to the revalidator handler.
|
|
67
|
-
* You must implement the revalidation logic in the revalidator.
|
|
68
|
-
*/
|
|
69
|
-
revalidatePaths?: string[];
|
|
70
|
-
/**
|
|
71
|
-
* Skip server-side revalidation for this request.
|
|
72
|
-
* Useful when autoRevalidateTags is enabled but you want to opt-out for specific mutations.
|
|
73
|
-
* You can still pass empty [] to revalidateTags to skip triggering revalidation.
|
|
74
|
-
* But this flag can be used if you want to revalidate client-side and skip server-side entirely.
|
|
75
|
-
* Eg. you don't fetch any data on server component and you might want to skip the overhead of revalidation.
|
|
76
|
-
*/
|
|
77
|
-
skipRevalidator?: boolean;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
|
|
81
|
-
|
|
82
|
-
export { type NextHookOptions, type NextOptions, type NextRequestOptionsBase, type RevalidateHandler, createEnlace };
|