routup 1.0.0-alpha.5 → 1.0.0-alpha.6
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/helpers/request/body.d.ts +4 -1
- package/dist/helpers/request/cookie.d.ts +2 -0
- package/dist/index.cjs +57 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +56 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { Request } from '../../type';
|
|
2
2
|
export declare function useRequestBody(req: Request): Record<string, any>;
|
|
3
3
|
export declare function useRequestBody(req: Request, key: string): any | undefined;
|
|
4
|
+
export declare function hasRequestBody(req: Request): boolean;
|
|
4
5
|
export declare function setRequestBody(req: Request, key: string, value: unknown): void;
|
|
5
|
-
export declare function setRequestBody(req: Request, record: Record<string, any
|
|
6
|
+
export declare function setRequestBody(req: Request, record: Record<string, any>): void;
|
|
7
|
+
export declare function extendRequestBody(req: Request, key: string, value: unknown): void;
|
|
8
|
+
export declare function extendRequestBody(req: Request, record: Record<string, any>): void;
|
|
@@ -2,5 +2,7 @@ import type { Request } from '../../type';
|
|
|
2
2
|
export declare function useRequestCookies(req: Request): Record<string, string>;
|
|
3
3
|
export declare function hasRequestCookies(req: Request): boolean;
|
|
4
4
|
export declare function useRequestCookie(req: Request, name: string): string | undefined;
|
|
5
|
+
export declare function setRequestCookies(req: Request, key: string, value: unknown): void;
|
|
5
6
|
export declare function setRequestCookies(req: Request, record: Record<string, any>): void;
|
|
7
|
+
export declare function extendRequestCookies(req: Request, key: string, value: string): void;
|
|
6
8
|
export declare function extendRequestCookies(req: Request, record: Record<string, any>): void;
|
package/dist/index.cjs
CHANGED
|
@@ -323,20 +323,11 @@ function useRequestBody(req, key) {
|
|
|
323
323
|
}
|
|
324
324
|
return typeof key === 'string' ? undefined : {};
|
|
325
325
|
}
|
|
326
|
+
function hasRequestBody(req) {
|
|
327
|
+
return 'body' in req || BodySymbol in req;
|
|
328
|
+
}
|
|
326
329
|
function setRequestBody(req, key, value) {
|
|
327
|
-
if (
|
|
328
|
-
if (typeof key === 'object') {
|
|
329
|
-
if (value) {
|
|
330
|
-
req[BodySymbol] = smob.merge(req[BodySymbol], key);
|
|
331
|
-
} else {
|
|
332
|
-
req[BodySymbol] = key;
|
|
333
|
-
}
|
|
334
|
-
} else {
|
|
335
|
-
req[BodySymbol][key] = value;
|
|
336
|
-
}
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
if (typeof key === 'object') {
|
|
330
|
+
if (isObject(key)) {
|
|
340
331
|
req[BodySymbol] = key;
|
|
341
332
|
return;
|
|
342
333
|
}
|
|
@@ -344,6 +335,27 @@ function setRequestBody(req, key, value) {
|
|
|
344
335
|
[key]: value
|
|
345
336
|
};
|
|
346
337
|
}
|
|
338
|
+
function extendRequestBody(req, key, value) {
|
|
339
|
+
if (hasRequestBody(req)) {
|
|
340
|
+
const body = useRequestBody(req);
|
|
341
|
+
// body can not be merged :/
|
|
342
|
+
if (!isObject(body)) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (isObject(key)) {
|
|
346
|
+
req[BodySymbol] = smob.merge({}, key, body);
|
|
347
|
+
} else {
|
|
348
|
+
body[key] = value;
|
|
349
|
+
req[BodySymbol] = body;
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (isObject(key)) {
|
|
354
|
+
setRequestBody(req, key);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
setRequestBody(req, key, value);
|
|
358
|
+
}
|
|
347
359
|
|
|
348
360
|
/*
|
|
349
361
|
* Copyright (c) 2022.
|
|
@@ -372,15 +384,32 @@ function hasRequestCookies(req) {
|
|
|
372
384
|
function useRequestCookie(req, name) {
|
|
373
385
|
return useRequestCookies(req)[name];
|
|
374
386
|
}
|
|
375
|
-
function setRequestCookies(req,
|
|
376
|
-
|
|
387
|
+
function setRequestCookies(req, key, value) {
|
|
388
|
+
if (isObject(key)) {
|
|
389
|
+
req[CookieSymbol] = key;
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
req[CookieSymbol] = {
|
|
393
|
+
[key]: value
|
|
394
|
+
};
|
|
377
395
|
}
|
|
378
|
-
function extendRequestCookies(req,
|
|
379
|
-
if (
|
|
380
|
-
|
|
396
|
+
function extendRequestCookies(req, key, value) {
|
|
397
|
+
if (hasRequestCookies(req)) {
|
|
398
|
+
const cookies = useRequestCookies(req);
|
|
399
|
+
if (isObject(key)) {
|
|
400
|
+
req[CookieSymbol] = smob.merge({}, key, cookies);
|
|
401
|
+
} else {
|
|
402
|
+
cookies[key] = value;
|
|
403
|
+
req[CookieSymbol] = cookies;
|
|
404
|
+
}
|
|
405
|
+
req[CookieSymbol] = smob.merge(req[CookieSymbol], cookies);
|
|
381
406
|
return;
|
|
382
407
|
}
|
|
383
|
-
|
|
408
|
+
if (isObject(key)) {
|
|
409
|
+
setRequestCookies(req, key);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
setRequestCookies(req, key, value);
|
|
384
413
|
}
|
|
385
414
|
|
|
386
415
|
const envSymbol = Symbol.for('ReqEnv');
|
|
@@ -680,10 +709,10 @@ function useRequestQuery(req, key) {
|
|
|
680
709
|
}
|
|
681
710
|
return req[QuerySymbol];
|
|
682
711
|
}
|
|
683
|
-
return {};
|
|
712
|
+
return typeof key === 'string' ? undefined : {};
|
|
684
713
|
}
|
|
685
714
|
function hasRequestQuery(req) {
|
|
686
|
-
return QuerySymbol in req && isObject(req[QuerySymbol]);
|
|
715
|
+
return QuerySymbol in req && isObject(req[QuerySymbol]) || 'query' in req && isObject(req.query);
|
|
687
716
|
}
|
|
688
717
|
function setRequestQuery(req, key, value) {
|
|
689
718
|
if (isObject(key)) {
|
|
@@ -695,11 +724,13 @@ function setRequestQuery(req, key, value) {
|
|
|
695
724
|
};
|
|
696
725
|
}
|
|
697
726
|
function extendRequestQuery(req, key, value) {
|
|
698
|
-
if (
|
|
727
|
+
if (hasRequestQuery(req)) {
|
|
728
|
+
const query = useRequestQuery(req);
|
|
699
729
|
if (isObject(key)) {
|
|
700
|
-
req[QuerySymbol] = smob.merge(
|
|
730
|
+
req[QuerySymbol] = smob.merge({}, key, query);
|
|
701
731
|
} else {
|
|
702
|
-
|
|
732
|
+
query[key] = value;
|
|
733
|
+
req[QuerySymbol] = query;
|
|
703
734
|
}
|
|
704
735
|
return;
|
|
705
736
|
}
|
|
@@ -1554,6 +1585,7 @@ exports.buildTrustProxyFn = buildTrustProxyFn;
|
|
|
1554
1585
|
exports.cleanDoubleSlashes = cleanDoubleSlashes;
|
|
1555
1586
|
exports.createEtag = createEtag;
|
|
1556
1587
|
exports.createRequestTimeout = createRequestTimeout;
|
|
1588
|
+
exports.extendRequestBody = extendRequestBody;
|
|
1557
1589
|
exports.extendRequestCookies = extendRequestCookies;
|
|
1558
1590
|
exports.extendRequestQuery = extendRequestQuery;
|
|
1559
1591
|
exports.generateETag = generateETag;
|
|
@@ -1573,6 +1605,7 @@ exports.getRequestHostName = getRequestHostName;
|
|
|
1573
1605
|
exports.getRequestIP = getRequestIP;
|
|
1574
1606
|
exports.getRequestProtocol = getRequestProtocol;
|
|
1575
1607
|
exports.hasLeadingSlash = hasLeadingSlash;
|
|
1608
|
+
exports.hasRequestBody = hasRequestBody;
|
|
1576
1609
|
exports.hasRequestCookies = hasRequestCookies;
|
|
1577
1610
|
exports.hasRequestQuery = hasRequestQuery;
|
|
1578
1611
|
exports.hasTrailingSlash = hasTrailingSlash;
|