enlace 0.0.0-alpha.4 → 0.0.1-beta.2

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.
@@ -1,121 +1,74 @@
1
- type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
2
- type SchemaMethod = "$get" | "$post" | "$put" | "$patch" | "$delete";
3
- type EnlaceResponse<TData, TError> = {
4
- ok: true;
5
- status: number;
6
- data: TData;
7
- error?: never;
8
- } | {
9
- ok: false;
10
- status: number;
11
- data?: never;
12
- error: TError;
13
- };
14
- type EnlaceOptions = Omit<RequestInit, "method" | "body">;
15
- type FetchExecutor<TOptions = EnlaceOptions, TRequestOptions = RequestOptions<unknown>> = <TData, TError>(baseUrl: string, path: string[], method: HttpMethod, defaultOptions: TOptions, requestOptions?: TRequestOptions) => Promise<EnlaceResponse<TData, TError>>;
16
- type RequestOptions<TBody = never> = {
17
- body?: TBody;
18
- query?: Record<string, string | number | boolean | undefined>;
19
- headers?: HeadersInit;
20
- cache?: RequestCache;
21
- };
22
- type MethodDefinition = {
23
- data: unknown;
24
- error: unknown;
25
- body?: unknown;
26
- };
27
- /**
28
- * Helper to define an endpoint with proper typing.
29
- * Provides cleaner syntax than writing { data, error, body } manually.
30
- *
31
- * @example
32
- * type MyApi = {
33
- * posts: {
34
- * $get: Endpoint<Post[], ApiError>;
35
- * $post: Endpoint<Post, ApiError, CreatePost>; // with body
36
- * _: {
37
- * $get: Endpoint<Post, NotFoundError>;
38
- * };
39
- * };
40
- * };
41
- */
42
- type Endpoint<TData, TError, TBody = never> = [TBody] extends [never] ? {
43
- data: TData;
44
- error: TError;
45
- } : {
46
- data: TData;
47
- error: TError;
48
- body: TBody;
49
- };
50
- type ExtractMethodDef<TSchema, TMethod extends SchemaMethod> = TSchema extends {
51
- [K in TMethod]: infer M;
52
- } ? M extends MethodDefinition ? M : never : never;
53
- type ExtractData<TSchema, TMethod extends SchemaMethod> = ExtractMethodDef<TSchema, TMethod> extends {
54
- data: infer D;
55
- } ? D : never;
56
- type ExtractError<TSchema, TMethod extends SchemaMethod> = ExtractMethodDef<TSchema, TMethod> extends {
57
- error: infer E;
58
- } ? E : never;
59
- type ExtractBody<TSchema, TMethod extends SchemaMethod> = ExtractMethodDef<TSchema, TMethod> extends {
60
- body: infer B;
61
- } ? B : never;
62
- type HasMethod<TSchema, TMethod extends SchemaMethod> = TSchema extends {
63
- [K in TMethod]: MethodDefinition;
64
- } ? true : false;
65
- type MethodFn<TSchema, TMethod extends SchemaMethod, TRequestOptionsBase = object> = HasMethod<TSchema, TMethod> extends true ? ExtractBody<TSchema, TMethod> extends never ? (options?: RequestOptions<never> & TRequestOptionsBase) => Promise<EnlaceResponse<ExtractData<TSchema, TMethod>, ExtractError<TSchema, TMethod>>> : (options: RequestOptions<ExtractBody<TSchema, TMethod>> & TRequestOptionsBase) => Promise<EnlaceResponse<ExtractData<TSchema, TMethod>, ExtractError<TSchema, TMethod>>> : never;
66
- type IsSpecialKey<K> = K extends SchemaMethod | "_" ? true : false;
67
- type StaticPathKeys<TSchema> = {
68
- [K in keyof TSchema as IsSpecialKey<K> extends true ? never : K extends string ? K : never]: TSchema[K];
69
- };
70
- type ExtractDynamicSchema<TSchema> = TSchema extends {
71
- _: infer D;
72
- } ? D : never;
73
- type MethodOrPath<TSchema, TMethodName extends string, TSchemaMethod extends SchemaMethod, TRequestOptionsBase = object> = TMethodName extends keyof TSchema ? EnlaceClient<TSchema[TMethodName], TRequestOptionsBase> : MethodFn<TSchema, TSchemaMethod, TRequestOptionsBase>;
74
- type HttpMethods<TSchema, TRequestOptionsBase = object> = {
75
- get: MethodOrPath<TSchema, "get", "$get", TRequestOptionsBase>;
76
- post: MethodOrPath<TSchema, "post", "$post", TRequestOptionsBase>;
77
- put: MethodOrPath<TSchema, "put", "$put", TRequestOptionsBase>;
78
- patch: MethodOrPath<TSchema, "patch", "$patch", TRequestOptionsBase>;
79
- delete: MethodOrPath<TSchema, "delete", "$delete", TRequestOptionsBase>;
80
- };
81
- type DynamicAccess<TSchema, TRequestOptionsBase = object> = ExtractDynamicSchema<TSchema> extends never ? object : {
82
- [key: string]: EnlaceClient<ExtractDynamicSchema<TSchema>, TRequestOptionsBase>;
83
- [key: number]: EnlaceClient<ExtractDynamicSchema<TSchema>, TRequestOptionsBase>;
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[];
84
15
  };
85
- type MethodNameKeys = "get" | "post" | "put" | "patch" | "delete";
86
- type EnlaceClient<TSchema, TRequestOptionsBase = object> = HttpMethods<TSchema, TRequestOptionsBase> & DynamicAccess<TSchema, TRequestOptionsBase> & {
87
- [K in keyof StaticPathKeys<TSchema> as K extends MethodNameKeys ? never : K]: EnlaceClient<TSchema[K], TRequestOptionsBase>;
16
+
17
+ type EnlaceHookOptions = {
18
+ /**
19
+ * Auto-generate cache tags from URL path for GET requests.
20
+ * e.g., `/posts/1` generates tags `['posts', 'posts/1']`
21
+ * @default true
22
+ */
23
+ autoGenerateTags?: boolean;
24
+ /** Auto-revalidate generated tags after successful mutations. @default true */
25
+ autoRevalidateTags?: boolean;
26
+ /** Time in ms before cached data is considered stale. @default 0 (always stale) */
27
+ staleTime?: number;
88
28
  };
89
- type WildcardMethodFn<TRequestOptionsBase = object> = (options?: RequestOptions<unknown> & TRequestOptionsBase) => Promise<EnlaceResponse<unknown, unknown>>;
29
+
90
30
  /**
91
- * Wildcard client type - allows any path access when no schema is provided.
92
- * All methods are available at every level and return unknown types.
31
+ * Handler function called after successful mutations to trigger server-side revalidation.
32
+ * @param tags - Cache tags to revalidate
33
+ * @param paths - URL paths to revalidate
93
34
  */
94
- type WildcardClient<TRequestOptionsBase = object> = {
95
- get: WildcardMethodFn<TRequestOptionsBase>;
96
- post: WildcardMethodFn<TRequestOptionsBase>;
97
- put: WildcardMethodFn<TRequestOptionsBase>;
98
- patch: WildcardMethodFn<TRequestOptionsBase>;
99
- delete: WildcardMethodFn<TRequestOptionsBase>;
100
- } & {
101
- [key: string]: WildcardClient<TRequestOptionsBase>;
102
- [key: number]: WildcardClient<TRequestOptionsBase>;
103
- };
104
-
105
- type NextFetchOptions = {
106
- revalidate?: number | false;
107
- tags?: string[];
108
- };
109
35
  type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
110
- type NextEnlaceOptions = EnlaceOptions & {
111
- revalidate?: RevalidateHandler;
36
+ /** Next.js-specific options (third argument for createEnlace) */
37
+ type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & {
38
+ /**
39
+ * Handler called after successful mutations to trigger server-side revalidation.
40
+ * Receives auto-generated or manually specified tags and paths.
41
+ * @example
42
+ * ```ts
43
+ * createEnlace("http://localhost:3000/api/", {}, {
44
+ * revalidator: (tags, paths) => revalidateServerAction(tags, paths)
45
+ * });
46
+ * ```
47
+ */
48
+ revalidator?: RevalidateHandler;
112
49
  };
113
- type NextRequestOptionsBase = {
114
- next?: NextFetchOptions;
115
- revalidateTags?: string[];
50
+ /** Next.js hook options (third argument for createEnlaceHook) - extends React's EnlaceHookOptions */
51
+ type NextHookOptions = EnlaceHookOptions & Pick<NextOptions, "revalidator">;
52
+ /** Per-request options for Next.js fetch - extends React's base options */
53
+ type NextRequestOptionsBase = ReactRequestOptionsBase & {
54
+ /** Time in seconds to revalidate, or false to disable */
55
+ revalidate?: number | false;
56
+ /**
57
+ * URL paths to revalidate after mutation
58
+ * This doesn't do anything on the client by itself - it's passed to the revalidator handler.
59
+ * You must implement the revalidation logic in the revalidator.
60
+ */
116
61
  revalidatePaths?: string[];
62
+ /**
63
+ * Skip server-side revalidation for this request.
64
+ * Useful when autoRevalidateTags is enabled but you want to opt-out for specific mutations.
65
+ * You can still pass empty [] to revalidateTags to skip triggering revalidation.
66
+ * But this flag can be used if you want to revalidate client-side and skip server-side entirely.
67
+ * Eg. you don't fetch any data on server component and you might want to skip the overhead of revalidation.
68
+ */
69
+ skipRevalidator?: boolean;
117
70
  };
118
71
 
119
- declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: NextEnlaceOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
72
+ declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
120
73
 
121
- export { type Endpoint, type EnlaceClient, type EnlaceOptions, type EnlaceResponse, type FetchExecutor, type HttpMethod, type MethodDefinition, type NextEnlaceOptions, type NextFetchOptions, type NextRequestOptionsBase, type RequestOptions, type RevalidateHandler, type SchemaMethod, type WildcardClient, createEnlace };
74
+ export { type NextHookOptions, type NextOptions, type NextRequestOptionsBase, type RevalidateHandler, createEnlace };
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,7 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
28
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
20
 
30
21
  // src/next/index.ts
@@ -33,130 +24,54 @@ __export(next_exports, {
33
24
  createEnlace: () => createEnlace
34
25
  });
35
26
  module.exports = __toCommonJS(next_exports);
36
-
37
- // src/utils/buildUrl.ts
38
- var import_query_string = __toESM(require("query-string"));
39
- function buildUrl(baseUrl, path, query) {
40
- const normalizedBase = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
41
- const url = new URL(path.join("/"), normalizedBase);
42
- if (query) {
43
- url.search = import_query_string.default.stringify(query, { skipNull: true, skipEmptyString: true });
44
- }
45
- return url.toString();
46
- }
47
-
48
- // src/utils/isJsonBody.ts
49
- function isJsonBody(body) {
50
- if (body === null || body === void 0) return false;
51
- if (body instanceof FormData) return false;
52
- if (body instanceof Blob) return false;
53
- if (body instanceof ArrayBuffer) return false;
54
- if (body instanceof URLSearchParams) return false;
55
- if (body instanceof ReadableStream) return false;
56
- if (typeof body === "string") return false;
57
- return typeof body === "object";
58
- }
59
-
60
- // src/utils/mergeHeaders.ts
61
- function mergeHeaders(defaultHeaders, requestHeaders) {
62
- if (!defaultHeaders && !requestHeaders) return void 0;
63
- if (!defaultHeaders) return requestHeaders;
64
- if (!requestHeaders) return defaultHeaders;
65
- return {
66
- ...Object.fromEntries(new Headers(defaultHeaders)),
67
- ...Object.fromEntries(new Headers(requestHeaders))
68
- };
69
- }
70
-
71
- // src/core/fetch.ts
72
- async function executeFetch(baseUrl, path, method, defaultOptions, requestOptions) {
73
- const url = buildUrl(baseUrl, path, requestOptions?.query);
74
- let headers = mergeHeaders(defaultOptions.headers, requestOptions?.headers);
75
- const fetchOptions = {
76
- ...defaultOptions,
77
- method
78
- };
79
- if (headers) {
80
- fetchOptions.headers = headers;
81
- }
82
- if (requestOptions?.body !== void 0) {
83
- if (isJsonBody(requestOptions.body)) {
84
- fetchOptions.body = JSON.stringify(requestOptions.body);
85
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
86
- if (headers) {
87
- fetchOptions.headers = headers;
88
- }
89
- } else {
90
- fetchOptions.body = requestOptions.body;
91
- }
92
- }
93
- const response = await fetch(url, fetchOptions);
94
- const contentType = response.headers.get("content-type");
95
- const isJson = contentType?.includes("application/json");
96
- if (response.ok) {
97
- return {
98
- ok: true,
99
- status: response.status,
100
- data: isJson ? await response.json() : response
101
- };
102
- }
103
- return {
104
- ok: false,
105
- status: response.status,
106
- error: isJson ? await response.json() : response
107
- };
108
- }
109
-
110
- // src/core/proxy.ts
111
- var HTTP_METHODS = {
112
- get: "GET",
113
- post: "POST",
114
- put: "PUT",
115
- patch: "PATCH",
116
- delete: "DELETE"
117
- };
118
- function createProxyHandler(baseUrl, defaultOptions, path = [], fetchExecutor = executeFetch) {
119
- const handler = {
120
- get(_target, prop) {
121
- if (typeof prop === "symbol") return void 0;
122
- const method = HTTP_METHODS[prop];
123
- if (method) {
124
- return (options) => fetchExecutor(baseUrl, path, method, defaultOptions, options);
125
- }
126
- return createProxyHandler(baseUrl, defaultOptions, [...path, prop], fetchExecutor);
127
- }
128
- };
129
- return new Proxy({}, handler);
130
- }
27
+ var import_enlace_core2 = require("enlace-core");
131
28
 
132
29
  // src/next/fetch.ts
30
+ var import_enlace_core = require("enlace-core");
31
+
32
+ // src/utils/generateTags.ts
133
33
  function generateTags(path) {
134
34
  return path.map((_, i) => path.slice(0, i + 1).join("/"));
135
35
  }
136
- async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOptions) {
137
- const url = buildUrl(baseUrl, path, requestOptions?.query);
138
- let headers = mergeHeaders(defaultOptions.headers, requestOptions?.headers);
139
- const nextOptions = requestOptions?.next;
140
- const tags = nextOptions?.tags ?? generateTags(path);
36
+
37
+ // src/next/fetch.ts
38
+ async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
39
+ const {
40
+ autoGenerateTags = true,
41
+ autoRevalidateTags = true,
42
+ revalidator,
43
+ headers: defaultHeaders,
44
+ ...restOptions
45
+ } = combinedOptions;
46
+ const url = (0, import_enlace_core.buildUrl)(baseUrl, path, requestOptions?.query);
47
+ let headers = (0, import_enlace_core.mergeHeaders)(defaultHeaders, requestOptions?.headers);
48
+ const isGet = method === "GET";
49
+ const autoTags = generateTags(path);
141
50
  const fetchOptions = {
142
- ...defaultOptions,
51
+ ...restOptions,
143
52
  method
144
53
  };
145
54
  if (requestOptions?.cache) {
146
55
  fetchOptions.cache = requestOptions.cache;
147
56
  }
148
- const nextFetchOptions = { tags };
149
- if (nextOptions?.revalidate !== void 0) {
150
- nextFetchOptions.revalidate = nextOptions.revalidate;
57
+ if (isGet) {
58
+ const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
59
+ const nextFetchOptions = {};
60
+ if (tags) {
61
+ nextFetchOptions.tags = tags;
62
+ }
63
+ if (requestOptions?.revalidate !== void 0) {
64
+ nextFetchOptions.revalidate = requestOptions.revalidate;
65
+ }
66
+ fetchOptions.next = nextFetchOptions;
151
67
  }
152
- fetchOptions.next = nextFetchOptions;
153
68
  if (headers) {
154
69
  fetchOptions.headers = headers;
155
70
  }
156
71
  if (requestOptions?.body !== void 0) {
157
- if (isJsonBody(requestOptions.body)) {
72
+ if ((0, import_enlace_core.isJsonBody)(requestOptions.body)) {
158
73
  fetchOptions.body = JSON.stringify(requestOptions.body);
159
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
74
+ headers = (0, import_enlace_core.mergeHeaders)(headers, { "Content-Type": "application/json" });
160
75
  if (headers) {
161
76
  fetchOptions.headers = headers;
162
77
  }
@@ -168,9 +83,12 @@ async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOp
168
83
  const contentType = response.headers.get("content-type");
169
84
  const isJson = contentType?.includes("application/json");
170
85
  if (response.ok) {
171
- const { revalidateTags = [], revalidatePaths = [] } = requestOptions ?? {};
172
- if (revalidateTags.length || revalidatePaths.length) {
173
- defaultOptions.revalidate?.(revalidateTags, revalidatePaths);
86
+ if (!isGet && !requestOptions?.skipRevalidator) {
87
+ const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
88
+ const revalidatePaths = requestOptions?.revalidatePaths ?? [];
89
+ if (revalidateTags.length || revalidatePaths.length) {
90
+ revalidator?.(revalidateTags, revalidatePaths);
91
+ }
174
92
  }
175
93
  return {
176
94
  ok: true,
@@ -186,6 +104,8 @@ async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOp
186
104
  }
187
105
 
188
106
  // src/next/index.ts
189
- function createEnlace(baseUrl, defaultOptions = {}) {
190
- return createProxyHandler(baseUrl, defaultOptions, [], executeNextFetch);
107
+ __reExport(next_exports, require("enlace-core"), module.exports);
108
+ function createEnlace(baseUrl, defaultOptions = {}, nextOptions = {}) {
109
+ const combinedOptions = { ...defaultOptions, ...nextOptions };
110
+ return (0, import_enlace_core2.createProxyHandler)(baseUrl, combinedOptions, [], executeNextFetch);
191
111
  }
@@ -1,119 +1,51 @@
1
- // src/utils/buildUrl.ts
2
- import qs from "query-string";
3
- function buildUrl(baseUrl, path, query) {
4
- const normalizedBase = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
5
- const url = new URL(path.join("/"), normalizedBase);
6
- if (query) {
7
- url.search = qs.stringify(query, { skipNull: true, skipEmptyString: true });
8
- }
9
- return url.toString();
10
- }
11
-
12
- // src/utils/isJsonBody.ts
13
- function isJsonBody(body) {
14
- if (body === null || body === void 0) return false;
15
- if (body instanceof FormData) return false;
16
- if (body instanceof Blob) return false;
17
- if (body instanceof ArrayBuffer) return false;
18
- if (body instanceof URLSearchParams) return false;
19
- if (body instanceof ReadableStream) return false;
20
- if (typeof body === "string") return false;
21
- return typeof body === "object";
22
- }
23
-
24
- // src/utils/mergeHeaders.ts
25
- function mergeHeaders(defaultHeaders, requestHeaders) {
26
- if (!defaultHeaders && !requestHeaders) return void 0;
27
- if (!defaultHeaders) return requestHeaders;
28
- if (!requestHeaders) return defaultHeaders;
29
- return {
30
- ...Object.fromEntries(new Headers(defaultHeaders)),
31
- ...Object.fromEntries(new Headers(requestHeaders))
32
- };
33
- }
34
-
35
- // src/core/fetch.ts
36
- async function executeFetch(baseUrl, path, method, defaultOptions, requestOptions) {
37
- const url = buildUrl(baseUrl, path, requestOptions?.query);
38
- let headers = mergeHeaders(defaultOptions.headers, requestOptions?.headers);
39
- const fetchOptions = {
40
- ...defaultOptions,
41
- method
42
- };
43
- if (headers) {
44
- fetchOptions.headers = headers;
45
- }
46
- if (requestOptions?.body !== void 0) {
47
- if (isJsonBody(requestOptions.body)) {
48
- fetchOptions.body = JSON.stringify(requestOptions.body);
49
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
50
- if (headers) {
51
- fetchOptions.headers = headers;
52
- }
53
- } else {
54
- fetchOptions.body = requestOptions.body;
55
- }
56
- }
57
- const response = await fetch(url, fetchOptions);
58
- const contentType = response.headers.get("content-type");
59
- const isJson = contentType?.includes("application/json");
60
- if (response.ok) {
61
- return {
62
- ok: true,
63
- status: response.status,
64
- data: isJson ? await response.json() : response
65
- };
66
- }
67
- return {
68
- ok: false,
69
- status: response.status,
70
- error: isJson ? await response.json() : response
71
- };
72
- }
73
-
74
- // src/core/proxy.ts
75
- var HTTP_METHODS = {
76
- get: "GET",
77
- post: "POST",
78
- put: "PUT",
79
- patch: "PATCH",
80
- delete: "DELETE"
81
- };
82
- function createProxyHandler(baseUrl, defaultOptions, path = [], fetchExecutor = executeFetch) {
83
- const handler = {
84
- get(_target, prop) {
85
- if (typeof prop === "symbol") return void 0;
86
- const method = HTTP_METHODS[prop];
87
- if (method) {
88
- return (options) => fetchExecutor(baseUrl, path, method, defaultOptions, options);
89
- }
90
- return createProxyHandler(baseUrl, defaultOptions, [...path, prop], fetchExecutor);
91
- }
92
- };
93
- return new Proxy({}, handler);
94
- }
1
+ // src/next/index.ts
2
+ import {
3
+ createProxyHandler
4
+ } from "enlace-core";
95
5
 
96
6
  // src/next/fetch.ts
7
+ import {
8
+ buildUrl,
9
+ isJsonBody,
10
+ mergeHeaders
11
+ } from "enlace-core";
12
+
13
+ // src/utils/generateTags.ts
97
14
  function generateTags(path) {
98
15
  return path.map((_, i) => path.slice(0, i + 1).join("/"));
99
16
  }
100
- async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOptions) {
17
+
18
+ // src/next/fetch.ts
19
+ async function executeNextFetch(baseUrl, path, method, combinedOptions, requestOptions) {
20
+ const {
21
+ autoGenerateTags = true,
22
+ autoRevalidateTags = true,
23
+ revalidator,
24
+ headers: defaultHeaders,
25
+ ...restOptions
26
+ } = combinedOptions;
101
27
  const url = buildUrl(baseUrl, path, requestOptions?.query);
102
- let headers = mergeHeaders(defaultOptions.headers, requestOptions?.headers);
103
- const nextOptions = requestOptions?.next;
104
- const tags = nextOptions?.tags ?? generateTags(path);
28
+ let headers = mergeHeaders(defaultHeaders, requestOptions?.headers);
29
+ const isGet = method === "GET";
30
+ const autoTags = generateTags(path);
105
31
  const fetchOptions = {
106
- ...defaultOptions,
32
+ ...restOptions,
107
33
  method
108
34
  };
109
35
  if (requestOptions?.cache) {
110
36
  fetchOptions.cache = requestOptions.cache;
111
37
  }
112
- const nextFetchOptions = { tags };
113
- if (nextOptions?.revalidate !== void 0) {
114
- nextFetchOptions.revalidate = nextOptions.revalidate;
38
+ if (isGet) {
39
+ const tags = requestOptions?.tags ?? (autoGenerateTags ? autoTags : void 0);
40
+ const nextFetchOptions = {};
41
+ if (tags) {
42
+ nextFetchOptions.tags = tags;
43
+ }
44
+ if (requestOptions?.revalidate !== void 0) {
45
+ nextFetchOptions.revalidate = requestOptions.revalidate;
46
+ }
47
+ fetchOptions.next = nextFetchOptions;
115
48
  }
116
- fetchOptions.next = nextFetchOptions;
117
49
  if (headers) {
118
50
  fetchOptions.headers = headers;
119
51
  }
@@ -132,9 +64,12 @@ async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOp
132
64
  const contentType = response.headers.get("content-type");
133
65
  const isJson = contentType?.includes("application/json");
134
66
  if (response.ok) {
135
- const { revalidateTags = [], revalidatePaths = [] } = requestOptions ?? {};
136
- if (revalidateTags.length || revalidatePaths.length) {
137
- defaultOptions.revalidate?.(revalidateTags, revalidatePaths);
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
+ }
138
73
  }
139
74
  return {
140
75
  ok: true,
@@ -150,8 +85,10 @@ async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOp
150
85
  }
151
86
 
152
87
  // src/next/index.ts
153
- function createEnlace(baseUrl, defaultOptions = {}) {
154
- return createProxyHandler(baseUrl, defaultOptions, [], executeNextFetch);
88
+ export * from "enlace-core";
89
+ function createEnlace(baseUrl, defaultOptions = {}, nextOptions = {}) {
90
+ const combinedOptions = { ...defaultOptions, ...nextOptions };
91
+ return createProxyHandler(baseUrl, combinedOptions, [], executeNextFetch);
155
92
  }
156
93
  export {
157
94
  createEnlace
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "enlace",
3
- "version": "0.0.0-alpha.4",
3
+ "version": "0.0.1-beta.2",
4
+ "license": "MIT",
4
5
  "files": [
5
6
  "dist"
6
7
  ],
@@ -14,33 +15,23 @@
14
15
  "types": "./dist/next/index.d.ts",
15
16
  "import": "./dist/next/index.mjs",
16
17
  "require": "./dist/next/index.js"
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"
17
23
  }
18
24
  },
19
- "license": "MIT",
20
- "scripts": {
21
- "dev": "tsup --watch",
22
- "build": "tsup",
23
- "typecheck": "tsc --noEmit",
24
- "lint": "eslint src --max-warnings 0",
25
- "prepublishOnly": "npm run build && npm run typecheck && npm run lint"
25
+ "dependencies": {
26
+ "enlace-core": "0.0.1-beta.2"
26
27
  },
27
28
  "peerDependencies": {
28
29
  "react": "^19"
29
30
  },
30
- "devDependencies": {
31
- "@eslint/js": "^9.39.1",
32
- "@types/react": "^19.1.0",
33
- "eslint": "^9.39.1",
34
- "eslint-plugin-react": "^7.37.5",
35
- "globals": "^16.5.0",
36
- "jiti": "^2.6.1",
37
- "prettier": "^3.7.4",
38
- "tsup": "^8.5.1",
39
- "tsx": "^4.21.0",
40
- "typescript": "5.9.2",
41
- "typescript-eslint": "^8.48.1"
42
- },
43
- "dependencies": {
44
- "query-string": "^9.3.1"
31
+ "scripts": {
32
+ "dev": "tsup --watch",
33
+ "build": "tsup",
34
+ "typecheck": "tsc --noEmit",
35
+ "lint": "eslint src --max-warnings 0"
45
36
  }
46
- }
37
+ }