1
- import { createClient , createConfig } from "@hey-api/client-next" ;
2
- import { queryOptions } from "@tanstack/react-query" ;
3
-
4
- import type { Client , ClientOptions , CreateClientConfig , Options as HeyApiOptions , TDataShape } from "@hey-api/client-next" ;
5
-
6
- export type Options < TData extends TDataShape = TDataShape , TThrowOnError extends boolean = boolean > = HeyApiOptions < TData , TThrowOnError > & {
7
- /**
8
- * You can provide a client instance returned by `createClient()` instead of
9
- * individual options. This might be also useful if you want to implement a
10
- * custom client.
11
- */
12
- client ?: Client ;
13
- /**
14
- * You can pass arbitrary values through the `meta` object. This can be
15
- * used to access values that aren't defined as part of the SDK function.
16
- */
17
- meta ?: Record < string , unknown > ;
18
- } ;
19
-
1
+ import { createClient , createConfig } from '@hey-api/client-next'
2
+ import { queryOptions } from '@tanstack/react-query'
3
+
4
+ import type {
5
+ Client ,
6
+ ClientOptions ,
7
+ CreateClientConfig ,
8
+ Options as HeyApiOptions ,
9
+ TDataShape ,
10
+ } from '@hey-api/client-next'
11
+
12
+ export type Options <
13
+ TData extends TDataShape = TDataShape ,
14
+ TThrowOnError extends boolean = boolean ,
15
+ > = HeyApiOptions < TData , TThrowOnError > & {
16
+ /**
17
+ * You can provide a client instance returned by `createClient()` instead of
18
+ * individual options. This might be also useful if you want to implement a
19
+ * custom client.
20
+ */
21
+ client ?: Client
22
+ /**
23
+ * You can pass arbitrary values through the `meta` object. This can be
24
+ * used to access values that aren't defined as part of the SDK function.
25
+ */
26
+ meta ?: Record < string , unknown >
27
+ }
20
28
21
29
export const createClientConfig : CreateClientConfig = ( config ) => ( {
22
30
...config ,
23
31
baseUrl : 'https://fakerestapi.azurewebsites.net/' ,
24
32
} )
25
33
26
-
27
- export const _heyApiClient = createClient ( createClientConfig ( createConfig < ClientOptions > ( {
28
- baseUrl : 'https://fakerestapi.azurewebsites.net'
29
- } ) ) ) ;
34
+ export const _heyApiClient = createClient (
35
+ createClientConfig (
36
+ createConfig < ClientOptions > ( {
37
+ baseUrl : 'https://fakerestapi.azurewebsites.net' ,
38
+ } ) ,
39
+ ) ,
40
+ )
30
41
31
42
export type GetApiV1ActivitiesData = {
32
- body ?: never ;
33
- path ?: never ;
34
- query ?: never ;
35
- url : '/api/v1/Activities' ;
36
- } ;
43
+ body ?: never
44
+ path ?: never
45
+ query ?: never
46
+ url : '/api/v1/Activities'
47
+ }
37
48
38
49
export type Activity = {
39
- id ?: number ;
40
- title ?: string | null ;
41
- dueDate ?: string ;
42
- completed ?: boolean ;
43
- } ;
50
+ id ?: number
51
+ title ?: string | null
52
+ dueDate ?: string
53
+ completed ?: boolean
54
+ }
44
55
45
56
export type Author = {
46
- id ?: number ;
47
- idBook ?: number ;
48
- firstName ?: string | null ;
49
- lastName ?: string | null ;
50
- } ;
57
+ id ?: number
58
+ idBook ?: number
59
+ firstName ?: string | null
60
+ lastName ?: string | null
61
+ }
51
62
52
63
export type GetApiV1ActivitiesResponses = {
53
- /**
54
- * Success
55
- */
56
- 200 : Array < Activity > ;
57
- } ;
64
+ /**
65
+ * Success
66
+ */
67
+ 200 : Array < Activity >
68
+ }
58
69
59
70
export type GetApiV1AuthorsData = {
60
- body ?: never ;
61
- path ?: never ;
62
- query ?: never ;
63
- url : '/api/v1/Authors' ;
64
- } ;
71
+ body ?: never
72
+ path ?: never
73
+ query ?: never
74
+ url : '/api/v1/Authors'
75
+ }
65
76
66
- export type GetApiV1ActivitiesResponse = GetApiV1ActivitiesResponses [ keyof GetApiV1ActivitiesResponses ] ;
77
+ export type GetApiV1ActivitiesResponse =
78
+ GetApiV1ActivitiesResponses [ keyof GetApiV1ActivitiesResponses ]
67
79
68
80
export type QueryKey < TOptions extends Options > = [
69
- Pick < TOptions , 'baseUrl' | 'body' | 'headers' | 'path' | 'query' > & {
70
- _id : string ;
71
- _infinite ?: boolean ;
72
- }
73
- ] ;
74
-
75
-
76
- const createQueryKey = < TOptions extends Options > ( id : string , options ?: TOptions , infinite ?: boolean ) : [
77
- QueryKey < TOptions > [ 0 ]
78
- ] => {
79
- const params : QueryKey < TOptions > [ 0 ] = { _id : id , baseUrl : ( options ?. client ?? _heyApiClient ) . getConfig ( ) . baseUrl } as QueryKey < TOptions > [ 0 ] ;
80
- if ( infinite ) {
81
- params . _infinite = infinite ;
82
- }
83
- if ( options ?. body ) {
84
- params . body = options . body ;
85
- }
86
- if ( options ?. headers ) {
87
- params . headers = options . headers ;
88
- }
89
- if ( options ?. path ) {
90
- params . path = options . path ;
91
- }
92
- if ( options ?. query ) {
93
- params . query = options . query ;
94
- }
95
- return [
96
- params
97
- ] ;
98
- } ;
99
-
100
- export const getApiV1Activities = < TThrowOnError extends boolean = false > ( options ?: Options < GetApiV1ActivitiesData , TThrowOnError > ) => {
101
- return ( options ?. client ?? _heyApiClient ) . get < GetApiV1ActivitiesResponse , unknown , TThrowOnError > ( {
102
- url : '/api/v1/Activities' ,
103
- ...options
104
- } ) ;
105
- } ;
106
-
107
- export const getApiV1ActivitiesQueryKey = ( options ?: Options < GetApiV1ActivitiesData > ) => createQueryKey ( 'getApiV1Activities' , options ) ;
108
-
109
-
110
- export const getApiV1ActivitiesOptions = ( options ?: Options < GetApiV1ActivitiesData > ) => {
111
- return queryOptions ( {
112
- queryFn : async ( { queryKey, signal } ) => {
113
- const { data } = await getApiV1Activities ( {
114
- ...options ,
115
- ...queryKey [ 0 ] ,
116
- signal,
117
- throwOnError : true
118
- } ) ;
119
- return data ;
120
- } ,
121
- queryKey : getApiV1ActivitiesQueryKey ( options )
122
- } ) ;
123
- } ;
81
+ Pick < TOptions , 'baseUrl' | 'body' | 'headers' | 'path' | 'query' > & {
82
+ _id : string
83
+ _infinite ?: boolean
84
+ } ,
85
+ ]
86
+
87
+ const createQueryKey = < TOptions extends Options > (
88
+ id : string ,
89
+ options ?: TOptions ,
90
+ infinite ?: boolean ,
91
+ ) : [ QueryKey < TOptions > [ 0 ] ] => {
92
+ const params : QueryKey < TOptions > [ 0 ] = {
93
+ _id : id ,
94
+ baseUrl : ( options ?. client ?? _heyApiClient ) . getConfig ( ) . baseUrl ,
95
+ } as QueryKey < TOptions > [ 0 ]
96
+ if ( infinite ) {
97
+ params . _infinite = infinite
98
+ }
99
+ if ( options ?. body ) {
100
+ params . body = options . body
101
+ }
102
+ if ( options ?. headers ) {
103
+ params . headers = options . headers
104
+ }
105
+ if ( options ?. path ) {
106
+ params . path = options . path
107
+ }
108
+ if ( options ?. query ) {
109
+ params . query = options . query
110
+ }
111
+ return [ params ]
112
+ }
113
+
114
+ export const getApiV1Activities = < TThrowOnError extends boolean = false > (
115
+ options ?: Options < GetApiV1ActivitiesData , TThrowOnError > ,
116
+ ) => {
117
+ return ( options ?. client ?? _heyApiClient ) . get <
118
+ GetApiV1ActivitiesResponse ,
119
+ unknown ,
120
+ TThrowOnError
121
+ > ( {
122
+ url : '/api/v1/Activities' ,
123
+ ...options ,
124
+ } )
125
+ }
126
+
127
+ export const getApiV1ActivitiesQueryKey = (
128
+ options ?: Options < GetApiV1ActivitiesData > ,
129
+ ) => createQueryKey ( 'getApiV1Activities' , options )
130
+
131
+ export const getApiV1ActivitiesOptions = (
132
+ options ?: Options < GetApiV1ActivitiesData > ,
133
+ ) => {
134
+ return queryOptions ( {
135
+ queryFn : async ( { queryKey, signal } ) => {
136
+ const { data } = await getApiV1Activities ( {
137
+ ...options ,
138
+ ...queryKey [ 0 ] ,
139
+ signal,
140
+ throwOnError : true ,
141
+ } )
142
+ return data
143
+ } ,
144
+ queryKey : getApiV1ActivitiesQueryKey ( options ) ,
145
+ } )
146
+ }
124
147
125
148
export type GetApiV1AuthorsResponses = {
126
- /**
127
- * Success
128
- */
129
- 200 : Array < Author > ;
130
- } ;
131
-
132
- export type GetApiV1AuthorsResponse = GetApiV1AuthorsResponses [ keyof GetApiV1AuthorsResponses ] ;
133
-
134
-
135
- export const getApiV1Authors = < TThrowOnError extends boolean = false > ( options ?: Options < GetApiV1AuthorsData , TThrowOnError > ) => {
136
- return ( options ?. client ?? _heyApiClient ) . get < GetApiV1AuthorsResponse , unknown , TThrowOnError > ( {
137
- url : '/api/v1/Authors' ,
138
- ...options
139
- } ) ;
140
- } ;
141
-
142
- export const getApiV1AuthorsQueryKey = ( options ?: Options < GetApiV1AuthorsData > ) => createQueryKey ( 'getApiV1Authors' , options ) ;
143
-
144
- export const getApiV1AuthorsOptions = ( options ?: Options < GetApiV1AuthorsData > ) => {
145
- return queryOptions ( {
146
- queryFn : async ( { queryKey, signal } ) => {
147
- const { data } = await getApiV1Authors ( {
148
- ...options ,
149
- ...queryKey [ 0 ] ,
150
- signal,
151
- throwOnError : true
152
- } ) ;
153
- return data ;
154
- } ,
155
- queryKey : getApiV1AuthorsQueryKey ( options )
156
- } ) ;
157
- } ;
149
+ /**
150
+ * Success
151
+ */
152
+ 200 : Array < Author >
153
+ }
154
+
155
+ export type GetApiV1AuthorsResponse =
156
+ GetApiV1AuthorsResponses [ keyof GetApiV1AuthorsResponses ]
157
+
158
+ export const getApiV1Authors = < TThrowOnError extends boolean = false > (
159
+ options ?: Options < GetApiV1AuthorsData , TThrowOnError > ,
160
+ ) => {
161
+ return ( options ?. client ?? _heyApiClient ) . get <
162
+ GetApiV1AuthorsResponse ,
163
+ unknown ,
164
+ TThrowOnError
165
+ > ( {
166
+ url : '/api/v1/Authors' ,
167
+ ...options ,
168
+ } )
169
+ }
170
+
171
+ export const getApiV1AuthorsQueryKey = (
172
+ options ?: Options < GetApiV1AuthorsData > ,
173
+ ) => createQueryKey ( 'getApiV1Authors' , options )
174
+
175
+ export const getApiV1AuthorsOptions = (
176
+ options ?: Options < GetApiV1AuthorsData > ,
177
+ ) => {
178
+ return queryOptions ( {
179
+ queryFn : async ( { queryKey, signal } ) => {
180
+ const { data } = await getApiV1Authors ( {
181
+ ...options ,
182
+ ...queryKey [ 0 ] ,
183
+ signal,
184
+ throwOnError : true ,
185
+ } )
186
+ return data
187
+ } ,
188
+ queryKey : getApiV1AuthorsQueryKey ( options ) ,
189
+ } )
190
+ }
0 commit comments