Skip to content

Commit 9404af4

Browse files
authoredJun 20, 2024
feat(fetch): include status code in fetch client response (#1470)
* feat(fetch): include http status code in `fetch` response * chore: rerun `orval` in next sample app * chore: update return object in custom fetch function * chore: display http status code in next sample app
1 parent d5592b6 commit 9404af4

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed
 

‎packages/fetch/src/index.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ ${
6262
6363
return \`${route}${queryParams ? '?${normalizedParams.toString()}' : ''}\`
6464
}\n`;
65+
66+
const responseTypeName = `${operationName}Response`;
67+
const responseTypeImplementation = `export type ${operationName}Response = {
68+
data: ${response.definition.success || 'unknown'};
69+
status: number;
70+
}`;
71+
6572
const getUrlFnProperties = props
6673
.filter(
6774
(prop) =>
@@ -79,7 +86,7 @@ ${
7986
.join(',');
8087

8188
const args = `${toObjectString(props, 'implementation')} ${isRequestOptions ? `options?: RequestInit` : ''}`;
82-
const retrunType = `Promise<${response.definition.success || 'unknown'}>`;
89+
const retrunType = `Promise<${responseTypeName}>`;
8390

8491
const globalFetchOptions = isObject(override?.requestOptions)
8592
? `${stringify(override?.requestOptions)?.slice(1, -1)?.trim()}`
@@ -102,7 +109,12 @@ ${
102109
${fetchBodyOption}
103110
}
104111
`;
105-
const fetchResponseImplementation = `const res = await fetch(${fetchFnOptions})\n\nreturn res.json()`;
112+
const fetchResponseImplementation = `const res = await fetch(${fetchFnOptions}
113+
)
114+
const data = await res.json()
115+
116+
return { status: res.status, data }
117+
`;
106118
const customFetchResponseImplementation = `return ${mutator?.name}<${retrunType}>(${fetchFnOptions});`;
107119

108120
const bodyForm = generateFormDataAndUrlEncodedFunction({
@@ -120,7 +132,9 @@ ${
120132
const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {\n${fetchImplementationBody}}`;
121133

122134
const implementation =
123-
`${getUrlFnImplementation}\n` + `${fetchImplementation}\n`;
135+
`${responseTypeImplementation}\n\n` +
136+
`${getUrlFnImplementation}\n` +
137+
`${fetchImplementation}\n`;
124138

125139
return implementation;
126140
};

‎samples/next-app-with-fetch/app/gen/pets/pets.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ type NonReadonly<T> = [T] extends [UnionToIntersection<T>]
4343
/**
4444
* @summary List all pets
4545
*/
46+
export type listPetsResponse = {
47+
data: Pets;
48+
status: number;
49+
};
50+
4651
export const getListPetsUrl = (params?: ListPetsParams) => {
4752
const normalizedParams = new URLSearchParams();
4853

@@ -60,8 +65,8 @@ export const getListPetsUrl = (params?: ListPetsParams) => {
6065
export const listPets = async (
6166
params?: ListPetsParams,
6267
options?: RequestInit,
63-
): Promise<Pets> => {
64-
return customFetch<Promise<Pets>>(getListPetsUrl(params), {
68+
): Promise<listPetsResponse> => {
69+
return customFetch<Promise<listPetsResponse>>(getListPetsUrl(params), {
6570
...options,
6671
method: 'GET',
6772
});
@@ -70,15 +75,20 @@ export const listPets = async (
7075
/**
7176
* @summary Create a pet
7277
*/
78+
export type createPetsResponse = {
79+
data: Pet;
80+
status: number;
81+
};
82+
7383
export const getCreatePetsUrl = () => {
7484
return `http://localhost:3000/pets`;
7585
};
7686

7787
export const createPets = async (
7888
createPetsBodyItem: CreatePetsBodyItem[],
7989
options?: RequestInit,
80-
): Promise<Pet> => {
81-
return customFetch<Promise<Pet>>(getCreatePetsUrl(), {
90+
): Promise<createPetsResponse> => {
91+
return customFetch<Promise<createPetsResponse>>(getCreatePetsUrl(), {
8292
...options,
8393
method: 'POST',
8494
body: JSON.stringify(createPetsBodyItem),
@@ -88,15 +98,20 @@ export const createPets = async (
8898
/**
8999
* @summary Update a pet
90100
*/
101+
export type updatePetsResponse = {
102+
data: Pet;
103+
status: number;
104+
};
105+
91106
export const getUpdatePetsUrl = () => {
92107
return `http://localhost:3000/pets`;
93108
};
94109

95110
export const updatePets = async (
96111
pet: NonReadonly<Pet>,
97112
options?: RequestInit,
98-
): Promise<Pet> => {
99-
return customFetch<Promise<Pet>>(getUpdatePetsUrl(), {
113+
): Promise<updatePetsResponse> => {
114+
return customFetch<Promise<updatePetsResponse>>(getUpdatePetsUrl(), {
100115
...options,
101116
method: 'PUT',
102117
body: JSON.stringify(pet),
@@ -106,15 +121,20 @@ export const updatePets = async (
106121
/**
107122
* @summary Info for a specific pet
108123
*/
124+
export type showPetByIdResponse = {
125+
data: Pet;
126+
status: number;
127+
};
128+
109129
export const getShowPetByIdUrl = (petId: string) => {
110130
return `http://localhost:3000/pets/${petId}`;
111131
};
112132

113133
export const showPetById = async (
114134
petId: string,
115135
options?: RequestInit,
116-
): Promise<Pet> => {
117-
return customFetch<Promise<Pet>>(getShowPetByIdUrl(petId), {
136+
): Promise<showPetByIdResponse> => {
137+
return customFetch<Promise<showPetByIdResponse>>(getShowPetByIdUrl(petId), {
118138
...options,
119139
method: 'GET',
120140
});
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { listPets } from './gen/pets/pets';
22

33
export default async function Pets() {
4-
const pets = await listPets();
4+
const { data: pets, status } = await listPets();
55

66
return (
77
<div>
88
<h1 className="text-4xl">Pets by server actions</h1>
9+
910
<ul>
1011
{pets.map((pet) => (
1112
<li key={pet.id}>{pet.name}</li>
1213
))}
1314
</ul>
15+
16+
<h2 className="text-xl">Status: {status}</h2>
1417
</div>
1518
);
1619
}

‎samples/next-app-with-fetch/custom-fetch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ export const customFetch = async <T>(
5353
const response = await fetch(request);
5454
const data = await getBody<T>(response);
5555

56-
return data;
56+
return { status: response.status, data } as T;
5757
};

0 commit comments

Comments
 (0)
Please sign in to comment.