hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.64k stars 65 forks source link

[TypeScript] Interface changes in last minor version #69

Closed ljosberinn closed 4 years ago

ljosberinn commented 4 years ago

Hey,

wanted to ask why the signature changed? Now most of my code is failing because res.json() does not exist on ServerResponse. Given that's still the recommended way of usage in the docs... res.send doesnt exist either.

I can circumvent it with generics, but is this intended?

.get<NextApiRequest & AuthenticatedRequest, NextApiResponse>((req, res) => {
    res.json(req[SESSION_COOKIE_NAME]);
  });
ljosberinn commented 4 years ago

For now, I've added these proxy types:

import { NextApiResponse, NextApiRequest } from 'next';
import {
  RequestHandler as NextConnectRequestHandler,
  Middleware as NextConnectMiddleware,
} from 'next-connect';

export type Middleware<T = {}, S = {}> = NextConnectMiddleware<
  NextApiRequest & T,
  NextApiResponse & S
>;
export type RequestHandler<T = {}, S = {}> = NextConnectRequestHandler<
  NextApiRequest & T,
  NextApiResponse & S
>;

and use them like this:

const loginHandler: RequestHandler = async (req, res, next) => {}

export default nextConnect().post(loginHandler);

In fact, I prefer this approach but it should imo be documented :)

hoangvvo commented 4 years ago

I'm certainly sorry. it was really irresponsible for me to do a breaking change in a minor. Some people seem to use next-connect with getServerSideProps and others in normal pages, which have only Node IncomingMessage and ServerResponse as req and res.

I'm wondering what is the best way to support both API Routes and that.