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.js
CHANGED
|
@@ -21,408 +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
|
-
sortObjectKeys({
|
|
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;
|
|
117
|
-
}
|
|
118
|
-
Object.assign(existing, entry);
|
|
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);
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
function isStale(key, staleTime) {
|
|
151
|
-
const entry = cache.get(key);
|
|
152
|
-
if (!entry) return true;
|
|
153
|
-
return Date.now() - entry.timestamp > staleTime;
|
|
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;
|
|
171
|
-
}
|
|
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 resolvePath(path, pathParams) {
|
|
188
|
-
if (!pathParams) return path;
|
|
189
|
-
return path.map((segment) => {
|
|
190
|
-
if (segment.startsWith(":")) {
|
|
191
|
-
const paramName = segment.slice(1);
|
|
192
|
-
const value = pathParams[paramName];
|
|
193
|
-
if (value === void 0) {
|
|
194
|
-
throw new Error(`Missing path parameter: ${paramName}`);
|
|
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);
|
|
195
57
|
}
|
|
196
|
-
return String(value);
|
|
197
58
|
}
|
|
198
|
-
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
function useQueryMode(api, trackedCall, options) {
|
|
202
|
-
const { autoGenerateTags, staleTime, enabled } = options;
|
|
203
|
-
const queryKey = createQueryKey(trackedCall);
|
|
204
|
-
const requestOptions = trackedCall.options;
|
|
205
|
-
const resolvedPath = resolvePath(trackedCall.path, requestOptions?.pathParams);
|
|
206
|
-
const queryTags = requestOptions?.tags ?? (autoGenerateTags ? generateTags(resolvedPath) : []);
|
|
207
|
-
const getCacheState = (includeNeedsFetch = false) => {
|
|
208
|
-
const cached = getCache(queryKey);
|
|
209
|
-
const hasCachedData = cached?.data !== void 0;
|
|
210
|
-
const isFetching = !!cached?.promise;
|
|
211
|
-
const needsFetch = includeNeedsFetch && (!hasCachedData || isStale(queryKey, staleTime));
|
|
212
|
-
return {
|
|
213
|
-
loading: !hasCachedData && (isFetching || needsFetch),
|
|
214
|
-
fetching: isFetching || needsFetch,
|
|
215
|
-
ok: cached?.ok,
|
|
216
|
-
data: cached?.data,
|
|
217
|
-
error: cached?.error
|
|
218
|
-
};
|
|
59
|
+
onSuccess?.(payload);
|
|
219
60
|
};
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
dispatch({
|
|
227
|
-
type: "RESET",
|
|
228
|
-
state: { loading: false, fetching: false, ok: void 0, data: void 0, error: void 0 }
|
|
229
|
-
});
|
|
230
|
-
return () => {
|
|
231
|
-
mountedRef.current = false;
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
dispatch({ type: "RESET", state: getCacheState(true) });
|
|
235
|
-
const unsubscribe = subscribeCache(queryKey, () => {
|
|
236
|
-
if (mountedRef.current) {
|
|
237
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
const doFetch = () => {
|
|
241
|
-
const cached2 = getCache(queryKey);
|
|
242
|
-
if (cached2?.promise) {
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
dispatch({ type: "FETCH_START" });
|
|
246
|
-
let current = api;
|
|
247
|
-
for (const segment of resolvedPath) {
|
|
248
|
-
current = current[segment];
|
|
249
|
-
}
|
|
250
|
-
const method = current[trackedCall.method];
|
|
251
|
-
const fetchPromise = method(trackedCall.options).then((res) => {
|
|
252
|
-
if (mountedRef.current) {
|
|
253
|
-
setCache(queryKey, {
|
|
254
|
-
data: res.ok ? res.data : void 0,
|
|
255
|
-
error: res.ok ? void 0 : res.error,
|
|
256
|
-
ok: res.ok,
|
|
257
|
-
timestamp: Date.now(),
|
|
258
|
-
tags: queryTags
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
setCache(queryKey, {
|
|
263
|
-
promise: fetchPromise,
|
|
264
|
-
tags: queryTags
|
|
265
|
-
});
|
|
266
|
-
};
|
|
267
|
-
fetchRef.current = doFetch;
|
|
268
|
-
const cached = getCache(queryKey);
|
|
269
|
-
if (cached?.data !== void 0 && !isStale(queryKey, staleTime)) {
|
|
270
|
-
dispatch({ type: "SYNC_CACHE", state: getCacheState() });
|
|
271
|
-
} else {
|
|
272
|
-
doFetch();
|
|
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;
|
|
273
67
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
fetchRef.current = null;
|
|
277
|
-
unsubscribe();
|
|
278
|
-
};
|
|
279
|
-
}, [queryKey, enabled]);
|
|
280
|
-
(0, import_react.useEffect)(() => {
|
|
281
|
-
if (queryTags.length === 0) return;
|
|
282
|
-
return onRevalidate((invalidatedTags) => {
|
|
283
|
-
const hasMatch = invalidatedTags.some((tag) => queryTags.includes(tag));
|
|
284
|
-
if (hasMatch && mountedRef.current && fetchRef.current) {
|
|
285
|
-
fetchRef.current();
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
}, [JSON.stringify(queryTags)]);
|
|
289
|
-
return state;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
// src/react/types.ts
|
|
293
|
-
var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
|
|
294
|
-
|
|
295
|
-
// src/react/trackingProxy.ts
|
|
296
|
-
function createTrackingProxy(onTrack) {
|
|
297
|
-
const createProxy = (path = []) => {
|
|
298
|
-
return new Proxy(() => {
|
|
299
|
-
}, {
|
|
300
|
-
get(_, prop) {
|
|
301
|
-
if (HTTP_METHODS.includes(prop)) {
|
|
302
|
-
const methodFn = (options) => {
|
|
303
|
-
onTrack({
|
|
304
|
-
trackedCall: { path, method: prop, options },
|
|
305
|
-
selectorPath: null,
|
|
306
|
-
selectorMethod: null
|
|
307
|
-
});
|
|
308
|
-
return Promise.resolve({ ok: true, data: void 0 });
|
|
309
|
-
};
|
|
310
|
-
onTrack({
|
|
311
|
-
trackedCall: null,
|
|
312
|
-
selectorPath: path,
|
|
313
|
-
selectorMethod: prop
|
|
314
|
-
});
|
|
315
|
-
return methodFn;
|
|
316
|
-
}
|
|
317
|
-
return createProxy([...path, prop]);
|
|
318
|
-
}
|
|
319
|
-
});
|
|
320
|
-
};
|
|
321
|
-
return createProxy();
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// src/react/useSelectorMode.ts
|
|
325
|
-
var import_react2 = require("react");
|
|
326
|
-
function resolvePath2(path, pathParams) {
|
|
327
|
-
if (!pathParams) return path;
|
|
328
|
-
return path.map((segment) => {
|
|
329
|
-
if (segment.startsWith(":")) {
|
|
330
|
-
const paramName = segment.slice(1);
|
|
331
|
-
const value = pathParams[paramName];
|
|
332
|
-
if (value === void 0) {
|
|
333
|
-
throw new Error(`Missing path parameter: ${paramName}`);
|
|
334
|
-
}
|
|
335
|
-
return String(value);
|
|
68
|
+
if (requestOptions?.revalidate !== void 0) {
|
|
69
|
+
nextFetchOptions.revalidate = requestOptions.revalidate;
|
|
336
70
|
}
|
|
337
|
-
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
function hasPathParams(path) {
|
|
341
|
-
return path.some((segment) => segment.startsWith(":"));
|
|
342
|
-
}
|
|
343
|
-
function useSelectorMode(config) {
|
|
344
|
-
const { method, api, path, methodName, autoRevalidateTags } = config;
|
|
345
|
-
const [state, dispatch] = (0, import_react2.useReducer)(hookReducer, initialState);
|
|
346
|
-
const methodRef = (0, import_react2.useRef)(method);
|
|
347
|
-
const apiRef = (0, import_react2.useRef)(api);
|
|
348
|
-
const triggerRef = (0, import_react2.useRef)(null);
|
|
349
|
-
const pathRef = (0, import_react2.useRef)(path);
|
|
350
|
-
const methodNameRef = (0, import_react2.useRef)(methodName);
|
|
351
|
-
const autoRevalidateRef = (0, import_react2.useRef)(autoRevalidateTags);
|
|
352
|
-
methodRef.current = method;
|
|
353
|
-
apiRef.current = api;
|
|
354
|
-
pathRef.current = path;
|
|
355
|
-
methodNameRef.current = methodName;
|
|
356
|
-
autoRevalidateRef.current = autoRevalidateTags;
|
|
357
|
-
if (!triggerRef.current) {
|
|
358
|
-
triggerRef.current = (async (...args) => {
|
|
359
|
-
dispatch({ type: "FETCH_START" });
|
|
360
|
-
const options = args[0];
|
|
361
|
-
const resolvedPath = resolvePath2(pathRef.current, options?.pathParams);
|
|
362
|
-
let res;
|
|
363
|
-
if (hasPathParams(pathRef.current)) {
|
|
364
|
-
let current = apiRef.current;
|
|
365
|
-
for (const segment of resolvedPath) {
|
|
366
|
-
current = current[segment];
|
|
367
|
-
}
|
|
368
|
-
const resolvedMethod = current[methodNameRef.current];
|
|
369
|
-
res = await resolvedMethod(...args);
|
|
370
|
-
} else {
|
|
371
|
-
res = await methodRef.current(...args);
|
|
372
|
-
}
|
|
373
|
-
if (res.ok) {
|
|
374
|
-
dispatch({ type: "FETCH_SUCCESS", data: res.data });
|
|
375
|
-
const tagsToInvalidate = options?.revalidateTags ?? (autoRevalidateRef.current ? generateTags(resolvedPath) : []);
|
|
376
|
-
if (tagsToInvalidate.length > 0) {
|
|
377
|
-
invalidateTags(tagsToInvalidate);
|
|
378
|
-
}
|
|
379
|
-
} else {
|
|
380
|
-
dispatch({ type: "FETCH_ERROR", error: res.error });
|
|
381
|
-
}
|
|
382
|
-
return res;
|
|
383
|
-
});
|
|
71
|
+
nextRequestOptions.next = nextFetchOptions;
|
|
384
72
|
}
|
|
385
|
-
return
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
73
|
+
return (0, import_enlace_core.executeFetch)(
|
|
74
|
+
baseUrl,
|
|
75
|
+
path,
|
|
76
|
+
method,
|
|
77
|
+
{ ...coreOptions, onSuccess: nextOnSuccess },
|
|
78
|
+
nextRequestOptions
|
|
79
|
+
);
|
|
389
80
|
}
|
|
390
81
|
|
|
391
|
-
// src/
|
|
392
|
-
function
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
let trackingResult = {
|
|
401
|
-
trackedCall: null,
|
|
402
|
-
selectorPath: null,
|
|
403
|
-
selectorMethod: null
|
|
404
|
-
};
|
|
405
|
-
const trackingProxy = createTrackingProxy((result2) => {
|
|
406
|
-
trackingResult = result2;
|
|
407
|
-
});
|
|
408
|
-
const result = selectorOrQuery(
|
|
409
|
-
trackingProxy
|
|
410
|
-
);
|
|
411
|
-
if (typeof result === "function") {
|
|
412
|
-
const actualResult = selectorOrQuery(api);
|
|
413
|
-
return useSelectorMode({
|
|
414
|
-
method: actualResult,
|
|
415
|
-
api,
|
|
416
|
-
path: trackingResult.selectorPath ?? [],
|
|
417
|
-
methodName: trackingResult.selectorMethod ?? "",
|
|
418
|
-
autoRevalidateTags
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
return useQueryMode(
|
|
422
|
-
api,
|
|
423
|
-
trackingResult.trackedCall,
|
|
424
|
-
{ autoGenerateTags, staleTime, enabled: queryOptions?.enabled ?? true }
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
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
|
+
);
|
|
428
91
|
}
|