Skip to content

Catch all segments dynamic api routes #989

Answered by Xunnamius
rinvii asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there! NTARH has support for all API route types. Testing catch-all routes shouldn't be too different from any other route, you just have to provide the expected params object.

Using the Next.js docs as an example (given URL /api/post/a/b/c):

/* file: pages/api/post/[...slug].ts */
import type { NextApiRequest, NextApiResponse } from 'next'
 
export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const { slug } = req.query
  res.end(`Post: ${slug.join(', ')}`)
}
/* file: test/main.test.ts */
import { testApiHandler } from 'next-test-api-route-handler';
// Import the handler under test from the pages/api directory
import * as pagesHandler from '../pages/api/[...s…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Xunnamius
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #988 on February 12, 2024 19:31.