diff --git a/lib/index.js b/lib/index.js index 35cb74a..f6ad5c2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -53,56 +53,59 @@ function Controller(path, paths) { if (paths === void 0) { paths = {}; } var handled = false; return function (req, res) { return __awaiter(_this, void 0, void 0, function () { - var _a, url, urlWithourQueryParams, urlParts, handlePathUrl, totalHandlers, _loop_1, path_1, state_1; + var _a, url, urlWithourQueryParams, urlParts, handlePathUrl, hasHandlers, _loop_1, path_1, state_1; return __generator(this, function (_b) { _a = req.url, url = _a === void 0 ? "" : _a; urlWithourQueryParams = url.split("?")[0]; urlParts = urlWithourQueryParams.split("/"); handlePathUrl = "/api" + path; - totalHandlers = Object.keys(paths).length; - _loop_1 = function (path_1) { - var _c = path_1.split(" "), method = _c[0], handleUrl = _c[1]; - var $handleUrl = handlePathUrl + handleUrl.split("?")[0]; - var handleParts = $handleUrl.split("/"); - var finalHandler = []; - var finalQuery = {}; - handleParts.forEach(function (handlePart, i) { - if (handlePart.startsWith("[") && handlePart.endsWith("]")) { - var withoutBrackets = handlePart.replace(/\[|\]/g, ""); - finalQuery[withoutBrackets] = urlParts[i]; - finalHandler.push(urlParts[i]); - } - else { - finalHandler.push(handlePart); - } - }); - if (finalHandler.join("/") === urlParts.join("/")) { - var withQ = __assign(__assign({}, req.query), finalQuery); - if (req.method === method) { - req.query = withQ; - paths[path_1](req, res); - handled = true; - return "break"; - } - else { - if (!("".concat(req.method, " ").concat(handleUrl) in paths) && !handled) { - res.status(405); - res.send("cannot ".concat(req.method, " ").concat(finalHandler.join("/"))); + hasHandlers = false; + try { + _loop_1 = function (path_1) { + var _c = path_1.split(" "), method = _c[0], handleUrl = _c[1]; + var $handleUrl = handlePathUrl + handleUrl.split("?")[0]; + var handleParts = $handleUrl.split("/"); + var finalHandler = []; + var finalQuery = {}; + handleParts.forEach(function (handlePart, i) { + if (handlePart.startsWith("[") && handlePart.endsWith("]")) { + hasHandlers = true; + var withoutBrackets = handlePart.replace(/\[|\]/g, ""); + finalQuery[withoutBrackets] = urlParts[i]; + finalHandler.push(urlParts[i]); + } + else { + finalHandler.push(handlePart); + } + }); + if (finalHandler.join("/") === urlParts.join("/") && !handled) { + hasHandlers = true; + if (req.method === method) { + var withQ = __assign(__assign({}, req.query), finalQuery); + req.query = withQ; + paths[path_1](req, res); + handled = true; + return "break"; } } + }; + for (path_1 in paths) { + state_1 = _loop_1(path_1); + if (state_1 === "break") + break; + } + } + catch (err) { + } + finally { + if (hasHandlers) { + res.status(405); + res.send("cannot ".concat(req.method, " ").concat(req.url)); } else { res.status(404); - res.send("Not found"); - if (Object.keys(paths).indexOf(path_1) < totalHandlers - 1) { - return "break"; - } + res.send("not found"); } - }; - for (path_1 in paths) { - state_1 = _loop_1(path_1); - if (state_1 === "break") - break; } return [2 /*return*/]; }); diff --git a/package.json b/package.json index 3deff82..68c3b77 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-rest-controller", - "version": "0.0.3", + "version": "0.0.4", "description": "A REST helper for Next apis", "main": "lib/index.js", "repository": "https://github.com/atomic-state/rest-next", diff --git a/src/index.ts b/src/index.ts index 4896dd8..4de411d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,52 +14,56 @@ export function Controller(path: string, paths: ControllerMethods = {}) { const handlePathUrl = "/api" + path - const totalHandlers = Object.keys(paths).length + // const totalHandlers = Object.keys(paths).length - for (let path in paths) { - const [method, handleUrl] = path.split(" ") + let hasHandlers = false - let $handleUrl = handlePathUrl + handleUrl.split("?")[0] + try { + for (let path in paths) { + const [method, handleUrl] = path.split(" ") - const handleParts = $handleUrl.split("/") + let $handleUrl = handlePathUrl + handleUrl.split("?")[0] - let finalHandler: string[] = [] + const handleParts = $handleUrl.split("/") - let finalQuery: any = {} + let finalHandler: string[] = [] - handleParts.forEach((handlePart, i) => { - if (handlePart.startsWith("[") && handlePart.endsWith("]")) { - const withoutBrackets = handlePart.replace(/\[|\]/g, "") + let finalQuery: any = {} - finalQuery[withoutBrackets] = urlParts[i] + handleParts.forEach((handlePart, i) => { + if (handlePart.startsWith("[") && handlePart.endsWith("]")) { + hasHandlers = true + const withoutBrackets = handlePart.replace(/\[|\]/g, "") - finalHandler.push(urlParts[i] as never) - } else { - finalHandler.push(handlePart as never) - } - }) + finalQuery[withoutBrackets] = urlParts[i] + + finalHandler.push(urlParts[i] as never) + } else { + finalHandler.push(handlePart as never) + } + }) - if (finalHandler.join("/") === urlParts.join("/")) { - const withQ = { ...req.query, ...finalQuery } + if (finalHandler.join("/") === urlParts.join("/") && !handled) { + hasHandlers = true - if (req.method === method) { - req.query = withQ + if (req.method === method) { + const withQ = { ...req.query, ...finalQuery } + req.query = withQ - paths[path](req, res) - handled = true - break - } else { - if (!(`${req.method} ${handleUrl}` in paths) && !handled) { - res.status(405) - res.send(`cannot ${req.method} ${finalHandler.join("/")}`) + paths[path](req, res) + handled = true + break } } + } + } catch (err) { + } finally { + if (hasHandlers) { + res.status(405) + res.send(`cannot ${req.method} ${req.url}`) } else { res.status(404) - res.send(`Not found`) - if (Object.keys(paths).indexOf(path) < totalHandlers - 1) { - break - } + res.send("not found") } } }