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

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/dist/index.d.ts CHANGED
@@ -1,121 +1,83 @@
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;
1
+ import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
2
+ export * from 'enlace-core';
3
+
4
+ /** Per-request options for React hooks */
5
+ type ReactRequestOptionsBase = {
6
+ /**
7
+ * Cache tags for caching (GET requests only)
8
+ * This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
9
+ * But can be manually specified to override auto-generation.
10
+ * */
11
+ tags?: string[];
12
+ /** Tags to invalidate after mutation (triggers refetch in matching queries) */
13
+ revalidateTags?: string[];
14
+ /**
15
+ * Path parameters for dynamic URL segments.
16
+ * Used to replace :paramName placeholders in the URL path.
17
+ * @example
18
+ * // With path api.products[':id'].delete
19
+ * trigger({ pathParams: { id: '123' } }) // → DELETE /products/123
20
+ */
21
+ pathParams?: Record<string, string | number>;
21
22
  };
22
- type MethodDefinition = {
23
- data: unknown;
24
- error: unknown;
25
- body?: unknown;
23
+ /** Options for createEnlaceHookReact factory */
24
+ type EnlaceHookOptions = {
25
+ /**
26
+ * Auto-generate cache tags from URL path for GET requests.
27
+ * e.g., `/posts/1` generates tags `['posts', 'posts/1']`
28
+ * @default true
29
+ */
30
+ autoGenerateTags?: boolean;
31
+ /** Auto-revalidate generated tags after successful mutations. @default true */
32
+ autoRevalidateTags?: boolean;
33
+ /** Time in ms before cached data is considered stale. @default 0 (always stale) */
34
+ staleTime?: number;
35
+ /** Callback called on successful API responses */
36
+ onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
37
+ /** Callback called on error responses (HTTP errors or network failures) */
38
+ onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
26
39
  };
40
+
27
41
  /**
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
- * };
42
+ * Handler function called after successful mutations to trigger server-side revalidation.
43
+ * @param tags - Cache tags to revalidate
44
+ * @param paths - URL paths to revalidate
41
45
  */
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];
46
+ type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
47
+ /** Next.js-specific options (third argument for createEnlaceNext) */
48
+ type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
49
+ /**
50
+ * Handler called after successful mutations to trigger server-side revalidation.
51
+ * Receives auto-generated or manually specified tags and paths.
52
+ * @example
53
+ * ```ts
54
+ * createEnlaceNext("http://localhost:3000/api/", {}, {
55
+ * revalidator: (tags, paths) => revalidateServerAction(tags, paths)
56
+ * });
57
+ * ```
58
+ */
59
+ revalidator?: RevalidateHandler;
69
60
  };
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>;
61
+ /** Per-request options for Next.js fetch - extends React's base options */
62
+ type NextRequestOptionsBase = ReactRequestOptionsBase & {
63
+ /** Time in seconds to revalidate, or false to disable */
64
+ revalidate?: number | false;
65
+ /**
66
+ * URL paths to revalidate after mutation
67
+ * This doesn't do anything on the client by itself - it's passed to the revalidator handler.
68
+ * You must implement the revalidation logic in the revalidator.
69
+ */
70
+ revalidatePaths?: string[];
71
+ /**
72
+ * Skip server-side revalidation for this request.
73
+ * Useful when autoRevalidateTags is enabled but you want to opt-out for specific mutations.
74
+ * You can still pass empty [] to revalidateTags to skip triggering revalidation.
75
+ * But this flag can be used if you want to revalidate client-side and skip server-side entirely.
76
+ * Eg. you don't fetch any data on server component and you might want to skip the overhead of revalidation.
77
+ */
78
+ skipRevalidator?: boolean;
80
79
  };
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>;
84
- };
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>;
88
- };
89
- type WildcardMethodFn<TRequestOptionsBase = object> = (options?: RequestOptions<unknown> & TRequestOptionsBase) => Promise<EnlaceResponse<unknown, unknown>>;
90
- /**
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.
93
- */
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
- declare function createProxyHandler<TSchema extends object, TOptions = EnlaceOptions>(baseUrl: string, defaultOptions: TOptions, path?: string[], fetchExecutor?: FetchExecutor<TOptions, RequestOptions<unknown>>): TSchema;
106
80
 
107
- declare function executeFetch<TData, TError>(baseUrl: string, path: string[], method: HttpMethod, defaultOptions: EnlaceOptions, requestOptions?: RequestOptions<unknown>): Promise<EnlaceResponse<TData, TError>>;
108
-
109
- /**
110
- * Creates an API client.
111
- *
112
- * @example
113
- * // Typed mode - with schema
114
- * const api = createEnlace<MyApi>('https://api.example.com');
115
- *
116
- * // Untyped mode - no schema
117
- * const api = createEnlace('https://api.example.com');
118
- */
119
- declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions): unknown extends TSchema ? WildcardClient : EnlaceClient<TSchema>;
81
+ declare function createEnlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
120
82
 
121
- export { type Endpoint, type EnlaceClient, type EnlaceOptions, type EnlaceResponse, type FetchExecutor, type HttpMethod, type MethodDefinition, type RequestOptions, type SchemaMethod, type WildcardClient, createEnlace, createProxyHandler, executeFetch };
83
+ export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };
package/dist/index.js CHANGED
@@ -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,121 +15,77 @@ 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/index.ts
31
22
  var src_exports = {};
32
23
  __export(src_exports, {
33
- createEnlace: () => createEnlace,
34
- createProxyHandler: () => createProxyHandler,
35
- executeFetch: () => executeFetch
24
+ createEnlaceNext: () => createEnlaceNext
36
25
  });
37
26
  module.exports = __toCommonJS(src_exports);
27
+ __reExport(src_exports, require("enlace-core"), module.exports);
38
28
 
39
- // src/utils/buildUrl.ts
40
- var import_query_string = __toESM(require("query-string"));
41
- function buildUrl(baseUrl, path, query) {
42
- const normalizedBase = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
43
- const url = new URL(path.join("/"), normalizedBase);
44
- if (query) {
45
- url.search = import_query_string.default.stringify(query, { skipNull: true, skipEmptyString: true });
46
- }
47
- return url.toString();
48
- }
29
+ // src/next/index.ts
30
+ var import_enlace_core2 = require("enlace-core");
49
31
 
50
- // src/utils/isJsonBody.ts
51
- function isJsonBody(body) {
52
- if (body === null || body === void 0) return false;
53
- if (body instanceof FormData) return false;
54
- if (body instanceof Blob) return false;
55
- if (body instanceof ArrayBuffer) return false;
56
- if (body instanceof URLSearchParams) return false;
57
- if (body instanceof ReadableStream) return false;
58
- if (typeof body === "string") return false;
59
- return typeof body === "object";
60
- }
32
+ // src/next/fetch.ts
33
+ var import_enlace_core = require("enlace-core");
61
34
 
62
- // src/utils/mergeHeaders.ts
63
- function mergeHeaders(defaultHeaders, requestHeaders) {
64
- if (!defaultHeaders && !requestHeaders) return void 0;
65
- if (!defaultHeaders) return requestHeaders;
66
- if (!requestHeaders) return defaultHeaders;
67
- return {
68
- ...Object.fromEntries(new Headers(defaultHeaders)),
69
- ...Object.fromEntries(new Headers(requestHeaders))
70
- };
35
+ // src/utils/generateTags.ts
36
+ function generateTags(path) {
37
+ return path.map((_, i) => path.slice(0, i + 1).join("/"));
71
38
  }
72
39
 
73
- // src/core/fetch.ts
74
- async function executeFetch(baseUrl, path, method, defaultOptions, requestOptions) {
75
- const url = buildUrl(baseUrl, path, requestOptions?.query);
76
- let headers = mergeHeaders(defaultOptions.headers, requestOptions?.headers);
77
- const fetchOptions = {
78
- ...defaultOptions,
79
- method
80
- };
81
- if (headers) {
82
- fetchOptions.headers = headers;
83
- }
84
- if (requestOptions?.body !== void 0) {
85
- if (isJsonBody(requestOptions.body)) {
86
- fetchOptions.body = JSON.stringify(requestOptions.body);
87
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
88
- if (headers) {
89
- fetchOptions.headers = headers;
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);
90
57
  }
91
- } else {
92
- fetchOptions.body = requestOptions.body;
93
58
  }
94
- }
95
- const response = await fetch(url, fetchOptions);
96
- const contentType = response.headers.get("content-type");
97
- const isJson = contentType?.includes("application/json");
98
- if (response.ok) {
99
- return {
100
- ok: true,
101
- status: response.status,
102
- data: isJson ? await response.json() : response
103
- };
104
- }
105
- return {
106
- ok: false,
107
- status: response.status,
108
- error: isJson ? await response.json() : response
59
+ onSuccess?.(payload);
109
60
  };
110
- }
111
-
112
- // src/core/proxy.ts
113
- var HTTP_METHODS = {
114
- get: "GET",
115
- post: "POST",
116
- put: "PUT",
117
- patch: "PATCH",
118
- delete: "DELETE"
119
- };
120
- function createProxyHandler(baseUrl, defaultOptions, path = [], fetchExecutor = executeFetch) {
121
- const handler = {
122
- get(_target, prop) {
123
- if (typeof prop === "symbol") return void 0;
124
- const method = HTTP_METHODS[prop];
125
- if (method) {
126
- return (options) => fetchExecutor(baseUrl, path, method, defaultOptions, options);
127
- }
128
- return createProxyHandler(baseUrl, defaultOptions, [...path, prop], fetchExecutor);
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;
129
67
  }
130
- };
131
- return new Proxy({}, handler);
68
+ if (requestOptions?.revalidate !== void 0) {
69
+ nextFetchOptions.revalidate = requestOptions.revalidate;
70
+ }
71
+ nextRequestOptions.next = nextFetchOptions;
72
+ }
73
+ return (0, import_enlace_core.executeFetch)(
74
+ baseUrl,
75
+ path,
76
+ method,
77
+ { ...coreOptions, onSuccess: nextOnSuccess },
78
+ nextRequestOptions
79
+ );
132
80
  }
133
81
 
134
- // src/core/index.ts
135
- function createEnlace(baseUrl, defaultOptions = {}) {
136
- return createProxyHandler(baseUrl, defaultOptions);
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
+ );
137
91
  }
package/dist/index.mjs CHANGED
@@ -1,104 +1,73 @@
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
- }
1
+ // src/index.ts
2
+ export * from "enlace-core";
11
3
 
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
- }
4
+ // src/next/index.ts
5
+ import {
6
+ createProxyHandler
7
+ } from "enlace-core";
23
8
 
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
- }
9
+ // src/next/fetch.ts
10
+ import {
11
+ executeFetch
12
+ } from "enlace-core";
34
13
 
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
- };
14
+ // src/utils/generateTags.ts
15
+ function generateTags(path) {
16
+ return path.map((_, i) => path.slice(0, i + 1).join("/"));
72
17
  }
73
18
 
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);
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);
89
36
  }
90
- return createProxyHandler(baseUrl, defaultOptions, [...path, prop], fetchExecutor);
91
37
  }
38
+ onSuccess?.(payload);
92
39
  };
93
- return new Proxy({}, handler);
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;
46
+ }
47
+ if (requestOptions?.revalidate !== void 0) {
48
+ nextFetchOptions.revalidate = requestOptions.revalidate;
49
+ }
50
+ nextRequestOptions.next = nextFetchOptions;
51
+ }
52
+ return executeFetch(
53
+ baseUrl,
54
+ path,
55
+ method,
56
+ { ...coreOptions, onSuccess: nextOnSuccess },
57
+ nextRequestOptions
58
+ );
94
59
  }
95
60
 
96
- // src/core/index.ts
97
- function createEnlace(baseUrl, defaultOptions = {}) {
98
- return createProxyHandler(baseUrl, defaultOptions);
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
+ );
99
70
  }
100
71
  export {
101
- createEnlace,
102
- createProxyHandler,
103
- executeFetch
72
+ createEnlaceNext
104
73
  };
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.10",
4
+ "license": "MIT",
4
5
  "files": [
5
6
  "dist"
6
7
  ],
@@ -10,37 +11,22 @@
10
11
  "import": "./dist/index.mjs",
11
12
  "require": "./dist/index.js"
12
13
  },
13
- "./next": {
14
- "types": "./dist/next/index.d.ts",
15
- "import": "./dist/next/index.mjs",
16
- "require": "./dist/next/index.js"
14
+ "./hook": {
15
+ "types": "./dist/hook/index.d.ts",
16
+ "import": "./dist/hook/index.mjs",
17
+ "require": "./dist/hook/index.js"
17
18
  }
18
19
  },
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"
20
+ "dependencies": {
21
+ "enlace-core": "0.0.1-beta.8"
26
22
  },
27
23
  "peerDependencies": {
28
24
  "react": "^19"
29
25
  },
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"
26
+ "scripts": {
27
+ "dev": "tsup --watch",
28
+ "build": "tsup",
29
+ "typecheck": "tsc --noEmit",
30
+ "lint": "eslint src --max-warnings 0"
45
31
  }
46
- }
32
+ }