Skip to content

Commit 8ea1ab6

Browse files
authored
Merge pull request #2 from danybeltran/fix-handled-var
FIX:
2 parents 06a7235 + 046f9db commit 8ea1ab6

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lib/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ exports.Controller = void 0;
5151
function Controller(path, paths) {
5252
var _this = this;
5353
if (paths === void 0) { paths = {}; }
54-
var handled = false;
5554
return function (req, res) { return __awaiter(_this, void 0, void 0, function () {
56-
var _a, url, urlWithourQueryParams, urlParts, handlePathUrl, hasHandlers, _loop_1, path_1, state_1;
55+
var handled, _a, url, urlWithourQueryParams, urlParts, handlePathUrl, hasHandlers, _loop_1, path_1, state_1;
5756
return __generator(this, function (_b) {
57+
handled = false;
5858
_a = req.url, url = _a === void 0 ? "" : _a;
5959
urlWithourQueryParams = url.split("?")[0];
6060
urlParts = urlWithourQueryParams.split("/");
@@ -98,13 +98,15 @@ function Controller(path, paths) {
9898
catch (err) {
9999
}
100100
finally {
101-
if (hasHandlers) {
102-
res.status(405);
103-
res.send("cannot ".concat(req.method, " ").concat(req.url));
104-
}
105-
else {
106-
res.status(404);
107-
res.send("not found");
101+
if (!handled) {
102+
if (hasHandlers) {
103+
res.status(405);
104+
res.send("cannot ".concat(req.method, " ").concat(req.url));
105+
}
106+
else {
107+
res.status(404);
108+
res.send("not found");
109+
}
108110
}
109111
}
110112
return [2 /*return*/];

src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ type ControllerMethods = {
55
}
66

77
export function Controller(path: string, paths: ControllerMethods = {}) {
8-
let handled = false
98
return async (req: NextApiRequest, res: NextApiResponse) => {
9+
let handled = false
1010
const { url = "" } = req
1111
const [urlWithourQueryParams] = url.split("?")
1212

1313
const urlParts = urlWithourQueryParams.split("/")
1414

1515
const handlePathUrl = "/api" + path
1616

17-
// const totalHandlers = Object.keys(paths).length
18-
1917
let hasHandlers = false
2018

2119
try {
@@ -58,12 +56,14 @@ export function Controller(path: string, paths: ControllerMethods = {}) {
5856
}
5957
} catch (err) {
6058
} finally {
61-
if (hasHandlers) {
62-
res.status(405)
63-
res.send(`cannot ${req.method} ${req.url}`)
64-
} else {
65-
res.status(404)
66-
res.send("not found")
59+
if (!handled) {
60+
if (hasHandlers) {
61+
res.status(405)
62+
res.send(`cannot ${req.method} ${req.url}`)
63+
} else {
64+
res.status(404)
65+
res.send("not found")
66+
}
6767
}
6868
}
6969
}

0 commit comments

Comments
 (0)