tangled
alpha
login
or
join now
mokkenstorm.dev
/
openapi-ts
0
fork
atom
fork of hey-api/openapi-ts because I need some additional things
0
fork
atom
overview
issues
pulls
pipelines
chore(pinia-colada): regenerate client
Dmitriy Brolnickij
6 months ago
448da2b0
9f7ef365
+376
-159
8 changed files
expand all
collapse all
unified
split
examples
openapi-ts-pinia-colada
src
client
@pinia
colada.gen.ts
client
client.gen.ts
types.gen.ts
utils.gen.ts
core
serverSentEvents.gen.ts
utils.gen.ts
index.ts
sdk.gen.ts
+215
-80
examples/openapi-ts-pinia-colada/src/client/@pinia/colada.gen.ts
···
1
1
// This file is auto-generated by @hey-api/openapi-ts
2
2
3
3
+
import { type _JSONValue, defineQueryOptions, type UseMutationOptions } from '@pinia/colada'
4
4
+
5
5
+
import { client } from '../client.gen'
3
6
import {
4
7
addPet,
5
8
createUser,
···
24
27
} from '../sdk.gen'
25
28
import type {
26
29
AddPetData,
30
30
+
AddPetResponse,
27
31
CreateUserData,
32
32
+
CreateUserResponse,
28
33
CreateUsersWithListInputData,
34
34
+
CreateUsersWithListInputResponse,
29
35
DeleteOrderData,
30
36
DeletePetData,
31
37
DeleteUserData,
···
38
44
LoginUserData,
39
45
LogoutUserData,
40
46
PlaceOrderData,
47
47
+
PlaceOrderResponse,
41
48
UpdatePetData,
49
49
+
UpdatePetResponse,
42
50
UpdatePetWithFormData,
51
51
+
UpdatePetWithFormResponse,
43
52
UpdateUserData,
44
44
-
UploadFileData
53
53
+
UploadFileData,
54
54
+
UploadFileResponse
45
55
} from '../types.gen'
46
56
47
57
/**
48
58
* Add a new pet to the store.
49
59
* Add a new pet to the store.
50
60
*/
51
51
-
export const addPetMutation = () => ({
52
52
-
mutation: async (options: Options<AddPetData>) => {
53
53
-
const { data } = await addPet(options)
61
61
+
export const addPetMutation = (
62
62
+
options?: Partial<Options<AddPetData>>
63
63
+
): UseMutationOptions<AddPetResponse, Options<AddPetData>, Error> => ({
64
64
+
mutation: async (fnOptions) => {
65
65
+
const { data } = await addPet({
66
66
+
...options,
67
67
+
...fnOptions,
68
68
+
throwOnError: true
69
69
+
})
54
70
return data
55
71
}
56
72
})
···
59
75
* Update an existing pet.
60
76
* Update an existing pet by Id.
61
77
*/
62
62
-
export const updatePetMutation = () => ({
63
63
-
mutation: async (options: Options<UpdatePetData>) => {
64
64
-
const { data } = await updatePet(options)
78
78
+
export const updatePetMutation = (
79
79
+
options?: Partial<Options<UpdatePetData>>
80
80
+
): UseMutationOptions<UpdatePetResponse, Options<UpdatePetData>, Error> => ({
81
81
+
mutation: async (fnOptions) => {
82
82
+
const { data } = await updatePet({
83
83
+
...options,
84
84
+
...fnOptions,
85
85
+
throwOnError: true
86
86
+
})
65
87
return data
66
88
}
67
89
})
68
90
91
91
+
export type QueryKey<TOptions extends Options> = [
92
92
+
Pick<TOptions, 'body' | 'path'> & {
93
93
+
_id: string
94
94
+
baseUrl?: _JSONValue
95
95
+
headers?: _JSONValue
96
96
+
query?: _JSONValue
97
97
+
tags?: _JSONValue
98
98
+
}
99
99
+
]
100
100
+
101
101
+
const createQueryKey = <TOptions extends Options>(
102
102
+
id: string,
103
103
+
options?: TOptions,
104
104
+
tags?: ReadonlyArray<string>
105
105
+
): [QueryKey<TOptions>[0]] => {
106
106
+
const params: QueryKey<TOptions>[0] = {
107
107
+
_id: id,
108
108
+
baseUrl: options?.baseUrl || (options?.client ?? client).getConfig().baseUrl
109
109
+
} as QueryKey<TOptions>[0]
110
110
+
if (tags) {
111
111
+
params.tags = tags as unknown as _JSONValue
112
112
+
}
113
113
+
if (options?.body) {
114
114
+
params.body = options.body
115
115
+
}
116
116
+
if (options?.headers) {
117
117
+
params.headers = options.headers as unknown as _JSONValue
118
118
+
}
119
119
+
if (options?.path) {
120
120
+
params.path = options.path
121
121
+
}
122
122
+
if (options?.query) {
123
123
+
params.query = options.query as unknown as _JSONValue
124
124
+
}
125
125
+
return [params]
126
126
+
}
127
127
+
128
128
+
export const findPetsByStatusQueryKey = (options: Options<FindPetsByStatusData>) =>
129
129
+
createQueryKey('findPetsByStatus', options)
130
130
+
69
131
/**
70
132
* Finds Pets by status.
71
133
* Multiple status values can be provided with comma separated strings.
72
134
*/
73
73
-
export const findPetsByStatusQuery = (options: Options<FindPetsByStatusData>) => ({
74
74
-
key: ['findPetsByStatus', options?.path],
75
75
-
query: async (context: { signal: AbortSignal }) => {
76
76
-
const { data } = await findPetsByStatus({
77
77
-
...options,
78
78
-
signal: context.signal,
79
79
-
throwOnError: true
80
80
-
})
81
81
-
return data
82
82
-
}
83
83
-
})
135
135
+
export const findPetsByStatusQuery = defineQueryOptions(
136
136
+
(options: Options<FindPetsByStatusData>) => ({
137
137
+
key: findPetsByStatusQueryKey(options),
138
138
+
query: async () => {
139
139
+
const { data } = await findPetsByStatus({
140
140
+
...options,
141
141
+
throwOnError: true
142
142
+
})
143
143
+
return data
144
144
+
}
145
145
+
})
146
146
+
)
147
147
+
148
148
+
export const findPetsByTagsQueryKey = (options: Options<FindPetsByTagsData>) =>
149
149
+
createQueryKey('findPetsByTags', options)
84
150
85
151
/**
86
152
* Finds Pets by tags.
87
153
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
88
154
*/
89
89
-
export const findPetsByTagsQuery = (options: Options<FindPetsByTagsData>) => ({
90
90
-
key: ['findPetsByTags', options?.path],
91
91
-
query: async (context: { signal: AbortSignal }) => {
155
155
+
export const findPetsByTagsQuery = defineQueryOptions((options: Options<FindPetsByTagsData>) => ({
156
156
+
key: findPetsByTagsQueryKey(options),
157
157
+
query: async () => {
92
158
const { data } = await findPetsByTags({
93
159
...options,
94
94
-
signal: context.signal,
95
160
throwOnError: true
96
161
})
97
162
return data
98
163
}
99
99
-
})
164
164
+
}))
100
165
101
166
/**
102
167
* Deletes a pet.
103
168
* Delete a pet.
104
169
*/
105
105
-
export const deletePetMutation = () => ({
106
106
-
mutation: async (options: Options<DeletePetData>) => {
107
107
-
const { data } = await deletePet(options)
170
170
+
export const deletePetMutation = (
171
171
+
options?: Partial<Options<DeletePetData>>
172
172
+
): UseMutationOptions<unknown, Options<DeletePetData>, Error> => ({
173
173
+
mutation: async (fnOptions) => {
174
174
+
const { data } = await deletePet({
175
175
+
...options,
176
176
+
...fnOptions,
177
177
+
throwOnError: true
178
178
+
})
108
179
return data
109
180
}
110
181
})
182
182
+
183
183
+
export const getPetByIdQueryKey = (options: Options<GetPetByIdData>) =>
184
184
+
createQueryKey('getPetById', options)
111
185
112
186
/**
113
187
* Find pet by ID.
114
188
* Returns a single pet.
115
189
*/
116
116
-
export const getPetByIdQuery = (options: Options<GetPetByIdData>) => ({
117
117
-
key: ['getPetById', options?.path],
118
118
-
query: async (context: { signal: AbortSignal }) => {
190
190
+
export const getPetByIdQuery = defineQueryOptions((options: Options<GetPetByIdData>) => ({
191
191
+
key: getPetByIdQueryKey(options),
192
192
+
query: async () => {
119
193
const { data } = await getPetById({
120
194
...options,
121
121
-
signal: context.signal,
122
195
throwOnError: true
123
196
})
124
197
return data
125
198
}
126
126
-
})
199
199
+
}))
127
200
128
201
/**
129
202
* Updates a pet in the store with form data.
130
203
* Updates a pet resource based on the form data.
131
204
*/
132
132
-
export const updatePetWithFormMutation = () => ({
133
133
-
mutation: async (options: Options<UpdatePetWithFormData>) => {
134
134
-
const { data } = await updatePetWithForm(options)
205
205
+
export const updatePetWithFormMutation = (
206
206
+
options?: Partial<Options<UpdatePetWithFormData>>
207
207
+
): UseMutationOptions<UpdatePetWithFormResponse, Options<UpdatePetWithFormData>, Error> => ({
208
208
+
mutation: async (fnOptions) => {
209
209
+
const { data } = await updatePetWithForm({
210
210
+
...options,
211
211
+
...fnOptions,
212
212
+
throwOnError: true
213
213
+
})
135
214
return data
136
215
}
137
216
})
···
140
219
* Uploads an image.
141
220
* Upload image of the pet.
142
221
*/
143
143
-
export const uploadFileMutation = () => ({
144
144
-
mutation: async (options: Options<UploadFileData>) => {
145
145
-
const { data } = await uploadFile(options)
222
222
+
export const uploadFileMutation = (
223
223
+
options?: Partial<Options<UploadFileData>>
224
224
+
): UseMutationOptions<UploadFileResponse, Options<UploadFileData>, Error> => ({
225
225
+
mutation: async (fnOptions) => {
226
226
+
const { data } = await uploadFile({
227
227
+
...options,
228
228
+
...fnOptions,
229
229
+
throwOnError: true
230
230
+
})
146
231
return data
147
232
}
148
233
})
149
234
235
235
+
export const getInventoryQueryKey = (options?: Options<GetInventoryData>) =>
236
236
+
createQueryKey('getInventory', options)
237
237
+
150
238
/**
151
239
* Returns pet inventories by status.
152
240
* Returns a map of status codes to quantities.
153
241
*/
154
154
-
export const getInventoryQuery = (options?: Options<GetInventoryData>) => ({
155
155
-
key: ['getInventory', options?.path],
156
156
-
query: async (context: { signal: AbortSignal }) => {
242
242
+
export const getInventoryQuery = defineQueryOptions((options?: Options<GetInventoryData>) => ({
243
243
+
key: getInventoryQueryKey(options),
244
244
+
query: async () => {
157
245
const { data } = await getInventory({
158
246
...options,
159
159
-
signal: context.signal,
160
247
throwOnError: true
161
248
})
162
249
return data
163
250
}
164
164
-
})
251
251
+
}))
165
252
166
253
/**
167
254
* Place an order for a pet.
168
255
* Place a new order in the store.
169
256
*/
170
170
-
export const placeOrderMutation = () => ({
171
171
-
mutation: async (options: Options<PlaceOrderData>) => {
172
172
-
const { data } = await placeOrder(options)
257
257
+
export const placeOrderMutation = (
258
258
+
options?: Partial<Options<PlaceOrderData>>
259
259
+
): UseMutationOptions<PlaceOrderResponse, Options<PlaceOrderData>, Error> => ({
260
260
+
mutation: async (fnOptions) => {
261
261
+
const { data } = await placeOrder({
262
262
+
...options,
263
263
+
...fnOptions,
264
264
+
throwOnError: true
265
265
+
})
173
266
return data
174
267
}
175
268
})
···
178
271
* Delete purchase order by identifier.
179
272
* For valid response try integer IDs with value < 1000. Anything above 1000 or non-integers will generate API errors.
180
273
*/
181
181
-
export const deleteOrderMutation = () => ({
182
182
-
mutation: async (options: Options<DeleteOrderData>) => {
183
183
-
const { data } = await deleteOrder(options)
274
274
+
export const deleteOrderMutation = (
275
275
+
options?: Partial<Options<DeleteOrderData>>
276
276
+
): UseMutationOptions<unknown, Options<DeleteOrderData>, Error> => ({
277
277
+
mutation: async (fnOptions) => {
278
278
+
const { data } = await deleteOrder({
279
279
+
...options,
280
280
+
...fnOptions,
281
281
+
throwOnError: true
282
282
+
})
184
283
return data
185
284
}
186
285
})
187
286
287
287
+
export const getOrderByIdQueryKey = (options: Options<GetOrderByIdData>) =>
288
288
+
createQueryKey('getOrderById', options)
289
289
+
188
290
/**
189
291
* Find purchase order by ID.
190
292
* For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.
191
293
*/
192
192
-
export const getOrderByIdQuery = (options: Options<GetOrderByIdData>) => ({
193
193
-
key: ['getOrderById', options?.path],
194
194
-
query: async (context: { signal: AbortSignal }) => {
294
294
+
export const getOrderByIdQuery = defineQueryOptions((options: Options<GetOrderByIdData>) => ({
295
295
+
key: getOrderByIdQueryKey(options),
296
296
+
query: async () => {
195
297
const { data } = await getOrderById({
196
298
...options,
197
197
-
signal: context.signal,
198
299
throwOnError: true
199
300
})
200
301
return data
201
302
}
202
202
-
})
303
303
+
}))
203
304
204
305
/**
205
306
* Create user.
206
307
* This can only be done by the logged in user.
207
308
*/
208
208
-
export const createUserMutation = () => ({
209
209
-
mutation: async (options: Options<CreateUserData>) => {
210
210
-
const { data } = await createUser(options)
309
309
+
export const createUserMutation = (
310
310
+
options?: Partial<Options<CreateUserData>>
311
311
+
): UseMutationOptions<CreateUserResponse, Options<CreateUserData>, Error> => ({
312
312
+
mutation: async (fnOptions) => {
313
313
+
const { data } = await createUser({
314
314
+
...options,
315
315
+
...fnOptions,
316
316
+
throwOnError: true
317
317
+
})
211
318
return data
212
319
}
213
320
})
···
216
323
* Creates list of users with given input array.
217
324
* Creates list of users with given input array.
218
325
*/
219
219
-
export const createUsersWithListInputMutation = () => ({
220
220
-
mutation: async (options: Options<CreateUsersWithListInputData>) => {
221
221
-
const { data } = await createUsersWithListInput(options)
326
326
+
export const createUsersWithListInputMutation = (
327
327
+
options?: Partial<Options<CreateUsersWithListInputData>>
328
328
+
): UseMutationOptions<
329
329
+
CreateUsersWithListInputResponse,
330
330
+
Options<CreateUsersWithListInputData>,
331
331
+
Error
332
332
+
> => ({
333
333
+
mutation: async (fnOptions) => {
334
334
+
const { data } = await createUsersWithListInput({
335
335
+
...options,
336
336
+
...fnOptions,
337
337
+
throwOnError: true
338
338
+
})
222
339
return data
223
340
}
224
341
})
225
342
343
343
+
export const loginUserQueryKey = (options?: Options<LoginUserData>) =>
344
344
+
createQueryKey('loginUser', options)
345
345
+
226
346
/**
227
347
* Logs user into the system.
228
348
* Log into the system.
229
349
*/
230
230
-
export const loginUserQuery = (options?: Options<LoginUserData>) => ({
231
231
-
key: ['loginUser', options?.path],
232
232
-
query: async (context: { signal: AbortSignal }) => {
350
350
+
export const loginUserQuery = defineQueryOptions((options?: Options<LoginUserData>) => ({
351
351
+
key: loginUserQueryKey(options),
352
352
+
query: async () => {
233
353
const { data } = await loginUser({
234
354
...options,
235
235
-
signal: context.signal,
236
355
throwOnError: true
237
356
})
238
357
return data
239
358
}
240
240
-
})
359
359
+
}))
360
360
+
361
361
+
export const logoutUserQueryKey = (options?: Options<LogoutUserData>) =>
362
362
+
createQueryKey('logoutUser', options)
241
363
242
364
/**
243
365
* Logs out current logged in user session.
244
366
* Log user out of the system.
245
367
*/
246
246
-
export const logoutUserQuery = (options?: Options<LogoutUserData>) => ({
247
247
-
key: ['logoutUser', options?.path],
248
248
-
query: async (context: { signal: AbortSignal }) => {
368
368
+
export const logoutUserQuery = defineQueryOptions((options?: Options<LogoutUserData>) => ({
369
369
+
key: logoutUserQueryKey(options),
370
370
+
query: async () => {
249
371
const { data } = await logoutUser({
250
372
...options,
251
251
-
signal: context.signal,
252
373
throwOnError: true
253
374
})
254
375
return data
255
376
}
256
256
-
})
377
377
+
}))
257
378
258
379
/**
259
380
* Delete user resource.
260
381
* This can only be done by the logged in user.
261
382
*/
262
262
-
export const deleteUserMutation = () => ({
263
263
-
mutation: async (options: Options<DeleteUserData>) => {
264
264
-
const { data } = await deleteUser(options)
383
383
+
export const deleteUserMutation = (
384
384
+
options?: Partial<Options<DeleteUserData>>
385
385
+
): UseMutationOptions<unknown, Options<DeleteUserData>, Error> => ({
386
386
+
mutation: async (fnOptions) => {
387
387
+
const { data } = await deleteUser({
388
388
+
...options,
389
389
+
...fnOptions,
390
390
+
throwOnError: true
391
391
+
})
265
392
return data
266
393
}
267
394
})
268
395
396
396
+
export const getUserByNameQueryKey = (options: Options<GetUserByNameData>) =>
397
397
+
createQueryKey('getUserByName', options)
398
398
+
269
399
/**
270
400
* Get user by user name.
271
401
* Get user detail based on username.
272
402
*/
273
273
-
export const getUserByNameQuery = (options: Options<GetUserByNameData>) => ({
274
274
-
key: ['getUserByName', options?.path],
275
275
-
query: async (context: { signal: AbortSignal }) => {
403
403
+
export const getUserByNameQuery = defineQueryOptions((options: Options<GetUserByNameData>) => ({
404
404
+
key: getUserByNameQueryKey(options),
405
405
+
query: async () => {
276
406
const { data } = await getUserByName({
277
407
...options,
278
278
-
signal: context.signal,
279
408
throwOnError: true
280
409
})
281
410
return data
282
411
}
283
283
-
})
412
412
+
}))
284
413
285
414
/**
286
415
* Update user resource.
287
416
* This can only be done by the logged in user.
288
417
*/
289
289
-
export const updateUserMutation = () => ({
290
290
-
mutation: async (options: Options<UpdateUserData>) => {
291
291
-
const { data } = await updateUser(options)
418
418
+
export const updateUserMutation = (
419
419
+
options?: Partial<Options<UpdateUserData>>
420
420
+
): UseMutationOptions<unknown, Options<UpdateUserData>, Error> => ({
421
421
+
mutation: async (fnOptions) => {
422
422
+
const { data } = await updateUser({
423
423
+
...options,
424
424
+
...fnOptions,
425
425
+
throwOnError: true
426
426
+
})
292
427
return data
293
428
}
294
429
})
+41
-13
examples/openapi-ts-pinia-colada/src/client/client/client.gen.ts
···
2
2
3
3
import { createSseClient } from '../core/serverSentEvents.gen'
4
4
import type { HttpMethod } from '../core/types.gen'
5
5
+
import { getValidRequestBody } from '../core/utils.gen'
5
6
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types.gen'
6
7
import {
7
8
buildUrl,
···
50
51
await opts.requestValidator(opts)
51
52
}
52
53
53
53
-
if (opts.body && opts.bodySerializer) {
54
54
+
if (opts.body !== undefined && opts.bodySerializer) {
54
55
opts.serializedBody = opts.bodySerializer(opts.body)
55
56
}
56
57
57
58
// remove Content-Type header if body is empty to avoid sending invalid requests
58
58
-
if (opts.serializedBody === undefined || opts.serializedBody === '') {
59
59
+
if (opts.body === undefined || opts.serializedBody === '') {
59
60
opts.headers.delete('Content-Type')
60
61
}
61
62
···
70
71
const requestInit: ReqInit = {
71
72
redirect: 'follow',
72
73
...opts,
73
73
-
body: opts.serializedBody
74
74
+
body: getValidRequestBody(opts)
74
75
}
75
76
76
77
let request = new Request(url, requestInit)
77
78
78
78
-
for (const fn of interceptors.request._fns) {
79
79
+
for (const fn of interceptors.request.fns) {
79
80
if (fn) {
80
81
request = await fn(request, opts)
81
82
}
···
86
87
const _fetch = opts.fetch!
87
88
let response = await _fetch(request)
88
89
89
89
-
for (const fn of interceptors.response._fns) {
90
90
+
for (const fn of interceptors.response.fns) {
90
91
if (fn) {
91
92
response = await fn(response, request, opts)
92
93
}
···
98
99
}
99
100
100
101
if (response.ok) {
102
102
+
const parseAs =
103
103
+
(opts.parseAs === 'auto'
104
104
+
? getParseAs(response.headers.get('Content-Type'))
105
105
+
: opts.parseAs) ?? 'json'
106
106
+
101
107
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
108
108
+
let emptyData: any
109
109
+
switch (parseAs) {
110
110
+
case 'arrayBuffer':
111
111
+
case 'blob':
112
112
+
case 'text':
113
113
+
emptyData = await response[parseAs]()
114
114
+
break
115
115
+
case 'formData':
116
116
+
emptyData = new FormData()
117
117
+
break
118
118
+
case 'stream':
119
119
+
emptyData = response.body
120
120
+
break
121
121
+
case 'json':
122
122
+
default:
123
123
+
emptyData = {}
124
124
+
break
125
125
+
}
102
126
return opts.responseStyle === 'data'
103
103
-
? {}
127
127
+
? emptyData
104
128
: {
105
105
-
data: {},
129
129
+
data: emptyData,
106
130
...result
107
131
}
108
132
}
109
133
110
110
-
const parseAs =
111
111
-
(opts.parseAs === 'auto'
112
112
-
? getParseAs(response.headers.get('Content-Type'))
113
113
-
: opts.parseAs) ?? 'json'
114
114
-
115
134
let data: any
116
135
switch (parseAs) {
117
136
case 'arrayBuffer':
···
160
179
const error = jsonError ?? textError
161
180
let finalError = error
162
181
163
163
-
for (const fn of interceptors.error._fns) {
182
182
+
for (const fn of interceptors.error.fns) {
164
183
if (fn) {
165
184
finalError = (await fn(error, response, request, opts)) as string
166
185
}
···
191
210
body: opts.body as BodyInit | null | undefined,
192
211
headers: opts.headers as unknown as Record<string, string>,
193
212
method,
213
213
+
onRequest: async (url, init) => {
214
214
+
let request = new Request(url, init)
215
215
+
for (const fn of interceptors.request.fns) {
216
216
+
if (fn) {
217
217
+
request = await fn(request, opts)
218
218
+
}
219
219
+
}
220
220
+
return request
221
221
+
},
194
222
url
195
223
})
196
224
}
+1
-1
examples/openapi-ts-pinia-colada/src/client/client/types.gen.ts
···
20
20
*
21
21
* @default globalThis.fetch
22
22
*/
23
23
-
fetch?: (request: Request) => ReturnType<typeof fetch>
23
23
+
fetch?: typeof fetch
24
24
/**
25
25
* Please don't use the Fetch client for Next.js applications. The `next`
26
26
* options won't have any effect.
+40
-35
examples/openapi-ts-pinia-colada/src/client/client/utils.gen.ts
···
176
176
return config
177
177
}
178
178
179
179
+
const headersEntries = (headers: Headers): Array<[string, string]> => {
180
180
+
const entries: Array<[string, string]> = []
181
181
+
headers.forEach((value, key) => {
182
182
+
entries.push([key, value])
183
183
+
})
184
184
+
return entries
185
185
+
}
186
186
+
179
187
export const mergeHeaders = (
180
188
...headers: Array<Required<Config>['headers'] | undefined>
181
189
): Headers => {
182
190
const mergedHeaders = new Headers()
183
191
for (const header of headers) {
184
184
-
if (!header || typeof header !== 'object') {
192
192
+
if (!header) {
185
193
continue
186
194
}
187
195
188
188
-
const iterator = header instanceof Headers ? header.entries() : Object.entries(header)
196
196
+
const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header)
189
197
190
198
for (const [key, value] of iterator) {
191
199
if (value === null) {
···
223
231
) => Res | Promise<Res>
224
232
225
233
class Interceptors<Interceptor> {
226
226
-
_fns: (Interceptor | null)[]
234
234
+
fns: Array<Interceptor | null> = []
227
235
228
228
-
constructor() {
229
229
-
this._fns = []
230
230
-
}
231
231
-
232
232
-
clear() {
233
233
-
this._fns = []
236
236
+
clear(): void {
237
237
+
this.fns = []
234
238
}
235
239
236
236
-
getInterceptorIndex(id: number | Interceptor): number {
237
237
-
if (typeof id === 'number') {
238
238
-
return this._fns[id] ? id : -1
239
239
-
} else {
240
240
-
return this._fns.indexOf(id)
240
240
+
eject(id: number | Interceptor): void {
241
241
+
const index = this.getInterceptorIndex(id)
242
242
+
if (this.fns[index]) {
243
243
+
this.fns[index] = null
241
244
}
242
245
}
243
243
-
exists(id: number | Interceptor) {
246
246
+
247
247
+
exists(id: number | Interceptor): boolean {
244
248
const index = this.getInterceptorIndex(id)
245
245
-
return !!this._fns[index]
249
249
+
return Boolean(this.fns[index])
246
250
}
247
251
248
248
-
eject(id: number | Interceptor) {
249
249
-
const index = this.getInterceptorIndex(id)
250
250
-
if (this._fns[index]) {
251
251
-
this._fns[index] = null
252
252
+
getInterceptorIndex(id: number | Interceptor): number {
253
253
+
if (typeof id === 'number') {
254
254
+
return this.fns[id] ? id : -1
252
255
}
256
256
+
return this.fns.indexOf(id)
253
257
}
254
258
255
255
-
update(id: number | Interceptor, fn: Interceptor) {
259
259
+
update(id: number | Interceptor, fn: Interceptor): number | Interceptor | false {
256
260
const index = this.getInterceptorIndex(id)
257
257
-
if (this._fns[index]) {
258
258
-
this._fns[index] = fn
261
261
+
if (this.fns[index]) {
262
262
+
this.fns[index] = fn
259
263
return id
260
260
-
} else {
261
261
-
return false
262
264
}
265
265
+
return false
263
266
}
264
267
265
265
-
use(fn: Interceptor) {
266
266
-
this._fns = [...this._fns, fn]
267
267
-
return this._fns.length - 1
268
268
+
use(fn: Interceptor): number {
269
269
+
this.fns.push(fn)
270
270
+
return this.fns.length - 1
268
271
}
269
272
}
270
273
271
271
-
// `createInterceptors()` response, meant for external use as it does not
272
272
-
// expose internals
273
274
export interface Middleware<Req, Res, Err, Options> {
274
274
-
error: Pick<Interceptors<ErrInterceptor<Err, Res, Req, Options>>, 'eject' | 'use'>
275
275
-
request: Pick<Interceptors<ReqInterceptor<Req, Options>>, 'eject' | 'use'>
276
276
-
response: Pick<Interceptors<ResInterceptor<Res, Req, Options>>, 'eject' | 'use'>
275
275
+
error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>
276
276
+
request: Interceptors<ReqInterceptor<Req, Options>>
277
277
+
response: Interceptors<ResInterceptor<Res, Req, Options>>
277
278
}
278
279
279
279
-
// do not add `Middleware` as return type so we can use _fns internally
280
280
-
export const createInterceptors = <Req, Res, Err, Options>() => ({
280
280
+
export const createInterceptors = <Req, Res, Err, Options>(): Middleware<
281
281
+
Req,
282
282
+
Res,
283
283
+
Err,
284
284
+
Options
285
285
+
> => ({
281
286
error: new Interceptors<ErrInterceptor<Err, Res, Req, Options>>(),
282
287
request: new Interceptors<ReqInterceptor<Req, Options>>(),
283
288
response: new Interceptors<ResInterceptor<Res, Req, Options>>()
+28
-1
examples/openapi-ts-pinia-colada/src/client/core/serverSentEvents.gen.ts
···
5
5
export type ServerSentEventsOptions<TData = unknown> = Omit<RequestInit, 'method'> &
6
6
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
7
7
/**
8
8
+
* Fetch API implementation. You can use this option to provide a custom
9
9
+
* fetch instance.
10
10
+
*
11
11
+
* @default globalThis.fetch
12
12
+
*/
13
13
+
fetch?: typeof fetch
14
14
+
/**
15
15
+
* Implementing clients can call request interceptors inside this hook.
16
16
+
*/
17
17
+
onRequest?: (url: string, init: RequestInit) => Promise<Request>
18
18
+
/**
8
19
* Callback invoked when a network or parsing error occurs during streaming.
9
20
*
10
21
* This option applies only if the endpoint returns a stream of events.
···
21
32
* @returns Nothing (void).
22
33
*/
23
34
onSseEvent?: (event: StreamEvent<TData>) => void
35
35
+
serializedBody?: RequestInit['body']
24
36
/**
25
37
* Default retry delay in milliseconds.
26
38
*
···
68
80
}
69
81
70
82
export const createSseClient = <TData = unknown>({
83
83
+
onRequest,
71
84
onSseError,
72
85
onSseEvent,
73
86
responseTransformer,
···
103
116
}
104
117
105
118
try {
106
106
-
const response = await fetch(url, { ...options, headers, signal })
119
119
+
const requestInit: RequestInit = {
120
120
+
redirect: 'follow',
121
121
+
...options,
122
122
+
body: options.serializedBody,
123
123
+
headers,
124
124
+
signal
125
125
+
}
126
126
+
let request = new Request(url, requestInit)
127
127
+
if (onRequest) {
128
128
+
request = await onRequest(url, requestInit)
129
129
+
}
130
130
+
// fetch must be assigned here, otherwise it would throw the error:
131
131
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
132
132
+
const _fetch = options.fetch ?? globalThis.fetch
133
133
+
const response = await _fetch(request)
107
134
108
135
if (!response.ok) throw new Error(`SSE failed: ${response.status} ${response.statusText}`)
109
136
+30
-1
examples/openapi-ts-pinia-colada/src/client/core/utils.gen.ts
···
1
1
// This file is auto-generated by @hey-api/openapi-ts
2
2
3
3
-
import type { QuerySerializer } from './bodySerializer.gen'
3
3
+
import type { BodySerializer, QuerySerializer } from './bodySerializer.gen'
4
4
import {
5
5
type ArraySeparatorStyle,
6
6
serializeArrayParam,
···
109
109
}
110
110
return url
111
111
}
112
112
+
113
113
+
export function getValidRequestBody(options: {
114
114
+
body?: unknown
115
115
+
bodySerializer?: BodySerializer | null
116
116
+
serializedBody?: unknown
117
117
+
}) {
118
118
+
const hasBody = options.body !== undefined
119
119
+
const isSerializedBody = hasBody && options.bodySerializer
120
120
+
121
121
+
if (isSerializedBody) {
122
122
+
if ('serializedBody' in options) {
123
123
+
const hasSerializedBody =
124
124
+
options.serializedBody !== undefined && options.serializedBody !== ''
125
125
+
126
126
+
return hasSerializedBody ? options.serializedBody : null
127
127
+
}
128
128
+
129
129
+
// not all clients implement a serializedBody property (i.e. client-axios)
130
130
+
return options.body !== '' ? options.body : null
131
131
+
}
132
132
+
133
133
+
// plain/text body
134
134
+
if (hasBody) {
135
135
+
return options.body
136
136
+
}
137
137
+
138
138
+
// no body was provided
139
139
+
return undefined
140
140
+
}
+1
examples/openapi-ts-pinia-colada/src/client/index.ts
···
1
1
// This file is auto-generated by @hey-api/openapi-ts
2
2
+
2
3
export * from './@pinia/colada.gen'
3
4
export * from './sdk.gen'
4
5
export * from './types.gen'
+20
-28
examples/openapi-ts-pinia-colada/src/client/sdk.gen.ts
···
1
1
// This file is auto-generated by @hey-api/openapi-ts
2
2
3
3
import type { Client, Options as ClientOptions, TDataShape } from './client'
4
4
-
import { client as _heyApiClient } from './client.gen'
4
4
+
import { client } from './client.gen'
5
5
import type {
6
6
AddPetData,
7
7
AddPetErrors,
···
86
86
export const addPet = <ThrowOnError extends boolean = false>(
87
87
options: Options<AddPetData, ThrowOnError>
88
88
) =>
89
89
-
(options.client ?? _heyApiClient).post<AddPetResponses, AddPetErrors, ThrowOnError>({
89
89
+
(options.client ?? client).post<AddPetResponses, AddPetErrors, ThrowOnError>({
90
90
security: [
91
91
{
92
92
scheme: 'bearer',
···
108
108
export const updatePet = <ThrowOnError extends boolean = false>(
109
109
options: Options<UpdatePetData, ThrowOnError>
110
110
) =>
111
111
-
(options.client ?? _heyApiClient).put<UpdatePetResponses, UpdatePetErrors, ThrowOnError>({
111
111
+
(options.client ?? client).put<UpdatePetResponses, UpdatePetErrors, ThrowOnError>({
112
112
security: [
113
113
{
114
114
scheme: 'bearer',
···
130
130
export const findPetsByStatus = <ThrowOnError extends boolean = false>(
131
131
options: Options<FindPetsByStatusData, ThrowOnError>
132
132
) =>
133
133
-
(options.client ?? _heyApiClient).get<
134
134
-
FindPetsByStatusResponses,
135
135
-
FindPetsByStatusErrors,
136
136
-
ThrowOnError
137
137
-
>({
133
133
+
(options.client ?? client).get<FindPetsByStatusResponses, FindPetsByStatusErrors, ThrowOnError>({
138
134
security: [
139
135
{
140
136
scheme: 'bearer',
···
152
148
export const findPetsByTags = <ThrowOnError extends boolean = false>(
153
149
options: Options<FindPetsByTagsData, ThrowOnError>
154
150
) =>
155
155
-
(options.client ?? _heyApiClient).get<
156
156
-
FindPetsByTagsResponses,
157
157
-
FindPetsByTagsErrors,
158
158
-
ThrowOnError
159
159
-
>({
151
151
+
(options.client ?? client).get<FindPetsByTagsResponses, FindPetsByTagsErrors, ThrowOnError>({
160
152
security: [
161
153
{
162
154
scheme: 'bearer',
···
174
166
export const deletePet = <ThrowOnError extends boolean = false>(
175
167
options: Options<DeletePetData, ThrowOnError>
176
168
) =>
177
177
-
(options.client ?? _heyApiClient).delete<DeletePetResponses, DeletePetErrors, ThrowOnError>({
169
169
+
(options.client ?? client).delete<DeletePetResponses, DeletePetErrors, ThrowOnError>({
178
170
security: [
179
171
{
180
172
scheme: 'bearer',
···
192
184
export const getPetById = <ThrowOnError extends boolean = false>(
193
185
options: Options<GetPetByIdData, ThrowOnError>
194
186
) =>
195
195
-
(options.client ?? _heyApiClient).get<GetPetByIdResponses, GetPetByIdErrors, ThrowOnError>({
187
187
+
(options.client ?? client).get<GetPetByIdResponses, GetPetByIdErrors, ThrowOnError>({
196
188
security: [
197
189
{
198
190
name: 'api_key',
···
214
206
export const updatePetWithForm = <ThrowOnError extends boolean = false>(
215
207
options: Options<UpdatePetWithFormData, ThrowOnError>
216
208
) =>
217
217
-
(options.client ?? _heyApiClient).post<
209
209
+
(options.client ?? client).post<
218
210
UpdatePetWithFormResponses,
219
211
UpdatePetWithFormErrors,
220
212
ThrowOnError
···
236
228
export const uploadFile = <ThrowOnError extends boolean = false>(
237
229
options: Options<UploadFileData, ThrowOnError>
238
230
) =>
239
239
-
(options.client ?? _heyApiClient).post<UploadFileResponses, UploadFileErrors, ThrowOnError>({
231
231
+
(options.client ?? client).post<UploadFileResponses, UploadFileErrors, ThrowOnError>({
240
232
bodySerializer: null,
241
233
security: [
242
234
{
···
259
251
export const getInventory = <ThrowOnError extends boolean = false>(
260
252
options?: Options<GetInventoryData, ThrowOnError>
261
253
) =>
262
262
-
(options?.client ?? _heyApiClient).get<GetInventoryResponses, GetInventoryErrors, ThrowOnError>({
254
254
+
(options?.client ?? client).get<GetInventoryResponses, GetInventoryErrors, ThrowOnError>({
263
255
security: [
264
256
{
265
257
name: 'api_key',
···
277
269
export const placeOrder = <ThrowOnError extends boolean = false>(
278
270
options?: Options<PlaceOrderData, ThrowOnError>
279
271
) =>
280
280
-
(options?.client ?? _heyApiClient).post<PlaceOrderResponses, PlaceOrderErrors, ThrowOnError>({
272
272
+
(options?.client ?? client).post<PlaceOrderResponses, PlaceOrderErrors, ThrowOnError>({
281
273
url: '/store/order',
282
274
...options,
283
275
headers: {
···
293
285
export const deleteOrder = <ThrowOnError extends boolean = false>(
294
286
options: Options<DeleteOrderData, ThrowOnError>
295
287
) =>
296
296
-
(options.client ?? _heyApiClient).delete<DeleteOrderResponses, DeleteOrderErrors, ThrowOnError>({
288
288
+
(options.client ?? client).delete<DeleteOrderResponses, DeleteOrderErrors, ThrowOnError>({
297
289
url: '/store/order/{orderId}',
298
290
...options
299
291
})
···
305
297
export const getOrderById = <ThrowOnError extends boolean = false>(
306
298
options: Options<GetOrderByIdData, ThrowOnError>
307
299
) =>
308
308
-
(options.client ?? _heyApiClient).get<GetOrderByIdResponses, GetOrderByIdErrors, ThrowOnError>({
300
300
+
(options.client ?? client).get<GetOrderByIdResponses, GetOrderByIdErrors, ThrowOnError>({
309
301
url: '/store/order/{orderId}',
310
302
...options
311
303
})
···
317
309
export const createUser = <ThrowOnError extends boolean = false>(
318
310
options?: Options<CreateUserData, ThrowOnError>
319
311
) =>
320
320
-
(options?.client ?? _heyApiClient).post<CreateUserResponses, CreateUserErrors, ThrowOnError>({
312
312
+
(options?.client ?? client).post<CreateUserResponses, CreateUserErrors, ThrowOnError>({
321
313
url: '/user',
322
314
...options,
323
315
headers: {
···
333
325
export const createUsersWithListInput = <ThrowOnError extends boolean = false>(
334
326
options?: Options<CreateUsersWithListInputData, ThrowOnError>
335
327
) =>
336
336
-
(options?.client ?? _heyApiClient).post<
328
328
+
(options?.client ?? client).post<
337
329
CreateUsersWithListInputResponses,
338
330
CreateUsersWithListInputErrors,
339
331
ThrowOnError
···
353
345
export const loginUser = <ThrowOnError extends boolean = false>(
354
346
options?: Options<LoginUserData, ThrowOnError>
355
347
) =>
356
356
-
(options?.client ?? _heyApiClient).get<LoginUserResponses, LoginUserErrors, ThrowOnError>({
348
348
+
(options?.client ?? client).get<LoginUserResponses, LoginUserErrors, ThrowOnError>({
357
349
url: '/user/login',
358
350
...options
359
351
})
···
365
357
export const logoutUser = <ThrowOnError extends boolean = false>(
366
358
options?: Options<LogoutUserData, ThrowOnError>
367
359
) =>
368
368
-
(options?.client ?? _heyApiClient).get<LogoutUserResponses, LogoutUserErrors, ThrowOnError>({
360
360
+
(options?.client ?? client).get<LogoutUserResponses, LogoutUserErrors, ThrowOnError>({
369
361
url: '/user/logout',
370
362
...options
371
363
})
···
377
369
export const deleteUser = <ThrowOnError extends boolean = false>(
378
370
options: Options<DeleteUserData, ThrowOnError>
379
371
) =>
380
380
-
(options.client ?? _heyApiClient).delete<DeleteUserResponses, DeleteUserErrors, ThrowOnError>({
372
372
+
(options.client ?? client).delete<DeleteUserResponses, DeleteUserErrors, ThrowOnError>({
381
373
url: '/user/{username}',
382
374
...options
383
375
})
···
389
381
export const getUserByName = <ThrowOnError extends boolean = false>(
390
382
options: Options<GetUserByNameData, ThrowOnError>
391
383
) =>
392
392
-
(options.client ?? _heyApiClient).get<GetUserByNameResponses, GetUserByNameErrors, ThrowOnError>({
384
384
+
(options.client ?? client).get<GetUserByNameResponses, GetUserByNameErrors, ThrowOnError>({
393
385
url: '/user/{username}',
394
386
...options
395
387
})
···
401
393
export const updateUser = <ThrowOnError extends boolean = false>(
402
394
options: Options<UpdateUserData, ThrowOnError>
403
395
) =>
404
404
-
(options.client ?? _heyApiClient).put<UpdateUserResponses, UpdateUserErrors, ThrowOnError>({
396
396
+
(options.client ?? client).put<UpdateUserResponses, UpdateUserErrors, ThrowOnError>({
405
397
url: '/user/{username}',
406
398
...options,
407
399
headers: {