Skip to content

Commit

Permalink
vercel#369 Limit deprecation warning to be shown only in DEV mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alisabzevari committed Feb 9, 2019
1 parent ee5687c commit ed1bdd4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/__tests__/development.ts
Expand Up @@ -3,7 +3,7 @@ import request from "request-promise";
import listen from "test-listen";

process.env.NODE_ENV = "development";
import micro, { RequestHandler } from "..";
import micro, { RequestHandler, send } from "..";

const getUrl = (fn: RequestHandler) => listen(micro(fn));

Expand Down Expand Up @@ -42,3 +42,17 @@ test("sendError shows stack in development with statusCode", async () => {
expect(err.message.indexOf("at fn (") !== -1).toBeTruthy();
}
});

test("calling exported send directly should log deprecated error", async () => {
const consoleErrorSpy = jest.spyOn(console, "error");

const fn: RequestHandler = (req, res) => {
send(res, 200, { cool: "object" });
};

const url = await getUrl(fn);
const resp = await request(url);

expect(JSON.parse(resp)).toEqual({ cool: "object" });
expect(consoleErrorSpy).toBeCalledTimes(2);
});
5 changes: 4 additions & 1 deletion src/index.ts
@@ -1,12 +1,15 @@
import { Server } from "http";

import { RequestHandler, run, send, serve, sendError } from "./micro";
import { RequestHandler, run, send, serve, sendError, DEV } from "./micro";
import { buffer, json, text } from "./helpers";
import { res, Body, HttpResponse } from "./http-message";
import { createError, logError } from "./error";

type Fn = (...args: any[]) => any;
function deprecate<T extends Fn>(message: string, errorCode: string, fn: T) {
if (!DEV) {
return fn;
}
return ((...args: any[]) => {
logError(message, errorCode);
return fn(...args);
Expand Down
2 changes: 1 addition & 1 deletion src/micro.ts
Expand Up @@ -14,7 +14,7 @@ export type RequestHandler = (
) => Promise<HttpHanderResult> | HttpHanderResult;

const { NODE_ENV } = process.env;
const DEV = NODE_ENV === "development";
export const DEV = NODE_ENV === "development";

export const serve = (fn: RequestHandler) =>
new Server((req, res) => run(req, res, fn));
Expand Down

0 comments on commit ed1bdd4

Please sign in to comment.