routup 1.0.0-alpha.5 → 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.
@@ -23,6 +23,7 @@ export declare enum HeaderName {
23
23
  COOKIE = "cookie",
24
24
  ETag = "etag",
25
25
  HOST = "host",
26
+ IF_MODIFIED_SINCE = "if-modified-since",
26
27
  IF_NONE_MATCH = "if-none-match",
27
28
  LAST_MODIFIED = "last-modified",
28
29
  LOCATION = "location",
@@ -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>, append?: boolean): void;
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
@@ -55,12 +55,7 @@ var node_http = require('node:http');
55
55
  return weak ? `W/${tag}` : tag;
56
56
  }
57
57
 
58
- /*
59
- * Copyright (c) 2022-2023.
60
- * Author Peter Placzek (tada5hi)
61
- * For the full copyright and license information,
62
- * view the LICENSE file that was distributed with this source code.
63
- */ function isObject(item) {
58
+ function isObject(item) {
64
59
  return !!item && typeof item === 'object' && !Array.isArray(item);
65
60
  }
66
61
 
@@ -106,12 +101,7 @@ function buildTrustProxyFn(input) {
106
101
  return proxyAddr.compile(input || []);
107
102
  }
108
103
 
109
- /*
110
- * Copyright (c) 2022-2022.
111
- * Author Peter Placzek (tada5hi)
112
- * For the full copyright and license information,
113
- * view the LICENSE file that was distributed with this source code.
114
- */ function isInstance(input, name) {
104
+ function isInstance(input, name) {
115
105
  return typeof input === 'object' && input !== null && input['@instanceof'] === Symbol.for(name);
116
106
  }
117
107
 
@@ -132,12 +122,7 @@ function getCharsetForMimeType(type) {
132
122
  return undefined;
133
123
  }
134
124
 
135
- /*
136
- * Copyright (c) 2022.
137
- * Author Peter Placzek (tada5hi)
138
- * For the full copyright and license information,
139
- * view the LICENSE file that was distributed with this source code.
140
- */ function isPath(input) {
125
+ function isPath(input) {
141
126
  return typeof input === 'string' || input instanceof RegExp;
142
127
  }
143
128
 
@@ -167,12 +152,7 @@ function isPromise(p) {
167
152
  });
168
153
  }
169
154
 
170
- /*
171
- * Copyright (c) 2022-2023.
172
- * Author Peter Placzek (tada5hi)
173
- * For the full copyright and license information,
174
- * view the LICENSE file that was distributed with this source code.
175
- */ const TRAILING_SLASH_RE = /\/$|\/\?/;
155
+ const TRAILING_SLASH_RE = /\/$|\/\?/;
176
156
  function hasTrailingSlash(input = '', queryParams = false) {
177
157
  if (!queryParams) {
178
158
  return input.endsWith('/');
@@ -255,12 +235,7 @@ function getConfigOption(key) {
255
235
  return config.get(key);
256
236
  }
257
237
 
258
- /*
259
- * Copyright (c) 2022-2023.
260
- * Author Peter Placzek (tada5hi)
261
- * For the full copyright and license information,
262
- * view the LICENSE file that was distributed with this source code.
263
- */ exports.Method = void 0;
238
+ exports.Method = void 0;
264
239
  (function(Method) {
265
240
  Method["GET"] = 'get';
266
241
  Method["POST"] = 'post';
@@ -287,6 +262,7 @@ exports.HeaderName = void 0;
287
262
  HeaderName["COOKIE"] = 'cookie';
288
263
  HeaderName["ETag"] = 'etag';
289
264
  HeaderName["HOST"] = 'host';
265
+ HeaderName["IF_MODIFIED_SINCE"] = 'if-modified-since';
290
266
  HeaderName["IF_NONE_MATCH"] = 'if-none-match';
291
267
  HeaderName["LAST_MODIFIED"] = 'last-modified';
292
268
  HeaderName["LOCATION"] = 'location';
@@ -323,20 +299,11 @@ function useRequestBody(req, key) {
323
299
  }
324
300
  return typeof key === 'string' ? undefined : {};
325
301
  }
302
+ function hasRequestBody(req) {
303
+ return 'body' in req || BodySymbol in req;
304
+ }
326
305
  function setRequestBody(req, key, value) {
327
- if (BodySymbol in req) {
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') {
306
+ if (isObject(key)) {
340
307
  req[BodySymbol] = key;
341
308
  return;
342
309
  }
@@ -344,14 +311,30 @@ function setRequestBody(req, key, value) {
344
311
  [key]: value
345
312
  };
346
313
  }
314
+ function extendRequestBody(req, key, value) {
315
+ if (hasRequestBody(req)) {
316
+ const body = useRequestBody(req);
317
+ // body can not be merged :/
318
+ if (!isObject(body)) {
319
+ return;
320
+ }
321
+ if (isObject(key)) {
322
+ req[BodySymbol] = smob.merge({}, key, body);
323
+ } else {
324
+ body[key] = value;
325
+ req[BodySymbol] = body;
326
+ }
327
+ return;
328
+ }
329
+ if (isObject(key)) {
330
+ setRequestBody(req, key);
331
+ return;
332
+ }
333
+ setRequestBody(req, key, value);
334
+ }
347
335
 
348
- /*
349
- * Copyright (c) 2022.
350
- * Author Peter Placzek (tada5hi)
351
- * For the full copyright and license information,
352
- * view the LICENSE file that was distributed with this source code.
353
- */ function isRequestCacheable(req, modifiedTime) {
354
- const modifiedSince = req.headers['if-modified-since'];
336
+ function isRequestCacheable(req, modifiedTime) {
337
+ const modifiedSince = req.headers[exports.HeaderName.IF_MODIFIED_SINCE];
355
338
  if (!modifiedSince) {
356
339
  return false;
357
340
  }
@@ -372,15 +355,32 @@ function hasRequestCookies(req) {
372
355
  function useRequestCookie(req, name) {
373
356
  return useRequestCookies(req)[name];
374
357
  }
375
- function setRequestCookies(req, record) {
376
- req[CookieSymbol] = record;
358
+ function setRequestCookies(req, key, value) {
359
+ if (isObject(key)) {
360
+ req[CookieSymbol] = key;
361
+ return;
362
+ }
363
+ req[CookieSymbol] = {
364
+ [key]: value
365
+ };
377
366
  }
378
- function extendRequestCookies(req, record) {
379
- if (CookieSymbol in req) {
380
- req[CookieSymbol] = smob.merge(req[CookieSymbol], record);
367
+ function extendRequestCookies(req, key, value) {
368
+ if (hasRequestCookies(req)) {
369
+ const cookies = useRequestCookies(req);
370
+ if (isObject(key)) {
371
+ req[CookieSymbol] = smob.merge({}, key, cookies);
372
+ } else {
373
+ cookies[key] = value;
374
+ req[CookieSymbol] = cookies;
375
+ }
376
+ req[CookieSymbol] = smob.merge(req[CookieSymbol], cookies);
377
+ return;
378
+ }
379
+ if (isObject(key)) {
380
+ setRequestCookies(req, key);
381
381
  return;
382
382
  }
383
- req[CookieSymbol] = record;
383
+ setRequestCookies(req, key, value);
384
384
  }
385
385
 
386
386
  const envSymbol = Symbol.for('ReqEnv');
@@ -425,12 +425,7 @@ function unsetRequestEnv(req, key) {
425
425
  }
426
426
  }
427
427
 
428
- /*
429
- * Copyright (c) 2022.
430
- * Author Peter Placzek (tada5hi)
431
- * For the full copyright and license information,
432
- * view the LICENSE file that was distributed with this source code.
433
- */ function getRequestHeader(req, name) {
428
+ function getRequestHeader(req, name) {
434
429
  return req.headers[name];
435
430
  }
436
431
  function setRequestHeader(req, name, value) {
@@ -583,12 +578,7 @@ function getRequestIP(req, options) {
583
578
  return proxyAddr(req, trustProxy);
584
579
  }
585
580
 
586
- /*
587
- * Copyright (c) 2022.
588
- * Author Peter Placzek (tada5hi)
589
- * For the full copyright and license information,
590
- * view the LICENSE file that was distributed with this source code.
591
- */ const ReqMountPathSymbol = Symbol.for('ReqMountPath');
581
+ const ReqMountPathSymbol = Symbol.for('ReqMountPath');
592
582
  function useRequestMountPath(req) {
593
583
  if (ReqMountPathSymbol in req) {
594
584
  return req[ReqMountPathSymbol];
@@ -599,12 +589,7 @@ function setRequestMountPath(req, basePath) {
599
589
  req[ReqMountPathSymbol] = basePath;
600
590
  }
601
591
 
602
- /*
603
- * Copyright (c) 2022.
604
- * Author Peter Placzek (tada5hi)
605
- * For the full copyright and license information,
606
- * view the LICENSE file that was distributed with this source code.
607
- */ const ParamsSymbol = Symbol.for('ReqParams');
592
+ const ParamsSymbol = Symbol.for('ReqParams');
608
593
  function useRequestParams(req) {
609
594
  /* istanbul ignore next */ if ('params' in req) {
610
595
  return req.params;
@@ -626,15 +611,20 @@ function setRequestParam(req, key, value) {
626
611
  setRequestParams(req, params);
627
612
  }
628
613
 
614
+ const PathSymbol = Symbol.for('ReqPath');
629
615
  function useRequestPath(req) {
630
616
  if ('path' in req) {
631
617
  return req.path;
632
618
  }
619
+ if (PathSymbol in req) {
620
+ return req[PathSymbol];
621
+ }
633
622
  if (typeof req.url === 'undefined') {
634
623
  return '/';
635
624
  }
636
625
  const parsed = new node_url.URL(req.url, 'http://localhost/');
637
- return parsed.pathname;
626
+ req[PathSymbol] = parsed.pathname;
627
+ return req[PathSymbol];
638
628
  }
639
629
 
640
630
  function getRequestProtocol(req, options) {
@@ -680,10 +670,10 @@ function useRequestQuery(req, key) {
680
670
  }
681
671
  return req[QuerySymbol];
682
672
  }
683
- return {};
673
+ return typeof key === 'string' ? undefined : {};
684
674
  }
685
675
  function hasRequestQuery(req) {
686
- return QuerySymbol in req && isObject(req[QuerySymbol]);
676
+ return QuerySymbol in req && isObject(req[QuerySymbol]) || 'query' in req && isObject(req.query);
687
677
  }
688
678
  function setRequestQuery(req, key, value) {
689
679
  if (isObject(key)) {
@@ -695,11 +685,13 @@ function setRequestQuery(req, key, value) {
695
685
  };
696
686
  }
697
687
  function extendRequestQuery(req, key, value) {
698
- if (QuerySymbol in req) {
688
+ if (hasRequestQuery(req)) {
689
+ const query = useRequestQuery(req);
699
690
  if (isObject(key)) {
700
- req[QuerySymbol] = smob.merge(req[QuerySymbol], key);
691
+ req[QuerySymbol] = smob.merge({}, key, query);
701
692
  } else {
702
- req[QuerySymbol][key] = value;
693
+ query[key] = value;
694
+ req[QuerySymbol] = query;
703
695
  }
704
696
  return;
705
697
  }
@@ -710,12 +702,7 @@ function extendRequestQuery(req, key, value) {
710
702
  setRequestQuery(req, key, value);
711
703
  }
712
704
 
713
- /*
714
- * Copyright (c) 2022.
715
- * Author Peter Placzek (tada5hi)
716
- * For the full copyright and license information,
717
- * view the LICENSE file that was distributed with this source code.
718
- */ function setResponseCacheHeaders(res, options) {
705
+ function setResponseCacheHeaders(res, options) {
719
706
  options = options || {};
720
707
  const cacheControls = [
721
708
  'public'
@@ -730,12 +717,7 @@ function extendRequestQuery(req, key, value) {
730
717
  res.setHeader('cache-control', cacheControls.join(', '));
731
718
  }
732
719
 
733
- /*
734
- * Copyright (c) 2022.
735
- * Author Peter Placzek (tada5hi)
736
- * For the full copyright and license information,
737
- * view the LICENSE file that was distributed with this source code.
738
- */ function appendResponseHeader(res, name, value) {
720
+ function appendResponseHeader(res, name, value) {
739
721
  let header = res.getHeader(name);
740
722
  if (!header) {
741
723
  res.setHeader(name, value);
@@ -927,12 +909,7 @@ function sendCreated(res, chunk) {
927
909
  return send(res, chunk);
928
910
  }
929
911
 
930
- /*
931
- * Copyright (c) 2022.
932
- * Author Peter Placzek (tada5hi)
933
- * For the full copyright and license information,
934
- * view the LICENSE file that was distributed with this source code.
935
- */ function sendStream(res, stream, fn) {
912
+ function sendStream(res, stream, fn) {
936
913
  stream.on('open', ()=>{
937
914
  stream.pipe(res);
938
915
  });
@@ -1554,6 +1531,7 @@ exports.buildTrustProxyFn = buildTrustProxyFn;
1554
1531
  exports.cleanDoubleSlashes = cleanDoubleSlashes;
1555
1532
  exports.createEtag = createEtag;
1556
1533
  exports.createRequestTimeout = createRequestTimeout;
1534
+ exports.extendRequestBody = extendRequestBody;
1557
1535
  exports.extendRequestCookies = extendRequestCookies;
1558
1536
  exports.extendRequestQuery = extendRequestQuery;
1559
1537
  exports.generateETag = generateETag;
@@ -1573,6 +1551,7 @@ exports.getRequestHostName = getRequestHostName;
1573
1551
  exports.getRequestIP = getRequestIP;
1574
1552
  exports.getRequestProtocol = getRequestProtocol;
1575
1553
  exports.hasLeadingSlash = hasLeadingSlash;
1554
+ exports.hasRequestBody = hasRequestBody;
1576
1555
  exports.hasRequestCookies = hasRequestCookies;
1577
1556
  exports.hasRequestQuery = hasRequestQuery;
1578
1557
  exports.hasTrailingSlash = hasTrailingSlash;