princejs 1.9.7 → 2.1.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/README.md +209 -7
- package/dist/adapters/node.d.ts +47 -0
- package/dist/adapters/node.d.ts.map +1 -0
- package/dist/middleware.js +246 -272
- package/dist/prince.d.ts +26 -4
- package/dist/prince.d.ts.map +1 -1
- package/dist/prince.js +92 -7
- package/package.json +11 -8
package/dist/prince.d.ts
CHANGED
|
@@ -12,8 +12,14 @@ export interface PrinceRequest extends Request {
|
|
|
12
12
|
session?: any;
|
|
13
13
|
apiKey?: string;
|
|
14
14
|
sseSend?: (data: any, event?: string, id?: string) => void;
|
|
15
|
+
cookies?: Record<string, string>;
|
|
16
|
+
ip?: string;
|
|
15
17
|
[key: string]: any;
|
|
16
18
|
}
|
|
19
|
+
export type OnRequest = (req: PrinceRequest) => void | Promise<void>;
|
|
20
|
+
export type OnBeforeHandle = (req: PrinceRequest, path: string, method: string) => void | Promise<void>;
|
|
21
|
+
export type OnAfterHandle = (req: PrinceRequest, res: Response, path: string, method: string) => void | Promise<void>;
|
|
22
|
+
export type OnError = (err: any, req: PrinceRequest, path: string, method: string) => void | Promise<void>;
|
|
17
23
|
interface WebSocketHandler {
|
|
18
24
|
open?: (ws: any) => void;
|
|
19
25
|
message?: (ws: any, msg: string | Buffer) => void;
|
|
@@ -28,10 +34,18 @@ declare class ResponseBuilder {
|
|
|
28
34
|
private _body;
|
|
29
35
|
status(code: number): this;
|
|
30
36
|
header(key: string, value: string): this;
|
|
31
|
-
json(data: any):
|
|
32
|
-
text(data: string):
|
|
33
|
-
html(data: string):
|
|
34
|
-
redirect(url: string, status?: number):
|
|
37
|
+
json(data: any): this;
|
|
38
|
+
text(data: string): this;
|
|
39
|
+
html(data: string): this;
|
|
40
|
+
redirect(url: string, status?: number): this;
|
|
41
|
+
cookie(name: string, value: string, options?: {
|
|
42
|
+
maxAge?: number;
|
|
43
|
+
path?: string;
|
|
44
|
+
domain?: string;
|
|
45
|
+
secure?: boolean;
|
|
46
|
+
httpOnly?: boolean;
|
|
47
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
48
|
+
}): this;
|
|
35
49
|
build(): Response;
|
|
36
50
|
}
|
|
37
51
|
export interface RouteSchema {
|
|
@@ -62,6 +76,10 @@ export declare class Prince {
|
|
|
62
76
|
private staticRoutes;
|
|
63
77
|
private staticMiddlewares;
|
|
64
78
|
private routeCache;
|
|
79
|
+
private onRequestHooks;
|
|
80
|
+
private onBeforeHandleHooks;
|
|
81
|
+
private onAfterHandleHooks;
|
|
82
|
+
private onErrorHooks;
|
|
65
83
|
constructor(devMode?: boolean);
|
|
66
84
|
use(mw: Middleware): this;
|
|
67
85
|
/**
|
|
@@ -77,6 +95,10 @@ export declare class Prince {
|
|
|
77
95
|
*/
|
|
78
96
|
plugin<TOptions = any>(plugin: PrincePlugin<TOptions>, options?: TOptions): this;
|
|
79
97
|
error(fn: (err: any, req: PrinceRequest) => Response): this;
|
|
98
|
+
onRequest(hook: OnRequest): this;
|
|
99
|
+
onBeforeHandle(hook: OnBeforeHandle): this;
|
|
100
|
+
onAfterHandle(hook: OnAfterHandle): this;
|
|
101
|
+
onError(hook: OnError): this;
|
|
80
102
|
json(data: any, status?: number): Response;
|
|
81
103
|
response(): ResponseBuilder;
|
|
82
104
|
get(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
package/dist/prince.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prince.d.ts","sourceRoot":"","sources":["../src/prince.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAe,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE9E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,KAAK,IAAI,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;AACpC,KAAK,UAAU,GAAG,CAAC,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,IAA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+CJ,SAAS;YAoCT,cAAc;IAyEtB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC;IAiC5C,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC;IAsB5C;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,OAAO,CACL,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EACxC,QAAQ,SAAU,EAClB,aAAa,GAAE,aAAkB,GAChC,cAAc,GAAG;QAAE,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,YAAY,GAAG,UAAU,CAAC,EAAE,KAAK,cAAc,CAAA;KAAE;IAuF3J,MAAM,CAAC,IAAI,SAAO;CA8CnB;AAiCD,eAAO,MAAM,MAAM,GAAI,aAAW,WAAoB,CAAC"}
|
package/dist/prince.js
CHANGED
|
@@ -134,27 +134,71 @@ class ResponseBuilder {
|
|
|
134
134
|
json(data) {
|
|
135
135
|
this._headers["Content-Type"] = "application/json";
|
|
136
136
|
this._body = JSON.stringify(data);
|
|
137
|
-
return this
|
|
137
|
+
return this;
|
|
138
138
|
}
|
|
139
139
|
text(data) {
|
|
140
140
|
this._headers["Content-Type"] = "text/plain";
|
|
141
141
|
this._body = data;
|
|
142
|
-
return this
|
|
142
|
+
return this;
|
|
143
143
|
}
|
|
144
144
|
html(data) {
|
|
145
145
|
this._headers["Content-Type"] = "text/html";
|
|
146
146
|
this._body = data;
|
|
147
|
-
return this
|
|
147
|
+
return this;
|
|
148
148
|
}
|
|
149
149
|
redirect(url, status = 302) {
|
|
150
150
|
this._status = status;
|
|
151
151
|
this._headers["Location"] = url;
|
|
152
|
-
return this
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
cookie(name, value, options) {
|
|
155
|
+
let cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
156
|
+
if (options?.maxAge)
|
|
157
|
+
cookieStr += `; Max-Age=${options.maxAge}`;
|
|
158
|
+
if (options?.path)
|
|
159
|
+
cookieStr += `; Path=${options.path}`;
|
|
160
|
+
if (options?.domain)
|
|
161
|
+
cookieStr += `; Domain=${options.domain}`;
|
|
162
|
+
if (options?.secure)
|
|
163
|
+
cookieStr += "; Secure";
|
|
164
|
+
if (options?.httpOnly)
|
|
165
|
+
cookieStr += "; HttpOnly";
|
|
166
|
+
if (options?.sameSite)
|
|
167
|
+
cookieStr += `; SameSite=${options.sameSite}`;
|
|
168
|
+
const existing = this._headers["Set-Cookie"];
|
|
169
|
+
this._headers["Set-Cookie"] = existing ? `${existing}, ${cookieStr}` : cookieStr;
|
|
170
|
+
return this;
|
|
153
171
|
}
|
|
154
172
|
build() {
|
|
155
173
|
return new Response(this._body, { status: this._status, headers: this._headers });
|
|
156
174
|
}
|
|
157
175
|
}
|
|
176
|
+
function parseCookies(cookieHeader) {
|
|
177
|
+
const cookies = {};
|
|
178
|
+
if (!cookieHeader)
|
|
179
|
+
return cookies;
|
|
180
|
+
cookieHeader.split(";").forEach((pair) => {
|
|
181
|
+
const [name, ...value] = pair.split("=");
|
|
182
|
+
if (name)
|
|
183
|
+
cookies[decodeURIComponent(name.trim())] = decodeURIComponent((value.join("=") || "").trim());
|
|
184
|
+
});
|
|
185
|
+
return cookies;
|
|
186
|
+
}
|
|
187
|
+
function detectIP(req) {
|
|
188
|
+
const forwarded = req.headers.get("x-forwarded-for");
|
|
189
|
+
if (forwarded)
|
|
190
|
+
return forwarded.split(",")[0].trim();
|
|
191
|
+
const realIp = req.headers.get("x-real-ip");
|
|
192
|
+
if (realIp)
|
|
193
|
+
return realIp;
|
|
194
|
+
const cfIp = req.headers.get("cf-connecting-ip");
|
|
195
|
+
if (cfIp)
|
|
196
|
+
return cfIp;
|
|
197
|
+
const clientIp = req.headers.get("x-client-ip");
|
|
198
|
+
if (clientIp)
|
|
199
|
+
return clientIp;
|
|
200
|
+
return "127.0.0.1";
|
|
201
|
+
}
|
|
158
202
|
function zodToJsonSchema(schema) {
|
|
159
203
|
const d = schema._def;
|
|
160
204
|
const typeName = d?.typeName ?? d?.type ?? "";
|
|
@@ -310,6 +354,10 @@ class Prince {
|
|
|
310
354
|
staticRoutes = new Map;
|
|
311
355
|
staticMiddlewares = new Map;
|
|
312
356
|
routeCache = new Map;
|
|
357
|
+
onRequestHooks = [];
|
|
358
|
+
onBeforeHandleHooks = [];
|
|
359
|
+
onAfterHandleHooks = [];
|
|
360
|
+
onErrorHooks = [];
|
|
313
361
|
constructor(devMode = false) {
|
|
314
362
|
this.devMode = devMode;
|
|
315
363
|
}
|
|
@@ -325,6 +373,22 @@ class Prince {
|
|
|
325
373
|
this.errorHandler = fn;
|
|
326
374
|
return this;
|
|
327
375
|
}
|
|
376
|
+
onRequest(hook) {
|
|
377
|
+
this.onRequestHooks.push(hook);
|
|
378
|
+
return this;
|
|
379
|
+
}
|
|
380
|
+
onBeforeHandle(hook) {
|
|
381
|
+
this.onBeforeHandleHooks.push(hook);
|
|
382
|
+
return this;
|
|
383
|
+
}
|
|
384
|
+
onAfterHandle(hook) {
|
|
385
|
+
this.onAfterHandleHooks.push(hook);
|
|
386
|
+
return this;
|
|
387
|
+
}
|
|
388
|
+
onError(hook) {
|
|
389
|
+
this.onErrorHooks.push(hook);
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
328
392
|
json(data, status = 200) {
|
|
329
393
|
return new Response(JSON.stringify(data), {
|
|
330
394
|
status,
|
|
@@ -572,9 +636,12 @@ class Prince {
|
|
|
572
636
|
}
|
|
573
637
|
return null;
|
|
574
638
|
}
|
|
575
|
-
async executeHandler(req, handler, params, query, routeMiddlewares) {
|
|
639
|
+
async executeHandler(req, handler, params, query, routeMiddlewares, method, pathname) {
|
|
576
640
|
Object.defineProperty(req, "params", { value: params, writable: true, configurable: true });
|
|
577
641
|
Object.defineProperty(req, "query", { value: query, writable: true, configurable: true });
|
|
642
|
+
const cookieHeader = req.headers.get("cookie") || "";
|
|
643
|
+
Object.defineProperty(req, "cookies", { value: parseCookies(cookieHeader), writable: true, configurable: true });
|
|
644
|
+
Object.defineProperty(req, "ip", { value: detectIP(req), writable: true, configurable: true });
|
|
578
645
|
if (["POST", "PUT", "PATCH"].includes(req.method) && !req.parsedBody) {
|
|
579
646
|
const parsed = await this.parseBody(req);
|
|
580
647
|
if (parsed) {
|
|
@@ -593,6 +660,9 @@ class Prince {
|
|
|
593
660
|
},
|
|
594
661
|
configurable: true
|
|
595
662
|
});
|
|
663
|
+
for (const hook of this.onBeforeHandleHooks) {
|
|
664
|
+
await hook(req, pathname, method);
|
|
665
|
+
}
|
|
596
666
|
const allMiddlewares = routeMiddlewares.length > 0 ? [...this.middlewares, ...routeMiddlewares] : this.middlewares;
|
|
597
667
|
let i = 0;
|
|
598
668
|
const next = async () => {
|
|
@@ -604,19 +674,28 @@ class Prince {
|
|
|
604
674
|
const res = await handler(req);
|
|
605
675
|
if (res instanceof Response)
|
|
606
676
|
return res;
|
|
677
|
+
if (res instanceof ResponseBuilder)
|
|
678
|
+
return res.build();
|
|
607
679
|
if (typeof res === "string")
|
|
608
680
|
return new Response(res);
|
|
609
681
|
if (res instanceof Uint8Array)
|
|
610
682
|
return new Response(res);
|
|
611
683
|
return this.json(res);
|
|
612
684
|
};
|
|
613
|
-
|
|
685
|
+
const response = await next();
|
|
686
|
+
for (const hook of this.onAfterHandleHooks) {
|
|
687
|
+
await hook(req, response.clone(), pathname, method);
|
|
688
|
+
}
|
|
689
|
+
return response;
|
|
614
690
|
}
|
|
615
691
|
async handleFetch(req) {
|
|
616
692
|
const url = new URL(req.url);
|
|
617
693
|
const r = req;
|
|
618
694
|
const method = req.method;
|
|
619
695
|
const pathname = url.pathname;
|
|
696
|
+
for (const hook of this.onRequestHooks) {
|
|
697
|
+
await hook(r);
|
|
698
|
+
}
|
|
620
699
|
const routeMatch = this.findRoute(method, pathname);
|
|
621
700
|
if (!routeMatch) {
|
|
622
701
|
return this.json({ error: "Not Found" }, 404);
|
|
@@ -630,12 +709,18 @@ class Prince {
|
|
|
630
709
|
}
|
|
631
710
|
});
|
|
632
711
|
}
|
|
633
|
-
return this.executeHandler(r, routeMatch.handler, routeMatch.params, url.searchParams, routeMatch.middlewares);
|
|
712
|
+
return this.executeHandler(r, routeMatch.handler, routeMatch.params, url.searchParams, routeMatch.middlewares, method, pathname);
|
|
634
713
|
}
|
|
635
714
|
async fetch(req) {
|
|
715
|
+
const url = new URL(req.url);
|
|
716
|
+
const pathname = url.pathname;
|
|
717
|
+
const method = req.method;
|
|
636
718
|
try {
|
|
637
719
|
return await this.handleFetch(req);
|
|
638
720
|
} catch (err) {
|
|
721
|
+
for (const hook of this.onErrorHooks) {
|
|
722
|
+
await hook(err, req, pathname, method);
|
|
723
|
+
}
|
|
639
724
|
if (this.errorHandler)
|
|
640
725
|
return this.errorHandler(err, req);
|
|
641
726
|
if (this.devMode) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "princejs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "An easy and fast backend framework that is among the top three — by a 13yo developer, for developers.",
|
|
5
5
|
"main": "dist/prince.js",
|
|
6
6
|
"types": "dist/prince.d.ts",
|
|
@@ -45,6 +45,10 @@
|
|
|
45
45
|
"import": "./dist/adapters/deno.js",
|
|
46
46
|
"types": "./dist/adapters/deno.d.ts"
|
|
47
47
|
},
|
|
48
|
+
"./node": {
|
|
49
|
+
"import": "./dist/adapters/node.js",
|
|
50
|
+
"types": "./dist/adapters/node.d.ts"
|
|
51
|
+
},
|
|
48
52
|
"./client": {
|
|
49
53
|
"import": "./dist/client.js",
|
|
50
54
|
"types": "./dist/client.d.ts"
|
|
@@ -92,24 +96,23 @@
|
|
|
92
96
|
},
|
|
93
97
|
"devDependencies": {
|
|
94
98
|
"@types/bun": "^1.3.2",
|
|
95
|
-
"@types/jsonwebtoken": "^9.0.10",
|
|
96
99
|
"bun-types": "latest",
|
|
97
|
-
"fast-jwt": "^5.0.0",
|
|
98
|
-
"jsonwebtoken": "^9.0.2",
|
|
99
100
|
"typescript": "^5.9.3",
|
|
100
101
|
"zod": "^4.1.12"
|
|
101
102
|
},
|
|
102
103
|
"peerDependencies": {
|
|
103
|
-
"zod": "^4.1.12"
|
|
104
|
+
"zod": "^4.1.12",
|
|
105
|
+
"jose": "^6.1.2"
|
|
104
106
|
},
|
|
105
107
|
"peerDependenciesMeta": {
|
|
106
108
|
"zod": {
|
|
107
109
|
"optional": true
|
|
110
|
+
},
|
|
111
|
+
"jose": {
|
|
112
|
+
"optional": true
|
|
108
113
|
}
|
|
109
114
|
},
|
|
110
|
-
"dependencies": {
|
|
111
|
-
"jose": "^6.1.2"
|
|
112
|
-
},
|
|
115
|
+
"dependencies": {},
|
|
113
116
|
"scripts": {
|
|
114
117
|
"build:js": "bun build src/prince.ts --outdir dist --target bun --external zod --external jose && bun build src/middleware.ts --outdir dist --target bun && bun build src/helpers.ts --outdir dist --target bun && bun build src/scheduler.ts --outdir dist --target bun && bun build bin/create.ts --outdir dist --target bun && bun build src/jsx.ts --outdir dist --target bun && bun build src/db.ts --outdir dist --target bun --format esm && bun build src/client.ts --outdir dist --format esm && bun build src/adapters/vercel.ts --outdir dist/adapters --format esm && bun build src/adapters/cloudflare.ts --outdir dist/adapters --format esm && bun build src/adapters/deno.ts --outdir dist/adapters --format esm",
|
|
115
118
|
"build:types": "tsc --emitDeclarationOnly --skipLibCheck",
|