enlace 0.0.1-beta.1 → 0.0.1-beta.11

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,4 +1,4 @@
1
- import { WildcardClient, EnlaceClient, EnlaceResponse, EnlaceOptions } from 'enlace-core';
1
+ import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
2
2
  export * from 'enlace-core';
3
3
 
4
4
  /** Per-request options for React hooks */
@@ -11,51 +11,16 @@ type ReactRequestOptionsBase = {
11
11
  tags?: string[];
12
12
  /** Tags to invalidate after mutation (triggers refetch in matching queries) */
13
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>;
14
22
  };
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
-
23
+ /** Options for createEnlaceHookReact factory */
59
24
  type EnlaceHookOptions = {
60
25
  /**
61
26
  * Auto-generate cache tags from URL path for GET requests.
@@ -67,31 +32,57 @@ type EnlaceHookOptions = {
67
32
  autoRevalidateTags?: boolean;
68
33
  /** Time in ms before cached data is considered stale. @default 0 (always stale) */
69
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;
70
39
  };
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
- };
40
+
75
41
  /**
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 } })}
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
88
45
  */
89
- declare function createEnlaceHook<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: EnlaceHookOptions): EnlaceHook<TSchema>;
90
-
91
- type Listener = (tags: string[]) => void;
92
- declare function invalidateTags(tags: string[]): void;
93
- declare function onRevalidate(callback: Listener): () => void;
46
+ type ServerRevalidateHandler = (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
+ * serverRevalidator: (tags, paths) => revalidateServerAction(tags, paths)
56
+ * });
57
+ * ```
58
+ */
59
+ serverRevalidator?: ServerRevalidateHandler;
60
+ /**
61
+ * Skip server-side revalidation by default for all mutations.
62
+ * Individual requests can override with serverRevalidate: true.
63
+ * Useful for CSR-heavy apps where server cache invalidation is rarely needed.
64
+ * @default false
65
+ */
66
+ skipServerRevalidation?: boolean;
67
+ };
68
+ /** Per-request options for Next.js fetch - extends React's base options */
69
+ type NextRequestOptionsBase = ReactRequestOptionsBase & {
70
+ /** Time in seconds to revalidate, or false to disable */
71
+ revalidate?: number | false;
72
+ /**
73
+ * URL paths to revalidate after mutation.
74
+ * Passed to the serverRevalidator handler.
75
+ */
76
+ revalidatePaths?: string[];
77
+ /**
78
+ * Control server-side revalidation for this specific request.
79
+ * - true: Force server revalidation
80
+ * - false: Skip server revalidation
81
+ * When undefined, follows the global skipServerRevalidation setting.
82
+ */
83
+ serverRevalidate?: boolean;
84
+ };
94
85
 
95
- declare function clearCache(key?: string): void;
86
+ declare function createEnlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
96
87
 
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 };
88
+ export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { WildcardClient, EnlaceClient, EnlaceResponse, EnlaceOptions } from 'enlace-core';
1
+ import { EnlaceCallbackPayload, EnlaceErrorCallbackPayload, EnlaceCallbacks, EnlaceOptions, WildcardClient, EnlaceClient } from 'enlace-core';
2
2
  export * from 'enlace-core';
3
3
 
4
4
  /** Per-request options for React hooks */
@@ -11,51 +11,16 @@ type ReactRequestOptionsBase = {
11
11
  tags?: string[];
12
12
  /** Tags to invalidate after mutation (triggers refetch in matching queries) */
13
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>;
14
22
  };
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
-
23
+ /** Options for createEnlaceHookReact factory */
59
24
  type EnlaceHookOptions = {
60
25
  /**
61
26
  * Auto-generate cache tags from URL path for GET requests.
@@ -67,31 +32,57 @@ type EnlaceHookOptions = {
67
32
  autoRevalidateTags?: boolean;
68
33
  /** Time in ms before cached data is considered stale. @default 0 (always stale) */
69
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;
70
39
  };
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
- };
40
+
75
41
  /**
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 } })}
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
88
45
  */
89
- declare function createEnlaceHook<TSchema = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions, hookOptions?: EnlaceHookOptions): EnlaceHook<TSchema>;
90
-
91
- type Listener = (tags: string[]) => void;
92
- declare function invalidateTags(tags: string[]): void;
93
- declare function onRevalidate(callback: Listener): () => void;
46
+ type ServerRevalidateHandler = (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
+ * serverRevalidator: (tags, paths) => revalidateServerAction(tags, paths)
56
+ * });
57
+ * ```
58
+ */
59
+ serverRevalidator?: ServerRevalidateHandler;
60
+ /**
61
+ * Skip server-side revalidation by default for all mutations.
62
+ * Individual requests can override with serverRevalidate: true.
63
+ * Useful for CSR-heavy apps where server cache invalidation is rarely needed.
64
+ * @default false
65
+ */
66
+ skipServerRevalidation?: boolean;
67
+ };
68
+ /** Per-request options for Next.js fetch - extends React's base options */
69
+ type NextRequestOptionsBase = ReactRequestOptionsBase & {
70
+ /** Time in seconds to revalidate, or false to disable */
71
+ revalidate?: number | false;
72
+ /**
73
+ * URL paths to revalidate after mutation.
74
+ * Passed to the serverRevalidator handler.
75
+ */
76
+ revalidatePaths?: string[];
77
+ /**
78
+ * Control server-side revalidation for this specific request.
79
+ * - true: Force server revalidation
80
+ * - false: Skip server revalidation
81
+ * When undefined, follows the global skipServerRevalidation setting.
82
+ */
83
+ serverRevalidate?: boolean;
84
+ };
94
85
 
95
- declare function clearCache(key?: string): void;
86
+ declare function createEnlaceNext<TSchema = unknown, TDefaultError = unknown>(baseUrl: string, defaultOptions?: EnlaceOptions | null, nextOptions?: NextOptions): unknown extends TSchema ? WildcardClient<NextRequestOptionsBase> : EnlaceClient<TSchema, TDefaultError, NextRequestOptionsBase>;
96
87
 
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 };
88
+ export { type NextOptions, type NextRequestOptionsBase, createEnlaceNext };