princejs 1.8.2 → 1.8.3
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 +15 -0
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +102 -12526
- package/dist/prince.d.ts +10 -6
- package/dist/prince.d.ts.map +1 -1
- package/dist/prince.js +42 -24
- package/package.json +1 -1
package/dist/prince.d.ts
CHANGED
|
@@ -7,6 +7,9 @@ export interface PrinceRequest extends Request {
|
|
|
7
7
|
user?: any;
|
|
8
8
|
params?: Record<string, string>;
|
|
9
9
|
query?: URLSearchParams;
|
|
10
|
+
session?: any;
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
sseSend?: (data: any, event?: string, id?: string) => void;
|
|
10
13
|
[key: string]: any;
|
|
11
14
|
}
|
|
12
15
|
interface WebSocketHandler {
|
|
@@ -37,18 +40,19 @@ export declare class Prince {
|
|
|
37
40
|
private openapiData;
|
|
38
41
|
private router;
|
|
39
42
|
private staticRoutes;
|
|
43
|
+
private staticMiddlewares;
|
|
40
44
|
private routeCache;
|
|
41
45
|
constructor(devMode?: boolean);
|
|
42
46
|
use(mw: Middleware): this;
|
|
43
47
|
error(fn: (err: any, req: PrinceRequest) => Response): this;
|
|
44
48
|
json(data: any, status?: number): Response;
|
|
45
49
|
response(): ResponseBuilder;
|
|
46
|
-
get(path: string,
|
|
47
|
-
post(path: string,
|
|
48
|
-
put(path: string,
|
|
49
|
-
delete(path: string,
|
|
50
|
-
patch(path: string,
|
|
51
|
-
options(path: string,
|
|
50
|
+
get(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
|
51
|
+
post(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
|
52
|
+
put(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
|
53
|
+
delete(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
|
54
|
+
patch(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
|
55
|
+
options(path: string, ...args: (RouteHandler | Middleware)[]): this;
|
|
52
56
|
ws(path: string, handlers: WebSocketHandler): this;
|
|
53
57
|
private add;
|
|
54
58
|
private buildRouter;
|
package/dist/prince.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prince.d.ts","sourceRoot":"","sources":["../src/prince.ts"],"names":[],"mappings":"AAIA,KAAK,IAAI,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;AACpC,KAAK,UAAU,GAAG,CAAC,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,GAAG,QAAQ,GAAG,SAAS,CAAC;AAC3G,KAAK,aAAa,GAAG,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,GAAG,UAAU,CAAC;AAE1E,MAAM,WAAW,aAAc,SAAQ,OAAO;IAC5C,UAAU,CAAC,EAAE,GAAG,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,UAAU,gBAAgB;IACxB,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,KAAK,IAAI,CAAC;IACzB,OAAO,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,CAAC;IAClD,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1D,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,KAAK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Cache = new Map;
|
|
50
51
|
constructor(devMode = false) {
|
|
51
52
|
this.devMode = devMode;
|
|
@@ -67,39 +68,44 @@ class Prince {
|
|
|
67
68
|
response() {
|
|
68
69
|
return new ResponseBuilder;
|
|
69
70
|
}
|
|
70
|
-
get(path,
|
|
71
|
-
return this.add("GET", path,
|
|
71
|
+
get(path, ...args) {
|
|
72
|
+
return this.add("GET", path, args);
|
|
72
73
|
}
|
|
73
|
-
post(path,
|
|
74
|
-
return this.add("POST", path,
|
|
74
|
+
post(path, ...args) {
|
|
75
|
+
return this.add("POST", path, args);
|
|
75
76
|
}
|
|
76
|
-
put(path,
|
|
77
|
-
return this.add("PUT", path,
|
|
77
|
+
put(path, ...args) {
|
|
78
|
+
return this.add("PUT", path, args);
|
|
78
79
|
}
|
|
79
|
-
delete(path,
|
|
80
|
-
return this.add("DELETE", path,
|
|
80
|
+
delete(path, ...args) {
|
|
81
|
+
return this.add("DELETE", path, args);
|
|
81
82
|
}
|
|
82
|
-
patch(path,
|
|
83
|
-
return this.add("PATCH", path,
|
|
83
|
+
patch(path, ...args) {
|
|
84
|
+
return this.add("PATCH", path, args);
|
|
84
85
|
}
|
|
85
|
-
options(path,
|
|
86
|
-
return this.add("OPTIONS", path,
|
|
86
|
+
options(path, ...args) {
|
|
87
|
+
return this.add("OPTIONS", path, args);
|
|
87
88
|
}
|
|
88
89
|
ws(path, handlers) {
|
|
89
90
|
this.wsRoutes[path] = handlers;
|
|
90
91
|
return this;
|
|
91
92
|
}
|
|
92
|
-
add(method, path,
|
|
93
|
+
add(method, path, args) {
|
|
93
94
|
if (!path.startsWith("/"))
|
|
94
95
|
path = "/" + path;
|
|
95
96
|
if (path !== "/" && path.endsWith("/"))
|
|
96
97
|
path = path.slice(0, -1);
|
|
97
98
|
const parts = path === "/" ? [""] : path.split("/").slice(1);
|
|
98
|
-
|
|
99
|
+
const handler = args[args.length - 1];
|
|
100
|
+
const middlewares = args.slice(0, -1);
|
|
101
|
+
this.rawRoutes.push({ method, path, parts, handler, middlewares });
|
|
99
102
|
const isStaticRoute = !parts.some((part) => part.includes(":") || part.includes("*") || part.includes("("));
|
|
100
103
|
if (isStaticRoute) {
|
|
101
104
|
const staticKey = `${method}:${path}`;
|
|
102
105
|
this.staticRoutes.set(staticKey, handler);
|
|
106
|
+
if (middlewares.length > 0) {
|
|
107
|
+
this.staticMiddlewares.set(staticKey, middlewares);
|
|
108
|
+
}
|
|
103
109
|
}
|
|
104
110
|
this.routeCache.clear();
|
|
105
111
|
this.router = null;
|
|
@@ -111,6 +117,7 @@ class Prince {
|
|
|
111
117
|
const root = {
|
|
112
118
|
pattern: "",
|
|
113
119
|
handlers: {},
|
|
120
|
+
middlewares: {},
|
|
114
121
|
children: []
|
|
115
122
|
};
|
|
116
123
|
for (const route of this.rawRoutes) {
|
|
@@ -138,6 +145,7 @@ class Prince {
|
|
|
138
145
|
const newNode = {
|
|
139
146
|
pattern: part,
|
|
140
147
|
handlers: {},
|
|
148
|
+
middlewares: {},
|
|
141
149
|
children: []
|
|
142
150
|
};
|
|
143
151
|
if (part.startsWith(":")) {
|
|
@@ -152,6 +160,9 @@ class Prince {
|
|
|
152
160
|
}
|
|
153
161
|
}
|
|
154
162
|
currentNode.handlers[route.method] = route.handler;
|
|
163
|
+
if (route.middlewares.length > 0) {
|
|
164
|
+
currentNode.middlewares[route.method] = route.middlewares;
|
|
165
|
+
}
|
|
155
166
|
}
|
|
156
167
|
findRoute(method, pathname) {
|
|
157
168
|
const cacheKey = `${method}:${pathname}`;
|
|
@@ -161,7 +172,11 @@ class Prince {
|
|
|
161
172
|
const staticKey = `${method}:${pathname}`;
|
|
162
173
|
const staticHandler = this.staticRoutes.get(staticKey);
|
|
163
174
|
if (staticHandler) {
|
|
164
|
-
const result2 = {
|
|
175
|
+
const result2 = {
|
|
176
|
+
handler: staticHandler,
|
|
177
|
+
params: {},
|
|
178
|
+
middlewares: this.staticMiddlewares.get(staticKey) || []
|
|
179
|
+
};
|
|
165
180
|
this.routeCache.set(cacheKey, result2);
|
|
166
181
|
return result2;
|
|
167
182
|
}
|
|
@@ -187,6 +202,7 @@ class Prince {
|
|
|
187
202
|
const methodNotAllowed = {
|
|
188
203
|
handler: null,
|
|
189
204
|
params: {},
|
|
205
|
+
middlewares: [],
|
|
190
206
|
allowedMethods: Array.from(allowedMethods)
|
|
191
207
|
};
|
|
192
208
|
this.routeCache.set(cacheKey, methodNotAllowed);
|
|
@@ -199,9 +215,8 @@ class Prince {
|
|
|
199
215
|
const routeParts = routePath === "/" ? [""] : routePath.split("/").slice(1);
|
|
200
216
|
const requestParts = requestPath === "/" ? [""] : requestPath.split("/").slice(1);
|
|
201
217
|
if (routeParts.length !== requestParts.length) {
|
|
202
|
-
if (routeParts.includes("**"))
|
|
218
|
+
if (routeParts.includes("**"))
|
|
203
219
|
return true;
|
|
204
|
-
}
|
|
205
220
|
return false;
|
|
206
221
|
}
|
|
207
222
|
for (let i = 0;i < routeParts.length; i++) {
|
|
@@ -219,7 +234,8 @@ class Prince {
|
|
|
219
234
|
matchRoute(node, segments, method, params = {}, index = 0) {
|
|
220
235
|
if (index === segments.length) {
|
|
221
236
|
const handler = node.handlers[method];
|
|
222
|
-
|
|
237
|
+
const middlewares = node.middlewares[method] || [];
|
|
238
|
+
return handler ? { handler, params, middlewares } : null;
|
|
223
239
|
}
|
|
224
240
|
const segment = segments[index];
|
|
225
241
|
for (const child of node.children) {
|
|
@@ -250,8 +266,9 @@ class Prince {
|
|
|
250
266
|
for (const child of node.children) {
|
|
251
267
|
if (child.isCatchAll) {
|
|
252
268
|
const handler = child.handlers[method];
|
|
269
|
+
const middlewares = child.middlewares[method] || [];
|
|
253
270
|
if (handler) {
|
|
254
|
-
return { handler, params };
|
|
271
|
+
return { handler, params, middlewares };
|
|
255
272
|
}
|
|
256
273
|
}
|
|
257
274
|
}
|
|
@@ -289,10 +306,10 @@ class Prince {
|
|
|
289
306
|
}
|
|
290
307
|
return null;
|
|
291
308
|
}
|
|
292
|
-
async executeHandler(req, handler, params, query) {
|
|
309
|
+
async executeHandler(req, handler, params, query, routeMiddlewares) {
|
|
293
310
|
Object.defineProperty(req, "params", { value: params, writable: true, configurable: true });
|
|
294
311
|
Object.defineProperty(req, "query", { value: query, writable: true, configurable: true });
|
|
295
|
-
if (["POST", "PUT", "PATCH"].includes(req.method)) {
|
|
312
|
+
if (["POST", "PUT", "PATCH"].includes(req.method) && !req.parsedBody) {
|
|
296
313
|
const parsed = await this.parseBody(req);
|
|
297
314
|
if (parsed) {
|
|
298
315
|
if (typeof parsed === "object" && "files" in parsed && "fields" in parsed) {
|
|
@@ -310,10 +327,11 @@ class Prince {
|
|
|
310
327
|
},
|
|
311
328
|
configurable: true
|
|
312
329
|
});
|
|
330
|
+
const allMiddlewares = routeMiddlewares.length > 0 ? [...this.middlewares, ...routeMiddlewares] : this.middlewares;
|
|
313
331
|
let i = 0;
|
|
314
332
|
const next = async () => {
|
|
315
|
-
while (i <
|
|
316
|
-
const result = await
|
|
333
|
+
while (i < allMiddlewares.length) {
|
|
334
|
+
const result = await allMiddlewares[i++](req, next);
|
|
317
335
|
if (result instanceof Response)
|
|
318
336
|
return result;
|
|
319
337
|
}
|
|
@@ -346,7 +364,7 @@ class Prince {
|
|
|
346
364
|
}
|
|
347
365
|
});
|
|
348
366
|
}
|
|
349
|
-
return this.executeHandler(r, routeMatch.handler, routeMatch.params, url.searchParams);
|
|
367
|
+
return this.executeHandler(r, routeMatch.handler, routeMatch.params, url.searchParams, routeMatch.middlewares);
|
|
350
368
|
}
|
|
351
369
|
async fetch(req) {
|
|
352
370
|
try {
|
package/package.json
CHANGED