Skip to content

Commit f9b4652

Browse files
committed
Adds option to disable helpers
1 parent dee6dbe commit f9b4652

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"editor.tabSize": 2
2+
"editor.tabSize": 2,
3+
"typescript.tsdk": "node_modules/typescript/lib"
34
}

src/__tests__/vercel-node-server.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NowRequest, NowResponse } from '@vercel/node';
2-
import { Server } from 'http';
2+
import { Server, IncomingMessage, ServerResponse } from 'http';
33
import axios from 'axios';
44
import fs from 'fs';
55
import path from 'path';
@@ -298,3 +298,51 @@ describe('request handling', () => {
298298
});
299299
});
300300
});
301+
302+
describe('disableHelpers', () => {
303+
let simpleServer: Server;
304+
let simpleServerUrl: string;
305+
let route: (req: IncomingMessage, res: ServerResponse) => void;
306+
307+
beforeAll(async () => {
308+
simpleServer = createServer(
309+
(req, res) => {
310+
if (route) {
311+
return route(req, res);
312+
}
313+
},
314+
{ disableHelpers: true }
315+
);
316+
simpleServerUrl = await listen(simpleServer);
317+
});
318+
319+
afterAll(() => {
320+
simpleServer.close();
321+
});
322+
323+
it('does not apply the vercel helpers', async () => {
324+
// ARRANGE
325+
let actualRequest: any;
326+
let actualResponse: any;
327+
route = (req, res) => {
328+
actualRequest = req;
329+
actualResponse = res;
330+
res.end();
331+
};
332+
333+
// ACT
334+
await axios.get(`${simpleServerUrl}?one=item1&two=item2`, {
335+
headers: {
336+
Cookie: 'foo=bar',
337+
},
338+
});
339+
340+
// ASSERT
341+
expect(actualRequest.query).toBeUndefined();
342+
expect(actualRequest.body).toBeUndefined();
343+
expect(actualRequest.cookies).toBeUndefined();
344+
expect(actualResponse.status).toBeUndefined();
345+
expect(actualResponse.json).toBeUndefined();
346+
expect(actualResponse.send).toBeUndefined();
347+
});
348+
});

src/index.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
NowRequest,
66
NowResponse,
77
} from '@vercel/node';
8-
import { IncomingMessage, ServerResponse } from 'http';
8+
import { IncomingMessage, ServerResponse, Server, RequestListener } from 'http';
99
import { parse } from 'cookie';
1010
import { parse as parseContentType } from 'content-type';
1111
import { parse as parseQS } from 'querystring';
@@ -109,11 +109,32 @@ export const enhanceResponse = (res: ServerResponse): NowResponse => {
109109
return nowRes;
110110
};
111111

112-
export const createServer = (
113-
route: (req: NowRequest, res: NowResponse) => any | Promise<any>
114-
) =>
115-
micro(async (req: IncomingMessage, res: ServerResponse) => {
116-
const nowReq = await enhanceRequest(req);
117-
const nowRes = enhanceResponse(res);
118-
return await route(nowReq, nowRes);
119-
});
112+
export interface Config {
113+
disableHelpers?: boolean;
114+
}
115+
116+
interface DefaultConfig {
117+
disableHelpers: false;
118+
}
119+
120+
type NowRoute = (req: NowRequest, res: NowResponse) => void;
121+
122+
export const createServer = <C extends Config = DefaultConfig>(
123+
route: C extends undefined
124+
? NowRoute
125+
: C['disableHelpers'] extends true
126+
? RequestListener
127+
: NowRoute,
128+
config?: C
129+
) => {
130+
if (config != null && config.disableHelpers) {
131+
// @ts-expect-error
132+
return new Server(route);
133+
} else {
134+
return micro(async (req: IncomingMessage, res: ServerResponse) => {
135+
const nowReq = await enhanceRequest(req);
136+
const nowRes = enhanceResponse(res);
137+
return await route(nowReq, nowRes);
138+
});
139+
}
140+
};

0 commit comments

Comments
 (0)