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.
@@ -1,121 +0,0 @@
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>;
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
- type NextFetchOptions = {
106
- revalidate?: number | false;
107
- tags?: string[];
108
- };
109
- type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
110
- type NextEnlaceOptions = EnlaceOptions & {
111
- revalidate?: RevalidateHandler;
112
- };
113
- type NextRequestOptionsBase = {
114
- next?: NextFetchOptions;
115
- revalidateTags?: string[];
116
- revalidatePaths?: string[];
117
- };
118
-
119
- declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: NextEnlaceOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
120
-
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 };
@@ -1,121 +0,0 @@
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>;
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
- type NextFetchOptions = {
106
- revalidate?: number | false;
107
- tags?: string[];
108
- };
109
- type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
110
- type NextEnlaceOptions = EnlaceOptions & {
111
- revalidate?: RevalidateHandler;
112
- };
113
- type NextRequestOptionsBase = {
114
- next?: NextFetchOptions;
115
- revalidateTags?: string[];
116
- revalidatePaths?: string[];
117
- };
118
-
119
- declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: NextEnlaceOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
120
-
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 };
@@ -1,191 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
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
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/next/index.ts
31
- var next_exports = {};
32
- __export(next_exports, {
33
- createEnlace: () => createEnlace
34
- });
35
- 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
- }
131
-
132
- // src/next/fetch.ts
133
- function generateTags(path) {
134
- return path.map((_, i) => path.slice(0, i + 1).join("/"));
135
- }
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);
141
- const fetchOptions = {
142
- ...defaultOptions,
143
- method
144
- };
145
- if (requestOptions?.cache) {
146
- fetchOptions.cache = requestOptions.cache;
147
- }
148
- const nextFetchOptions = { tags };
149
- if (nextOptions?.revalidate !== void 0) {
150
- nextFetchOptions.revalidate = nextOptions.revalidate;
151
- }
152
- fetchOptions.next = nextFetchOptions;
153
- if (headers) {
154
- fetchOptions.headers = headers;
155
- }
156
- if (requestOptions?.body !== void 0) {
157
- if (isJsonBody(requestOptions.body)) {
158
- fetchOptions.body = JSON.stringify(requestOptions.body);
159
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
160
- if (headers) {
161
- fetchOptions.headers = headers;
162
- }
163
- } else {
164
- fetchOptions.body = requestOptions.body;
165
- }
166
- }
167
- const response = await fetch(url, fetchOptions);
168
- const contentType = response.headers.get("content-type");
169
- const isJson = contentType?.includes("application/json");
170
- if (response.ok) {
171
- const { revalidateTags = [], revalidatePaths = [] } = requestOptions ?? {};
172
- if (revalidateTags.length || revalidatePaths.length) {
173
- defaultOptions.revalidate?.(revalidateTags, revalidatePaths);
174
- }
175
- return {
176
- ok: true,
177
- status: response.status,
178
- data: isJson ? await response.json() : response
179
- };
180
- }
181
- return {
182
- ok: false,
183
- status: response.status,
184
- error: isJson ? await response.json() : response
185
- };
186
- }
187
-
188
- // src/next/index.ts
189
- function createEnlace(baseUrl, defaultOptions = {}) {
190
- return createProxyHandler(baseUrl, defaultOptions, [], executeNextFetch);
191
- }
@@ -1,158 +0,0 @@
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
- }
95
-
96
- // src/next/fetch.ts
97
- function generateTags(path) {
98
- return path.map((_, i) => path.slice(0, i + 1).join("/"));
99
- }
100
- async function executeNextFetch(baseUrl, path, method, defaultOptions, requestOptions) {
101
- 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);
105
- const fetchOptions = {
106
- ...defaultOptions,
107
- method
108
- };
109
- if (requestOptions?.cache) {
110
- fetchOptions.cache = requestOptions.cache;
111
- }
112
- const nextFetchOptions = { tags };
113
- if (nextOptions?.revalidate !== void 0) {
114
- nextFetchOptions.revalidate = nextOptions.revalidate;
115
- }
116
- fetchOptions.next = nextFetchOptions;
117
- if (headers) {
118
- fetchOptions.headers = headers;
119
- }
120
- if (requestOptions?.body !== void 0) {
121
- if (isJsonBody(requestOptions.body)) {
122
- fetchOptions.body = JSON.stringify(requestOptions.body);
123
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
124
- if (headers) {
125
- fetchOptions.headers = headers;
126
- }
127
- } else {
128
- fetchOptions.body = requestOptions.body;
129
- }
130
- }
131
- const response = await fetch(url, fetchOptions);
132
- const contentType = response.headers.get("content-type");
133
- const isJson = contentType?.includes("application/json");
134
- if (response.ok) {
135
- const { revalidateTags = [], revalidatePaths = [] } = requestOptions ?? {};
136
- if (revalidateTags.length || revalidatePaths.length) {
137
- defaultOptions.revalidate?.(revalidateTags, revalidatePaths);
138
- }
139
- return {
140
- ok: true,
141
- status: response.status,
142
- data: isJson ? await response.json() : response
143
- };
144
- }
145
- return {
146
- ok: false,
147
- status: response.status,
148
- error: isJson ? await response.json() : response
149
- };
150
- }
151
-
152
- // src/next/index.ts
153
- function createEnlace(baseUrl, defaultOptions = {}) {
154
- return createProxyHandler(baseUrl, defaultOptions, [], executeNextFetch);
155
- }
156
- export {
157
- createEnlace
158
- };