Skip to content

Typed request #13

@sualko

Description

@sualko

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

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions