Route handler typing and fetch typing
Answered
Silver posted this in #help-forum
SilverOP
Hello,
I'm new to next js and I'd like to know if there is a way to type a route handler as well as a fetch request.
I've made this test
And the only type I can to is
Which isn't ideal as Response isn't generic to I can't tell it what will be contained in the response.
Pretty much the same with fetch
The type of res.json is always Promise<any> do I have no other choice but to make an as like so ?
In order to be typed ?
I'm new to next js and I'd like to know if there is a way to type a route handler as well as a fetch request.
I've made this test
export async function GET() {
const allTest = await db.select().from(test);
return Response.json(allTest);
} And the only type I can to is
export async function GET() : Promise<Response> {
const allTest = await db.select().from(test);
return Response.json(allTest);
}Which isn't ideal as Response isn't generic to I can't tell it what will be contained in the response.
Pretty much the same with fetch
const allTest = await fetch(
`http://${process.env.NEXT_PUBLIC_VERCEL_URL}/api/test`,
).then((res) => res.json())The type of res.json is always Promise<any> do I have no other choice but to make an as like so ?
async function getTestData() {
if (!process.env.NEXT_PUBLIC_VERCEL_URL) {
throw new Error("NEXT_PUBLIC_VERCEL_URL is not defined");
}
const response = await fetch(
`http://${process.env.NEXT_PUBLIC_VERCEL_URL}/api/all`,
);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json() as Promise<test[]>;
}In order to be typed ?
Answered by Clown
You can use Zod to do a quick check.
With fetch you can tell typescript what the type will be:
With fetch you can tell typescript what the type will be:
const allTest: Type = await fetch..13 Replies
@Silver Hello,
I'm new to next js and I'd like to know if there is a way to type a route handler as well as a fetch request.
I've made this test
export async function GET() {
const allTest = await db.select().from(test);
return Response.json(allTest);
}
And the only type I can to is
export async function GET() : Promise<Response> {
const allTest = await db.select().from(test);
return Response.json(allTest);
}
Which isn't ideal as Response isn't generic to I can't tell it what will be contained in the response.
Pretty much the same with fetch
const allTest = await fetch(
`http://${process.env.NEXT_PUBLIC_VERCEL_URL}/api/test`,
).then((res) => res.json())
The type of res.json is always Promise<any> do I have no other choice but to make an as like so ?
async function getTestData() {
if (!process.env.NEXT_PUBLIC_VERCEL_URL) {
throw new Error("NEXT_PUBLIC_VERCEL_URL is not defined");
}
const response = await fetch(
`http://${process.env.NEXT_PUBLIC_VERCEL_URL}/api/all`,
);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json() as Promise<test[]>;
}
In order to be typed ?
You can use Zod to do a quick check.
With fetch you can tell typescript what the type will be:
With fetch you can tell typescript what the type will be:
const allTest: Type = await fetch..Answer
^ obviously will throw error if the type isnt the same. You wont have as much control as you would with zod or something other type check Library
SilverOP
Thanks, I was starting to take a look at
NextResponse and NextRequest but it don't really seems usefull neitherI was hoping there was some kind of way to define the type on the route handler and like that the component would have known what it was
NextResponse and NextRequest just add additional stuff to
Response and RequestSilverOP
Yes that's what I saw : /
@Silver I was hoping there was some kind of way to define the type on the route handler and like that the component would have known what it was
Those are merely api endpoints. The data must be sent be like that
For example plain text or json, both of which dont have "types"
Well they do but you get what i mean
SilverOP
yes yes I'm not really used to those way of handling json, as I'm more using Golang usually and when I unmarshal the json in the end it will be typed the way I want and not a Promise<any>
And is making the getData return something with an as is bad or good ?
@Clown You can use Zod to do a quick check.
With fetch you can tell typescript what the type will be:
`const allTest: Type = await fetch.. `
Tbh when doing something like .json(), if i can gurantee the result will either be well formatted or an error i just do this.
If there's a chance the response or request can be malformed i add something like Zod.
If there's a chance the response or request can be malformed i add something like Zod.
SilverOP
Thanks ! I'm sure of my data in that case as either I'll have the good data either an error 🙏 thanks a lot for the detailled answer