+1
-1
| { | ||
| "name": "rastapi", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "Fast and easy-to-use API maker made for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "rastapi.js", |
+53
-18
@@ -116,2 +116,3 @@ // noinspection JSUnusedGlobalSymbols | ||
| this._ended = false; | ||
| this.url = request.url; | ||
| } | ||
@@ -151,21 +152,50 @@ | ||
| this._server = (http ? require("http") : require("https")).createServer((req, res) => { | ||
| if (req.method === "GET") { | ||
| const response = new RastResponse(req, res); | ||
| if (this._readers.has(Method.GET) && this._readers.get(Method.GET).has(req.url)) { | ||
| const res = this._readers.get(Method.GET).get(req.url); | ||
| const cb = res.cb(response); | ||
| if (cb === undefined) return; | ||
| if (res.type === "file") response.file(cb); | ||
| else response.end(JSON.stringify(typeof cb === "object" ? cb : {error: "Invalid response"})); | ||
| if (!this._readers.has(req.method)) return; | ||
| if (req.url === "/favicon.ico") return; | ||
| const urls = Array.from(this._readers.get(req.method)).map(i => i[0]); | ||
| let vars = {}; | ||
| const dataUrl = urls.find(i => i === req.url) || urls.find(a => { | ||
| vars = {}; | ||
| const A = a.split("/").slice(1); | ||
| const B = req.url.split("/").slice(1); | ||
| if (A.length !== B.length) return false; | ||
| let all = false; | ||
| for (let i = 0; i < A.length; i++) { | ||
| if (A[i] === "*") { | ||
| all = true; | ||
| continue; | ||
| } | ||
| if (A[i].startsWith(":")) { | ||
| vars[A[i].split("").slice(1).join("")] = B[i]; | ||
| continue; | ||
| } | ||
| if (A[i] !== B[i] && !all) return false; | ||
| } | ||
| return true; | ||
| }); | ||
| if (req.method === "GET") { | ||
| const response = new RastResponse(req, res, vars); | ||
| if (this._readers.get(Method.GET).has(dataUrl)) { | ||
| const res = this._readers.get(Method.GET).get(dataUrl); | ||
| const cb = res.cb(response, vars); | ||
| if (cb === undefined) return; | ||
| if (res.type === "file") response.file(cb); | ||
| else response.end(JSON.stringify(typeof cb === "object" ? cb : {error: "Invalid response"})); | ||
| } else { | ||
| if (this._readers.has("404")) { | ||
| response.end(JSON.stringify(this._readers.get("404").get("*").cb(response))); | ||
| } | ||
| } | ||
| } else if (req.method === "POST") { | ||
| return; // TODO | ||
| let body = ""; | ||
| req.on("data", c => body += c); | ||
| req.on("end", () => { | ||
| res.writeHead(StatusCodes.OK, {"Content-Type": "text/json"}); | ||
| res.end(body); | ||
| }); | ||
| } | ||
| } else if (req.method === "POST") { | ||
| return; // TODO | ||
| let body = ""; | ||
| req.on("data", c => body += c); | ||
| req.on("end", () => { | ||
| res.writeHead(StatusCodes.OK, {"Content-Type": "text/json"}); | ||
| res.end(body); | ||
| }); | ||
| } | ||
| }); | ||
| ) | ||
| ; | ||
| } | ||
@@ -211,3 +241,4 @@ | ||
| class RastServerHTTP extends RastServer { | ||
| class RastServerHTTP | ||
| extends RastServer { | ||
| constructor() { | ||
@@ -232,2 +263,6 @@ super(); | ||
| static notFound() { | ||
| mode = {method: "404", url: "*"}; | ||
| } | ||
| static VERSION = "0.0.1"; | ||
@@ -234,0 +269,0 @@ |
+20
-1
@@ -59,2 +59,21 @@ # RastAPI | ||
| server.file(() => "./myfile.json"); | ||
| ``` | ||
| ``` | ||
| ### Using url variables | ||
| ```javascript | ||
| $rast.get("/test/:test"); | ||
| server.json((res, vars) => { | ||
| return {"message": "You are in /test/" + vars.test}; | ||
| }); | ||
| ``` | ||
| ### Not found page | ||
| ```javascript | ||
| $rast.notFound(); | ||
| server.json(() => { | ||
| return {"message": "Not Found 🙁"}; | ||
| }); | ||
| ``` | ||
| { | ||
| "message": "Hello, World!" | ||
| } |
| require("../rastapi"); | ||
| const server = new $rast.httpServer(); | ||
| $rast.get("/"); | ||
| server.json(() => { | ||
| return {"message": "Hello World ✨"}; | ||
| }); | ||
| $rast.get("/test"); | ||
| server.file(() => "../api.json"); | ||
| server.listen().then(() => console.log("I am listening ✨")); |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11633
15.04%251
10.57%79
33.9%4
-33.33%