question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Next.js + Typescript] How to add req.params with NextApiRequest

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

When I use typescript and nextjs. I have some problem with NextApiRequest. I mean there is no params property in NextApiRequest. so How about add params property in NextApiRequest interface?

now I use like this

server.get(
    "/post/:postId",
    (req: NextApiRequest & Express.Request, res: NextApiResponse) => {
      return app.render(req, res, "/post", { postId: req.params.postId });
    },
  );

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
canibanoglucommented, Jan 8, 2020

I was dicussing how to define API endpoints in Next.js with a colleague at work and when he told me that it is a bit iffy because you have to put the endpoint definitions under pages/ in a new folder called api.

Upon some discussion, it feels like there is a bit of a mixing of concerns here. Here’s my reasoning and my questions about the choice here

I expect all routes under pages/ to be returning an actual page, something that has HTML content in its reponse. Thinking along these lines, an API endpoint is fundamentally different, it is meant to interface with another program over HTTP to deal with data, not markup. (I guess you could argue that HTML endpoints are a specialized case of API endpoints, but still…)

Moreover, I would expect API endpoints and HTML endpoints to have different scaling/refactoring needs as an application grows and evolves.

My questions are:

  1. What was the choice behind having the api/ folder under pages/? Maybe I’m missing something critical here but apart from ease of implementation, I can’t find a good reason for mixing these two.

  2. Has this been discussed before or are there any ongoing discussions about this? I also don’t want to hijack this issue because it is a bit out of the scope for the original question. Should I create another issue and take the discussion there?

They are very similar yet also different and I believe it would be a better idea to have a top level api/ folder for API endpoint definitions (the naming of the folder can be different of course).

Read more comments on GitHub >

github_iconTop Results From Across the Web

nextjs API with typescript, restricting NextApiRequest query ...
In this endpoint handler, is there a way to restrict req.query in NextJS NextApiRequest to just string types rather than string | string[]...
Read more >
Dynamic API Routes
You can add the dynamic routes used for pages to API Routes too. ... export default function handler(req, res) { const { pid...
Read more >
API Routes: Request Helpers
API Routes provide built-in request helpers that parse the incoming request. Learn more about them here.
Read more >
Basic Features: TypeScript | Next.js
Next.js supports TypeScript by default and has built-in types for pages ... import type { NextApiRequest, NextApiResponse } from 'next' export default (req: ......
Read more >
API Routes: Introduction
For an API route to work, you need to export a function as default (a.k.a request handler), which then receives the following parameters:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found