next-joi icon indicating copy to clipboard operation
next-joi copied to clipboard

Typed request

Open sualko opened this issue 3 years ago • 2 comments

Thanks for this small little, but handy middleware. Great work :clap:

I extended the default request interface to have a strongly typed parameter and thought, maybe this is also useful for others, or maybe it could even be integrated.

import type { NextApiRequest, NextApiResponse } from 'next'

export interface ValidatedApiRequest<Body = any, Query extends NextApiRequest['query'] = NextApiRequest['query']> extends NextApiRequest {
    body: Body
    query: Query
}

const query = Joi.object({
    id: Joi.string().required(),
});

async function handler(
  req: ValidatedApiRequest<undefined, {id: string}>,
  res: NextApiResponse
) {
//...
}

export default validator({query}, handler;

sualko avatar Jan 23 '22 12:01 sualko

Hi, @sualko! Thanks for taking the time to open this issue and explain your findings :-) I'd be open to exploring this since it may help us with #9.

sergioalvz avatar Jan 23 '22 18:01 sergioalvz

That sounds great. I will write some lines to the other issue, because I want the same as the author :smile:

sualko avatar Jan 24 '22 08:01 sualko