enlace 0.0.1-beta.2 → 0.0.1-beta.20

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,74 +0,0 @@
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[];
15
- };
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;
28
- };
29
-
30
- /**
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
34
- */
35
- type RevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
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;
49
- };
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
- */
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;
70
- };
71
-
72
- declare function createEnlace<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, NextRequestOptionsBase>;
73
-
74
- export { type NextHookOptions, type NextOptions, type NextRequestOptionsBase, type RevalidateHandler, createEnlace };
@@ -1,111 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
-
21
- // src/next/index.ts
22
- var next_exports = {};
23
- __export(next_exports, {
24
- createEnlace: () => createEnlace
25
- });
26
- module.exports = __toCommonJS(next_exports);
27
- var import_enlace_core2 = require("enlace-core");
28
-
29
- // src/next/fetch.ts
30
- var import_enlace_core = require("enlace-core");
31
-
32
- // src/utils/generateTags.ts
33
- function generateTags(path) {
34
- return path.map((_, i) => path.slice(0, i + 1).join("/"));
35
- }
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);
50
- const fetchOptions = {
51
- ...restOptions,
52
- method
53
- };
54
- if (requestOptions?.cache) {
55
- fetchOptions.cache = requestOptions.cache;
56
- }
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;
67
- }
68
- if (headers) {
69
- fetchOptions.headers = headers;
70
- }
71
- if (requestOptions?.body !== void 0) {
72
- if ((0, import_enlace_core.isJsonBody)(requestOptions.body)) {
73
- fetchOptions.body = JSON.stringify(requestOptions.body);
74
- headers = (0, import_enlace_core.mergeHeaders)(headers, { "Content-Type": "application/json" });
75
- if (headers) {
76
- fetchOptions.headers = headers;
77
- }
78
- } else {
79
- fetchOptions.body = requestOptions.body;
80
- }
81
- }
82
- const response = await fetch(url, fetchOptions);
83
- const contentType = response.headers.get("content-type");
84
- const isJson = contentType?.includes("application/json");
85
- if (response.ok) {
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
- }
92
- }
93
- return {
94
- ok: true,
95
- status: response.status,
96
- data: isJson ? await response.json() : response
97
- };
98
- }
99
- return {
100
- ok: false,
101
- status: response.status,
102
- error: isJson ? await response.json() : response
103
- };
104
- }
105
-
106
- // src/next/index.ts
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);
111
- }
@@ -1,95 +0,0 @@
1
- // src/next/index.ts
2
- import {
3
- createProxyHandler
4
- } from "enlace-core";
5
-
6
- // src/next/fetch.ts
7
- import {
8
- buildUrl,
9
- isJsonBody,
10
- mergeHeaders
11
- } from "enlace-core";
12
-
13
- // src/utils/generateTags.ts
14
- function generateTags(path) {
15
- return path.map((_, i) => path.slice(0, i + 1).join("/"));
16
- }
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;
27
- const url = buildUrl(baseUrl, path, requestOptions?.query);
28
- let headers = mergeHeaders(defaultHeaders, requestOptions?.headers);
29
- const isGet = method === "GET";
30
- const autoTags = generateTags(path);
31
- const fetchOptions = {
32
- ...restOptions,
33
- method
34
- };
35
- if (requestOptions?.cache) {
36
- fetchOptions.cache = requestOptions.cache;
37
- }
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;
48
- }
49
- if (headers) {
50
- fetchOptions.headers = headers;
51
- }
52
- if (requestOptions?.body !== void 0) {
53
- if (isJsonBody(requestOptions.body)) {
54
- fetchOptions.body = JSON.stringify(requestOptions.body);
55
- headers = mergeHeaders(headers, { "Content-Type": "application/json" });
56
- if (headers) {
57
- fetchOptions.headers = headers;
58
- }
59
- } else {
60
- fetchOptions.body = requestOptions.body;
61
- }
62
- }
63
- const response = await fetch(url, fetchOptions);
64
- const contentType = response.headers.get("content-type");
65
- const isJson = contentType?.includes("application/json");
66
- if (response.ok) {
67
- if (!isGet && !requestOptions?.skipRevalidator) {
68
- const revalidateTags = requestOptions?.revalidateTags ?? (autoRevalidateTags ? autoTags : []);
69
- const revalidatePaths = requestOptions?.revalidatePaths ?? [];
70
- if (revalidateTags.length || revalidatePaths.length) {
71
- revalidator?.(revalidateTags, revalidatePaths);
72
- }
73
- }
74
- return {
75
- ok: true,
76
- status: response.status,
77
- data: isJson ? await response.json() : response
78
- };
79
- }
80
- return {
81
- ok: false,
82
- status: response.status,
83
- error: isJson ? await response.json() : response
84
- };
85
- }
86
-
87
- // src/next/index.ts
88
- export * from "enlace-core";
89
- function createEnlace(baseUrl, defaultOptions = {}, nextOptions = {}) {
90
- const combinedOptions = { ...defaultOptions, ...nextOptions };
91
- return createProxyHandler(baseUrl, combinedOptions, [], executeNextFetch);
92
- }
93
- export {
94
- createEnlace
95
- };