|
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' |
12 |
| -import type { |
13 |
| - CreatePetsBody, |
14 |
| - ListPetsParams |
15 |
| -} from '../model' |
16 |
| -import { |
17 |
| - faker |
18 |
| -} from '@faker-js/faker' |
19 |
| -import { |
20 |
| - HttpResponse, |
21 |
| - delay, |
22 |
| - http |
23 |
| -} from 'msw' |
24 |
| -import type { |
25 |
| - Pet, |
26 |
| - Pets |
27 |
| -} from '../model' |
| 7 | +import axios from 'axios'; |
| 8 | +import type { AxiosRequestConfig, AxiosResponse } from 'axios'; |
| 9 | +import type { CreatePetsBody, ListPetsParams } from '../model'; |
| 10 | +import { faker } from '@faker-js/faker'; |
| 11 | +import { HttpResponse, delay, http } from 'msw'; |
| 12 | +import type { Pet, Pets } from '../model'; |
28 | 13 | import listPetsMutator from '../mutator/response-type';
|
29 | 14 |
|
30 |
| - |
31 |
| - |
32 |
| - |
33 |
| - /** |
| 15 | +/** |
34 | 16 | * @summary List all pets
|
35 | 17 | */
|
36 |
| -export const listPets = ( |
37 |
| - params?: ListPetsParams, |
38 |
| - version: number = 1, |
39 |
| - ) => { |
40 |
| - return listPetsMutator<Pets>( |
41 |
| - {url: `/v${version}/pets`, method: 'GET', |
42 |
| - params |
43 |
| - }, |
44 |
| - ); |
45 |
| - } |
46 |
| - |
| 18 | +export const listPets = (params?: ListPetsParams, version: number = 1) => { |
| 19 | + return listPetsMutator<Pets>({ |
| 20 | + url: `/v${version}/pets`, |
| 21 | + method: 'GET', |
| 22 | + params, |
| 23 | + }); |
| 24 | +}; |
| 25 | + |
47 | 26 | /**
|
48 | 27 | * @summary Create a pet
|
49 | 28 | */
|
50 | 29 | export const createPets = <TData = AxiosResponse<void>>(
|
51 |
| - createPetsBody: CreatePetsBody, |
52 |
| - version: number = 1, options?: AxiosRequestConfig |
53 |
| - ): Promise<TData> => { |
54 |
| - return axios.post( |
55 |
| - `/v${version}/pets`, |
56 |
| - createPetsBody,options |
57 |
| - ); |
58 |
| - } |
| 30 | + createPetsBody: CreatePetsBody, |
| 31 | + version: number = 1, |
| 32 | + options?: AxiosRequestConfig, |
| 33 | +): Promise<TData> => { |
| 34 | + return axios.post(`/v${version}/pets`, createPetsBody, options); |
| 35 | +}; |
59 | 36 |
|
60 | 37 | /**
|
61 | 38 | * @summary Info for a specific pet
|
62 | 39 | */
|
63 | 40 | export const showPetById = <TData = AxiosResponse<Pet>>(
|
64 |
| - petId: string, |
65 |
| - version: number = 1, options?: AxiosRequestConfig |
66 |
| - ): Promise<TData> => { |
67 |
| - return axios.get( |
68 |
| - `/v${version}/pets/${petId}`,options |
69 |
| - ); |
70 |
| - } |
71 |
| - |
| 41 | + petId: string, |
| 42 | + version: number = 1, |
| 43 | + options?: AxiosRequestConfig, |
| 44 | +): Promise<TData> => { |
| 45 | + return axios.get(`/v${version}/pets/${petId}`, options); |
| 46 | +}; |
72 | 47 |
|
73 | 48 | type AwaitedInput<T> = PromiseLike<T> | T;
|
74 | 49 |
|
75 |
| - type Awaited<O> = O extends AwaitedInput<infer T> ? T : never; |
76 |
| - |
77 |
| -export type ListPetsResult = NonNullable<Awaited<ReturnType<typeof listPets>>> |
78 |
| -export type CreatePetsResult = AxiosResponse<void> |
79 |
| -export type ShowPetByIdResult = AxiosResponse<Pet> |
80 |
| - |
81 |
| - |
82 |
| -export const getListPetsResponseMock = (overrideResponse: any = {}): Pets => (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]), id: faker.number.int({min: undefined, max: undefined}), name: 'jon', tag: faker.helpers.arrayElement(['jon', null]), ...overrideResponse}))) |
83 |
| - |
84 |
| -export const getShowPetByIdResponseMock = () => ((() => ({ |
85 |
| - id: faker.number.int({ min: 1, max: 99 }), |
86 |
| - name: faker.person.firstName(), |
87 |
| - tag: faker.helpers.arrayElement([ |
88 |
| - faker.word.sample(), |
89 |
| - void 0 |
90 |
| - ]) |
91 |
| - }))()) |
92 |
| - |
| 50 | +type Awaited<O> = O extends AwaitedInput<infer T> ? T : never; |
| 51 | + |
| 52 | +export type ListPetsResult = NonNullable<Awaited<ReturnType<typeof listPets>>>; |
| 53 | +export type CreatePetsResult = AxiosResponse<void>; |
| 54 | +export type ShowPetByIdResult = AxiosResponse<Pet>; |
| 55 | + |
| 56 | +export const getListPetsResponseMock = (overrideResponse: any = {}): Pets => |
| 57 | + Array.from( |
| 58 | + { length: faker.number.int({ min: 1, max: 10 }) }, |
| 59 | + (_, i) => i + 1, |
| 60 | + ).map(() => ({ |
| 61 | + age: faker.helpers.arrayElement([ |
| 62 | + faker.number.int({ min: 0, max: 30 }), |
| 63 | + undefined, |
| 64 | + ]), |
| 65 | + id: faker.number.int({ min: undefined, max: undefined }), |
| 66 | + name: 'jon', |
| 67 | + tag: faker.helpers.arrayElement(['jon', null]), |
| 68 | + ...overrideResponse, |
| 69 | + })); |
| 70 | + |
| 71 | +export const getShowPetByIdResponseMock = () => |
| 72 | + (() => ({ |
| 73 | + id: faker.number.int({ min: 1, max: 99 }), |
| 74 | + name: faker.person.firstName(), |
| 75 | + tag: faker.helpers.arrayElement([faker.word.sample(), void 0]), |
| 76 | + }))(); |
93 | 77 |
|
94 | 78 | export const getListPetsMockHandler = (overrideResponse?: Pets) => {
|
95 | 79 | return http.get('*/v:version/pets', async () => {
|
96 | 80 | await delay(1000);
|
97 |
| - return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getListPetsResponseMock()), |
| 81 | + return new HttpResponse( |
| 82 | + JSON.stringify( |
| 83 | + overrideResponse ? overrideResponse : getListPetsResponseMock(), |
| 84 | + ), |
98 | 85 | {
|
99 | 86 | status: 200,
|
100 | 87 | headers: {
|
101 | 88 | 'Content-Type': 'application/json',
|
102 |
| - } |
103 |
| - } |
104 |
| - ) |
105 |
| - }) |
106 |
| -} |
| 89 | + }, |
| 90 | + }, |
| 91 | + ); |
| 92 | + }); |
| 93 | +}; |
107 | 94 |
|
108 | 95 | export const getCreatePetsMockHandler = () => {
|
109 | 96 | return http.post('*/v:version/pets', async () => {
|
110 | 97 | await delay(1000);
|
111 |
| - return new HttpResponse(null, |
112 |
| - { |
113 |
| - status: 200, |
114 |
| - headers: { |
115 |
| - 'Content-Type': 'application/json', |
116 |
| - } |
117 |
| - } |
118 |
| - ) |
119 |
| - }) |
120 |
| -} |
| 98 | + return new HttpResponse(null, { |
| 99 | + status: 200, |
| 100 | + headers: { |
| 101 | + 'Content-Type': 'application/json', |
| 102 | + }, |
| 103 | + }); |
| 104 | + }); |
| 105 | +}; |
121 | 106 |
|
122 | 107 | export const getShowPetByIdMockHandler = (overrideResponse?: Pet) => {
|
123 | 108 | return http.get('*/v:version/pets/:petId', async () => {
|
124 | 109 | await delay(1000);
|
125 |
| - return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getShowPetByIdResponseMock()), |
| 110 | + return new HttpResponse( |
| 111 | + JSON.stringify( |
| 112 | + overrideResponse ? overrideResponse : getShowPetByIdResponseMock(), |
| 113 | + ), |
126 | 114 | {
|
127 | 115 | status: 200,
|
128 | 116 | headers: {
|
129 | 117 | 'Content-Type': 'application/json',
|
130 |
| - } |
131 |
| - } |
132 |
| - ) |
133 |
| - }) |
134 |
| -} |
| 118 | + }, |
| 119 | + }, |
| 120 | + ); |
| 121 | + }); |
| 122 | +}; |
135 | 123 | export const getSwaggerPetstoreMock = () => [
|
136 | 124 | getListPetsMockHandler(),
|
137 | 125 | getCreatePetsMockHandler(),
|
138 |
| - getShowPetByIdMockHandler()] |
| 126 | + getShowPetByIdMockHandler(), |
| 127 | +]; |
0 commit comments