Skip to content

Commit 06a7235

Browse files
authored
Merge pull request #1 from danybeltran/fix-incosistent-url-bugs
FIX:
2 parents 29cf0b9 + 3172ec5 commit 06a7235

File tree

3 files changed

+78
-71
lines changed

3 files changed

+78
-71
lines changed

lib/index.js

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,56 +53,59 @@ function Controller(path, paths) {
5353
if (paths === void 0) { paths = {}; }
5454
var handled = false;
5555
return function (req, res) { return __awaiter(_this, void 0, void 0, function () {
56-
var _a, url, urlWithourQueryParams, urlParts, handlePathUrl, totalHandlers, _loop_1, path_1, state_1;
56+
var _a, url, urlWithourQueryParams, urlParts, handlePathUrl, hasHandlers, _loop_1, path_1, state_1;
5757
return __generator(this, function (_b) {
5858
_a = req.url, url = _a === void 0 ? "" : _a;
5959
urlWithourQueryParams = url.split("?")[0];
6060
urlParts = urlWithourQueryParams.split("/");
6161
handlePathUrl = "/api" + path;
62-
totalHandlers = Object.keys(paths).length;
63-
_loop_1 = function (path_1) {
64-
var _c = path_1.split(" "), method = _c[0], handleUrl = _c[1];
65-
var $handleUrl = handlePathUrl + handleUrl.split("?")[0];
66-
var handleParts = $handleUrl.split("/");
67-
var finalHandler = [];
68-
var finalQuery = {};
69-
handleParts.forEach(function (handlePart, i) {
70-
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
71-
var withoutBrackets = handlePart.replace(/\[|\]/g, "");
72-
finalQuery[withoutBrackets] = urlParts[i];
73-
finalHandler.push(urlParts[i]);
74-
}
75-
else {
76-
finalHandler.push(handlePart);
77-
}
78-
});
79-
if (finalHandler.join("/") === urlParts.join("/")) {
80-
var withQ = __assign(__assign({}, req.query), finalQuery);
81-
if (req.method === method) {
82-
req.query = withQ;
83-
paths[path_1](req, res);
84-
handled = true;
85-
return "break";
86-
}
87-
else {
88-
if (!("".concat(req.method, " ").concat(handleUrl) in paths) && !handled) {
89-
res.status(405);
90-
res.send("cannot ".concat(req.method, " ").concat(finalHandler.join("/")));
62+
hasHandlers = false;
63+
try {
64+
_loop_1 = function (path_1) {
65+
var _c = path_1.split(" "), method = _c[0], handleUrl = _c[1];
66+
var $handleUrl = handlePathUrl + handleUrl.split("?")[0];
67+
var handleParts = $handleUrl.split("/");
68+
var finalHandler = [];
69+
var finalQuery = {};
70+
handleParts.forEach(function (handlePart, i) {
71+
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
72+
hasHandlers = true;
73+
var withoutBrackets = handlePart.replace(/\[|\]/g, "");
74+
finalQuery[withoutBrackets] = urlParts[i];
75+
finalHandler.push(urlParts[i]);
76+
}
77+
else {
78+
finalHandler.push(handlePart);
79+
}
80+
});
81+
if (finalHandler.join("/") === urlParts.join("/") && !handled) {
82+
hasHandlers = true;
83+
if (req.method === method) {
84+
var withQ = __assign(__assign({}, req.query), finalQuery);
85+
req.query = withQ;
86+
paths[path_1](req, res);
87+
handled = true;
88+
return "break";
9189
}
9290
}
91+
};
92+
for (path_1 in paths) {
93+
state_1 = _loop_1(path_1);
94+
if (state_1 === "break")
95+
break;
96+
}
97+
}
98+
catch (err) {
99+
}
100+
finally {
101+
if (hasHandlers) {
102+
res.status(405);
103+
res.send("cannot ".concat(req.method, " ").concat(req.url));
93104
}
94105
else {
95106
res.status(404);
96-
res.send("Not found");
97-
if (Object.keys(paths).indexOf(path_1) < totalHandlers - 1) {
98-
return "break";
99-
}
107+
res.send("not found");
100108
}
101-
};
102-
for (path_1 in paths) {
103-
state_1 = _loop_1(path_1);
104-
if (state_1 === "break")
105-
break;
106109
}
107110
return [2 /*return*/];
108111
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-rest-controller",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "A REST helper for Next apis",
55
"main": "lib/index.js",
66
"repository": "https://github.com/atomic-state/rest-next",

src/index.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,56 @@ export function Controller(path: string, paths: ControllerMethods = {}) {
1414

1515
const handlePathUrl = "/api" + path
1616

17-
const totalHandlers = Object.keys(paths).length
17+
// const totalHandlers = Object.keys(paths).length
1818

19-
for (let path in paths) {
20-
const [method, handleUrl] = path.split(" ")
19+
let hasHandlers = false
2120

22-
let $handleUrl = handlePathUrl + handleUrl.split("?")[0]
21+
try {
22+
for (let path in paths) {
23+
const [method, handleUrl] = path.split(" ")
2324

24-
const handleParts = $handleUrl.split("/")
25+
let $handleUrl = handlePathUrl + handleUrl.split("?")[0]
2526

26-
let finalHandler: string[] = []
27+
const handleParts = $handleUrl.split("/")
2728

28-
let finalQuery: any = {}
29+
let finalHandler: string[] = []
2930

30-
handleParts.forEach((handlePart, i) => {
31-
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
32-
const withoutBrackets = handlePart.replace(/\[|\]/g, "")
31+
let finalQuery: any = {}
3332

34-
finalQuery[withoutBrackets] = urlParts[i]
33+
handleParts.forEach((handlePart, i) => {
34+
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
35+
hasHandlers = true
36+
const withoutBrackets = handlePart.replace(/\[|\]/g, "")
3537

36-
finalHandler.push(urlParts[i] as never)
37-
} else {
38-
finalHandler.push(handlePart as never)
39-
}
40-
})
38+
finalQuery[withoutBrackets] = urlParts[i]
39+
40+
finalHandler.push(urlParts[i] as never)
41+
} else {
42+
finalHandler.push(handlePart as never)
43+
}
44+
})
4145

42-
if (finalHandler.join("/") === urlParts.join("/")) {
43-
const withQ = { ...req.query, ...finalQuery }
46+
if (finalHandler.join("/") === urlParts.join("/") && !handled) {
47+
hasHandlers = true
4448

45-
if (req.method === method) {
46-
req.query = withQ
49+
if (req.method === method) {
50+
const withQ = { ...req.query, ...finalQuery }
51+
req.query = withQ
4752

48-
paths[path](req, res)
49-
handled = true
50-
break
51-
} else {
52-
if (!(`${req.method} ${handleUrl}` in paths) && !handled) {
53-
res.status(405)
54-
res.send(`cannot ${req.method} ${finalHandler.join("/")}`)
53+
paths[path](req, res)
54+
handled = true
55+
break
5556
}
5657
}
58+
}
59+
} catch (err) {
60+
} finally {
61+
if (hasHandlers) {
62+
res.status(405)
63+
res.send(`cannot ${req.method} ${req.url}`)
5764
} else {
5865
res.status(404)
59-
res.send(`Not found`)
60-
if (Object.keys(paths).indexOf(path) < totalHandlers - 1) {
61-
break
62-
}
66+
res.send("not found")
6367
}
6468
}
6569
}

0 commit comments

Comments
 (0)