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/LICENSE +21 -0
- package/README.md +728 -0
- package/dist/hook/index.d.mts +173 -0
- package/dist/hook/index.d.ts +173 -0
- package/dist/hook/index.js +516 -0
- package/dist/hook/index.mjs +498 -0
- package/dist/index.d.mts +75 -113
- package/dist/index.d.ts +75 -113
- package/dist/index.js +55 -101
- package/dist/index.mjs +60 -91
- package/package.json +14 -28
- package/dist/next/index.d.mts +0 -121
- package/dist/next/index.d.ts +0 -121
- package/dist/next/index.js +0 -191
- package/dist/next/index.mjs +0 -158
package/dist/index.d.ts
CHANGED
|
@@ -1,121 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
*
|
|
29
|
-
*
|
|
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
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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/
|
|
40
|
-
var
|
|
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/
|
|
51
|
-
|
|
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/
|
|
63
|
-
function
|
|
64
|
-
|
|
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/
|
|
74
|
-
async function
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
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/
|
|
135
|
-
function
|
|
136
|
-
|
|
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/
|
|
2
|
-
|
|
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/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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/
|
|
36
|
-
|
|
37
|
-
|
|
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/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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/
|
|
97
|
-
function
|
|
98
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
-
"./
|
|
14
|
-
"types": "./dist/
|
|
15
|
-
"import": "./dist/
|
|
16
|
-
"require": "./dist/
|
|
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
|
-
"
|
|
20
|
-
|
|
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
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
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
|
+
}
|