@thisisagile/easy 12.7.9 → 12.7.13

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.
@@ -1,22 +1,16 @@
1
1
  import { CacheAge } from "./CacheAge";
2
2
  export declare class CacheControl {
3
3
  readonly enabled: boolean;
4
- protected constructor(enabled?: boolean);
5
- protected _maxAge?: number;
6
- protected _sharedMaxAge?: number;
7
- protected _noCache?: boolean;
8
- protected _mustRevalidate?: boolean;
9
- protected _private?: boolean;
10
- protected _public?: boolean;
11
- protected _immutable?: boolean;
12
- protected _staleWhileRevalidate?: number;
4
+ private directives;
5
+ name: string;
6
+ protected constructor(enabled?: boolean, directives?: Record<string, boolean | CacheAge | undefined>);
13
7
  static disabled: () => CacheControl;
14
8
  static OneSecond: () => CacheControl;
15
9
  static fiveSeconds: () => CacheControl;
16
10
  static tenSeconds: () => CacheControl;
17
11
  static thirtySeconds: () => CacheControl;
18
12
  static sixtySeconds: () => CacheControl;
19
- static custom: (maxAge?: CacheAge, staleWhileRevalidate?: number) => CacheControl;
13
+ static custom: (maxAge?: CacheAge, staleWhileRevalidate?: CacheAge) => CacheControl;
20
14
  readonly maxAge: (ca?: CacheAge) => this;
21
15
  readonly sharedMaxAge: (ca?: CacheAge) => this;
22
16
  readonly noCache: (a?: boolean) => this;
@@ -24,7 +18,7 @@ export declare class CacheControl {
24
18
  readonly private: (a?: boolean) => this;
25
19
  readonly public: (a?: boolean) => this;
26
20
  readonly immutable: (a?: boolean) => this;
27
- readonly staleWhileRevalidate: (a?: number) => this;
21
+ readonly staleWhileRevalidate: (ca?: CacheAge) => this;
28
22
  value: () => string;
29
- name: string;
23
+ toString(): string;
30
24
  }
@@ -3,54 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CacheControl = void 0;
4
4
  const types_1 = require("../types");
5
5
  const CacheAge_1 = require("./CacheAge");
6
+ const utils_1 = require("../utils");
6
7
  class CacheControl {
7
- constructor(enabled = true) {
8
+ constructor(enabled = true, directives = {}) {
8
9
  this.enabled = enabled;
9
- this.maxAge = (ca) => {
10
- this._maxAge = ca ? CacheAge_1.cacheAge.toSeconds(ca) : ca;
11
- return this;
12
- };
13
- this.sharedMaxAge = (ca) => {
14
- this._sharedMaxAge = ca ? CacheAge_1.cacheAge.toSeconds(ca) : ca;
15
- return this;
16
- };
17
- this.noCache = (a) => {
18
- this._noCache = a;
19
- return this;
20
- };
21
- this.mustRevalidate = (a) => {
22
- this._mustRevalidate = a;
23
- return this;
24
- };
25
- this.private = (a) => {
26
- this._private = a;
27
- return this;
28
- };
29
- this.public = (a) => {
30
- this._public = a;
31
- return this;
32
- };
33
- this.immutable = (a) => {
34
- this._immutable = a;
35
- return this;
36
- };
37
- this.staleWhileRevalidate = (a) => {
38
- this._staleWhileRevalidate = a;
39
- return this;
40
- };
41
- this.value = () => {
42
- const directives = [];
43
- (0, types_1.isDefined)(this._maxAge) && directives.push(`max-age=${this._maxAge}`);
44
- (0, types_1.isDefined)(this._sharedMaxAge) && directives.push(`s-maxage=${this._sharedMaxAge}`);
45
- this._noCache === true && directives.push(`no-cache`);
46
- this._mustRevalidate === true && directives.push(`must-revalidate`);
47
- this._private === true && directives.push(`private`);
48
- this._public === true && directives.push(`public`);
49
- this._immutable === true && directives.push(`immutable`);
50
- (0, types_1.isDefined)(this._staleWhileRevalidate) && directives.push(`stale-while-revalidate=${this._staleWhileRevalidate}`);
51
- return directives.join(',');
52
- };
10
+ this.directives = directives;
53
11
  this.name = 'Cache-Control';
12
+ this.maxAge = (ca) => (0, types_1.on)(this, t => t.directives['max-age'] = ca);
13
+ this.sharedMaxAge = (ca) => (0, types_1.on)(this, t => t.directives['s-maxage'] = ca);
14
+ this.noCache = (a) => (0, types_1.on)(this, t => t.directives['no-cache'] = a);
15
+ this.mustRevalidate = (a) => (0, types_1.on)(this, t => t.directives['must-revalidate'] = a);
16
+ this.private = (a) => (0, types_1.on)(this, t => t.directives['private'] = a);
17
+ this.public = (a) => (0, types_1.on)(this, t => t.directives['public'] = a);
18
+ this.immutable = (a) => (0, types_1.on)(this, t => t.directives['immutable'] = a);
19
+ this.staleWhileRevalidate = (ca) => (0, types_1.on)(this, t => t.directives['stale-while-revalidate'] = ca);
20
+ this.value = () => this.toString();
21
+ }
22
+ toString() {
23
+ return (0, types_1.meta)(this.directives).entries().mapDefined(([k, v]) => (0, utils_1.ifDefined)(v, (0, types_1.isNumber)(v) ? `${k}=${CacheAge_1.cacheAge.toSeconds(v)}` : k)).join(',');
54
24
  }
55
25
  }
56
26
  exports.CacheControl = CacheControl;
@@ -1 +1 @@
1
- {"version":3,"file":"CacheControl.js","sourceRoot":"","sources":["../../src/http/CacheControl.ts"],"names":[],"mappings":";;;AAAA,oCAAqC;AACrC,yCAA8C;AAE9C,MAAa,YAAY;IACvB,YAA+B,UAAU,IAAI;QAAd,YAAO,GAAP,OAAO,CAAO;QAmBpC,WAAM,GAAG,CAAC,EAAa,EAAQ,EAAE;YACxC,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC,mBAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAChD,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QAEO,iBAAY,GAAG,CAAC,EAAa,EAAQ,EAAE;YAC9C,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC,CAAC,CAAC,mBAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACtD,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QAEO,YAAO,GAAG,CAAC,CAAW,EAAQ,EAAE;YACvC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;YAClB,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QAEO,mBAAc,GAAG,CAAC,CAAW,EAAQ,EAAE;YAC9C,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC;YACzB,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QA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a,GAAG,GAAG,EAAE,CAAC,IAAI,YAAY,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAAC;AAE7E,yBAAY,GAAG,GAAG,EAAE,CAAC,IAAI,YAAY,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAAC;AAE5E,mBAAM,GAAG,CAAC,MAAiB,EAAE,oBAA+B,EAAE,EAAE,CAAC,IAAI,YAAY,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,oBAAoB,CAAC,oBAAoB,CAAC,CAAC"}
@@ -6,3 +6,4 @@ export declare type Construct<T> = Get<T> | Constructor<T>;
6
6
  export declare const isConstructor: <T>(c?: unknown) => c is Constructor<T>;
7
7
  export declare const ofConstruct: <T>(c: Construct<T>, ...args: unknown[]) => T;
8
8
  export declare const toName: (subject?: unknown, postfix?: string) => string;
9
+ export declare const on: <T>(t: T, f: (t: T) => unknown) => T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toName = exports.ofConstruct = exports.isConstructor = void 0;
3
+ exports.on = exports.toName = exports.ofConstruct = exports.isConstructor = void 0;
4
4
  const Func_1 = require("./Func");
5
5
  const Is_1 = require("./Is");
6
6
  const isConstructor = (c) => ((0, Is_1.isDefined)(c) && (0, Func_1.isFunc)(c) && c.prototype && c.prototype.constructor) === c;
@@ -9,4 +9,9 @@ const ofConstruct = (c, ...args) => ((0, exports.isConstructor)(c) ? new c(...ar
9
9
  exports.ofConstruct = ofConstruct;
10
10
  const toName = (subject, postfix = '') => subject?.constructor?.name?.replace(postfix, '').toLowerCase() ?? '';
11
11
  exports.toName = toName;
12
+ const on = (t, f) => {
13
+ f(t);
14
+ return t;
15
+ };
16
+ exports.on = on;
12
17
  //# sourceMappingURL=Constructor.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Constructor.js","sourceRoot":"","sources":["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E,GAAG,IAAe,EAAK,EAAE,CAAC,CAAC,IAAA,qBAAa,EAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAA,aAAM,EAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAA7I,QAAA,WAAW,eAAkI;AAEnJ,MAAM,MAAM,GAAG,CAAC,OAAiB,EAAE,OAAO,GAAG,EAAE,EAAU,EAAE,CAAE,OAAe,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC;AAApI,QAAA,MAAM,UAA8H;AAE1I,MAAM,EAAE,GAAG,CAAI,CAAI,EAAE,CAAoB,EAAK,EAAE;IACnD,CAAC,CAAC,CAAC,CAAC,CAAC;IACL,OAAO,CAAC,CAAC;AACb,CAAC,CAAC;AAHW,QAAA,EAAE,MAGb"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thisisagile/easy",
3
- "version": "12.7.9",
3
+ "version": "12.7.13",
4
4
  "description": "Straightforward library for building domain-driven microservice architectures",
5
5
  "author": "Sander Hoogendoorn",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "access": "public"
32
32
  },
33
33
  "devDependencies": {
34
- "@thisisagile/easy-test": "12.7.9",
34
+ "@thisisagile/easy-test": "12.7.13",
35
35
  "@types/form-urlencoded": "^4.4.0",
36
36
  "@types/jsonwebtoken": "^8.5.9",
37
37
  "@types/luxon": "3.0.2",
@@ -1,78 +1,46 @@
1
- import { isDefined } from '../types';
1
+ import {isNumber, meta, on} from '../types';
2
2
  import {cacheAge, CacheAge} from "./CacheAge";
3
+ import {ifDefined} from "../utils";
3
4
 
4
5
  export class CacheControl {
5
- protected constructor(readonly enabled = true) {}
6
-
7
- protected _maxAge?: number;
8
- protected _sharedMaxAge?: number;
9
- protected _noCache?: boolean;
10
- protected _mustRevalidate?: boolean;
11
- protected _private?: boolean;
12
- protected _public?: boolean;
13
- protected _immutable?: boolean;
14
- protected _staleWhileRevalidate?: number;
15
-
16
- static disabled = () => new CacheControl(false);
17
- static OneSecond = () => new CacheControl().maxAge(1).staleWhileRevalidate(1);
18
- static fiveSeconds = () => new CacheControl().maxAge(5).staleWhileRevalidate(5);
19
- static tenSeconds = () => new CacheControl().maxAge(10).staleWhileRevalidate(10);
20
- static thirtySeconds = () => new CacheControl().maxAge(30).staleWhileRevalidate(30);
21
- static sixtySeconds = () => new CacheControl().maxAge(60).staleWhileRevalidate(60);
22
- static custom = (maxAge?: CacheAge, staleWhileRevalidate?: number) => new CacheControl().maxAge(maxAge).staleWhileRevalidate(staleWhileRevalidate);
23
-
24
- readonly maxAge = (ca?: CacheAge): this => {
25
- this._maxAge = ca ? cacheAge.toSeconds(ca) : ca;
26
- return this;
27
- };
28
-
29
- readonly sharedMaxAge = (ca?: CacheAge): this => {
30
- this._sharedMaxAge = ca ? cacheAge.toSeconds(ca) : ca;
31
- return this;
32
- };
33
-
34
- readonly noCache = (a?: boolean): this => {
35
- this._noCache = a;
36
- return this;
37
- };
38
-
39
- readonly mustRevalidate = (a?: boolean): this => {
40
- this._mustRevalidate = a;
41
- return this;
42
- };
43
-
44
- readonly private = (a?: boolean): this => {
45
- this._private = a;
46
- return this;
47
- };
48
-
49
- readonly public = (a?: boolean): this => {
50
- this._public = a;
51
- return this;
52
- };
53
-
54
- readonly immutable = (a?: boolean): this => {
55
- this._immutable = a;
56
- return this;
57
- };
58
-
59
- readonly staleWhileRevalidate = (a?: number): this => {
60
- this._staleWhileRevalidate = a;
61
- return this;
62
- };
63
-
64
- value = (): string => {
65
- const directives: string[] = [];
66
- isDefined(this._maxAge) && directives.push(`max-age=${this._maxAge}`);
67
- isDefined(this._sharedMaxAge) && directives.push(`s-maxage=${this._sharedMaxAge}`);
68
- this._noCache === true && directives.push(`no-cache`);
69
- this._mustRevalidate === true && directives.push(`must-revalidate`);
70
- this._private === true && directives.push(`private`);
71
- this._public === true && directives.push(`public`);
72
- this._immutable === true && directives.push(`immutable`);
73
- isDefined(this._staleWhileRevalidate) && directives.push(`stale-while-revalidate=${this._staleWhileRevalidate}`);
74
- return directives.join(',');
75
- };
76
-
77
- name = 'Cache-Control';
6
+ name = 'Cache-Control';
7
+
8
+ protected constructor(readonly enabled = true, private directives: Record<string, boolean | CacheAge | undefined> = {}) {
9
+ }
10
+
11
+ static disabled = () => new CacheControl(false);
12
+
13
+ static OneSecond = () => new CacheControl().maxAge(1).staleWhileRevalidate(1);
14
+
15
+ static fiveSeconds = () => new CacheControl().maxAge(5).staleWhileRevalidate(5);
16
+
17
+ static tenSeconds = () => new CacheControl().maxAge(10).staleWhileRevalidate(10);
18
+
19
+ static thirtySeconds = () => new CacheControl().maxAge(30).staleWhileRevalidate(30);
20
+
21
+ static sixtySeconds = () => new CacheControl().maxAge(60).staleWhileRevalidate(60);
22
+
23
+ static custom = (maxAge?: CacheAge, staleWhileRevalidate?: CacheAge) => new CacheControl().maxAge(maxAge).staleWhileRevalidate(staleWhileRevalidate);
24
+
25
+ readonly maxAge = (ca?: CacheAge): this => on(this, t => t.directives['max-age'] = ca);
26
+
27
+ readonly sharedMaxAge = (ca?: CacheAge): this => on(this, t=> t.directives['s-maxage'] = ca);
28
+
29
+ readonly noCache = (a?: boolean): this => on(this, t => t.directives['no-cache'] = a);
30
+
31
+ readonly mustRevalidate = (a?: boolean): this => on(this, t => t.directives['must-revalidate'] = a);
32
+
33
+ readonly private = (a?: boolean): this => on(this, t => t.directives['private'] = a);
34
+
35
+ readonly public = (a?: boolean): this => on(this, t => t.directives['public'] = a);
36
+
37
+ readonly immutable = (a?: boolean): this => on(this, t => t.directives['immutable'] = a);
38
+
39
+ readonly staleWhileRevalidate = (ca?: CacheAge): this => on(this, t => t.directives['stale-while-revalidate'] = ca);
40
+
41
+ value = (): string => this.toString();
42
+
43
+ toString(): string {
44
+ return meta(this.directives).entries().mapDefined(([k, v]) => ifDefined(v, isNumber(v) ? `${k}=${cacheAge.toSeconds(v)}` : k)).join(',');
45
+ }
78
46
  }
@@ -11,3 +11,8 @@ export const isConstructor = <T>(c?: unknown): c is Constructor<T> => (isDefined
11
11
  export const ofConstruct = <T>(c: Construct<T>, ...args: unknown[]): T => (isConstructor<T>(c) ? new c(...args) : isFunc<T, unknown>(c) ? c(...args) : c);
12
12
 
13
13
  export const toName = (subject?: unknown, postfix = ''): string => (subject as any)?.constructor?.name?.replace(postfix, '').toLowerCase() ?? '';
14
+
15
+ export const on = <T>(t: T, f: (t: T) => unknown): T => {
16
+ f(t);
17
+ return t;
18
+ };