-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpetstore-v3.ts
247 lines (200 loc) · 6.18 KB
/
petstore-v3.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
// Auto-generated by https://github.com/vladkens/apigen-ts
// Source: https://petstore3.swagger.io/api/v3/openapi.json
type Headers = Record<string, string>
export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise<Headers>)
export interface ApigenConfig {
baseUrl: string
headers: ApigenHeaders
}
export interface ApigenRequest extends Omit<RequestInit, "body"> {
search?: Record<string, unknown>
body?: unknown
}
export class ApiClient {
ISO_FORMAT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/
Config: ApigenConfig
constructor(config?: Partial<ApigenConfig>) {
this.Config = { baseUrl: "/", headers: {}, ...config }
}
PopulateDates<T>(d: T): T {
if (d === null || d === undefined || typeof d !== "object") return d
const t = d as unknown as Record<string, unknown>
for (const [k, v] of Object.entries(t)) {
if (typeof v === "string" && this.ISO_FORMAT.test(v)) t[k] = new Date(v)
else if (typeof v === "object") this.PopulateDates(v)
}
return d
}
async ParseError(rep: Response) {
try {
return await rep.json()
} catch (e) {
throw rep
}
}
PrepareFetchUrl(path: string): URL {
let base = this.Config.baseUrl
if ("location" in globalThis && (base === "" || base.startsWith("/"))) {
const { location } = globalThis as unknown as { location: { origin: string } }
base = `${location.origin}${base.endsWith("/") ? base : `/${base}`}`
}
return new URL(path, base)
}
async Fetch<T>(method: string, path: string, opts: ApigenRequest = {}): Promise<T> {
const url = this.PrepareFetchUrl(path)
for (const [k, v] of Object.entries(opts?.search ?? {})) {
url.searchParams.append(k, Array.isArray(v) ? v.join(",") : (v as string))
}
const configHeaders =
typeof this.Config.headers === "function"
? await this.Config.headers(method, path)
: this.Config.headers
const headers = new Headers({ ...configHeaders, ...opts.headers })
const ct = headers.get("content-type") ?? "application/json"
let body: FormData | URLSearchParams | string | undefined = undefined
if (ct === "multipart/form-data" || ct === "application/x-www-form-urlencoded") {
headers.delete("content-type")
body = ct === "multipart/form-data" ? new FormData() : new URLSearchParams()
for (const [k, v] of Object.entries(opts.body as Record<string, string>)) {
body.append(k, v)
}
}
if (ct === "application/json" && typeof opts.body !== "string") {
headers.set("content-type", "application/json")
body = JSON.stringify(opts.body)
}
const credentials = opts.credentials ?? "include"
const rep = await fetch(url.toString(), { method, ...opts, headers, body, credentials })
if (!rep.ok) throw await this.ParseError(rep)
const rs = await rep.text()
try {
return this.PopulateDates(JSON.parse(rs) as T)
} catch (e) {
return rs as unknown as T
}
}
pet = {
addPet: (body: Pet) => {
return this.Fetch<Pet>("post", "/pet", { body })
},
updatePet: (body: Pet) => {
return this.Fetch<Pet>("put", "/pet", { body })
},
findPetsByStatus: (search: { status?: "available" | "pending" | "sold" }) => {
return this.Fetch<Pet[]>("get", "/pet/findByStatus", { search })
},
findPetsByTags: (search: { tags?: string[] }) => {
return this.Fetch<Pet[]>("get", "/pet/findByTags", { search })
},
getPetById: (petId: number) => {
return this.Fetch<Pet>("get", `/pet/${petId}`, {})
},
updatePetWithForm: (
petId: number,
search: {
name?: string
status?: string
},
) => {
return this.Fetch<void>("post", `/pet/${petId}`, { search })
},
deletePet: (petId: number) => {
return this.Fetch<void>("delete", `/pet/${petId}`, {})
},
uploadFile: (
petId: number,
search: {
additionalMetadata?: string
},
) => {
return this.Fetch<ApiResponse>("post", `/pet/${petId}/uploadImage`, { search })
},
}
store = {
getInventory: () => {
return this.Fetch<Record<string, number>>("get", "/store/inventory", {})
},
placeOrder: (body: Order) => {
return this.Fetch<Order>("post", "/store/order", { body })
},
getOrderById: (orderId: number) => {
return this.Fetch<Order>("get", `/store/order/${orderId}`, {})
},
deleteOrder: (orderId: number) => {
return this.Fetch<void>("delete", `/store/order/${orderId}`, {})
},
}
user = {
createUser: (body: User) => {
return this.Fetch<void>("post", "/user", { body })
},
createUsersWithListInput: (body: User[]) => {
return this.Fetch<User>("post", "/user/createWithList", { body })
},
loginUser: (search: { username?: string; password?: string }) => {
return this.Fetch<string>("get", "/user/#", { search })
},
logoutUser: () => {
return this.Fetch<void>("get", "/user/logout", {})
},
getUserByName: (username: string) => {
return this.Fetch<User>("get", `/user/${username}`, {})
},
updateUser: (username: string, body: User) => {
return this.Fetch<void>("put", `/user/${username}`, { body })
},
deleteUser: (username: string) => {
return this.Fetch<void>("delete", `/user/${username}`, {})
},
}
}
export type Address = {
street?: string
city?: string
state?: string
zip?: string
}
export type ApiResponse = {
code?: number
type?: string
message?: string
}
export type Category = {
id?: number
name?: string
}
export type Customer = {
id?: number
username?: string
address?: Address[]
}
export type Order = {
id?: number
petId?: number
quantity?: number
shipDate?: Date
status?: "placed" | "approved" | "delivered"
complete?: boolean
}
export type Pet = {
id?: number
name: string
category?: Category
photoUrls: string[]
tags?: Tag[]
status?: "available" | "pending" | "sold"
}
export type Tag = {
id?: number
name?: string
}
export type User = {
id?: number
username?: string
firstName?: string
lastName?: string
email?: string
password?: string
phone?: string
userStatus?: number
}