Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch): add type to response data in fetch client #1773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ ${
`
: `const res = await fetch(${fetchFnOptions})

const data = await res.json()
const data:${response.definition.success} = await res.json()

${override.fetch.includeHttpResponseReturnType ? 'return { status: res.status, data, headers: res.headers }' : `return data as ${responseTypeName}`}
`;
Expand Down
20 changes: 10 additions & 10 deletions samples/basic/api/endpoints/petstoreFromFileSpecWithConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,28 @@ export const PetCallingCode = {
} as const;

export interface Pet {
/**
* @minimum 0
* @maximum 30
* @exclusiveMinimum
* @exclusiveMaximum
*/
age?: number;
callingCode?: PetCallingCode;
country?: PetCountry;
email?: string;
id: number;
/**
* Name of pet
* @minLength 40
* @maxLength 0
*/
name: string;
/**
* @minimum 0
* @maximum 30
* @exclusiveMinimum
* @exclusiveMaximum
*/
age?: number;
/**
* @nullable
* @pattern ^\\d{3}-\\d{2}-\\d{4}$
*/
tag?: string | null;
email?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ export const getListPetsResponseMock = (): PetsArray =>
{ length: faker.number.int({ min: 1, max: 10 }) },
(_, i) => i + 1,
).map(() => ({
id: faker.number.int({ min: undefined, max: undefined }),
name: 'jon',
age: faker.helpers.arrayElement([
faker.number.int({ min: 0, max: 30 }),
undefined,
]),
tag: faker.helpers.arrayElement(['jon', null]),
email: faker.helpers.arrayElement([faker.internet.email(), undefined]),
callingCode: faker.helpers.arrayElement([
faker.helpers.arrayElement(['+33', '+420', '+33'] as const),
undefined,
Expand All @@ -92,10 +96,6 @@ export const getListPetsResponseMock = (): PetsArray =>
] 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]),
}));

export const getListPetsNestedArrayResponseMock = (
Expand All @@ -106,10 +106,14 @@ export const getListPetsNestedArrayResponseMock = (
{ length: faker.number.int({ min: 1, max: 10 }) },
(_, i) => i + 1,
).map(() => ({
id: faker.number.int({ min: undefined, max: undefined }),
name: 'jon',
age: faker.helpers.arrayElement([
faker.number.int({ min: 0, max: 30 }),
undefined,
]),
tag: faker.helpers.arrayElement(['jon', null]),
email: faker.helpers.arrayElement([faker.internet.email(), undefined]),
callingCode: faker.helpers.arrayElement([
faker.helpers.arrayElement(['+33', '+420', '+33'] as const),
undefined,
Expand All @@ -121,10 +125,6 @@ export const getListPetsNestedArrayResponseMock = (
] 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,
]),
Expand Down
20 changes: 10 additions & 10 deletions samples/basic/api/model/pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import type { PetCallingCode } from './petCallingCode';
import type { PetCountry } from './petCountry';

export interface Pet {
/**
* @minimum 0
* @maximum 30
* @exclusiveMinimum
* @exclusiveMaximum
*/
age?: number;
callingCode?: PetCallingCode;
country?: PetCountry;
email?: string;
id: number;
/**
* Name of pet
* @minLength 40
* @maxLength 0
*/
name: string;
/**
* @minimum 0
* @maximum 30
* @exclusiveMinimum
* @exclusiveMaximum
*/
age?: number;
/**
* @nullable
* @pattern ^\\d{3}-\\d{2}-\\d{4}$
*/
tag?: string | null;
email?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const listPets = async (
method: 'GET',
});

const data = await res.json();
const data: Pets = await res.json();

return { status: res.status, data, headers: res.headers };
};
Expand Down Expand Up @@ -72,7 +72,7 @@ export const createPets = async (
body: JSON.stringify(createPetsBodyItem),
});

const data = await res.json();
const data: Pet = await res.json();

return { status: res.status, data, headers: res.headers };
};
Expand Down Expand Up @@ -101,7 +101,7 @@ export const updatePets = async (
body: JSON.stringify(pet),
});

const data = await res.json();
const data: Pet = await res.json();

return { status: res.status, data, headers: res.headers };
};
Expand All @@ -128,7 +128,7 @@ export const showPetById = async (
method: 'GET',
});

const data = await res.json();
const data: Pet = await res.json();

return { status: res.status, data, headers: res.headers };
};
16 changes: 8 additions & 8 deletions samples/hono/hono-with-zod/src/petstore.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export const DachshundBreed = {
} as const;

export interface Dachshund {
breed: DachshundBreed;
length: number;
breed: DachshundBreed;
}

export type LabradoodleBreed =
Expand All @@ -57,8 +57,8 @@ export const LabradoodleBreed = {
} as const;

export interface Labradoodle {
breed: LabradoodleBreed;
cuteness: number;
breed: LabradoodleBreed;
}

export type DogType = (typeof DogType)[keyof typeof DogType];
Expand Down Expand Up @@ -98,19 +98,19 @@ export const PetCallingCode = {
export type Pet =
| (Dog & {
'@id'?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
email?: string;
id: number;
name: string;
tag?: string;
email?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
})
| (Cat & {
'@id'?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
email?: string;
id: number;
name: string;
tag?: string;
email?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
});
2 changes: 1 addition & 1 deletion samples/next-app-with-fetch/app/gen/models/dachshund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
import type { DachshundBreed } from './dachshundBreed';

export interface Dachshund {
breed: DachshundBreed;
length: number;
breed: DachshundBreed;
}
2 changes: 1 addition & 1 deletion samples/next-app-with-fetch/app/gen/models/labradoodle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
import type { LabradoodleBreed } from './labradoodleBreed';

export interface Labradoodle {
breed: LabradoodleBreed;
cuteness: number;
breed: LabradoodleBreed;
}
12 changes: 6 additions & 6 deletions samples/next-app-with-fetch/app/gen/models/pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import type { PetCountry } from './petCountry';
export type Pet =
| (Dog & {
'@id'?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
email?: string;
id: number;
name: string;
tag?: string;
email?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
})
| (Cat & {
'@id'?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
email?: string;
id: number;
name: string;
tag?: string;
email?: string;
callingCode?: PetCallingCode;
country?: PetCountry;
});
Loading
Loading