Skip to content

Commit 63c4319

Browse files
committed
add README.md and update
1 parent 7513a50 commit 63c4319

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## REST controller for Next.js
2+
3+
#### How does it work?
4+
5+
Create restful controllers in Next.js
6+
7+
#### Example:
8+
9+
Inside `/pages/api/auth/[...handler]`
10+
11+
(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)
12+
13+
```ts
14+
import { Controller } from "next-rest-controller"
15+
16+
const AuthHandler = Controller({
17+
async "GET /auth"(req, res){
18+
res.status(401)
19+
res.send("Forbidden")
20+
},
21+
async "GET /[id]/info"(req, res){
22+
res.send("Info for " + req.query.id)
23+
}
24+
})
25+
26+
```
27+
28+
Sending, for example, a POST request that would be handled by a GET handler, will send a `405` status code

lib/index.js

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exports.Controller = void 0;
1515
function Controller(paths) {
1616
if (paths === void 0) { paths = {}; }
1717
return function (req, res) {
18+
var handled = false;
1819
var _a = req.url, url = _a === void 0 ? "" : _a;
1920
var urlWithourQueryParams = url.split("?")[0];
2021
var urlParts = urlWithourQueryParams.split("/");
@@ -23,42 +24,32 @@ function Controller(paths) {
2324
var _b = path.split(" "), method = _b[0], handleUrl = _b[1];
2425
var $handleUrl = handlePathUrl + handleUrl.split("?")[0];
2526
var handleParts = $handleUrl.split("/");
26-
if (handleParts.length === urlParts.length) {
27-
var finalHandler_1 = [];
28-
var finalQuery_1 = {};
29-
handleParts.forEach(function (handlePart, i) {
30-
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
31-
var withoutBrackets = handlePart.replace(/\[|\]/g, "");
32-
finalQuery_1[withoutBrackets] = urlParts[i];
33-
finalHandler_1.push(urlParts[i]);
34-
}
35-
else {
36-
finalHandler_1.push(handlePart);
37-
}
38-
});
39-
if (finalHandler_1.join("/") === urlParts.join("/")) {
40-
var withQ = __assign(__assign({}, req.query), finalQuery_1);
41-
if (req.method === method) {
42-
req.query = withQ;
43-
paths[path](req, res);
44-
}
45-
else {
46-
res.status(405);
47-
res.send("Cannot ".concat(req.method, " ").concat(url));
48-
}
27+
var finalHandler = [];
28+
var finalQuery = {};
29+
handleParts.forEach(function (handlePart, i) {
30+
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
31+
var withoutBrackets = handlePart.replace(/\[|\]/g, "");
32+
finalQuery[withoutBrackets] = urlParts[i];
33+
finalHandler.push(urlParts[i]);
34+
}
35+
else {
36+
finalHandler.push(handlePart);
37+
}
38+
});
39+
if (finalHandler.join("/") === urlParts.join("/") && !handled) {
40+
var withQ = __assign(__assign({}, req.query), finalQuery);
41+
if (req.method === method) {
42+
req.query = withQ;
43+
paths[path](req, res);
44+
}
45+
else {
46+
res.status(405);
47+
res.send("Cannot ".concat(req.method, " ").concat(url));
4948
}
50-
return "break";
51-
}
52-
else {
53-
res.status(404);
54-
res.send("Not found");
55-
return "break";
5649
}
5750
};
5851
for (var path in paths) {
59-
var state_1 = _loop_1(path);
60-
if (state_1 === "break")
61-
break;
52+
_loop_1(path);
6253
}
6354
};
6455
}

src/index.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type ControllerMethods = {
66

77
export function Controller(paths: ControllerMethods = {}) {
88
return (req: NextApiRequest, res: NextApiResponse) => {
9+
let handled = false
10+
911
const { url = "" } = req
1012
const [urlWithourQueryParams] = url.split("?")
1113

@@ -20,43 +22,34 @@ export function Controller(paths: ControllerMethods = {}) {
2022

2123
const handleParts = $handleUrl.split("/")
2224

23-
if (handleParts.length === urlParts.length) {
24-
let finalHandler: string[] = []
25+
let finalHandler: string[] = []
2526

26-
let finalQuery: any = {}
27+
let finalQuery: any = {}
2728

28-
handleParts.forEach((handlePart, i) => {
29-
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
30-
const withoutBrackets = handlePart.replace(/\[|\]/g, "")
29+
handleParts.forEach((handlePart, i) => {
30+
if (handlePart.startsWith("[") && handlePart.endsWith("]")) {
31+
const withoutBrackets = handlePart.replace(/\[|\]/g, "")
3132

32-
finalQuery[withoutBrackets] = urlParts[i]
33+
finalQuery[withoutBrackets] = urlParts[i]
3334

34-
finalHandler.push(urlParts[i] as never)
35-
} else {
36-
finalHandler.push(handlePart as never)
37-
}
38-
})
35+
finalHandler.push(urlParts[i] as never)
36+
} else {
37+
finalHandler.push(handlePart as never)
38+
}
39+
})
3940

40-
if (finalHandler.join("/") === urlParts.join("/")) {
41-
const withQ = { ...req.query, ...finalQuery }
41+
if (finalHandler.join("/") === urlParts.join("/") && !handled) {
42+
const withQ = { ...req.query, ...finalQuery }
4243

43-
if (req.method === method) {
44-
req.query = withQ
44+
if (req.method === method) {
45+
req.query = withQ
4546

46-
paths[path](req, res)
47-
} else {
48-
res.status(405)
47+
paths[path](req, res)
48+
} else {
49+
res.status(405)
4950

50-
res.send(`Cannot ${req.method} ${url}`)
51-
}
51+
res.send(`Cannot ${req.method} ${url}`)
5252
}
53-
break
54-
} else {
55-
res.status(404)
56-
57-
res.send("Not found")
58-
59-
break
6053
}
6154
}
6255
}

0 commit comments

Comments
 (0)