weifuwu 0.22.0 → 0.22.2
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/index.js +52 -26
- package/dist/test-utils.d.ts +10 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1703,6 +1703,12 @@ var TestResponseImpl = class {
|
|
|
1703
1703
|
async text() {
|
|
1704
1704
|
return this.response.text();
|
|
1705
1705
|
}
|
|
1706
|
+
async bytes() {
|
|
1707
|
+
return this.response.bytes();
|
|
1708
|
+
}
|
|
1709
|
+
async arrayBuffer() {
|
|
1710
|
+
return this.response.arrayBuffer();
|
|
1711
|
+
}
|
|
1706
1712
|
};
|
|
1707
1713
|
var TestRequest = class {
|
|
1708
1714
|
headers = {};
|
|
@@ -1783,29 +1789,29 @@ var TestApp = class {
|
|
|
1783
1789
|
this.router.use(mw);
|
|
1784
1790
|
return this;
|
|
1785
1791
|
}
|
|
1786
|
-
/** Register a GET route */
|
|
1787
|
-
get(path2,
|
|
1788
|
-
this.router.get(path2,
|
|
1792
|
+
/** Register a GET route — supports route-level middleware via spread args. */
|
|
1793
|
+
get(path2, ...args) {
|
|
1794
|
+
this.router.get(path2, ...args);
|
|
1789
1795
|
return this;
|
|
1790
1796
|
}
|
|
1791
|
-
/** Register a POST route */
|
|
1792
|
-
post(path2,
|
|
1793
|
-
this.router.post(path2,
|
|
1797
|
+
/** Register a POST route. */
|
|
1798
|
+
post(path2, ...args) {
|
|
1799
|
+
this.router.post(path2, ...args);
|
|
1794
1800
|
return this;
|
|
1795
1801
|
}
|
|
1796
|
-
/** Register a PUT route */
|
|
1797
|
-
put(path2,
|
|
1798
|
-
this.router.put(path2,
|
|
1802
|
+
/** Register a PUT route. */
|
|
1803
|
+
put(path2, ...args) {
|
|
1804
|
+
this.router.put(path2, ...args);
|
|
1799
1805
|
return this;
|
|
1800
1806
|
}
|
|
1801
|
-
/** Register a PATCH route */
|
|
1802
|
-
patch(path2,
|
|
1803
|
-
this.router.patch(path2,
|
|
1807
|
+
/** Register a PATCH route. */
|
|
1808
|
+
patch(path2, ...args) {
|
|
1809
|
+
this.router.patch(path2, ...args);
|
|
1804
1810
|
return this;
|
|
1805
1811
|
}
|
|
1806
|
-
/** Register a DELETE route */
|
|
1807
|
-
delete(path2,
|
|
1808
|
-
this.router.delete(path2,
|
|
1812
|
+
/** Register a DELETE route. */
|
|
1813
|
+
delete(path2, ...args) {
|
|
1814
|
+
this.router.delete(path2, ...args);
|
|
1809
1815
|
return this;
|
|
1810
1816
|
}
|
|
1811
1817
|
/** Start building a GET request */
|
|
@@ -3428,17 +3434,30 @@ function user(options) {
|
|
|
3428
3434
|
}
|
|
3429
3435
|
function middleware() {
|
|
3430
3436
|
return async (req, ctx, next) => {
|
|
3431
|
-
const
|
|
3432
|
-
if (
|
|
3433
|
-
|
|
3437
|
+
const sessionUserId = ctx.session?.userId;
|
|
3438
|
+
if (sessionUserId) {
|
|
3439
|
+
const row = await findById(sessionUserId);
|
|
3440
|
+
if (row) {
|
|
3441
|
+
ctx.user = stripPassword(row);
|
|
3442
|
+
return next(req, ctx);
|
|
3443
|
+
}
|
|
3444
|
+
if (typeof ctx.session?.destroy === "function") {
|
|
3445
|
+
;
|
|
3446
|
+
ctx.session.destroy();
|
|
3447
|
+
} else {
|
|
3448
|
+
delete ctx.session?.userId;
|
|
3449
|
+
}
|
|
3434
3450
|
}
|
|
3435
|
-
const
|
|
3436
|
-
const
|
|
3437
|
-
if (
|
|
3438
|
-
|
|
3451
|
+
const header = req.headers.get("Authorization");
|
|
3452
|
+
const token = header?.startsWith("Bearer ") ? header.slice(7) : null;
|
|
3453
|
+
if (token) {
|
|
3454
|
+
const userData = await verify(token);
|
|
3455
|
+
if (userData) {
|
|
3456
|
+
ctx.user = userData;
|
|
3457
|
+
return next(req, ctx);
|
|
3458
|
+
}
|
|
3439
3459
|
}
|
|
3440
|
-
|
|
3441
|
-
return next(req, ctx);
|
|
3460
|
+
return new Response("Unauthorized", { status: 401, headers: { "WWW-Authenticate": "Bearer" } });
|
|
3442
3461
|
};
|
|
3443
3462
|
}
|
|
3444
3463
|
function router() {
|
|
@@ -3456,12 +3475,19 @@ function user(options) {
|
|
|
3456
3475
|
return Response.json({ error: err.message }, { status });
|
|
3457
3476
|
}
|
|
3458
3477
|
});
|
|
3459
|
-
r2.post("/login", async (req) => {
|
|
3478
|
+
r2.post("/login", async (req, ctx) => {
|
|
3460
3479
|
try {
|
|
3461
3480
|
const body = await req.json();
|
|
3462
3481
|
const result = await login(body);
|
|
3482
|
+
if (ctx.session) {
|
|
3483
|
+
;
|
|
3484
|
+
ctx.session.userId = result.user.id;
|
|
3485
|
+
ctx.session.role = result.user.role;
|
|
3486
|
+
}
|
|
3463
3487
|
const res = Response.json(result);
|
|
3464
|
-
|
|
3488
|
+
if (!ctx.session) {
|
|
3489
|
+
res.headers.set("Set-Cookie", `session=${result.token}; HttpOnly; SameSite=Lax; Path=/`);
|
|
3490
|
+
}
|
|
3465
3491
|
return res;
|
|
3466
3492
|
} catch (err) {
|
|
3467
3493
|
if (err instanceof z2.ZodError) {
|
package/dist/test-utils.d.ts
CHANGED
|
@@ -38,16 +38,16 @@ export declare class TestApp {
|
|
|
38
38
|
constructor();
|
|
39
39
|
/** Add global middleware */
|
|
40
40
|
use(mw: any): this;
|
|
41
|
-
/** Register a GET route */
|
|
42
|
-
get(path: string,
|
|
43
|
-
/** Register a POST route */
|
|
44
|
-
post(path: string,
|
|
45
|
-
/** Register a PUT route */
|
|
46
|
-
put(path: string,
|
|
47
|
-
/** Register a PATCH route */
|
|
48
|
-
patch(path: string,
|
|
49
|
-
/** Register a DELETE route */
|
|
50
|
-
delete(path: string,
|
|
41
|
+
/** Register a GET route — supports route-level middleware via spread args. */
|
|
42
|
+
get(path: string, ...args: any[]): this;
|
|
43
|
+
/** Register a POST route. */
|
|
44
|
+
post(path: string, ...args: any[]): this;
|
|
45
|
+
/** Register a PUT route. */
|
|
46
|
+
put(path: string, ...args: any[]): this;
|
|
47
|
+
/** Register a PATCH route. */
|
|
48
|
+
patch(path: string, ...args: any[]): this;
|
|
49
|
+
/** Register a DELETE route. */
|
|
50
|
+
delete(path: string, ...args: any[]): this;
|
|
51
51
|
/** Start building a GET request */
|
|
52
52
|
getReq(path: string): TestRequest;
|
|
53
53
|
/** Start building a POST request */
|