@navios/react-query 0.7.1 → 1.0.0-alpha.1

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.
Files changed (94) hide show
  1. package/CHANGELOG.md +161 -1
  2. package/README.md +152 -4
  3. package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
  4. package/dist/src/__tests__/errorSchema.spec.d.mts.map +1 -0
  5. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts +2 -0
  6. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts.map +1 -0
  7. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts +2 -0
  8. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts.map +1 -0
  9. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts +2 -0
  10. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts.map +1 -0
  11. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts +2 -0
  12. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts.map +1 -0
  13. package/dist/src/client/__type-tests__/query.spec-d.d.mts +2 -0
  14. package/dist/src/client/__type-tests__/query.spec-d.d.mts.map +1 -0
  15. package/dist/src/client/declare-client.d.mts +15 -8
  16. package/dist/src/client/declare-client.d.mts.map +1 -1
  17. package/dist/src/client/types/from-endpoint.d.mts +130 -0
  18. package/dist/src/client/types/from-endpoint.d.mts.map +1 -0
  19. package/dist/src/client/types/helpers.d.mts +74 -0
  20. package/dist/src/client/types/helpers.d.mts.map +1 -0
  21. package/dist/src/client/types/index.d.mts +21 -0
  22. package/dist/src/client/types/index.d.mts.map +1 -0
  23. package/dist/src/client/types/infinite-query.d.mts +61 -0
  24. package/dist/src/client/types/infinite-query.d.mts.map +1 -0
  25. package/dist/src/client/types/multipart-mutation.d.mts +98 -0
  26. package/dist/src/client/types/multipart-mutation.d.mts.map +1 -0
  27. package/dist/src/client/types/mutation.d.mts +75 -0
  28. package/dist/src/client/types/mutation.d.mts.map +1 -0
  29. package/dist/src/client/types/query.d.mts +65 -0
  30. package/dist/src/client/types/query.d.mts.map +1 -0
  31. package/dist/src/client/types.d.mts +1 -608
  32. package/dist/src/client/types.d.mts.map +1 -1
  33. package/dist/src/common/types.d.mts +30 -3
  34. package/dist/src/common/types.d.mts.map +1 -1
  35. package/dist/src/mutation/index.d.mts +1 -0
  36. package/dist/src/mutation/index.d.mts.map +1 -1
  37. package/dist/src/mutation/make-hook.d.mts +42 -16
  38. package/dist/src/mutation/make-hook.d.mts.map +1 -1
  39. package/dist/src/mutation/optimistic.d.mts +166 -0
  40. package/dist/src/mutation/optimistic.d.mts.map +1 -0
  41. package/dist/src/mutation/types.d.mts +51 -19
  42. package/dist/src/mutation/types.d.mts.map +1 -1
  43. package/dist/src/query/index.d.mts +1 -0
  44. package/dist/src/query/index.d.mts.map +1 -1
  45. package/dist/src/query/key-creator.d.mts.map +1 -1
  46. package/dist/src/query/make-infinite-options.d.mts +3 -2
  47. package/dist/src/query/make-infinite-options.d.mts.map +1 -1
  48. package/dist/src/query/make-options.d.mts +42 -12
  49. package/dist/src/query/make-options.d.mts.map +1 -1
  50. package/dist/src/query/prefetch.d.mts +245 -0
  51. package/dist/src/query/prefetch.d.mts.map +1 -0
  52. package/dist/src/query/types.d.mts +35 -17
  53. package/dist/src/query/types.d.mts.map +1 -1
  54. package/dist/tsconfig.tsbuildinfo +1 -1
  55. package/lib/index.cjs +445 -28
  56. package/lib/index.cjs.map +1 -1
  57. package/lib/index.d.cts +1022 -599
  58. package/lib/index.d.cts.map +1 -1
  59. package/lib/index.d.mts +1019 -596
  60. package/lib/index.d.mts.map +1 -1
  61. package/lib/index.mjs +441 -29
  62. package/lib/index.mjs.map +1 -1
  63. package/package.json +3 -3
  64. package/src/__tests__/declare-client.spec.mts +1 -2
  65. package/src/__tests__/errorSchema.spec.mts +391 -0
  66. package/src/__tests__/make-mutation.spec.mts +6 -5
  67. package/src/__tests__/makeDataTag.spec.mts +2 -1
  68. package/src/__tests__/makeQueryOptions.spec.mts +2 -1
  69. package/src/client/__type-tests__/from-endpoint.spec-d.mts +550 -0
  70. package/src/client/__type-tests__/infinite-query.spec-d.mts +648 -0
  71. package/src/client/__type-tests__/multipart-mutation.spec-d.mts +725 -0
  72. package/src/client/__type-tests__/mutation.spec-d.mts +757 -0
  73. package/src/client/__type-tests__/query.spec-d.mts +701 -0
  74. package/src/client/declare-client.mts +59 -34
  75. package/src/client/types/from-endpoint.mts +345 -0
  76. package/src/client/types/helpers.mts +140 -0
  77. package/src/client/types/index.mts +26 -0
  78. package/src/client/types/infinite-query.mts +133 -0
  79. package/src/client/types/multipart-mutation.mts +264 -0
  80. package/src/client/types/mutation.mts +176 -0
  81. package/src/client/types/query.mts +132 -0
  82. package/src/client/types.mts +1 -1935
  83. package/src/common/types.mts +48 -3
  84. package/src/mutation/index.mts +1 -0
  85. package/src/mutation/make-hook.mts +171 -63
  86. package/src/mutation/optimistic.mts +294 -0
  87. package/src/mutation/types.mts +102 -29
  88. package/src/query/index.mts +1 -0
  89. package/src/query/key-creator.mts +24 -13
  90. package/src/query/make-infinite-options.mts +53 -10
  91. package/src/query/make-options.mts +184 -43
  92. package/src/query/prefetch.mts +326 -0
  93. package/src/query/types.mts +76 -16
  94. package/src/client/__type-tests__/client-instance.spec-d.mts +0 -852
package/CHANGELOG.md CHANGED
@@ -1,6 +1,166 @@
1
1
  # Changelog
2
2
 
3
- ## [0.7.1] - 2025-12-20
3
+ ## [1.0.0-alpha.1] - 2026-01-07
4
+
5
+ ### Highlights
6
+
7
+ This is the first stable release of `@navios/react-query`. It includes major improvements to type safety, new helpers for common patterns like optimistic updates and SSR prefetching, and full support for discriminated union error handling.
8
+
9
+ ### Breaking Changes
10
+
11
+ - **Type file reorganization** - Client types have been split into modular files for better maintainability:
12
+ - `client/types.mts` → Split into `client/types/query.mts`, `client/types/mutation.mts`, `client/types/infinite-query.mts`, `client/types/multipart-mutation.mts`, `client/types/from-endpoint.mts`, and `client/types/helpers.mts`
13
+ - If you were importing internal types directly, update your imports to use the new paths or the re-exported types from the main entry point.
14
+
15
+ ### Added
16
+
17
+ #### Error Schema Support (Discriminated Union Mode)
18
+
19
+ Full support for `errorSchema` in queries and mutations when using `useDiscriminatorResponse: true` mode. API error responses are now returned as data (not thrown) and can be discriminated by status code:
20
+
21
+ ```typescript
22
+ const api = builder({ useDiscriminatorResponse: true })
23
+
24
+ const getUser = client.query({
25
+ method: 'GET',
26
+ url: '/users/$userId',
27
+ responseSchema: userSchema,
28
+ errorSchema: {
29
+ 400: z.object({ error: z.string(), code: z.number() }),
30
+ 404: z.object({ notFound: z.literal(true) }),
31
+ },
32
+ processResponse: (data) => {
33
+ // data is typed as: User | { error: string, code: number } | { notFound: true }
34
+ if ('error' in data) {
35
+ return { ok: false, error: data.error }
36
+ }
37
+ if ('notFound' in data) {
38
+ return { ok: false, error: 'User not found' }
39
+ }
40
+ return { ok: true, user: data }
41
+ },
42
+ })
43
+ ```
44
+
45
+ #### SSR/RSC Prefetch Helpers
46
+
47
+ New `createPrefetchHelper` and `createPrefetchHelpers` utilities for server-side rendering and React Server Components:
48
+
49
+ ```typescript
50
+ import { createPrefetchHelper, prefetchAll } from '@navios/react-query'
51
+ import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query'
52
+
53
+ // Create prefetch helper from query
54
+ const userPrefetch = createPrefetchHelper(getUser)
55
+
56
+ // Server Component
57
+ async function UserPage({ userId }: { userId: string }) {
58
+ const queryClient = new QueryClient()
59
+
60
+ await userPrefetch.prefetch(queryClient, { urlParams: { userId } })
61
+
62
+ return (
63
+ <HydrationBoundary state={dehydrate(queryClient)}>
64
+ <UserProfile userId={userId} />
65
+ </HydrationBoundary>
66
+ )
67
+ }
68
+ ```
69
+
70
+ **Prefetch helper methods:**
71
+
72
+ - `prefetch(queryClient, params)` - Prefetch data on the server
73
+ - `ensureData(queryClient, params)` - Ensure data exists, returns the data
74
+ - `getQueryOptions(params)` - Get raw query options for customization
75
+ - `prefetchMany(queryClient, paramsList)` - Prefetch multiple queries in parallel
76
+
77
+ **Batch prefetching:**
78
+
79
+ ```typescript
80
+ // Create multiple prefetch helpers at once
81
+ const prefetchers = createPrefetchHelpers({
82
+ user: getUser,
83
+ posts: getUserPosts,
84
+ })
85
+
86
+ // Or use prefetchAll for different queries
87
+ await prefetchAll(queryClient, [
88
+ { helper: userPrefetch, params: { urlParams: { userId } } },
89
+ {
90
+ helper: postsPrefetch,
91
+ params: { urlParams: { userId }, params: { limit: 10 } },
92
+ },
93
+ ])
94
+ ```
95
+
96
+ #### Optimistic Update Helpers
97
+
98
+ New `createOptimisticUpdate` and `createMultiOptimisticUpdate` utilities for type-safe optimistic updates:
99
+
100
+ ```typescript
101
+ import {
102
+ createMultiOptimisticUpdate,
103
+ createOptimisticUpdate,
104
+ } from '@navios/react-query'
105
+
106
+ const updateUser = client.mutation({
107
+ method: 'PATCH',
108
+ url: '/users/$userId',
109
+ requestSchema: updateUserSchema,
110
+ responseSchema: userSchema,
111
+ processResponse: (data) => data,
112
+ useContext: () => ({ queryClient: useQueryClient() }),
113
+ ...createOptimisticUpdate({
114
+ queryKey: ['users', userId],
115
+ updateFn: (oldData, variables) => ({
116
+ ...oldData,
117
+ ...variables.data,
118
+ }),
119
+ rollbackOnError: true, // default
120
+ invalidateOnSettled: true, // default
121
+ }),
122
+ })
123
+ ```
124
+
125
+ **Multi-query optimistic updates:**
126
+
127
+ ```typescript
128
+ const updateUser = client.mutation({
129
+ // ...
130
+ ...createMultiOptimisticUpdate([
131
+ {
132
+ queryKey: ['users', userId],
133
+ updateFn: (oldData, variables) => ({ ...oldData, ...variables.data }),
134
+ },
135
+ {
136
+ queryKey: ['users'],
137
+ updateFn: (oldList, variables) =>
138
+ (oldList ?? []).map((u) =>
139
+ u.id === userId ? { ...u, ...variables.data } : u,
140
+ ),
141
+ },
142
+ ]),
143
+ })
144
+ ```
145
+
146
+ #### New Type Exports
147
+
148
+ - `ComputeBaseResult<UseDiscriminator, Response, ErrorSchema>` - Compute result type based on discriminator mode
149
+ - `PrefetchHelper<TParams, TData, TError>` - Type for prefetch helper instances
150
+ - `QueryOptionsCreator<TParams, TData, TError>` - Type for query options creator functions
151
+ - `OptimisticUpdateConfig<TData, TVariables, TQueryData>` - Configuration for optimistic updates
152
+ - `OptimisticUpdateCallbacks<TData, TVariables, TQueryData>` - Return type from `createOptimisticUpdate`
153
+
154
+ ### Changed
155
+
156
+ - **Improved type inference** - Better type inference for `processResponse` callbacks, especially when using `errorSchema`
157
+ - **Modular type definitions** - Client types are now organized into separate files by concern:
158
+ - `types/query.mts` - Query-related types
159
+ - `types/mutation.mts` - Mutation-related types
160
+ - `types/infinite-query.mts` - Infinite query types
161
+ - `types/multipart-mutation.mts` - Multipart mutation types
162
+ - `types/from-endpoint.mts` - Types for `*FromEndpoint` methods
163
+ - `types/helpers.mts` - Helper types like `EndpointHelper`, `StreamHelper`, `ComputeBaseResult`
4
164
 
5
165
  ### Fixed
6
166
 
package/README.md CHANGED
@@ -9,7 +9,9 @@ Type-safe React Query integration for Navios API client with Zod schema validati
9
9
  - **React Query Integration** - Seamless integration with TanStack Query v5
10
10
  - **Declarative API** - Define endpoints once, use everywhere
11
11
  - **URL Parameters** - Built-in support for parameterized URLs (`/users/$userId`)
12
- - **Optimistic Updates** - First-class support via `onMutate` callback
12
+ - **Optimistic Updates** - First-class helpers via `createOptimisticUpdate`
13
+ - **SSR/RSC Support** - Built-in prefetch helpers for server-side rendering
14
+ - **Error Schema Support** - Type-safe error handling with discriminated unions
13
15
  - **Stream Support** - Handle file downloads and blob responses
14
16
 
15
17
  ## Installation
@@ -514,6 +516,148 @@ function AvatarUpload({ userId }: { userId: string }) {
514
516
  }
515
517
  ```
516
518
 
519
+ ## SSR/RSC Prefetch Helpers
520
+
521
+ For server-side rendering and React Server Components, use the prefetch helpers:
522
+
523
+ ```typescript
524
+ import { createPrefetchHelper, prefetchAll } from '@navios/react-query'
525
+ import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query'
526
+
527
+ // Create prefetch helper from your query
528
+ const userPrefetch = createPrefetchHelper(getUser)
529
+
530
+ // Server Component
531
+ async function UserPage({ userId }: { userId: string }) {
532
+ const queryClient = new QueryClient()
533
+
534
+ await userPrefetch.prefetch(queryClient, { urlParams: { userId } })
535
+
536
+ return (
537
+ <HydrationBoundary state={dehydrate(queryClient)}>
538
+ <UserProfile userId={userId} />
539
+ </HydrationBoundary>
540
+ )
541
+ }
542
+ ```
543
+
544
+ ### Batch Prefetching
545
+
546
+ ```typescript
547
+ // Create multiple helpers at once
548
+ const prefetchers = createPrefetchHelpers({
549
+ user: getUser,
550
+ posts: getUserPosts,
551
+ })
552
+
553
+ // Or prefetch different queries in parallel
554
+ await prefetchAll(queryClient, [
555
+ { helper: userPrefetch, params: { urlParams: { userId } } },
556
+ { helper: postsPrefetch, params: { urlParams: { userId }, params: { limit: 10 } } },
557
+ ])
558
+ ```
559
+
560
+ ### Helper Methods
561
+
562
+ - `prefetch(queryClient, params)` - Prefetch data on the server
563
+ - `ensureData(queryClient, params)` - Ensure data exists, returns the data
564
+ - `getQueryOptions(params)` - Get raw query options for customization
565
+ - `prefetchMany(queryClient, paramsList)` - Prefetch multiple queries in parallel
566
+
567
+ ## Optimistic Update Helpers
568
+
569
+ Simplify optimistic updates with the `createOptimisticUpdate` helper:
570
+
571
+ ```typescript
572
+ import { createOptimisticUpdate } from '@navios/react-query'
573
+
574
+ const updateUser = client.mutation({
575
+ method: 'PATCH',
576
+ url: '/users/$userId',
577
+ requestSchema: updateUserSchema,
578
+ responseSchema: userSchema,
579
+ processResponse: (data) => data,
580
+ useContext: () => ({ queryClient: useQueryClient() }),
581
+ ...createOptimisticUpdate({
582
+ queryKey: ['users', userId],
583
+ updateFn: (oldData, variables) => ({
584
+ ...oldData,
585
+ ...variables.data,
586
+ }),
587
+ rollbackOnError: true, // default
588
+ invalidateOnSettled: true, // default
589
+ }),
590
+ })
591
+ ```
592
+
593
+ ### Multi-Query Optimistic Updates
594
+
595
+ When a mutation affects multiple cached queries:
596
+
597
+ ```typescript
598
+ import { createMultiOptimisticUpdate } from '@navios/react-query'
599
+
600
+ const updateUser = client.mutation({
601
+ // ...config
602
+ ...createMultiOptimisticUpdate([
603
+ {
604
+ queryKey: ['users', userId],
605
+ updateFn: (oldData, variables) => ({ ...oldData, ...variables.data }),
606
+ },
607
+ {
608
+ queryKey: ['users'],
609
+ updateFn: (oldList, variables) =>
610
+ (oldList ?? []).map((u) =>
611
+ u.id === userId ? { ...u, ...variables.data } : u
612
+ ),
613
+ },
614
+ ]),
615
+ })
616
+ ```
617
+
618
+ ## Error Schema Support
619
+
620
+ When using `useDiscriminatorResponse: true` mode, API errors are returned as data instead of being thrown. This enables type-safe error discrimination:
621
+
622
+ ```typescript
623
+ const api = builder({ useDiscriminatorResponse: true })
624
+
625
+ const getUser = client.query({
626
+ method: 'GET',
627
+ url: '/users/$userId',
628
+ responseSchema: userSchema,
629
+ errorSchema: {
630
+ 400: z.object({ error: z.string(), code: z.number() }),
631
+ 404: z.object({ notFound: z.literal(true) }),
632
+ 500: z.object({ serverError: z.string() }),
633
+ },
634
+ processResponse: (data) => {
635
+ // data is typed as: User | { error: string, code: number } | { notFound: true } | { serverError: string }
636
+ if ('error' in data) {
637
+ return { ok: false as const, error: data.error }
638
+ }
639
+ if ('notFound' in data) {
640
+ return { ok: false as const, error: 'User not found' }
641
+ }
642
+ if ('serverError' in data) {
643
+ return { ok: false as const, error: data.serverError }
644
+ }
645
+ return { ok: true as const, user: data }
646
+ },
647
+ })
648
+
649
+ // In your component
650
+ function UserProfile({ userId }: { userId: string }) {
651
+ const result = getUser.useSuspense({ urlParams: { userId } })
652
+
653
+ if (!result.ok) {
654
+ return <ErrorMessage error={result.error} />
655
+ }
656
+
657
+ return <div>{result.user.name}</div>
658
+ }
659
+ ```
660
+
517
661
  ## API Reference
518
662
 
519
663
  ### `declareClient(options)`
@@ -594,11 +738,15 @@ The context passed to mutation callbacks includes:
594
738
  - `meta` - Mutation metadata
595
739
  - `onMutateResult` - Return value from `onMutate` (in `onSuccess`, `onError`, `onSettled`)
596
740
 
597
- ## Migration from 0.5.x
741
+ ## Migration to 1.0.0
742
+
743
+ See [CHANGELOG.md](./CHANGELOG.md) for full migration guide.
744
+
745
+ ### From 0.7.x
598
746
 
599
- See [CHANGELOG.md](./CHANGELOG.md) for migration guide from 0.5.x to 0.6.0.
747
+ - **Type file reorganization** - If importing internal types, update paths to use `client/types/*.mts`
600
748
 
601
- Key changes:
749
+ ### From 0.5.x to 0.6.x
602
750
 
603
751
  - Mutation callbacks now receive `(data, variables, context)` instead of `(queryClient, data, variables)`
604
752
  - Use `useContext` hook to provide `queryClient` and other dependencies
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=errorSchema.spec.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorSchema.spec.d.mts","sourceRoot":"","sources":["../../../src/__tests__/errorSchema.spec.mts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=from-endpoint.spec-d.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-endpoint.spec-d.d.mts","sourceRoot":"","sources":["../../../../src/client/__type-tests__/from-endpoint.spec-d.mts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=infinite-query.spec-d.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infinite-query.spec-d.d.mts","sourceRoot":"","sources":["../../../../src/client/__type-tests__/infinite-query.spec-d.mts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=multipart-mutation.spec-d.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multipart-mutation.spec-d.d.mts","sourceRoot":"","sources":["../../../../src/client/__type-tests__/multipart-mutation.spec-d.mts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=mutation.spec-d.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutation.spec-d.d.mts","sourceRoot":"","sources":["../../../../src/client/__type-tests__/mutation.spec-d.mts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=query.spec-d.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.spec-d.d.mts","sourceRoot":"","sources":["../../../../src/client/__type-tests__/query.spec-d.mts"],"names":[],"mappings":""}
@@ -1,30 +1,33 @@
1
- import type { HttpMethod } from '@navios/builder';
1
+ import type { ErrorSchemaRecord, HttpMethod } from '@navios/builder';
2
2
  import type { InfiniteData, MutationFunctionContext } from '@tanstack/react-query';
3
3
  import type { z, ZodObject, ZodType } from 'zod/v4';
4
4
  import type { ClientOptions, ProcessResponseFunction } from '../common/types.mjs';
5
5
  import type { MutationArgs } from '../mutation/types.mjs';
6
6
  import type { ClientInstance } from './types.mjs';
7
+ import type { ComputeBaseResult } from './types/helpers.mjs';
7
8
  /**
8
9
  * Configuration for declaring a query endpoint.
9
10
  */
10
- export interface QueryConfig<Method = HttpMethod, Url = string, QuerySchema = ZodObject, Response extends ZodType = ZodType, Result = z.output<Response>, RequestSchema = unknown> {
11
+ export interface QueryConfig<Method extends HttpMethod = HttpMethod, Url extends string = string, QuerySchema extends ZodObject | undefined = undefined, Response extends ZodType = ZodType, ErrorSchema extends ErrorSchemaRecord | undefined = undefined, Result = ComputeBaseResult<true, Response, ErrorSchema>, RequestSchema extends ZodType | undefined = undefined> {
11
12
  method: Method;
12
13
  url: Url;
13
14
  querySchema?: QuerySchema;
14
15
  responseSchema: Response;
16
+ errorSchema?: ErrorSchema;
15
17
  requestSchema?: RequestSchema;
16
- processResponse?: (data: z.output<Response>) => Result;
18
+ processResponse?: (data: ComputeBaseResult<true, Response, ErrorSchema>) => Result;
17
19
  }
18
20
  /**
19
21
  * Configuration for declaring an infinite query endpoint.
20
22
  */
21
- export type InfiniteQueryConfig<Method = HttpMethod, Url = string, QuerySchema extends ZodObject = ZodObject, Response extends ZodType = ZodType, PageResult = z.output<Response>, Result = InfiniteData<PageResult>, RequestSchema = unknown> = {
23
+ export type InfiniteQueryConfig<Method extends HttpMethod = HttpMethod, Url extends string = string, QuerySchema extends ZodObject = ZodObject, Response extends ZodType = ZodType, ErrorSchema extends ErrorSchemaRecord | undefined = undefined, PageResult = ComputeBaseResult<true, Response, ErrorSchema>, Result = InfiniteData<PageResult>, RequestSchema extends ZodType | undefined = undefined> = {
22
24
  method: Method;
23
25
  url: Url;
24
26
  querySchema: QuerySchema;
25
27
  responseSchema: Response;
28
+ errorSchema?: ErrorSchema;
26
29
  requestSchema?: RequestSchema;
27
- processResponse?: (data: z.output<Response>) => PageResult;
30
+ processResponse?: (data: ComputeBaseResult<true, Response, ErrorSchema>) => PageResult;
28
31
  select?: (data: InfiniteData<PageResult>) => Result;
29
32
  getNextPageParam: (lastPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema> | undefined;
30
33
  getPreviousPageParam?: (firstPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema>;
@@ -33,11 +36,12 @@ export type InfiniteQueryConfig<Method = HttpMethod, Url = string, QuerySchema e
33
36
  /**
34
37
  * Configuration for declaring a mutation endpoint.
35
38
  */
36
- export interface MutationConfig<Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'POST' | 'PUT' | 'PATCH' | 'DELETE', Url extends string = string, RequestSchema = Method extends 'DELETE' ? never : ZodObject, QuerySchema = unknown, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, TOnMutateResult = unknown, Context = unknown, UseKey extends boolean = false> {
39
+ export interface MutationConfig<Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'POST' | 'PUT' | 'PATCH' | 'DELETE', Url extends string = string, RequestSchema extends ZodType | undefined = Method extends 'DELETE' ? undefined : ZodType, QuerySchema extends ZodObject | undefined = undefined, Response extends ZodType = ZodType, ErrorSchema extends ErrorSchemaRecord | undefined = undefined, ReqResult = ComputeBaseResult<true, Response, ErrorSchema>, Result = unknown, TOnMutateResult = unknown, Context = unknown, UseKey extends boolean = false> {
37
40
  method: Method;
38
41
  url: Url;
39
42
  querySchema?: QuerySchema;
40
43
  responseSchema: Response;
44
+ errorSchema?: ErrorSchema;
41
45
  requestSchema?: RequestSchema;
42
46
  processResponse: ProcessResponseFunction<Result, ReqResult>;
43
47
  useContext?: () => Context;
@@ -57,12 +61,15 @@ export interface MutationConfig<Method extends 'POST' | 'PUT' | 'PATCH' | 'DELET
57
61
  /**
58
62
  * Creates a client instance for making type-safe queries and mutations.
59
63
  *
64
+ * @template UseDiscriminator - When `true`, errors are returned as union types.
65
+ * When `false` (default), errors are thrown and not included in TData.
66
+ *
60
67
  * @param options - Client configuration including the API builder and defaults
61
68
  * @returns A client instance with query, infiniteQuery, and mutation methods
62
69
  *
63
70
  * @example
64
71
  * ```typescript
65
- * const api = createBuilder({ baseUrl: '/api' });
72
+ * const api = builder({});
66
73
  * const client = declareClient({ api });
67
74
  *
68
75
  * const getUser = client.query({
@@ -75,5 +82,5 @@ export interface MutationConfig<Method extends 'POST' | 'PUT' | 'PATCH' | 'DELET
75
82
  * const { data } = useSuspenseQuery(getUser({ urlParams: { id: '123' } }));
76
83
  * ```
77
84
  */
78
- export declare function declareClient<Options extends ClientOptions>({ api, defaults, }: Options): ClientInstance;
85
+ export declare function declareClient<UseDiscriminator extends boolean = false>({ api, defaults, }: ClientOptions<UseDiscriminator>): ClientInstance<UseDiscriminator>;
79
86
  //# sourceMappingURL=declare-client.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"declare-client.d.mts","sourceRoot":"","sources":["../../../src/client/declare-client.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,UAAU,EACX,MAAM,iBAAiB,CAAA;AACxB,O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C,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAAC,gBAAgB,SAAS,OAAO,GAAG,KAAK,EAAE,EACtE,GAAG,EACH,QAAa,GACd,EAAE,aAAa,CAAC,gBAAgB,CAAC,GAAG,cAAc,CAAC,gBAAgB,CAAC,CAyNpE"}
@@ -0,0 +1,130 @@
1
+ import type { BaseEndpointOptions, EndpointOptions, InferEndpointParams, RequestArgs, Simplify, UrlHasParams, UrlParams } from '@navios/builder';
2
+ import type { DataTag, InfiniteData, MutationFunctionContext, UseMutationResult, UseSuspenseInfiniteQueryOptions, UseSuspenseQueryOptions } from '@tanstack/react-query';
3
+ import type { z, ZodObject, ZodType } from 'zod/v4';
4
+ import type { Split } from '../../common/types.mjs';
5
+ import type { MutationHelpers } from '../../mutation/types.mjs';
6
+ import type { QueryHelpers } from '../../query/types.mjs';
7
+ import type { ComputeBaseResult, EndpointHelper, StreamHelper } from './helpers.mjs';
8
+ /**
9
+ * Helper type to extract useKey from mutation options
10
+ */
11
+ type ExtractUseKey<Options> = Options extends {
12
+ useKey: infer U;
13
+ } ? U extends true ? true : false : false;
14
+ /**
15
+ * FromEndpoint methods using const generics pattern (simplified from multiple overloads).
16
+ *
17
+ * @template UseDiscriminator - When `true`, errors are returned as union types.
18
+ * When `false` (default), errors are thrown and not included in TData.
19
+ */
20
+ export interface ClientFromEndpointMethods<UseDiscriminator extends boolean = false> {
21
+ /**
22
+ * Creates a type-safe query from an existing endpoint with automatic type inference.
23
+ *
24
+ * Uses const generic pattern to infer types from the endpoint configuration.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const getUserEndpoint = api.declareEndpoint({
29
+ * method: 'GET',
30
+ * url: '/users/$userId',
31
+ * responseSchema: userSchema,
32
+ * })
33
+ *
34
+ * const getUser = client.queryFromEndpoint(getUserEndpoint, {
35
+ * processResponse: (data) => data,
36
+ * })
37
+ * ```
38
+ */
39
+ queryFromEndpoint<const Config extends EndpointOptions, TBaseResult = ComputeBaseResult<UseDiscriminator, Config['responseSchema'], Config['errorSchema']>, Result = TBaseResult>(endpoint: {
40
+ config: Config;
41
+ }, options?: {
42
+ processResponse?: (data: TBaseResult) => Result;
43
+ }): ((params: Simplify<InferEndpointParams<Config>>) => UseSuspenseQueryOptions<Result, Error, Result, DataTag<Split<Config['url'], '/'>, Result, Error>>) & QueryHelpers<Config['url'], Config['querySchema'] extends ZodObject ? Config['querySchema'] : undefined, Result, false, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined>;
44
+ /**
45
+ * Creates a type-safe infinite query from an existing endpoint with automatic type inference.
46
+ *
47
+ * Uses const generic pattern to infer types from the endpoint configuration.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const getUsersEndpoint = api.declareEndpoint({
52
+ * method: 'GET',
53
+ * url: '/users',
54
+ * querySchema: z.object({ page: z.number() }),
55
+ * responseSchema: z.array(userSchema),
56
+ * })
57
+ *
58
+ * const getUsers = client.infiniteQueryFromEndpoint(getUsersEndpoint, {
59
+ * getNextPageParam: (lastPage, allPages, lastPageParam) => {
60
+ * return lastPage.length > 0 ? { page: (lastPageParam?.page ?? 0) + 1 } : undefined
61
+ * },
62
+ * })
63
+ * ```
64
+ */
65
+ infiniteQueryFromEndpoint<const Config extends EndpointOptions & {
66
+ querySchema: ZodObject;
67
+ }, TBaseResult = ComputeBaseResult<UseDiscriminator, Config['responseSchema'], Config['errorSchema']>, PageResult = TBaseResult, Result = InfiniteData<PageResult>>(endpoint: {
68
+ config: Config;
69
+ }, options: {
70
+ processResponse?: (data: TBaseResult) => PageResult;
71
+ getNextPageParam: (lastPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<Config['querySchema']> | undefined, allPageParams: z.infer<Config['querySchema']>[] | undefined) => z.input<Config['querySchema']> | undefined;
72
+ getPreviousPageParam?: (firstPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<Config['querySchema']> | undefined, allPageParams: z.infer<Config['querySchema']>[] | undefined) => z.input<Config['querySchema']>;
73
+ }): ((params: Simplify<InferEndpointParams<Config>>) => UseSuspenseInfiniteQueryOptions<PageResult, Error, Result, DataTag<Split<Config['url'], '/'>, PageResult, Error>, z.output<Config['querySchema']>>) & QueryHelpers<Config['url'], Config['querySchema'], PageResult, true, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined>;
74
+ /**
75
+ * Creates a type-safe mutation from an existing endpoint with automatic type inference.
76
+ *
77
+ * Uses const generic pattern to infer types from the endpoint configuration.
78
+ * Handles both regular endpoints and stream endpoints.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const createUserEndpoint = api.declareEndpoint({
83
+ * method: 'POST',
84
+ * url: '/users',
85
+ * requestSchema: createUserSchema,
86
+ * responseSchema: userSchema,
87
+ * })
88
+ *
89
+ * const createUser = client.mutationFromEndpoint(createUserEndpoint, {
90
+ * processResponse: (data) => data,
91
+ * })
92
+ * ```
93
+ */
94
+ mutationFromEndpoint<const Config extends EndpointOptions | BaseEndpointOptions, TBaseResult = Config extends EndpointOptions ? ComputeBaseResult<UseDiscriminator, Config['responseSchema'], Config['errorSchema']> : Blob, Result = Config extends EndpointOptions ? ComputeBaseResult<UseDiscriminator, Config['responseSchema'], Config['errorSchema']> : Blob, OnMutateResult = unknown, Context = unknown>(endpoint: {
95
+ config: Config;
96
+ }, mutationOptions?: {
97
+ processResponse?: (data: TBaseResult) => Result | Promise<Result>;
98
+ useContext?: () => Context;
99
+ useKey?: boolean;
100
+ onMutate?: (variables: Simplify<RequestArgs<Config['url'], Config['querySchema'] extends ZodObject ? Config['querySchema'] : undefined, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined, Config['urlParamsSchema'] extends ZodObject ? Config['urlParamsSchema'] : undefined>>, context: Context & MutationFunctionContext) => OnMutateResult | Promise<OnMutateResult>;
101
+ onSuccess?: (data: NoInfer<Result>, variables: Simplify<RequestArgs<Config['url'], Config['querySchema'] extends ZodObject ? Config['querySchema'] : undefined, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined, Config['urlParamsSchema'] extends ZodObject ? Config['urlParamsSchema'] : undefined>>, context: Context & MutationFunctionContext & {
102
+ onMutateResult: OnMutateResult | undefined;
103
+ }) => void | Promise<void>;
104
+ onError?: (error: Error, variables: Simplify<RequestArgs<Config['url'], Config['querySchema'] extends ZodObject ? Config['querySchema'] : undefined, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined, Config['urlParamsSchema'] extends ZodObject ? Config['urlParamsSchema'] : undefined>>, context: Context & MutationFunctionContext & {
105
+ onMutateResult: OnMutateResult | undefined;
106
+ }) => void | Promise<void>;
107
+ onSettled?: (data: NoInfer<Result> | undefined, error: Error | null, variables: Simplify<RequestArgs<Config['url'], Config['querySchema'] extends ZodObject ? Config['querySchema'] : undefined, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined, Config['urlParamsSchema'] extends ZodObject ? Config['urlParamsSchema'] : undefined>>, context: Context & MutationFunctionContext & {
108
+ onMutateResult: OnMutateResult | undefined;
109
+ }) => void | Promise<void>;
110
+ }): ((...args: ExtractUseKey<typeof mutationOptions> extends true ? UrlHasParams<Config['url']> extends true ? [{
111
+ urlParams: UrlParams<Config['url']>;
112
+ }] : [] : []) => UseMutationResult<Result, Error, Simplify<RequestArgs<Config['url'], Config['querySchema'] extends ZodObject ? Config['querySchema'] : undefined, Config['requestSchema'] extends ZodType ? Config['requestSchema'] : undefined, Config['urlParamsSchema'] extends ZodObject ? Config['urlParamsSchema'] : undefined>>, OnMutateResult>) & (ExtractUseKey<typeof mutationOptions> extends true ? MutationHelpers<Config['url'], Result> : {}) & (Config extends EndpointOptions ? EndpointHelper<{
113
+ method: Config['method'];
114
+ url: Config['url'];
115
+ responseSchema: Config['responseSchema'];
116
+ querySchema: Config['querySchema'];
117
+ requestSchema: Config['requestSchema'];
118
+ errorSchema: Config['errorSchema'];
119
+ urlParamsSchema: Config['urlParamsSchema'];
120
+ }, UseDiscriminator> : StreamHelper<{
121
+ method: Config['method'];
122
+ url: Config['url'];
123
+ querySchema: Config['querySchema'];
124
+ requestSchema: Config['requestSchema'];
125
+ errorSchema: Config['errorSchema'];
126
+ urlParamsSchema: Config['urlParamsSchema'];
127
+ }, UseDiscriminator>);
128
+ }
129
+ export {};
130
+ //# sourceMappingURL=from-endpoint.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-endpoint.d.mts","sourceRoot":"","sources":["../../../../src/client/types/from-endpoint.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,SAAS,EACV,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EACV,OAAO,EACP,YAAY,EACZ,uBAAuB,EACvB,iBAAiB,EACjB,+BAA+B,EAC/B,uBAAuB,EACxB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EAAE,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAEnD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACzD,OAAO,KAAK,EACV,iBAAiB,EACjB,cAAc,EACd,YAAY,EACb,MAAM,eAAe,CAAA;AAEtB;;GAEG;AACH,KAAK,aAAa,CAAC,OAAO,IAAI,OAAO,SAAS;IAAE,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GAC7D,CAAC,SAAS,IAAI,GACZ,IAAI,GACJ,KAAK,GACP,KAAK,CAAA;AAET;;;;;GAKG;AACH,MAAM,WAAW,yBAAyB,CACxC,gBAAgB,SAAS,OAAO,GAAG,KAAK;IAExC;;;;;;;;;;;;;;;;;OAiBG;IACH,iBAAiB,CACf,KAAK,CAAC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