11/* eslint-disable @typescript-eslint/ban-types */
22import { initTRPC } from '@trpc/server' ;
33import { NextApiRequest , NextApiResponse } from 'next' ;
4+ import { IncomingMessage } from 'http' ;
5+ import { NextApiRequestCookies , NextApiRequestQuery } from 'next/dist/server/api-utils' ;
6+ import { Socket } from 'net' ;
47import { z } from 'zod' ;
58
69import {
@@ -11,6 +14,24 @@ import {
1114 createOpenApiNextHandler ,
1215} from '../../src' ;
1316
17+ type NextApiRequestOptions = Partial < NextApiRequestMock > ;
18+ class NextApiRequestMock extends IncomingMessage implements NextApiRequest {
19+ public query : NextApiRequestQuery = { } ;
20+ public cookies : NextApiRequestCookies = { } ;
21+ public env = { } ;
22+ public body : any ;
23+
24+ constructor ( options : NextApiRequestOptions ) {
25+ super ( new Socket ( ) ) ;
26+
27+ this . method = options . method ;
28+ this . body = options . body ;
29+ this . query = options . query ?? { } ;
30+ this . headers = options . headers ?? { } ;
31+ this . env = options . env ?? { } ;
32+ }
33+ }
34+
1435const createContextMock = jest . fn ( ) ;
1536const responseMetaMock = jest . fn ( ) ;
1637const onErrorMock = jest . fn ( ) ;
@@ -41,26 +62,30 @@ const createOpenApiNextHandlerCaller = <TRouter extends OpenApiRouter>(
4162 statusCode : number ;
4263 headers : Record < string , any > ;
4364 body : OpenApiResponse ;
44- /* eslint-disable-next-line @typescript-eslint/no-misused-promises, no-async-promise-executor */
4565 } > ( async ( resolve , reject ) => {
4666 const headers = new Map ( ) ;
4767 let body : any ;
48- const res : any = {
68+ const nextResponse = {
4969 statusCode : undefined ,
5070 setHeader : ( key : string , value : any ) => headers . set ( key , value ) ,
71+ getHeaders : ( ) => Object . fromEntries ( headers . entries ( ) ) ,
5172 end : ( data : string ) => {
5273 body = JSON . parse ( data ) ;
5374 } ,
54- } ;
75+ } as unknown as NextApiResponse ;
76+
77+ const nextRequest = new NextApiRequestMock ( {
78+ method : req . method ,
79+ query : req . query ,
80+ body : req . body ,
81+ headers : req . headers ,
82+ } ) ;
5583
5684 try {
57- await openApiNextHandler (
58- req as unknown as NextApiRequest ,
59- res as unknown as NextApiResponse ,
60- ) ;
85+ await openApiNextHandler ( nextRequest , nextResponse ) ;
6186 resolve ( {
62- statusCode : res . statusCode ,
63- headers : Object . fromEntries ( headers . entries ( ) ) ,
87+ statusCode : nextResponse . statusCode ,
88+ headers : nextResponse . getHeaders ( ) ,
6489 body,
6590 } ) ;
6691 } catch ( error ) {
@@ -122,7 +147,7 @@ describe('next adapter', () => {
122147 headers : { 'Content-Type' : 'application/json' } ,
123148 } ) ;
124149
125- // expect(res.statusCode).toBe(200);
150+ expect ( res . statusCode ) . toBe ( 200 ) ;
126151 expect ( res . body ) . toEqual ( { greeting : 'Hello Lily!' } ) ;
127152 expect ( createContextMock ) . toHaveBeenCalledTimes ( 1 ) ;
128153 expect ( responseMetaMock ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments