instantcommerce / next-api-decorators

Collection of decorators to create typed Next.js API routes, with easy request validation and transformation.
https://next-api-decorators.vercel.app
MIT License
409 stars 28 forks source link

How to create params decorator with async function #628

Open reynoldputra opened 12 months ago

reynoldputra commented 12 months ago

I created fullstack next js using next-auth and next-api-decorator. I want to get my user information in params


async function getSessionToken(req: NextApiRequest): Promise<JWT | null> {
  const token = await getToken({req});
  return token;
}

export const Session = createParamDecorator<JWT | undefined>(async (req) => {
  const sessionToken = await getSessionToken(req);
  return sessionToken || undefined;
});

src/apiDecorators/decorator/session.ts|12 col 62-78 error| Argument of type '(req: NextApiRequest) => Promise<JWT | undefined>' is not assignable to parameter of type 'ParamDecorator<JWT | undefined>'. Type 'Promise<JWT | undefined>' is not assignable to type 'JWT'. Index signature for type 'string' is missing in type 'Promise<JWT | undefined>'.

source : https://next-auth.js.org/configuration/nextjs#in-api-routes