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.
package/dist/index.d.mts CHANGED
@@ -1,61 +1,38 @@
1
- import { WildcardClient, EnlaceClient, EnlaceResponse, EnlaceOptions } from 'enlace-core';
1
+ import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, CoreRequestOptionsBase, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
2
2
  export * from 'enlace-core';
3
3
 
4
- /** Per-request options for React hooks */
5
- type ReactRequestOptionsBase = {
4
+ /**
5
+ * Per-request options for React hooks.
6
+ * Extends CoreRequestOptionsBase which provides conditional params support.
7
+ * The params option is only available when accessing dynamic URL segments (via _ or index access).
8
+ */
9
+ type ReactRequestOptionsBase = CoreRequestOptionsBase & {
6
10
  /**
7
11
  * Cache tags for caching (GET requests only)
8
12
  * This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
9
13
  * But can be manually specified to override auto-generation.
10
14
  * */
11
15
  tags?: string[];
16
+ /**
17
+ * Additional cache tags to merge with auto-generated tags.
18
+ * Use this when you want to extend (not replace) the auto-generated tags.
19
+ * @example
20
+ * api.posts.$get({ additionalTags: ['custom-tag'] })
21
+ * // If autoGenerateTags produces ['posts'], final tags: ['posts', 'custom-tag']
22
+ */
23
+ additionalTags?: string[];
12
24
  /** Tags to invalidate after mutation (triggers refetch in matching queries) */
13
25
  revalidateTags?: string[];
26
+ /**
27
+ * Additional revalidation tags to merge with auto-generated tags.
28
+ * Use this when you want to extend (not replace) the auto-generated revalidation tags.
29
+ * @example
30
+ * api.posts.$post({ body: {...}, additionalRevalidateTags: ['other-tag'] })
31
+ * // If autoRevalidateTags produces ['posts'], final tags: ['posts', 'other-tag']
32
+ */
33
+ additionalRevalidateTags?: string[];
14
34
  };
15
- type ApiClient<TSchema, TOptions = ReactRequestOptionsBase> = unknown extends TSchema ? WildcardClient<TOptions> : EnlaceClient<TSchema, TOptions>;
16
- type QueryFn<TSchema, TData, TError, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
17
- type SelectorFn<TSchema, TMethod, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => TMethod;
18
- type HookState = {
19
- loading: boolean;
20
- fetching: boolean;
21
- ok: boolean | undefined;
22
- data: unknown;
23
- error: unknown;
24
- };
25
- type TrackedCall = {
26
- path: string[];
27
- method: string;
28
- options: unknown;
29
- };
30
- declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete"];
31
- type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
32
- type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
33
- /** Discriminated union for hook response state - enables type narrowing on ok check */
34
- type HookResponseState<TData, TError> = {
35
- ok: undefined;
36
- data: undefined;
37
- error: undefined;
38
- } | {
39
- ok: true;
40
- data: TData;
41
- error: undefined;
42
- } | {
43
- ok: false;
44
- data: undefined;
45
- error: TError;
46
- };
47
- /** Result when hook is called with query function (auto-fetch mode) */
48
- type UseEnlaceQueryResult<TData, TError> = {
49
- loading: boolean;
50
- fetching: boolean;
51
- } & HookResponseState<TData, TError>;
52
- /** Result when hook is called with method selector (trigger mode) */
53
- type UseEnlaceSelectorResult<TMethod> = {
54
- trigger: TMethod;
55
- loading: boolean;
56
- fetching: boolean;
57
- } & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
58
-
35
+ /** Options for enlaceHookReact factory */
59
36
  type EnlaceHookOptions = {
60
37
  /**
61
38
  * Auto-generate cache tags from URL path for GET requests.
@@ -67,31 +44,59 @@ type EnlaceHookOptions = {
67
44
  autoRevalidateTags?: boolean;
68
45
  /** Time in ms before cached data is considered stale. @default 0 (always stale) */
69
46
  staleTime?: number;
47
+ /** Callback called on successful API responses */
48
+ onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
49
+ /** Callback called on error responses (HTTP errors or network failures) */
50
+ onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
70
51
  };
71
- type EnlaceHook<TSchema> = {
72
- <TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod>): UseEnlaceSelectorResult<TMethod>;
73
- <TData, TError>(queryFn: QueryFn<TSchema, TData, TError>): UseEnlaceQueryResult<TData, TError>;
74
- };
52
+
75
53
  /**
76
- * Creates a React hook for making API calls.
77
- * Called at module level to create a reusable hook.
78
- *
79
- * @example
80
- * const useAPI = createEnlaceHook<ApiSchema>('https://api.com');
81
- *
82
- * // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
83
- * const { loading, data, error } = useAPI((api) => api.posts.get({ query: { userId } }));
84
- *
85
- * // Selector mode - typed trigger for lazy calls
86
- * const { trigger, loading, data, error } = useAPI((api) => api.posts.delete);
87
- * onClick={() => trigger({ body: { id: 1 } })}
54
+ * Handler function called after successful mutations to trigger server-side revalidation.
55
+ * @param tags - Cache tags to revalidate
56
+ * @param paths - URL paths to revalidate
88
57
  */
89
- declare function createEnlaceHook<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: EnlaceHookOptions): EnlaceHook<TSchema>;
58
+ type ServerRevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
59
+ /** Next.js-specific options (third argument for enlaceNext) */
60
+ type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
61
+ /**
62
+ * Handler called after successful mutations to trigger server-side revalidation.
63
+ * Receives auto-generated or manually specified tags and paths.
64
+ * @example
65
+ * ```ts
66
+ * enlaceNext("http://localhost:3000/api/", {}, {
67
+ * serverRevalidator: (tags, paths) => revalidateServerAction(tags, paths)
68
+ * });
69
+ * ```
70
+ */
71
+ serverRevalidator?: ServerRevalidateHandler;
72
+ /**
73
+ * Skip server-side revalidation by default for all mutations.
74
+ * Individual requests can override with serverRevalidate: true.
75
+ * Useful for CSR-heavy apps where server cache invalidation is rarely needed.
76
+ * @default false
77
+ */
78
+ skipServerRevalidation?: boolean;
79
+ };
80
+ /** Per-request options for Next.js fetch - extends React's base options */
81
+ type NextRequestOptionsBase = ReactRequestOptionsBase & {
82
+ /** Time in seconds to revalidate, or false to disable */
83
+ revalidate?: number | false;
84
+ /**
85
+ * URL paths to revalidate after mutation.
86
+ * Passed to the serverRevalidator handler.
87
+ */
88
+ revalidatePaths?: string[];
89
+ /**
90
+ * Control server-side revalidation for this specific request.
91
+ * - true: Force server revalidation
92
+ * - false: Skip server revalidation
93
+ * When undefined, follows the global skipServerRevalidation setting.
94
+ */
95
+ serverRevalidate?: boolean;
96
+ };
90
97
 
91
- type Listener = (tags: string[]) => void;
92
- declare function invalidateTags(tags: string[]): void;
93
- declare function onRevalidate(callback: Listener): () => void;
98
+ declare function enlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
94
99
 
95
- declare function clearCache(key?: string): void;
100
+ declare function invalidateTags(tags: string[]): void;
96
101
 
97
- export { type ApiClient, type EnlaceHookOptions, HTTP_METHODS, type HookState, type QueryFn, type ReactRequestOptionsBase, type SelectorFn, type TrackedCall, type UseEnlaceQueryResult, type UseEnlaceSelectorResult, clearCache, createEnlaceHook, invalidateTags, onRevalidate };
102
+ export { type NextOptions, type NextRequestOptionsBase, enlaceNext, invalidateTags };
package/dist/index.d.ts CHANGED
@@ -1,61 +1,38 @@
1
- import { WildcardClient, EnlaceClient, EnlaceResponse, EnlaceOptions } from 'enlace-core';
1
+ import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, CoreRequestOptionsBase, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
2
2
  export * from 'enlace-core';
3
3
 
4
- /** Per-request options for React hooks */
5
- type ReactRequestOptionsBase = {
4
+ /**
5
+ * Per-request options for React hooks.
6
+ * Extends CoreRequestOptionsBase which provides conditional params support.
7
+ * The params option is only available when accessing dynamic URL segments (via _ or index access).
8
+ */
9
+ type ReactRequestOptionsBase = CoreRequestOptionsBase & {
6
10
  /**
7
11
  * Cache tags for caching (GET requests only)
8
12
  * This will auto generate tags from the URL path if not provided and autoGenerateTags is enabled.
9
13
  * But can be manually specified to override auto-generation.
10
14
  * */
11
15
  tags?: string[];
16
+ /**
17
+ * Additional cache tags to merge with auto-generated tags.
18
+ * Use this when you want to extend (not replace) the auto-generated tags.
19
+ * @example
20
+ * api.posts.$get({ additionalTags: ['custom-tag'] })
21
+ * // If autoGenerateTags produces ['posts'], final tags: ['posts', 'custom-tag']
22
+ */
23
+ additionalTags?: string[];
12
24
  /** Tags to invalidate after mutation (triggers refetch in matching queries) */
13
25
  revalidateTags?: string[];
26
+ /**
27
+ * Additional revalidation tags to merge with auto-generated tags.
28
+ * Use this when you want to extend (not replace) the auto-generated revalidation tags.
29
+ * @example
30
+ * api.posts.$post({ body: {...}, additionalRevalidateTags: ['other-tag'] })
31
+ * // If autoRevalidateTags produces ['posts'], final tags: ['posts', 'other-tag']
32
+ */
33
+ additionalRevalidateTags?: string[];
14
34
  };
15
- type ApiClient<TSchema, TOptions = ReactRequestOptionsBase> = unknown extends TSchema ? WildcardClient<TOptions> : EnlaceClient<TSchema, TOptions>;
16
- type QueryFn<TSchema, TData, TError, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => Promise<EnlaceResponse<TData, TError>>;
17
- type SelectorFn<TSchema, TMethod, TOptions = ReactRequestOptionsBase> = (api: ApiClient<TSchema, TOptions>) => TMethod;
18
- type HookState = {
19
- loading: boolean;
20
- fetching: boolean;
21
- ok: boolean | undefined;
22
- data: unknown;
23
- error: unknown;
24
- };
25
- type TrackedCall = {
26
- path: string[];
27
- method: string;
28
- options: unknown;
29
- };
30
- declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete"];
31
- type ExtractData<T> = T extends (...args: any[]) => Promise<EnlaceResponse<infer D, unknown>> ? D : never;
32
- type ExtractError<T> = T extends (...args: any[]) => Promise<EnlaceResponse<unknown, infer E>> ? E : never;
33
- /** Discriminated union for hook response state - enables type narrowing on ok check */
34
- type HookResponseState<TData, TError> = {
35
- ok: undefined;
36
- data: undefined;
37
- error: undefined;
38
- } | {
39
- ok: true;
40
- data: TData;
41
- error: undefined;
42
- } | {
43
- ok: false;
44
- data: undefined;
45
- error: TError;
46
- };
47
- /** Result when hook is called with query function (auto-fetch mode) */
48
- type UseEnlaceQueryResult<TData, TError> = {
49
- loading: boolean;
50
- fetching: boolean;
51
- } & HookResponseState<TData, TError>;
52
- /** Result when hook is called with method selector (trigger mode) */
53
- type UseEnlaceSelectorResult<TMethod> = {
54
- trigger: TMethod;
55
- loading: boolean;
56
- fetching: boolean;
57
- } & HookResponseState<ExtractData<TMethod>, ExtractError<TMethod>>;
58
-
35
+ /** Options for enlaceHookReact factory */
59
36
  type EnlaceHookOptions = {
60
37
  /**
61
38
  * Auto-generate cache tags from URL path for GET requests.
@@ -67,31 +44,59 @@ type EnlaceHookOptions = {
67
44
  autoRevalidateTags?: boolean;
68
45
  /** Time in ms before cached data is considered stale. @default 0 (always stale) */
69
46
  staleTime?: number;
47
+ /** Callback called on successful API responses */
48
+ onSuccess?: (payload: EnlaceCallbackPayload<unknown>) => void;
49
+ /** Callback called on error responses (HTTP errors or network failures) */
50
+ onError?: (payload: EnlaceErrorCallbackPayload<unknown>) => void;
70
51
  };
71
- type EnlaceHook<TSchema> = {
72
- <TMethod extends (...args: any[]) => Promise<EnlaceResponse<unknown, unknown>>>(selector: SelectorFn<TSchema, TMethod>): UseEnlaceSelectorResult<TMethod>;
73
- <TData, TError>(queryFn: QueryFn<TSchema, TData, TError>): UseEnlaceQueryResult<TData, TError>;
74
- };
52
+
75
53
  /**
76
- * Creates a React hook for making API calls.
77
- * Called at module level to create a reusable hook.
78
- *
79
- * @example
80
- * const useAPI = createEnlaceHook<ApiSchema>('https://api.com');
81
- *
82
- * // Query mode - auto-fetch (auto-tracks changes, no deps array needed)
83
- * const { loading, data, error } = useAPI((api) => api.posts.get({ query: { userId } }));
84
- *
85
- * // Selector mode - typed trigger for lazy calls
86
- * const { trigger, loading, data, error } = useAPI((api) => api.posts.delete);
87
- * onClick={() => trigger({ body: { id: 1 } })}
54
+ * Handler function called after successful mutations to trigger server-side revalidation.
55
+ * @param tags - Cache tags to revalidate
56
+ * @param paths - URL paths to revalidate
88
57
  */
89
- declare function createEnlaceHook<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: EnlaceHookOptions): EnlaceHook<TSchema>;
58
+ type ServerRevalidateHandler = (tags: string[], paths: string[]) => void | Promise<void>;
59
+ /** Next.js-specific options (third argument for enlaceNext) */
60
+ type NextOptions = Pick<EnlaceHookOptions, "autoGenerateTags" | "autoRevalidateTags"> & EnlaceCallbacks & {
61
+ /**
62
+ * Handler called after successful mutations to trigger server-side revalidation.
63
+ * Receives auto-generated or manually specified tags and paths.
64
+ * @example
65
+ * ```ts
66
+ * enlaceNext("http://localhost:3000/api/", {}, {
67
+ * serverRevalidator: (tags, paths) => revalidateServerAction(tags, paths)
68
+ * });
69
+ * ```
70
+ */
71
+ serverRevalidator?: ServerRevalidateHandler;
72
+ /**
73
+ * Skip server-side revalidation by default for all mutations.
74
+ * Individual requests can override with serverRevalidate: true.
75
+ * Useful for CSR-heavy apps where server cache invalidation is rarely needed.
76
+ * @default false
77
+ */
78
+ skipServerRevalidation?: boolean;
79
+ };
80
+ /** Per-request options for Next.js fetch - extends React's base options */
81
+ type NextRequestOptionsBase = ReactRequestOptionsBase & {
82
+ /** Time in seconds to revalidate, or false to disable */
83
+ revalidate?: number | false;
84
+ /**
85
+ * URL paths to revalidate after mutation.
86
+ * Passed to the serverRevalidator handler.
87
+ */
88
+ revalidatePaths?: string[];
89
+ /**
90
+ * Control server-side revalidation for this specific request.
91
+ * - true: Force server revalidation
92
+ * - false: Skip server revalidation
93
+ * When undefined, follows the global skipServerRevalidation setting.
94
+ */
95
+ serverRevalidate?: boolean;
96
+ };
90
97
 
91
- type Listener = (tags: string[]) => void;
92
- declare function invalidateTags(tags: string[]): void;
93
- declare function onRevalidate(callback: Listener): () => void;
98
+ declare function enlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
94
99
 
95
- declare function clearCache(key?: string): void;
100
+ declare function invalidateTags(tags: string[]): void;
96
101
 
97
- export { type ApiClient, type EnlaceHookOptions, HTTP_METHODS, type HookState, type QueryFn, type ReactRequestOptionsBase, type SelectorFn, type TrackedCall, type UseEnlaceQueryResult, type UseEnlaceSelectorResult, clearCache, createEnlaceHook, invalidateTags, onRevalidate };
102
+ export { type NextOptions, type NextRequestOptionsBase, enlaceNext, invalidateTags };