enlace 0.0.1-beta.3 → 0.0.1-beta.5

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.
@@ -5,9 +5,7 @@ import {
5
5
 
6
6
  // src/next/fetch.ts
7
7
  import {
8
- buildUrl,
9
- isJsonBody,
10
- mergeHeaders
8
+ executeFetch
11
9
  } from "enlace-core";
12
10
 
13
11
  // src/utils/generateTags.ts
@@ -21,20 +19,22 @@ async function executeNextFetch(baseUrl, path, method, combinedOptions, requestO
21
19
  autoGenerateTags = true,
22
20
  autoRevalidateTags = true,
23
21
  revalidator,
24
- headers: defaultHeaders,
25
- ...restOptions
22
+ onSuccess,
23
+ ...coreOptions
26
24
  } = combinedOptions;
27
- const url = buildUrl(baseUrl, path, requestOptions?.query);
28
- let headers = mergeHeaders(defaultHeaders, requestOptions?.headers);
29
25
  const isGet = method === "GET";
30
26
  const autoTags = generateTags(path);
31
- const fetchOptions = {
32
- ...restOptions,
33
- method
27
+ const nextOnSuccess = (payload) => {
28
+ if (!isGet && !requestOptions?.skipRevalidator) {
29
+ const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
30
+ const revalidatePaths = requestOptions?.revalidatePaths ?? [];
31
+ if (revalidateTags.length || revalidatePaths.length) {
32
+ revalidator?.(revalidateTags, revalidatePaths);
33
+ }
34
+ }
35
+ onSuccess?.(payload);
34
36
  };
35
- if (requestOptions?.cache) {
36
- fetchOptions.cache = requestOptions.cache;
37
- }
37
+ const nextRequestOptions = { ...requestOptions };
38
38
  if (isGet) {
39
39
  const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
40
40
  const nextFetchOptions = {};
@@ -44,51 +44,27 @@ async function executeNextFetch(baseUrl, path, method, combinedOptions, requestO
44
44
  if (requestOptions?.revalidate !== void 0) {
45
45
  nextFetchOptions.revalidate = requestOptions.revalidate;
46
46
  }
47
- fetchOptions.next = nextFetchOptions;
48
- }
49
- if (headers) {
50
- fetchOptions.headers = headers;
47
+ nextRequestOptions.next = nextFetchOptions;
51
48
  }
52
- if (requestOptions?.body !== void 0) {
53
- if (isJsonBody(requestOptions.body)) {
54
- fetchOptions.body = JSON.stringify(requestOptions.body);
55
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
56
- if (headers) {
57
- fetchOptions.headers = headers;
58
- }
59
- } else {
60
- fetchOptions.body = requestOptions.body;
61
- }
62
- }
63
- const response = await fetch(url, fetchOptions);
64
- const contentType = response.headers.get("content-type");
65
- const isJson = contentType?.includes("application/json");
66
- if (response.ok) {
67
- if (!isGet && !requestOptions?.skipRevalidator) {
68
- const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
69
- const revalidatePaths = requestOptions?.revalidatePaths ?? [];
70
- if (revalidateTags.length || revalidatePaths.length) {
71
- revalidator?.(revalidateTags, revalidatePaths);
72
- }
73
- }
74
- return {
75
- ok: true,
76
- status: response.status,
77
- data: isJson ? await response.json() : response
78
- };
79
- }
80
- return {
81
- ok: false,
82
- status: response.status,
83
- error: isJson ? await response.json() : response
84
- };
49
+ return executeFetch(
50
+ baseUrl,
51
+ path,
52
+ method,
53
+ { ...coreOptions, onSuccess: nextOnSuccess },
54
+ nextRequestOptions
55
+ );
85
56
  }
86
57
 
87
58
  // src/next/index.ts
88
59
  export * from "enlace-core";
89
60
  function createEnlace(baseUrl, defaultOptions = {}, nextOptions = {}) {
90
61
  const combinedOptions = { ...defaultOptions, ...nextOptions };
91
- return createProxyHandler(baseUrl, combinedOptions, [], executeNextFetch);
62
+ return createProxyHandler(
63
+ baseUrl,
64
+ combinedOptions,
65
+ [],
66
+ executeNextFetch
67
+ );
92
68
  }
93
69
  export {
94
70
  createEnlace
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "enlace",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.5",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"
@@ -23,7 +23,7 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "enlace-core": "0.0.1-beta.2"
26
+ "enlace-core": "0.0.1-beta.3"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "react": "^19"