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.
@@ -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/next/createEnlaceHook.ts
24
- var createEnlaceHook_exports = {};
25
- __export(createEnlaceHook_exports, {
26
- createEnlaceHook: () => createEnlaceHook
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(createEnlaceHook_exports);
29
+ module.exports = __toCommonJS(hook_exports);
29
30
 
30
- // src/next/index.ts
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
 
@@ -132,7 +45,7 @@ var initialState = {
132
45
  function hookReducer(state, action) {
133
46
  switch (action.type) {
134
47
  case "RESET":
135
- return action.state;
48
+ return action.state ?? initialState;
136
49
  case "FETCH_START":
137
50
  return {
138
51
  ...state,
@@ -162,6 +75,11 @@ function hookReducer(state, action) {
162
75
  }
163
76
  }
164
77
 
78
+ // src/utils/generateTags.ts
79
+ function generateTags(path) {
80
+ return path.map((_, i) => path.slice(0, i + 1).join("/"));
81
+ }
82
+
165
83
  // src/utils/sortObjectKeys.ts
166
84
  function sortObjectKeys(obj) {
167
85
  if (obj === null || typeof obj !== "object") return obj;
@@ -275,7 +193,10 @@ function useQueryMode(api, trackedCall, options) {
275
193
  const { autoGenerateTags, staleTime, enabled } = options;
276
194
  const queryKey = createQueryKey(trackedCall);
277
195
  const requestOptions = trackedCall.options;
278
- const resolvedPath = resolvePath(trackedCall.path, requestOptions?.pathParams);
196
+ const resolvedPath = resolvePath(
197
+ trackedCall.path,
198
+ requestOptions?.pathParams
199
+ );
279
200
  const queryTags = requestOptions?.tags ?? (autoGenerateTags ? generateTags(resolvedPath) : []);
280
201
  const getCacheState = (includeNeedsFetch = false) => {
281
202
  const cached = getCache(queryKey);
@@ -290,26 +211,22 @@ function useQueryMode(api, trackedCall, options) {
290
211
  error: cached?.error
291
212
  };
292
213
  };
293
- const [state, dispatch] = (0, import_react.useReducer)(hookReducer, null, () => getCacheState(true));
214
+ const [state, dispatch] = (0, import_react.useReducer)(
215
+ hookReducer,
216
+ null,
217
+ () => getCacheState(true)
218
+ );
294
219
  const mountedRef = (0, import_react.useRef)(true);
295
220
  const fetchRef = (0, import_react.useRef)(null);
296
221
  (0, import_react.useEffect)(() => {
297
222
  mountedRef.current = true;
298
223
  if (!enabled) {
299
- dispatch({
300
- type: "RESET",
301
- state: { loading: false, fetching: false, ok: void 0, data: void 0, error: void 0 }
302
- });
224
+ dispatch({ type: "RESET" });
303
225
  return () => {
304
226
  mountedRef.current = false;
305
227
  };
306
228
  }
307
229
  dispatch({ type: "RESET", state: getCacheState(true) });
308
- const unsubscribe = subscribeCache(queryKey, () => {
309
- if (mountedRef.current) {
310
- dispatch({ type: "SYNC_CACHE", state: getCacheState() });
311
- }
312
- });
313
230
  const doFetch = () => {
314
231
  const cached2 = getCache(queryKey);
315
232
  if (cached2?.promise) {
@@ -325,7 +242,7 @@ function useQueryMode(api, trackedCall, options) {
325
242
  if (mountedRef.current) {
326
243
  setCache(queryKey, {
327
244
  data: res.ok ? res.data : void 0,
328
- error: res.ok ? void 0 : res.error,
245
+ error: res.ok || res.status === 0 ? void 0 : res.error,
329
246
  ok: res.ok,
330
247
  timestamp: Date.now(),
331
248
  tags: queryTags
@@ -344,6 +261,11 @@ function useQueryMode(api, trackedCall, options) {
344
261
  } else {
345
262
  doFetch();
346
263
  }
264
+ const unsubscribe = subscribeCache(queryKey, () => {
265
+ if (mountedRef.current) {
266
+ dispatch({ type: "SYNC_CACHE", state: getCacheState() });
267
+ }
268
+ });
347
269
  return () => {
348
270
  mountedRef.current = false;
349
271
  fetchRef.current = null;
@@ -362,6 +284,38 @@ function useQueryMode(api, trackedCall, options) {
362
284
  return state;
363
285
  }
364
286
 
287
+ // src/react/types.ts
288
+ var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
289
+
290
+ // src/react/trackingProxy.ts
291
+ function createTrackingProxy(onTrack) {
292
+ const createProxy = (path = []) => {
293
+ return new Proxy(() => {
294
+ }, {
295
+ get(_, prop) {
296
+ if (HTTP_METHODS.includes(prop)) {
297
+ const methodFn = (options) => {
298
+ onTrack({
299
+ trackedCall: { path, method: prop, options },
300
+ selectorPath: null,
301
+ selectorMethod: null
302
+ });
303
+ return Promise.resolve({ ok: true, data: void 0 });
304
+ };
305
+ onTrack({
306
+ trackedCall: null,
307
+ selectorPath: path,
308
+ selectorMethod: prop
309
+ });
310
+ return methodFn;
311
+ }
312
+ return createProxy([...path, prop]);
313
+ }
314
+ });
315
+ };
316
+ return createProxy();
317
+ }
318
+
365
319
  // src/react/useSelectorMode.ts
366
320
  var import_react2 = require("react");
367
321
  function resolvePath2(path, pathParams) {
@@ -429,47 +383,113 @@ function useSelectorMode(config) {
429
383
  };
430
384
  }
431
385
 
432
- // src/react/types.ts
433
- var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
386
+ // src/react/createEnlaceHookReact.ts
387
+ function createEnlaceHookReact(baseUrl, defaultOptions = {}, hookOptions = {}) {
388
+ const {
389
+ autoGenerateTags = true,
390
+ autoRevalidateTags = true,
391
+ staleTime = 0,
392
+ onSuccess,
393
+ onError
394
+ } = hookOptions;
395
+ const api = (0, import_enlace_core.createEnlace)(baseUrl, defaultOptions, { onSuccess, onError });
396
+ function useEnlaceHook(selectorOrQuery, queryOptions) {
397
+ let trackingResult = {
398
+ trackedCall: null,
399
+ selectorPath: null,
400
+ selectorMethod: null
401
+ };
402
+ const trackingProxy = createTrackingProxy((result2) => {
403
+ trackingResult = result2;
404
+ });
405
+ const result = selectorOrQuery(
406
+ trackingProxy
407
+ );
408
+ if (typeof result === "function") {
409
+ const actualResult = selectorOrQuery(api);
410
+ return useSelectorMode({
411
+ method: actualResult,
412
+ api,
413
+ path: trackingResult.selectorPath ?? [],
414
+ methodName: trackingResult.selectorMethod ?? "",
415
+ autoRevalidateTags
416
+ });
417
+ }
418
+ return useQueryMode(
419
+ api,
420
+ trackingResult.trackedCall,
421
+ { autoGenerateTags, staleTime, enabled: queryOptions?.enabled ?? true }
422
+ );
423
+ }
424
+ return useEnlaceHook;
425
+ }
434
426
 
435
- // src/react/trackingProxy.ts
436
- function createTrackingProxy(onTrack) {
437
- const createProxy = (path = []) => {
438
- return new Proxy(() => {
439
- }, {
440
- get(_, prop) {
441
- if (HTTP_METHODS.includes(prop)) {
442
- const methodFn = (options) => {
443
- onTrack({
444
- trackedCall: { path, method: prop, options },
445
- selectorPath: null,
446
- selectorMethod: null
447
- });
448
- return Promise.resolve({ ok: true, data: void 0 });
449
- };
450
- onTrack({
451
- trackedCall: null,
452
- selectorPath: path,
453
- selectorMethod: prop
454
- });
455
- return methodFn;
456
- }
457
- return createProxy([...path, prop]);
427
+ // src/next/index.ts
428
+ var import_enlace_core3 = require("enlace-core");
429
+
430
+ // src/next/fetch.ts
431
+ var import_enlace_core2 = require("enlace-core");
432
+ async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
433
+ const {
434
+ autoGenerateTags = true,
435
+ autoRevalidateTags = true,
436
+ revalidator,
437
+ onSuccess,
438
+ ...coreOptions
439
+ } = combinedOptions;
440
+ const isGet = method === "GET";
441
+ const autoTags = generateTags(path);
442
+ const nextOnSuccess = (payload) => {
443
+ if (!isGet && !requestOptions?.skipRevalidator) {
444
+ const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
445
+ const revalidatePaths = requestOptions?.revalidatePaths ?? [];
446
+ if (revalidateTags.length || revalidatePaths.length) {
447
+ revalidator?.(revalidateTags, revalidatePaths);
458
448
  }
459
- });
449
+ }
450
+ onSuccess?.(payload);
460
451
  };
461
- return createProxy();
452
+ const nextRequestOptions = { ...requestOptions };
453
+ if (isGet) {
454
+ const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
455
+ const nextFetchOptions = {};
456
+ if (tags) {
457
+ nextFetchOptions.tags = tags;
458
+ }
459
+ if (requestOptions?.revalidate !== void 0) {
460
+ nextFetchOptions.revalidate = requestOptions.revalidate;
461
+ }
462
+ nextRequestOptions.next = nextFetchOptions;
463
+ }
464
+ return (0, import_enlace_core2.executeFetch)(
465
+ baseUrl,
466
+ path,
467
+ method,
468
+ { ...coreOptions, onSuccess: nextOnSuccess },
469
+ nextRequestOptions
470
+ );
471
+ }
472
+
473
+ // src/next/index.ts
474
+ function createEnlaceNext(baseUrl, defaultOptions = {}, nextOptions = {}) {
475
+ const combinedOptions = { ...defaultOptions, ...nextOptions };
476
+ return (0, import_enlace_core3.createProxyHandler)(
477
+ baseUrl,
478
+ combinedOptions,
479
+ [],
480
+ executeNextFetch
481
+ );
462
482
  }
463
483
 
464
- // src/next/createEnlaceHook.ts
465
- function createEnlaceHook(baseUrl, defaultOptions = {}, hookOptions = {}) {
484
+ // src/next/createEnlaceHookNext.ts
485
+ function createEnlaceHookNext(baseUrl, defaultOptions = {}, hookOptions = {}) {
466
486
  const {
467
487
  autoGenerateTags = true,
468
488
  autoRevalidateTags = true,
469
489
  staleTime = 0,
470
490
  ...nextOptions
471
491
  } = hookOptions;
472
- const api = createEnlace(baseUrl, defaultOptions, {
492
+ const api = createEnlaceNext(baseUrl, defaultOptions, {
473
493
  autoGenerateTags,
474
494
  autoRevalidateTags,
475
495
  ...nextOptions