Skip to content

Commit e65b78a

Browse files
committed
Fix: uses base path instead of __dirname
1 parent 63c4319 commit e65b78a

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ Inside `/pages/api/auth/[...handler]`
1111
(filename must be a rest operator if you want customized URLs, but you can also use normal api handlers filenames, though they have to share the same URL in your controller)
1212

1313
```ts
14+
// inside /pages/api/auth/[...handler]
15+
1416
import { Controller } from "next-rest-controller"
1517

16-
const AuthHandler = Controller({
17-
async "GET /auth"(req, res){
18+
// The first argument is the 'base' segment that will be used to map the correct url.
19+
const AuthHandler = Controller("/auth", {
20+
async "GET /auth"(req, res) {
1821
res.status(401)
1922
res.send("Forbidden")
2023
},
21-
async "GET /[id]/info"(req, res){
24+
async "GET /[id]/info"(req, res) {
2225
res.send("Info for " + req.query.id)
2326
}
2427
})
25-
2628
```
2729

30+
#### Explanation
31+
When adding a handler/method, it should start with an HTTP verb, followed by a space, and a url to handle (with or without query params using square brackets).
32+
2833
Sending, for example, a POST request that would be handled by a GET handler, will send a `405` status code

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { NextApiRequest, NextApiResponse } from "next/types";
22
declare type ControllerMethods = {
33
[k: string]: (req: NextApiRequest, res: NextApiResponse) => void;
44
};
5-
export declare function Controller(paths?: ControllerMethods): (req: NextApiRequest, res: NextApiResponse) => void;
5+
export declare function Controller(path: string, paths?: ControllerMethods): (req: NextApiRequest, res: NextApiResponse) => void;
66
export {};

lib/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ var __assign = (this && this.__assign) || function () {
1212
};
1313
Object.defineProperty(exports, "__esModule", { value: true });
1414
exports.Controller = void 0;
15-
function Controller(paths) {
15+
function Controller(path, paths) {
1616
if (paths === void 0) { paths = {}; }
1717
return function (req, res) {
1818
var handled = false;
1919
var _a = req.url, url = _a === void 0 ? "" : _a;
2020
var urlWithourQueryParams = url.split("?")[0];
2121
var urlParts = urlWithourQueryParams.split("/");
22-
var handlePathUrl = "/api/" + __dirname.split("/api/").at(-1);
23-
var _loop_1 = function (path) {
24-
var _b = path.split(" "), method = _b[0], handleUrl = _b[1];
22+
var handlePathUrl = "/api" + path;
23+
var _loop_1 = function (path_1) {
24+
var _b = path_1.split(" "), method = _b[0], handleUrl = _b[1];
2525
var $handleUrl = handlePathUrl + handleUrl.split("?")[0];
2626
var handleParts = $handleUrl.split("/");
2727
var finalHandler = [];
@@ -40,16 +40,23 @@ function Controller(paths) {
4040
var withQ = __assign(__assign({}, req.query), finalQuery);
4141
if (req.method === method) {
4242
req.query = withQ;
43-
paths[path](req, res);
43+
paths[path_1](req, res);
44+
handled = true;
4445
}
4546
else {
46-
res.status(405);
47-
res.send("Cannot ".concat(req.method, " ").concat(url));
47+
if (!("".concat(req.method, " ").concat(handleUrl) in paths)) {
48+
res.status(405);
49+
res.send("cannot ".concat(req.method, " ").concat(finalHandler.join("/")));
50+
}
4851
}
4952
}
53+
else {
54+
res.status(404);
55+
res.send("Not found");
56+
}
5057
};
51-
for (var path in paths) {
52-
_loop_1(path);
58+
for (var path_1 in paths) {
59+
_loop_1(path_1);
5360
}
5461
};
5562
}

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.1",
3+
"version": "0.0.2",
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ type ControllerMethods = {
44
[k: string]: (req: NextApiRequest, res: NextApiResponse) => void
55
}
66

7-
export function Controller(paths: ControllerMethods = {}) {
7+
export function Controller(path: string, paths: ControllerMethods = {}) {
88
return (req: NextApiRequest, res: NextApiResponse) => {
99
let handled = false
10-
1110
const { url = "" } = req
1211
const [urlWithourQueryParams] = url.split("?")
1312

1413
const urlParts = urlWithourQueryParams.split("/")
1514

16-
const handlePathUrl = "/api/" + __dirname.split("/api/").at(-1)
15+
const handlePathUrl = "/api" + path
1716

1817
for (let path in paths) {
1918
const [method, handleUrl] = path.split(" ")
@@ -45,11 +44,17 @@ export function Controller(paths: ControllerMethods = {}) {
4544
req.query = withQ
4645

4746
paths[path](req, res)
48-
} else {
49-
res.status(405)
5047

51-
res.send(`Cannot ${req.method} ${url}`)
48+
handled = true
49+
} else {
50+
if (!(`${req.method} ${handleUrl}` in paths)) {
51+
res.status(405)
52+
res.send(`cannot ${req.method} ${finalHandler.join("/")}`)
53+
}
5254
}
55+
} else {
56+
res.status(404)
57+
res.send(`Not found`)
5358
}
5459
}
5560
}

0 commit comments

Comments
 (0)