-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetstore-openapi.d.ts
371 lines (319 loc) · 14 KB
/
petstore-openapi.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Code generated by typed-fetch. DO NOT EDIT.
// https://github.com/RPGillespie6/typed-fetch
// Shared types
type RequestInitExtended = {
// If you want the response data to be parse as something other than json (json is default)
parseAs?: "json" | "text" | "blob" | "arrayBuffer" | "formData" | "bytes";
// local body serializer -- allows you to customize how the body is serialized before sending
// normally not needed unless you are using something like XML instead of JSON
bodySerializer?: (body: any) => BodyInit | null;
// local query serializer -- allows you to customize how the query is serialized before sending
// normally not needed unless you are using some custom array serialization like {foo: [1,2,3,4]} => ?foo=1;2;3;4
querySerializer?: (query: any) => string;
}
// Component types
type ComponentSchemaAddress = {
/** Example: Palo Alto */
city?: string;
/** Example: CA */
state?: string;
/** Example: 437 Lytton */
street?: string;
/** Example: 94301 */
zip?: string;
}
type ComponentSchemaApiResponse = {
code?: number;
message?: string;
type?: string;
}
type ComponentSchemaCategory = {
id?: number;
/** Example: Dogs */
name?: string;
}
type ComponentSchemaCustomer = {
address?: ComponentSchemaAddress[];
id?: number;
/** Example: fehguy */
username?: string;
}
type ComponentSchemaOrder = {
complete?: boolean;
id?: number;
petId?: number;
quantity?: number;
shipDate?: string;
/** Order Status; Example: approved */
status?: 'placed' | 'approved' | 'delivered';
}
type ComponentSchemaPet = {
category?: ComponentSchemaCategory;
id?: number;
/** Example: doggie */
name: string;
photoUrls: string[];
/** pet status in the store */
status?: 'available' | 'pending' | 'sold';
tags?: ComponentSchemaTag[];
}
type ComponentSchemaTag = {
id?: number;
name?: string;
}
type ComponentSchemaUser = {
/** Example: [email protected] */
email?: string;
/** Example: John */
firstName?: string;
id?: number;
/** Example: James */
lastName?: string;
/** Example: 12345 */
password?: string;
/** Example: 12345 */
phone?: string;
/** User Status */
userStatus?: number;
/** Example: theUser */
username?: string;
}
// Request/Response types
// PUT /pet
type BodyPutPet = ComponentSchemaPet;
type RequestPutPet = Omit<RequestInit, 'body'> & { body: BodyPutPet; } & RequestInitExtended;
type ResponseDataPutPet = ComponentSchemaPet;
type ResponseErrorPutPet = {};
// POST /pet
type BodyPostPet = ComponentSchemaPet;
type RequestPostPet = Omit<RequestInit, 'body'> & { body: BodyPostPet; } & RequestInitExtended;
type ResponseDataPostPet = ComponentSchemaPet;
type ResponseErrorPostPet = {};
// GET /pet/findByStatus
type ParamGetPetFindByStatus = {
query?: {
/** Status values that need to be considered for filter */
status?: 'available' | 'pending' | 'sold';
};
}
type RequestGetPetFindByStatus = RequestInit & { params?: ParamGetPetFindByStatus; } & RequestInitExtended;
type ResponseDataGetPetFindByStatus = ComponentSchemaPet[];
type ResponseErrorGetPetFindByStatus = {};
// GET /pet/findByTags
type ParamGetPetFindByTags = {
query?: {
/** Tags to filter by */
tags?: string[];
};
}
type RequestGetPetFindByTags = RequestInit & { params?: ParamGetPetFindByTags; } & RequestInitExtended;
type ResponseDataGetPetFindByTags = ComponentSchemaPet[];
type ResponseErrorGetPetFindByTags = {};
// GET /pet/{petId}
type ParamGetPetPetId = {
path: {
/** ID of pet to return */
petId: number;
};
}
type RequestGetPetPetId = RequestInit & { params: ParamGetPetPetId; } & RequestInitExtended;
type ResponseDataGetPetPetId = ComponentSchemaPet;
type ResponseErrorGetPetPetId = {};
// POST /pet/{petId}
type ParamPostPetPetId = {
path: {
/** ID of pet that needs to be updated */
petId: number;
};
query?: {
/** Name of pet that needs to be updated */
name?: string;
/** Status of pet that needs to be updated */
status?: string;
};
}
type RequestPostPetPetId = RequestInit & { params: ParamPostPetPetId; } & RequestInitExtended;
type ResponseDataPostPetPetId = {};
type ResponseErrorPostPetPetId = {};
// DELETE /pet/{petId}
type ParamDeletePetPetId = {
header?: {
api_key?: string;
};
path: {
/** Pet id to delete */
petId: number;
};
}
type RequestDeletePetPetId = RequestInit & { params: ParamDeletePetPetId; } & RequestInitExtended;
type ResponseDataDeletePetPetId = {};
type ResponseErrorDeletePetPetId = {};
// POST /pet/{petId}/uploadImage
type ParamPostPetPetIdUploadImage = {
path: {
/** ID of pet to update */
petId: number;
};
query?: {
/** Additional Metadata */
additionalMetadata?: string;
};
}
type BodyPostPetPetIdUploadImage = string;
type RequestPostPetPetIdUploadImage = Omit<RequestInit, 'body'> & { params: ParamPostPetPetIdUploadImage; body?: BodyPostPetPetIdUploadImage; } & RequestInitExtended;
type ResponseDataPostPetPetIdUploadImage = ComponentSchemaApiResponse;
type ResponseErrorPostPetPetIdUploadImage = {};
// GET /store/inventory
type RequestGetStoreInventory = RequestInit & RequestInitExtended;
type ResponseDataGetStoreInventory = {
[key: string]: number;
};
type ResponseErrorGetStoreInventory = {};
// POST /store/order
type BodyPostStoreOrder = ComponentSchemaOrder;
type RequestPostStoreOrder = Omit<RequestInit, 'body'> & { body?: BodyPostStoreOrder; } & RequestInitExtended;
type ResponseDataPostStoreOrder = ComponentSchemaOrder;
type ResponseErrorPostStoreOrder = {};
// GET /store/order/{orderId}
type ParamGetStoreOrderOrderId = {
path: {
/** ID of order that needs to be fetched */
orderId: number;
};
}
type RequestGetStoreOrderOrderId = RequestInit & { params: ParamGetStoreOrderOrderId; } & RequestInitExtended;
type ResponseDataGetStoreOrderOrderId = ComponentSchemaOrder;
type ResponseErrorGetStoreOrderOrderId = {};
// DELETE /store/order/{orderId}
type ParamDeleteStoreOrderOrderId = {
path: {
/** ID of the order that needs to be deleted */
orderId: number;
};
}
type RequestDeleteStoreOrderOrderId = RequestInit & { params: ParamDeleteStoreOrderOrderId; } & RequestInitExtended;
type ResponseDataDeleteStoreOrderOrderId = {};
type ResponseErrorDeleteStoreOrderOrderId = {};
// POST /user
type BodyPostUser = ComponentSchemaUser;
type RequestPostUser = Omit<RequestInit, 'body'> & { body?: BodyPostUser; } & RequestInitExtended;
type ResponseDataPostUser = ComponentSchemaUser;
type ResponseErrorPostUser = ComponentSchemaUser;
// POST /user/createWithList
type BodyPostUserCreateWithList = ComponentSchemaUser[];
type RequestPostUserCreateWithList = Omit<RequestInit, 'body'> & { body?: BodyPostUserCreateWithList; } & RequestInitExtended;
type ResponseDataPostUserCreateWithList = ComponentSchemaUser;
type ResponseErrorPostUserCreateWithList = {};
// GET /user/login
type ParamGetUserLogin = {
query?: {
/** The user name for login */
username?: string;
/** The password for login in clear text */
password?: string;
};
}
type RequestGetUserLogin = RequestInit & { params?: ParamGetUserLogin; } & RequestInitExtended;
type ResponseDataGetUserLogin = string;
type ResponseErrorGetUserLogin = {};
// GET /user/logout
type RequestGetUserLogout = RequestInit & RequestInitExtended;
type ResponseDataGetUserLogout = {};
type ResponseErrorGetUserLogout = {};
// GET /user/{username}
type ParamGetUserUsername = {
path: {
/** The name that needs to be fetched. Use user1 for testing. */
username: string;
};
}
type RequestGetUserUsername = RequestInit & { params: ParamGetUserUsername; } & RequestInitExtended;
type ResponseDataGetUserUsername = ComponentSchemaUser;
type ResponseErrorGetUserUsername = {};
// PUT /user/{username}
type ParamPutUserUsername = {
path: {
/** name that needs to be updated */
username: string;
};
}
type BodyPutUserUsername = ComponentSchemaUser;
type RequestPutUserUsername = Omit<RequestInit, 'body'> & { params: ParamPutUserUsername; body?: BodyPutUserUsername; } & RequestInitExtended;
type ResponseDataPutUserUsername = {};
type ResponseErrorPutUserUsername = {};
// DELETE /user/{username}
type ParamDeleteUserUsername = {
path: {
/** The name that needs to be deleted */
username: string;
};
}
type RequestDeleteUserUsername = RequestInit & { params: ParamDeleteUserUsername; } & RequestInitExtended;
type ResponseDataDeleteUserUsername = {};
type ResponseErrorDeleteUserUsername = {};
// Response Generics
type DataResponse<D> = { data: D; error: undefined; response: Response; };
type ErrorResponse<E> = { data: undefined; error: E; response: Response; };
type FetchResponse<D, E> = DataResponse<D> | ErrorResponse<E>;
// Generics Type Lookups
// These are lookup tables for each method type (GET, POST, etc) to match the url to its payload
type GetTypesLookup = {
"/pet/findByStatus": { init?: RequestGetPetFindByStatus, response: FetchResponse<ResponseDataGetPetFindByStatus, ResponseErrorGetPetFindByStatus> },
"/pet/findByTags": { init?: RequestGetPetFindByTags, response: FetchResponse<ResponseDataGetPetFindByTags, ResponseErrorGetPetFindByTags> },
"/pet/{petId}": { init: RequestGetPetPetId, response: FetchResponse<ResponseDataGetPetPetId, ResponseErrorGetPetPetId> },
"/store/inventory": { init?: RequestGetStoreInventory, response: FetchResponse<ResponseDataGetStoreInventory, ResponseErrorGetStoreInventory> },
"/store/order/{orderId}": { init: RequestGetStoreOrderOrderId, response: FetchResponse<ResponseDataGetStoreOrderOrderId, ResponseErrorGetStoreOrderOrderId> },
"/user/login": { init?: RequestGetUserLogin, response: FetchResponse<ResponseDataGetUserLogin, ResponseErrorGetUserLogin> },
"/user/logout": { init?: RequestGetUserLogout, response: FetchResponse<ResponseDataGetUserLogout, ResponseErrorGetUserLogout> },
"/user/{username}": { init: RequestGetUserUsername, response: FetchResponse<ResponseDataGetUserUsername, ResponseErrorGetUserUsername> }
};
type DeleteTypesLookup = {
"/pet/{petId}": { init: RequestDeletePetPetId, response: FetchResponse<ResponseDataDeletePetPetId, ResponseErrorDeletePetPetId> },
"/store/order/{orderId}": { init: RequestDeleteStoreOrderOrderId, response: FetchResponse<ResponseDataDeleteStoreOrderOrderId, ResponseErrorDeleteStoreOrderOrderId> },
"/user/{username}": { init: RequestDeleteUserUsername, response: FetchResponse<ResponseDataDeleteUserUsername, ResponseErrorDeleteUserUsername> }
};
type PutTypesLookup = {
"/pet": { init: RequestPutPet, response: FetchResponse<ResponseDataPutPet, ResponseErrorPutPet> },
"/user/{username}": { init: RequestPutUserUsername, response: FetchResponse<ResponseDataPutUserUsername, ResponseErrorPutUserUsername> }
};
type PostTypesLookup = {
"/pet": { init: RequestPostPet, response: FetchResponse<ResponseDataPostPet, ResponseErrorPostPet> },
"/pet/{petId}": { init: RequestPostPetPetId, response: FetchResponse<ResponseDataPostPetPetId, ResponseErrorPostPetPetId> },
"/pet/{petId}/uploadImage": { init: RequestPostPetPetIdUploadImage, response: FetchResponse<ResponseDataPostPetPetIdUploadImage, ResponseErrorPostPetPetIdUploadImage> },
"/store/order": { init?: RequestPostStoreOrder, response: FetchResponse<ResponseDataPostStoreOrder, ResponseErrorPostStoreOrder> },
"/user": { init?: RequestPostUser, response: FetchResponse<ResponseDataPostUser, ResponseErrorPostUser> },
"/user/createWithList": { init?: RequestPostUserCreateWithList, response: FetchResponse<ResponseDataPostUserCreateWithList, ResponseErrorPostUserCreateWithList> }
};
/*
We could just generate a bunch of overloaded functions in the interface like:
GET(url: "/pet", init?: FetchRequestGetPet): Promise<FetchResponse<GetPetResponse, GetPetError>>;
GET(url: "/store/order/{orderId}", init: FetchRequestGetPet2): Promise<FetchResponse<GetPet2Response, GetPet2Error>>;
GET(url: "/user/login", init?: FetchRequestGetPet3): Promise<FetchResponse<GetPet3Response, GetPet3Error>>;
etc
This approach is valid and correctly type checks request and response shape.
However, for some reason, VSCode intellisense doesn't work well with overloads;
it gets confused as to which overload you want and won't list out request body properties, etc.
So instead, we just use 1 generic function per method type using a lookup table to match the url to its payload.
This seems to work better with VSCode intellisense...
...However, it requires some evil TypeScript magic to make a single generic function behave like the group of overloads above
With generics, it's much trickier to allow both init and init? in the same function signature
These two helpers below are the only solution I was able to find to achieve that, at the cost of readability...
TODO: Revisit this in the future to see if overloads work better with intellisense, because it's a much simpler solution
and doesn't require the evil TypeScript magic
*/
// https://stackoverflow.com/questions/52984808/is-there-a-way-to-get-all-required-properties-of-a-typescript-object
// Example: OptionalKeys<{a: string, b?: number}> = "b"
type OptionalKeys<T extends object> = keyof { [K in keyof T as {} extends Pick<T, K> ? K : never]: any }
// https://stackoverflow.com/questions/77714794/how-to-use-void-to-make-function-parameters-optional-when-using-generics
// Basically, this is a way to achieve i.e. GET(url, init?) if init is optional, and GET(url, init) if init is required
type ClientMethod<Lookup extends Record<string, any>> = <Url extends keyof Lookup>(
url: Url,
...[init]: "init" extends OptionalKeys<Lookup[Url]> ? [init?: Lookup[Url]["init"]] : [init: Lookup[Url]["init"]]
) => Lookup[Url]["response"];
// end evil TypeScript magic
export interface Client {
GET: ClientMethod<GetTypesLookup>;
DELETE: ClientMethod<DeleteTypesLookup>;
PUT: ClientMethod<PutTypesLookup>;
POST: ClientMethod<PostTypesLookup>;
}