|
4 | 4 | * Swagger Petstore
|
5 | 5 | * OpenAPI spec version: 1.0.0
|
6 | 6 | */
|
7 |
| -import axios from 'axios' |
8 |
| -import type { |
9 |
| - AxiosRequestConfig, |
10 |
| - AxiosResponse |
11 |
| -} from 'axios' |
| 7 | +import axios from 'axios'; |
| 8 | +import type { AxiosRequestConfig, AxiosResponse } from 'axios'; |
12 | 9 | import type {
|
13 | 10 | CreatePetsBody,
|
14 | 11 | ListPetsNestedArrayParams,
|
15 |
| - ListPetsParams |
16 |
| -} from '../model' |
17 |
| -import { |
18 |
| - faker |
19 |
| -} from '@faker-js/faker' |
20 |
| -import { |
21 |
| - HttpResponse, |
22 |
| - delay, |
23 |
| - http |
24 |
| -} from 'msw' |
25 |
| -import type { |
26 |
| - Pet, |
27 |
| - PetsArray, |
28 |
| - PetsNestedArray |
29 |
| -} from '../model' |
| 12 | + ListPetsParams, |
| 13 | +} from '../model'; |
| 14 | +import { faker } from '@faker-js/faker'; |
| 15 | +import { HttpResponse, delay, http } from 'msw'; |
| 16 | +import type { Pet, PetsArray, PetsNestedArray } from '../model'; |
30 | 17 | import listPetsMutator from '../mutator/response-type';
|
31 | 18 |
|
32 |
| - |
33 |
| - |
34 |
| - |
35 |
| - /** |
| 19 | +/** |
36 | 20 | * @summary List all pets
|
37 | 21 | */
|
38 |
| -export const listPets = ( |
39 |
| - params?: ListPetsParams, |
40 |
| - version: number = 1, |
41 |
| - ) => { |
42 |
| - return listPetsMutator<PetsArray>( |
43 |
| - {url: `/v${version}/pets`, method: 'GET', |
44 |
| - params |
45 |
| - }, |
46 |
| - ); |
47 |
| - } |
48 |
| - |
| 22 | +export const listPets = (params?: ListPetsParams, version: number = 1) => { |
| 23 | + return listPetsMutator<PetsArray>({ |
| 24 | + url: `/v${version}/pets`, |
| 25 | + method: 'GET', |
| 26 | + params, |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
49 | 30 | /**
|
50 | 31 | * @summary Create a pet
|
51 | 32 | */
|
52 | 33 | export const createPets = <TData = AxiosResponse<void>>(
|
53 |
| - createPetsBody: CreatePetsBody, |
54 |
| - version: number = 1, options?: AxiosRequestConfig |
55 |
| - ): Promise<TData> => { |
56 |
| - return axios.post( |
57 |
| - `/v${version}/pets`, |
58 |
| - createPetsBody,options |
59 |
| - ); |
60 |
| - } |
| 34 | + createPetsBody: CreatePetsBody, |
| 35 | + version: number = 1, |
| 36 | + options?: AxiosRequestConfig, |
| 37 | +): Promise<TData> => { |
| 38 | + return axios.post(`/v${version}/pets`, createPetsBody, options); |
| 39 | +}; |
61 | 40 |
|
62 | 41 | /**
|
63 | 42 | * @summary List all pets as nested array
|
64 | 43 | */
|
65 | 44 | export const listPetsNestedArray = <TData = AxiosResponse<PetsNestedArray>>(
|
66 |
| - params?: ListPetsNestedArrayParams, |
67 |
| - version: number = 1, options?: AxiosRequestConfig |
68 |
| - ): Promise<TData> => { |
69 |
| - return axios.get( |
70 |
| - `/v${version}/pets-nested-array`,{ |
| 45 | + params?: ListPetsNestedArrayParams, |
| 46 | + version: number = 1, |
| 47 | + options?: AxiosRequestConfig, |
| 48 | +): Promise<TData> => { |
| 49 | + return axios.get(`/v${version}/pets-nested-array`, { |
71 | 50 | ...options,
|
72 |
| - params: {...params, ...options?.params},} |
73 |
| - ); |
74 |
| - } |
| 51 | + params: { ...params, ...options?.params }, |
| 52 | + }); |
| 53 | +}; |
75 | 54 |
|
76 | 55 | /**
|
77 | 56 | * @summary Info for a specific pet
|
78 | 57 | */
|
79 | 58 | export const showPetById = <TData = AxiosResponse<Pet>>(
|
80 |
| - petId: string, |
81 |
| - version: number = 1, options?: AxiosRequestConfig |
82 |
| - ): Promise<TData> => { |
83 |
| - return axios.get( |
84 |
| - `/v${version}/pets/${petId}`,options |
85 |
| - ); |
86 |
| - } |
87 |
| - |
| 59 | + petId: string, |
| 60 | + version: number = 1, |
| 61 | + options?: AxiosRequestConfig, |
| 62 | +): Promise<TData> => { |
| 63 | + return axios.get(`/v${version}/pets/${petId}`, options); |
| 64 | +}; |
88 | 65 |
|
89 | 66 | type AwaitedInput<T> = PromiseLike<T> | T;
|
90 | 67 |
|
91 |
| - type Awaited<O> = O extends AwaitedInput<infer T> ? T : never; |
92 |
| - |
93 |
| -export type ListPetsResult = NonNullable<Awaited<ReturnType<typeof listPets>>> |
94 |
| -export type CreatePetsResult = AxiosResponse<void> |
95 |
| -export type ListPetsNestedArrayResult = AxiosResponse<PetsNestedArray> |
96 |
| -export type ShowPetByIdResult = AxiosResponse<Pet> |
97 |
| - |
98 |
| - |
99 |
| -export const getListPetsResponseMock = (): PetsArray => (Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => ({age: faker.helpers.arrayElement([faker.number.int({min: 0, max: 30}), undefined]), callingCode: faker.helpers.arrayElement([faker.helpers.arrayElement(['+33','+420','+33'] as const), undefined]), country: faker.helpers.arrayElement([faker.helpers.arrayElement(['People\'s Republic of China','Uruguay'] as const), undefined]), email: faker.helpers.arrayElement([faker.internet.email(), undefined]), id: faker.number.int({min: undefined, max: undefined}), name: 'jon', tag: faker.helpers.arrayElement(['jon', null])}))) |
100 |
| - |
101 |
| -export const getListPetsNestedArrayResponseMock = (overrideResponse: Partial< PetsNestedArray > = {}): PetsNestedArray => ({data: faker.helpers.arrayElement([Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => ({age: faker.helpers.arrayElement([faker.number.int({min: 0, max: 30}), undefined]), callingCode: faker.helpers.arrayElement([faker.helpers.arrayElement(['+33','+420','+33'] as const), undefined]), country: faker.helpers.arrayElement([faker.helpers.arrayElement(['People\'s Republic of China','Uruguay'] as const), undefined]), email: faker.helpers.arrayElement([faker.internet.email(), undefined]), id: faker.number.int({min: undefined, max: undefined}), name: 'jon', tag: faker.helpers.arrayElement(['jon', null])})), undefined]), ...overrideResponse}) |
102 |
| - |
103 |
| -export const getShowPetByIdResponseMock = () => ((() => ({ |
104 |
| - id: faker.number.int({ min: 1, max: 99 }), |
105 |
| - name: faker.person.firstName(), |
106 |
| - tag: faker.helpers.arrayElement([ |
107 |
| - faker.word.sample(), |
108 |
| - void 0 |
109 |
| - ]) |
110 |
| - }))()) |
111 |
| - |
112 |
| - |
113 |
| -export const getListPetsMockHandler = (overrideResponse?: PetsArray | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => PetsArray)) => { |
| 68 | +type Awaited<O> = O extends AwaitedInput<infer T> ? T : never; |
| 69 | + |
| 70 | +export type ListPetsResult = NonNullable<Awaited<ReturnType<typeof listPets>>>; |
| 71 | +export type CreatePetsResult = AxiosResponse<void>; |
| 72 | +export type ListPetsNestedArrayResult = AxiosResponse<PetsNestedArray>; |
| 73 | +export type ShowPetByIdResult = AxiosResponse<Pet>; |
| 74 | + |
| 75 | +export const getListPetsResponseMock = (): PetsArray => |
| 76 | + Array.from( |
| 77 | + { length: faker.number.int({ min: 1, max: 10 }) }, |
| 78 | + (_, i) => i + 1, |
| 79 | + ).map(() => ({ |
| 80 | + age: faker.helpers.arrayElement([ |
| 81 | + faker.number.int({ min: 0, max: 30 }), |
| 82 | + undefined, |
| 83 | + ]), |
| 84 | + callingCode: faker.helpers.arrayElement([ |
| 85 | + faker.helpers.arrayElement(['+33', '+420', '+33'] as const), |
| 86 | + undefined, |
| 87 | + ]), |
| 88 | + country: faker.helpers.arrayElement([ |
| 89 | + faker.helpers.arrayElement([ |
| 90 | + "People's Republic of China", |
| 91 | + 'Uruguay', |
| 92 | + ] as const), |
| 93 | + undefined, |
| 94 | + ]), |
| 95 | + email: faker.helpers.arrayElement([faker.internet.email(), undefined]), |
| 96 | + id: faker.number.int({ min: undefined, max: undefined }), |
| 97 | + name: 'jon', |
| 98 | + tag: faker.helpers.arrayElement(['jon', null]), |
| 99 | + })); |
| 100 | + |
| 101 | +export const getListPetsNestedArrayResponseMock = ( |
| 102 | + overrideResponse: Partial<PetsNestedArray> = {}, |
| 103 | +): PetsNestedArray => ({ |
| 104 | + data: faker.helpers.arrayElement([ |
| 105 | + Array.from( |
| 106 | + { length: faker.number.int({ min: 1, max: 10 }) }, |
| 107 | + (_, i) => i + 1, |
| 108 | + ).map(() => ({ |
| 109 | + age: faker.helpers.arrayElement([ |
| 110 | + faker.number.int({ min: 0, max: 30 }), |
| 111 | + undefined, |
| 112 | + ]), |
| 113 | + callingCode: faker.helpers.arrayElement([ |
| 114 | + faker.helpers.arrayElement(['+33', '+420', '+33'] as const), |
| 115 | + undefined, |
| 116 | + ]), |
| 117 | + country: faker.helpers.arrayElement([ |
| 118 | + faker.helpers.arrayElement([ |
| 119 | + "People's Republic of China", |
| 120 | + 'Uruguay', |
| 121 | + ] as const), |
| 122 | + undefined, |
| 123 | + ]), |
| 124 | + email: faker.helpers.arrayElement([faker.internet.email(), undefined]), |
| 125 | + id: faker.number.int({ min: undefined, max: undefined }), |
| 126 | + name: 'jon', |
| 127 | + tag: faker.helpers.arrayElement(['jon', null]), |
| 128 | + })), |
| 129 | + undefined, |
| 130 | + ]), |
| 131 | + ...overrideResponse, |
| 132 | +}); |
| 133 | + |
| 134 | +export const getShowPetByIdResponseMock = () => |
| 135 | + (() => ({ |
| 136 | + id: faker.number.int({ min: 1, max: 99 }), |
| 137 | + name: faker.person.firstName(), |
| 138 | + tag: faker.helpers.arrayElement([faker.word.sample(), void 0]), |
| 139 | + }))(); |
| 140 | + |
| 141 | +export const getListPetsMockHandler = ( |
| 142 | + overrideResponse?: |
| 143 | + | PetsArray |
| 144 | + | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => PetsArray), |
| 145 | +) => { |
114 | 146 | return http.get('*/v:version/pets', async (info) => {
|
115 | 147 | await delay(1000);
|
116 |
| - return new HttpResponse(JSON.stringify(overrideResponse !== undefined |
117 |
| - ? (typeof overrideResponse === "function" ? overrideResponse(info) : overrideResponse) |
118 |
| - : getListPetsResponseMock()), |
| 148 | + return new HttpResponse( |
| 149 | + JSON.stringify( |
| 150 | + overrideResponse !== undefined |
| 151 | + ? typeof overrideResponse === 'function' |
| 152 | + ? overrideResponse(info) |
| 153 | + : overrideResponse |
| 154 | + : getListPetsResponseMock(), |
| 155 | + ), |
119 | 156 | {
|
120 | 157 | status: 200,
|
121 | 158 | headers: {
|
122 | 159 | 'Content-Type': 'application/json',
|
123 |
| - } |
124 |
| - } |
125 |
| - ) |
126 |
| - }) |
127 |
| -} |
| 160 | + }, |
| 161 | + }, |
| 162 | + ); |
| 163 | + }); |
| 164 | +}; |
128 | 165 |
|
129 | 166 | export const getCreatePetsMockHandler = () => {
|
130 | 167 | return http.post('*/v:version/pets', async () => {
|
131 | 168 | await delay(1000);
|
132 |
| - return new HttpResponse(null, |
133 |
| - { |
134 |
| - status: 200, |
135 |
| - headers: { |
136 |
| - 'Content-Type': 'application/json', |
137 |
| - } |
138 |
| - } |
139 |
| - ) |
140 |
| - }) |
141 |
| -} |
142 |
| - |
143 |
| -export const getListPetsNestedArrayMockHandler = (overrideResponse?: PetsNestedArray | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => PetsNestedArray)) => { |
| 169 | + return new HttpResponse(null, { |
| 170 | + status: 200, |
| 171 | + headers: { |
| 172 | + 'Content-Type': 'application/json', |
| 173 | + }, |
| 174 | + }); |
| 175 | + }); |
| 176 | +}; |
| 177 | + |
| 178 | +export const getListPetsNestedArrayMockHandler = ( |
| 179 | + overrideResponse?: |
| 180 | + | PetsNestedArray |
| 181 | + | (( |
| 182 | + info: Parameters<Parameters<typeof http.get>[1]>[0], |
| 183 | + ) => PetsNestedArray), |
| 184 | +) => { |
144 | 185 | return http.get('*/v:version/pets-nested-array', async (info) => {
|
145 | 186 | await delay(1000);
|
146 |
| - return new HttpResponse(JSON.stringify(overrideResponse !== undefined |
147 |
| - ? (typeof overrideResponse === "function" ? overrideResponse(info) : overrideResponse) |
148 |
| - : getListPetsNestedArrayResponseMock()), |
| 187 | + return new HttpResponse( |
| 188 | + JSON.stringify( |
| 189 | + overrideResponse !== undefined |
| 190 | + ? typeof overrideResponse === 'function' |
| 191 | + ? overrideResponse(info) |
| 192 | + : overrideResponse |
| 193 | + : getListPetsNestedArrayResponseMock(), |
| 194 | + ), |
149 | 195 | {
|
150 | 196 | status: 200,
|
151 | 197 | headers: {
|
152 | 198 | 'Content-Type': 'application/json',
|
153 |
| - } |
154 |
| - } |
155 |
| - ) |
156 |
| - }) |
157 |
| -} |
158 |
| - |
159 |
| -export const getShowPetByIdMockHandler = (overrideResponse?: Pet | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Pet)) => { |
| 199 | + }, |
| 200 | + }, |
| 201 | + ); |
| 202 | + }); |
| 203 | +}; |
| 204 | + |
| 205 | +export const getShowPetByIdMockHandler = ( |
| 206 | + overrideResponse?: |
| 207 | + | Pet |
| 208 | + | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Pet), |
| 209 | +) => { |
160 | 210 | return http.get('*/v:version/pets/:petId', async (info) => {
|
161 | 211 | await delay(1000);
|
162 |
| - return new HttpResponse(JSON.stringify(overrideResponse !== undefined |
163 |
| - ? (typeof overrideResponse === "function" ? overrideResponse(info) : overrideResponse) |
164 |
| - : getShowPetByIdResponseMock()), |
| 212 | + return new HttpResponse( |
| 213 | + JSON.stringify( |
| 214 | + overrideResponse !== undefined |
| 215 | + ? typeof overrideResponse === 'function' |
| 216 | + ? overrideResponse(info) |
| 217 | + : overrideResponse |
| 218 | + : getShowPetByIdResponseMock(), |
| 219 | + ), |
165 | 220 | {
|
166 | 221 | status: 200,
|
167 | 222 | headers: {
|
168 | 223 | 'Content-Type': 'application/json',
|
169 |
| - } |
170 |
| - } |
171 |
| - ) |
172 |
| - }) |
173 |
| -} |
| 224 | + }, |
| 225 | + }, |
| 226 | + ); |
| 227 | + }); |
| 228 | +}; |
174 | 229 | export const getSwaggerPetstoreMock = () => [
|
175 | 230 | getListPetsMockHandler(),
|
176 | 231 | getCreatePetsMockHandler(),
|
177 | 232 | getListPetsNestedArrayMockHandler(),
|
178 |
| - getShowPetByIdMockHandler()] |
| 233 | + getShowPetByIdMockHandler(), |
| 234 | +]; |
0 commit comments