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

Add separate function for parsing body in App Router #467

Closed
wants to merge 1 commit into from
Closed

Add separate function for parsing body in App Router #467

wants to merge 1 commit into from

Conversation

mavic111
Copy link
Contributor

When NextRequest type is added in parseBody function, it is hard to differentiate between NextRequest and NextApiRequest. It cannot be done using req instanceof NextRequest because it returns false even the req type is NextRequest.

app/api/webhooks/sanity/route.ts

import { NextRequest } from 'next/server'
import { parseBody } from 'next-sanity/webhook'

export async function POST(req: NextRequest) {
  console.log(req instanceof NextRequest) // return false
  const { body, isValidSignature } = await parseBody(req, process.env.SECRET) // TypeError
  ...
  return NextResponse.json({ isNextRequest: req instanceof NextRequest })
}

parseBody.ts

...
let body
if (req instanceof NextRequest) { // always return false
  body = JSON.stringify(await req.json())
} else {
  body = await readBody(req) // not compatible with NextRequest causing TypeError
}
...

I think it is better to have separate function for parsing body in the App Router route handler. Then we can use the new function to parse body in route handler like this below.

app/api/webhooks/sanity/route.ts

import { NextRequest, NextResponse } from 'next/server'
import { parseAppBody } from 'next-sanity/webhook'

export async function POST(req: NextRequest) {
  const { body, isValidSignature } = await parseAppBody(req, process.env.SECRET)
  if (!isValidSignature) {
      return (
        NextResponse.json({
          message: 'Invalid signature'
        }),
        {
          status: 401,
          headers: {
            'Content-Type': 'application/json',
          },
        }
      )
    }
  // Do something like revalidation
  ...
}

@mavic111 mavic111 requested a review from a team as a code owner June 22, 2023 09:54
Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks!

@stipsan
Copy link
Member

stipsan commented Jun 29, 2023

We'll revisit this after sanity-io/client#249

@mavic111 mavic111 closed this by deleting the head repository Jul 28, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants