princejs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/prince.js +80 -0
- package/package.json +32 -0
package/dist/prince.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// prince.ts
|
|
3
|
+
class Prince {
|
|
4
|
+
routes = new Map;
|
|
5
|
+
middlewares = [];
|
|
6
|
+
use(middleware) {
|
|
7
|
+
this.middlewares.push(middleware);
|
|
8
|
+
}
|
|
9
|
+
error(handler) {
|
|
10
|
+
this.use(async (req, next) => {
|
|
11
|
+
try {
|
|
12
|
+
return await next();
|
|
13
|
+
} catch (err) {
|
|
14
|
+
return handler(err, req);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
json(data, status = 200) {
|
|
19
|
+
return new Response(JSON.stringify(data), {
|
|
20
|
+
status,
|
|
21
|
+
headers: { "Content-Type": "application/json" }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
get(path, handler) {
|
|
25
|
+
this.add("GET", path, handler);
|
|
26
|
+
}
|
|
27
|
+
post(path, handler) {
|
|
28
|
+
this.add("POST", path, handler);
|
|
29
|
+
}
|
|
30
|
+
put(path, handler) {
|
|
31
|
+
this.add("PUT", path, handler);
|
|
32
|
+
}
|
|
33
|
+
delete(path, handler) {
|
|
34
|
+
this.add("DELETE", path, handler);
|
|
35
|
+
}
|
|
36
|
+
patch(path, handler) {
|
|
37
|
+
this.add("PATCH", path, handler);
|
|
38
|
+
}
|
|
39
|
+
options(path, handler) {
|
|
40
|
+
this.add("OPTIONS", path, handler);
|
|
41
|
+
}
|
|
42
|
+
head(path, handler) {
|
|
43
|
+
this.add("HEAD", path, handler);
|
|
44
|
+
}
|
|
45
|
+
add(method, path, handler) {
|
|
46
|
+
if (!this.routes.has(path))
|
|
47
|
+
this.routes.set(path, new Map);
|
|
48
|
+
this.routes.get(path).set(method, handler);
|
|
49
|
+
}
|
|
50
|
+
listen(port = 3000) {
|
|
51
|
+
Bun.serve({
|
|
52
|
+
port,
|
|
53
|
+
fetch: async (req) => {
|
|
54
|
+
const url = new URL(req.url);
|
|
55
|
+
let index = 0;
|
|
56
|
+
const next = async () => {
|
|
57
|
+
if (index < this.middlewares.length) {
|
|
58
|
+
return await this.middlewares[index++](req, next);
|
|
59
|
+
}
|
|
60
|
+
const route = this.routes.get(url.pathname);
|
|
61
|
+
const handler = route?.get(req.method) || route?.get("GET");
|
|
62
|
+
if (!handler) {
|
|
63
|
+
return this.json({ error: "Route not found" }, 404);
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
const result = await handler(req);
|
|
67
|
+
return result instanceof Response ? result : this.json(result);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
throw e;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return await next();
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
console.log(`PrinceJS v1.0.0 running at http://localhost:${port}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
Prince
|
|
80
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "princejs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "An easy and fast backend framework — by a 13yo developer, for developers.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"backend",
|
|
12
|
+
"api",
|
|
13
|
+
"framework",
|
|
14
|
+
"bun",
|
|
15
|
+
"easy",
|
|
16
|
+
"princejs"
|
|
17
|
+
],
|
|
18
|
+
"author": "Matthew Micheal",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": "https://github.com/MatthewTheCoder1218/princejs",
|
|
21
|
+
"homepage": "https://github.com/MatthewTheCoder1218/princejs",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/bun": "^1.3.2",
|
|
27
|
+
"bun-types": "latest"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "bun build prince.ts --outdir dist --target bun"
|
|
31
|
+
}
|
|
32
|
+
}
|