@art-ws/common 1.0.4 → 2.0.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.
Files changed (140) hide show
  1. package/dist/{cancellation → es/cancellation}/cancellation-token-source.d.ts +1 -2
  2. package/dist/es/cancellation/cancellation-token-source.js +24 -0
  3. package/dist/{cancellation → es/cancellation}/cancellation-token.d.ts +1 -2
  4. package/dist/{cancellation → es/cancellation}/cancellation-token.js +4 -8
  5. package/dist/{cancellation → es/cancellation}/index.d.ts +0 -1
  6. package/dist/es/cancellation/index.js +4 -0
  7. package/dist/{cancellation → es/cancellation}/interfaces.d.ts +1 -2
  8. package/dist/es/cancellation/interfaces.js +5 -0
  9. package/dist/{heart-beat → es/heart-beat}/heart-beat.class.d.ts +1 -2
  10. package/dist/{heart-beat → es/heart-beat}/heart-beat.class.js +10 -15
  11. package/dist/{heart-beat → es/heart-beat}/heart-beat.types.d.ts +0 -1
  12. package/dist/es/heart-beat/heart-beat.types.js +1 -0
  13. package/dist/{heart-beat → es/heart-beat}/index.d.ts +0 -1
  14. package/dist/es/heart-beat/index.js +3 -0
  15. package/dist/{hr-time.d.ts → es/hr-time.d.ts} +1 -2
  16. package/dist/es/hr-time.js +9 -0
  17. package/dist/{index.d.ts → es/index.d.ts} +1 -1
  18. package/dist/es/index.js +13 -0
  19. package/dist/es/interpolate.d.ts +5 -0
  20. package/dist/es/interpolate.js +150 -0
  21. package/dist/es/json-file-loader.d.ts +25 -0
  22. package/dist/es/json-file-loader.js +108 -0
  23. package/dist/es/json-refs-resolver.d.ts +6 -0
  24. package/dist/{json-refs-resolver.js → es/json-refs-resolver.js} +13 -18
  25. package/dist/es/managed-pool/index.d.ts +1 -0
  26. package/dist/es/managed-pool/index.js +2 -0
  27. package/dist/{managed-pool → es/managed-pool}/managed-pool.d.ts +0 -1
  28. package/dist/{managed-pool → es/managed-pool}/managed-pool.js +9 -10
  29. package/dist/{managed-promise.d.ts → es/managed-promise.d.ts} +1 -1
  30. package/dist/es/managed-promise.js +24 -0
  31. package/dist/{mem-cache → es/mem-cache}/cache-stat.d.ts +0 -1
  32. package/dist/{mem-cache → es/mem-cache}/cache-stat.js +5 -12
  33. package/dist/{mem-cache → es/mem-cache}/index.d.ts +0 -1
  34. package/dist/es/mem-cache/index.js +3 -0
  35. package/dist/{mem-cache → es/mem-cache}/memory-cache.d.ts +3 -4
  36. package/dist/{mem-cache → es/mem-cache}/memory-cache.js +20 -21
  37. package/dist/{node-zone.d.ts → es/node-zone.d.ts} +3 -4
  38. package/dist/{node-zone.js → es/node-zone.js} +14 -22
  39. package/dist/es/object-hash/index.d.ts +1 -0
  40. package/dist/es/object-hash/index.js +2 -0
  41. package/dist/es/object-hash/object-hash.d.ts +1 -0
  42. package/dist/es/object-hash/object-hash.js +3 -0
  43. package/dist/es/shared/api-client/api-client.interface.d.ts +63 -0
  44. package/dist/es/shared/api-client/api-client.interface.js +15 -0
  45. package/dist/es/shared/api-client/decorators.d.ts +8 -0
  46. package/dist/es/shared/api-client/decorators.js +73 -0
  47. package/dist/es/shared/api-client/index.d.ts +2 -0
  48. package/dist/es/shared/api-client/index.js +3 -0
  49. package/dist/es/shared/api-client/make-url.d.ts +6 -0
  50. package/dist/es/shared/api-client/make-url.js +32 -0
  51. package/dist/{shared → es/shared}/assert-dsl.d.ts +4 -2
  52. package/dist/{shared → es/shared}/assert-dsl.js +30 -16
  53. package/dist/es/shared/await-if-async.d.ts +1 -0
  54. package/dist/es/shared/await-if-async.js +5 -0
  55. package/dist/{shared → es/shared}/disposable.d.ts +0 -1
  56. package/dist/es/shared/disposable.js +1 -0
  57. package/dist/{shared → es/shared}/index.d.ts +2 -1
  58. package/dist/es/shared/index.js +9 -0
  59. package/dist/{shared → es/shared}/optional.d.ts +0 -1
  60. package/dist/{shared → es/shared}/optional.js +6 -10
  61. package/dist/es/shared/promise-race.d.ts +7 -0
  62. package/dist/{shared → es/shared}/promise-race.js +10 -18
  63. package/dist/{shared → es/shared}/runnable.d.ts +1 -2
  64. package/dist/es/shared/runnable.js +6 -0
  65. package/dist/{shared → es/shared}/safe-call.d.ts +1 -2
  66. package/dist/es/shared/safe-call.js +8 -0
  67. package/package.json +36 -63
  68. package/dist/cancellation/cancellation-token-source.d.ts.map +0 -1
  69. package/dist/cancellation/cancellation-token-source.js +0 -31
  70. package/dist/cancellation/cancellation-token-source.js.map +0 -1
  71. package/dist/cancellation/cancellation-token.d.ts.map +0 -1
  72. package/dist/cancellation/cancellation-token.js.map +0 -1
  73. package/dist/cancellation/index.d.ts.map +0 -1
  74. package/dist/cancellation/index.js +0 -8
  75. package/dist/cancellation/index.js.map +0 -1
  76. package/dist/cancellation/interfaces.d.ts.map +0 -1
  77. package/dist/cancellation/interfaces.js +0 -10
  78. package/dist/cancellation/interfaces.js.map +0 -1
  79. package/dist/heart-beat/heart-beat.class.d.ts.map +0 -1
  80. package/dist/heart-beat/heart-beat.class.js.map +0 -1
  81. package/dist/heart-beat/heart-beat.types.d.ts.map +0 -1
  82. package/dist/heart-beat/heart-beat.types.js +0 -3
  83. package/dist/heart-beat/heart-beat.types.js.map +0 -1
  84. package/dist/heart-beat/index.d.ts.map +0 -1
  85. package/dist/heart-beat/index.js +0 -7
  86. package/dist/heart-beat/index.js.map +0 -1
  87. package/dist/hr-time.d.ts.map +0 -1
  88. package/dist/hr-time.js +0 -15
  89. package/dist/hr-time.js.map +0 -1
  90. package/dist/index.d.ts.map +0 -1
  91. package/dist/index.js +0 -16
  92. package/dist/index.js.map +0 -1
  93. package/dist/interpolate.d.ts +0 -3
  94. package/dist/interpolate.d.ts.map +0 -1
  95. package/dist/interpolate.js +0 -88
  96. package/dist/interpolate.js.map +0 -1
  97. package/dist/json-file-loader.d.ts +0 -17
  98. package/dist/json-file-loader.d.ts.map +0 -1
  99. package/dist/json-file-loader.js +0 -83
  100. package/dist/json-file-loader.js.map +0 -1
  101. package/dist/json-refs-resolver.d.ts +0 -7
  102. package/dist/json-refs-resolver.d.ts.map +0 -1
  103. package/dist/json-refs-resolver.js.map +0 -1
  104. package/dist/managed-pool/index.d.ts +0 -2
  105. package/dist/managed-pool/index.d.ts.map +0 -1
  106. package/dist/managed-pool/index.js +0 -6
  107. package/dist/managed-pool/index.js.map +0 -1
  108. package/dist/managed-pool/managed-pool.d.ts.map +0 -1
  109. package/dist/managed-pool/managed-pool.js.map +0 -1
  110. package/dist/managed-promise.d.ts.map +0 -1
  111. package/dist/managed-promise.js +0 -26
  112. package/dist/managed-promise.js.map +0 -1
  113. package/dist/mem-cache/cache-stat.d.ts.map +0 -1
  114. package/dist/mem-cache/cache-stat.js.map +0 -1
  115. package/dist/mem-cache/index.d.ts.map +0 -1
  116. package/dist/mem-cache/index.js +0 -7
  117. package/dist/mem-cache/index.js.map +0 -1
  118. package/dist/mem-cache/memory-cache.d.ts.map +0 -1
  119. package/dist/mem-cache/memory-cache.js.map +0 -1
  120. package/dist/node-zone.d.ts.map +0 -1
  121. package/dist/node-zone.js.map +0 -1
  122. package/dist/shared/assert-dsl.d.ts.map +0 -1
  123. package/dist/shared/assert-dsl.js.map +0 -1
  124. package/dist/shared/disposable.d.ts.map +0 -1
  125. package/dist/shared/disposable.js +0 -3
  126. package/dist/shared/disposable.js.map +0 -1
  127. package/dist/shared/index.d.ts.map +0 -1
  128. package/dist/shared/index.js +0 -11
  129. package/dist/shared/index.js.map +0 -1
  130. package/dist/shared/optional.d.ts.map +0 -1
  131. package/dist/shared/optional.js.map +0 -1
  132. package/dist/shared/promise-race.d.ts +0 -8
  133. package/dist/shared/promise-race.d.ts.map +0 -1
  134. package/dist/shared/promise-race.js.map +0 -1
  135. package/dist/shared/runnable.d.ts.map +0 -1
  136. package/dist/shared/runnable.js +0 -12
  137. package/dist/shared/runnable.js.map +0 -1
  138. package/dist/shared/safe-call.d.ts.map +0 -1
  139. package/dist/shared/safe-call.js +0 -13
  140. package/dist/shared/safe-call.js.map +0 -1
@@ -0,0 +1,24 @@
1
+ import BlueBirdPromise from "bluebird";
2
+ // http://bluebirdjs.com/docs/api/cancellation.html
3
+ export function ManagedPromise(args) {
4
+ let fulfill;
5
+ let rejected = () => { };
6
+ const promise = new BlueBirdPromise((resolve, reject, onCancel) => {
7
+ fulfill = resolve;
8
+ rejected = reject;
9
+ if (typeof onCancel === "function") {
10
+ onCancel(() => {
11
+ if (typeof args?.onCancel === "function") {
12
+ args.onCancel();
13
+ }
14
+ });
15
+ }
16
+ });
17
+ promise.resolvedValue = undefined;
18
+ promise.resolve = (value) => {
19
+ promise.resolvedValue = value;
20
+ return fulfill(value);
21
+ };
22
+ promise.reject = rejected;
23
+ return promise;
24
+ }
@@ -12,4 +12,3 @@ export declare class CacheStat {
12
12
  incMiss(): void;
13
13
  getStatistics(): CacheStatistics;
14
14
  }
15
- //# sourceMappingURL=cache-stat.d.ts.map
@@ -1,11 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CacheStat = void 0;
4
- class CacheStat {
5
- constructor() {
6
- this.total = 0;
7
- this.hit = 0;
8
- }
1
+ export class CacheStat {
2
+ total = 0;
3
+ hit = 0;
9
4
  incHit() {
10
5
  this.hit++;
11
6
  this.total++;
@@ -19,10 +14,8 @@ class CacheStat {
19
14
  total: this.total,
20
15
  hit: this.hit,
21
16
  miss,
22
- hit_percent: this.total ? (this.hit * 100) / this.total : null,
23
- miss_percent: this.total ? (miss * 100) / this.total : null,
17
+ hit_percent: this.total ? (this.hit * 100) / this.total : 0,
18
+ miss_percent: this.total ? (miss * 100) / this.total : 0,
24
19
  };
25
20
  }
26
21
  }
27
- exports.CacheStat = CacheStat;
28
- //# sourceMappingURL=cache-stat.js.map
@@ -1,3 +1,2 @@
1
1
  export * from "./cache-stat";
2
2
  export * from "./memory-cache";
3
- //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ // created from 'create-ts-index'
2
+ export * from "./cache-stat";
3
+ export * from "./memory-cache";
@@ -1,7 +1,7 @@
1
- import { HeartBeat } from "../heart-beat";
1
+ import type { HeartBeat } from "../heart-beat";
2
2
  import { CacheStat } from "./cache-stat";
3
- export declare type CacheCanRemoveCallback = (value?: unknown, key?: string) => boolean;
4
- export declare type CacheOnRemoveCallback = (value?: unknown, key?: string) => void;
3
+ export type CacheCanRemoveCallback = (value?: unknown, key?: string) => boolean;
4
+ export type CacheOnRemoveCallback = (value?: unknown, key?: string) => void;
5
5
  export interface CacheItemOptions {
6
6
  timeToIdle?: number;
7
7
  timeToLive?: number;
@@ -56,4 +56,3 @@ export declare class MemoryCacheBase {
56
56
  vacuum(): void;
57
57
  destroy(): void;
58
58
  }
59
- //# sourceMappingURL=memory-cache.d.ts.map
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MemoryCacheBase = exports.CacheItem = void 0;
4
- const slf_1 = require("@art-ws/slf");
5
- const promise_race_1 = require("../shared/promise-race");
6
- const cache_stat_1 = require("./cache-stat");
1
+ import { getLogger } from "@art-ws/slf";
2
+ import { synchronized } from "../shared/promise-race";
3
+ import { CacheStat } from "./cache-stat";
7
4
  function now() {
8
5
  return new Date().valueOf();
9
6
  }
@@ -11,12 +8,13 @@ const DEFAULT_TTL = 15 * 1000; // Milliseconds
11
8
  const defaultCacheItemOptions = {
12
9
  timeToLive: DEFAULT_TTL,
13
10
  };
14
- class CacheItem {
11
+ export class CacheItem {
12
+ createdAt = now();
13
+ lastHitAt = null;
14
+ cacheValue = null;
15
+ cacheHitCount = 0;
16
+ options;
15
17
  constructor(value, options) {
16
- this.createdAt = now();
17
- this.lastHitAt = null;
18
- this.cacheValue = null;
19
- this.cacheHitCount = 0;
20
18
  this.cacheValue = value;
21
19
  this.options = options || {};
22
20
  if (this.options.timeToIdle) {
@@ -61,16 +59,17 @@ class CacheItem {
61
59
  }
62
60
  }
63
61
  }
64
- exports.CacheItem = CacheItem;
65
62
  function isEmpty(v) {
66
63
  return v === undefined || v === null;
67
64
  }
68
- class MemoryCacheBase {
65
+ export class MemoryCacheBase {
66
+ logger = getLogger("MemoryCacheBase");
67
+ heartBeat;
68
+ cache = new Map();
69
+ expirationStrategy = null;
70
+ subscription = null;
71
+ stat = new CacheStat();
69
72
  constructor(options) {
70
- this.logger = slf_1.getLogger(MemoryCacheBase);
71
- this.cache = new Map();
72
- this.expirationStrategy = null;
73
- this.stat = new cache_stat_1.CacheStat();
74
73
  if (options) {
75
74
  this.expirationStrategy = options.expirationStrategy ?? null;
76
75
  }
@@ -117,6 +116,8 @@ class MemoryCacheBase {
117
116
  checkItemExpired(key, item) {
118
117
  this.logger.trace(() => [`checkItemExpired(${key})`]);
119
118
  const targetItem = item || this.cache.get(key);
119
+ if (!targetItem)
120
+ return;
120
121
  if (this.isItemExpired(targetItem)) {
121
122
  this.removeItem(key, targetItem);
122
123
  }
@@ -125,7 +126,7 @@ class MemoryCacheBase {
125
126
  const targetItem = item ?? this.cache.get(key);
126
127
  let canRemove = true;
127
128
  if (targetItem) {
128
- canRemove = targetItem.canRemove(key) ?? Boolean(force);
129
+ canRemove = targetItem.canRemove(key) || Boolean(force);
129
130
  if (canRemove) {
130
131
  targetItem.onRemove(key);
131
132
  }
@@ -154,7 +155,7 @@ class MemoryCacheBase {
154
155
  async getOrUpdateAsync(key, fn, options) {
155
156
  let value = this.get(key);
156
157
  if (isEmpty(value)) {
157
- value = await promise_race_1.racePromise(key, () => Promise.resolve(fn()), this);
158
+ value = await synchronized(key, () => Promise.resolve(fn()), this);
158
159
  this.set(key, value, options);
159
160
  }
160
161
  return value;
@@ -211,5 +212,3 @@ class MemoryCacheBase {
211
212
  }
212
213
  }
213
214
  }
214
- exports.MemoryCacheBase = MemoryCacheBase;
215
- //# sourceMappingURL=memory-cache.js.map
@@ -1,18 +1,18 @@
1
1
  export interface ZoneSpec {
2
2
  name: string;
3
- properties?: object;
3
+ properties?: Record<string, any>;
4
4
  }
5
5
  export declare class Zone {
6
6
  private readonly zoneData;
7
7
  private readonly zoneName;
8
8
  private readonly zoneParent;
9
+ uid: number;
9
10
  constructor(parent: Zone | null, spec: string | ZoneSpec | null);
10
- get data(): object;
11
+ get data(): Record<string, any>;
11
12
  get name(): string;
12
13
  get parent(): Zone | null;
13
14
  fork(spec: ZoneSpec): Zone;
14
15
  run<T>(callback: () => T, thisArg?: any, args?: any[]): T;
15
- wrap(callback: () => unknown): () => unknown;
16
16
  static isEnabled(): boolean;
17
17
  static enable(): void;
18
18
  static disable(): void;
@@ -20,4 +20,3 @@ export declare class Zone {
20
20
  getZoneWith(key: string): Zone | null;
21
21
  static get current(): Zone;
22
22
  }
23
- //# sourceMappingURL=node-zone.d.ts.map
@@ -1,17 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Zone = void 0;
4
- const tslib_1 = require("tslib");
5
- const async_hooks_1 = tslib_1.__importDefault(require("async_hooks"));
1
+ import asyncHooks from "async_hooks";
6
2
  // inspired by https://raw.githubusercontent.com/JsCommunity/node-zone/master/index.js
7
3
  // https://nodejs.org/api/async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
8
4
  // https://dzone.com/articles/beware-the-performance-cost-of-async-hooks-node-8
9
5
  // https://github.com/bmeurer/async-hooks-performance-impact
10
6
  const zones = Object.create(null);
11
7
  let current;
12
- const asyncHook = async_hooks_1.default.createHook({
8
+ const asyncHook = asyncHooks.createHook({
13
9
  init(uid) {
14
10
  zones[uid] = current;
11
+ if (current) {
12
+ current.uid = uid;
13
+ }
15
14
  },
16
15
  before(uid) {
17
16
  current = zones[uid];
@@ -43,11 +42,12 @@ const toggleAsyncHooks = (value) => {
43
42
  asyncHook.disable();
44
43
  }
45
44
  };
46
- class Zone {
45
+ export class Zone {
46
+ zoneData = Object.create(null);
47
+ zoneName = "root";
48
+ zoneParent = null;
49
+ uid = 0;
47
50
  constructor(parent, spec) {
48
- this.zoneData = Object.create(null);
49
- this.zoneName = "root";
50
- this.zoneParent = null;
51
51
  toggleAsyncHooks(true);
52
52
  if (parent !== null) {
53
53
  const data = (this.zoneData = Object.create(parent.data));
@@ -62,6 +62,7 @@ class Zone {
62
62
  if (properties) {
63
63
  for (const key in properties) {
64
64
  if (properties.hasOwnProperty(key)) {
65
+ ;
65
66
  data[key] = properties[key];
66
67
  }
67
68
  }
@@ -86,21 +87,14 @@ class Zone {
86
87
  const previous = current;
87
88
  try {
88
89
  current = this;
89
- return callback.apply(thisArg ?? null, args ?? []);
90
+ return callback.apply(thisArg ?? null, (args ?? []));
90
91
  }
91
92
  finally {
92
93
  current = previous;
93
94
  }
94
95
  }
95
- wrap(callback) {
96
- const { run: wrapper } = this;
97
- // https://stackoverflow.com/questions/41944650/this-implicitly-has-type-any-because-it-does-not-have-a-type-annotation
98
- return function (...args) {
99
- return wrapper(callback, this, args);
100
- };
101
- }
102
96
  static isEnabled() {
103
- return Boolean(asyncHooksEnabled);
97
+ return !!asyncHooksEnabled;
104
98
  }
105
99
  static enable() {
106
100
  toggleAsyncHooks(true);
@@ -110,7 +104,7 @@ class Zone {
110
104
  }
111
105
  // minimal zone.js compatibility
112
106
  get(key) {
113
- return this.zoneData[key] || null;
107
+ return this.zoneData[key] ?? null;
114
108
  }
115
109
  getZoneWith(key) {
116
110
  if (Object.prototype.hasOwnProperty.call(this.zoneData, key)) {
@@ -123,6 +117,4 @@ class Zone {
123
117
  return current;
124
118
  }
125
119
  }
126
- exports.Zone = Zone;
127
120
  const root = (current = new Zone(null, null));
128
- //# sourceMappingURL=node-zone.js.map
@@ -0,0 +1 @@
1
+ export * from "./object-hash";
@@ -0,0 +1,2 @@
1
+ // created from 'create-ts-index'
2
+ export * from "./object-hash";
@@ -0,0 +1 @@
1
+ export declare const getObjectHash: (o: Record<string, any>) => string;
@@ -0,0 +1,3 @@
1
+ import { hasher } from "node-object-hash";
2
+ const hashSortCoerce = hasher({ sort: true, coerce: true });
3
+ export const getObjectHash = (o) => hashSortCoerce.hash(o);
@@ -0,0 +1,63 @@
1
+ import { Observable } from "rxjs";
2
+ export type ApiClientHttpMethods = "get" | "post" | "put" | "delete" | "patch";
3
+ export interface IApiClientRequestOptions {
4
+ cached: boolean;
5
+ urlEncoded: boolean;
6
+ headers: Record<string, any>;
7
+ }
8
+ export interface IApiClientConfig {
9
+ production: boolean;
10
+ apiBaseUrl: string;
11
+ }
12
+ export interface IApiClientMethodOptions {
13
+ baseUrl: string;
14
+ parentUrl: string;
15
+ headers: Record<string, any>;
16
+ reqId: string;
17
+ }
18
+ export interface IApiClientRequestArgs<O = IApiClientRequestOptions> extends Partial<IApiClientMethodOptions> {
19
+ url: string;
20
+ method: ApiClientHttpMethods;
21
+ query?: Record<string, any>;
22
+ data?: Record<string, any>;
23
+ options?: Partial<O>;
24
+ }
25
+ export interface IApiClientAware {
26
+ apiClient: IApiClient;
27
+ options?: Partial<IApiClientMethodOptions>;
28
+ }
29
+ export interface IApiClientReverseRouteDef {
30
+ url: string;
31
+ method: ApiClientHttpMethods;
32
+ }
33
+ export interface IApiClientReverseRoute {
34
+ getReverseRoute(ref: IApiClientAware, key: string): IApiClientReverseRouteDef;
35
+ }
36
+ export interface IApiClientUrlMaker {
37
+ getUrl(options?: {
38
+ url?: string;
39
+ baseUrl?: string;
40
+ query?: any;
41
+ }): string;
42
+ }
43
+ export interface IApiClient extends Partial<IApiClientUrlMaker> {
44
+ request<T>(args: IApiClientRequestArgs): Observable<T>;
45
+ }
46
+ export interface IApiClientTokenProvider {
47
+ getToken(): string;
48
+ }
49
+ export declare class ApiClientConfig implements IApiClientConfig {
50
+ production: boolean;
51
+ apiBaseUrl: string;
52
+ }
53
+ export declare abstract class ApiClientTokenProvider implements IApiClientTokenProvider {
54
+ abstract getToken(): string;
55
+ }
56
+ export interface TApiClientConstructor<T extends ApiClientBase = ApiClientBase> {
57
+ new (apiClient: IApiClient, options?: Partial<IApiClientMethodOptions>): T;
58
+ }
59
+ export declare class ApiClientBase implements IApiClientAware {
60
+ apiClient: IApiClient;
61
+ options?: Partial<IApiClientMethodOptions> | undefined;
62
+ constructor(apiClient: IApiClient, options?: Partial<IApiClientMethodOptions> | undefined);
63
+ }
@@ -0,0 +1,15 @@
1
+ import { Observable } from "rxjs";
2
+ export class ApiClientConfig {
3
+ production = false;
4
+ apiBaseUrl = "";
5
+ }
6
+ export class ApiClientTokenProvider {
7
+ }
8
+ export class ApiClientBase {
9
+ apiClient;
10
+ options;
11
+ constructor(apiClient, options) {
12
+ this.apiClient = apiClient;
13
+ this.options = options;
14
+ }
15
+ }
@@ -0,0 +1,8 @@
1
+ import "reflect-metadata";
2
+ import type { IApiClientRequestOptions as RequestOptions, TApiClientConstructor } from "./api-client.interface";
3
+ export declare function ApiGet(url?: string, options?: Partial<RequestOptions>): (target: any, property: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
4
+ export declare function ApiPost(url?: string, options?: Partial<RequestOptions>): (target: any, property: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
5
+ export declare function ApiPut(url?: string, options?: Partial<RequestOptions>): (target: any, property: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
6
+ export declare function ApiDelete(url?: string, options?: Partial<RequestOptions>): (target: any, property: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
7
+ export declare function ApiPatch(url?: string, options?: Partial<RequestOptions>): (target: any, property: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
8
+ export declare function ApiGroup(url: string, clz?: TApiClientConstructor): (target: Object, key: string) => void;
@@ -0,0 +1,73 @@
1
+ import "reflect-metadata";
2
+ import { makeUrl } from "./make-url";
3
+ function getDecorator(method, url, options) {
4
+ return function (target, property, descriptor) {
5
+ if (descriptor === undefined) {
6
+ descriptor = Object.getOwnPropertyDescriptor(target, property);
7
+ }
8
+ descriptor.value = function (...args) {
9
+ const { apiClient, options: o2 } = this;
10
+ const [allArgs = {}, a2 = {}] = args || [];
11
+ const o1 = a2;
12
+ const r = makeUrl({ url: (o2?.parentUrl ?? "") + url, query: allArgs });
13
+ const effectiveURL = r.url;
14
+ const effectiveQuery = r.query;
15
+ const reqArgs = {
16
+ method,
17
+ url: effectiveURL,
18
+ query: effectiveQuery,
19
+ options,
20
+ baseUrl: o1?.baseUrl ?? o2?.baseUrl,
21
+ headers: o1?.headers ?? o2?.headers,
22
+ reqId: o1?.reqId ?? o2?.reqId,
23
+ };
24
+ return apiClient.request(reqArgs);
25
+ };
26
+ return descriptor;
27
+ };
28
+ }
29
+ export function ApiGet(url, options) {
30
+ return getDecorator("get", url ?? "", options ?? {});
31
+ }
32
+ export function ApiPost(url, options) {
33
+ return getDecorator("post", url ?? "", options ?? {});
34
+ }
35
+ export function ApiPut(url, options) {
36
+ return getDecorator("put", url ?? "", options ?? {});
37
+ }
38
+ export function ApiDelete(url, options) {
39
+ return getDecorator("delete", url ?? "", options ?? {});
40
+ }
41
+ export function ApiPatch(url, options) {
42
+ return getDecorator("patch", url ?? "", options ?? {});
43
+ }
44
+ // https://gist.github.com/remojansen/16c661a7afd68e22ac6e
45
+ export function ApiGroup(url, clz) {
46
+ return function (target, key) {
47
+ // https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata
48
+ const propType = Reflect.getMetadata("design:type", target, key);
49
+ const Clz = clz ?? propType;
50
+ const propKey = `__${key}__`;
51
+ const getter = function () {
52
+ const self = this;
53
+ let ref = self[propKey];
54
+ if (!ref) {
55
+ ref = new Clz(self.apiClient, {
56
+ ...self.options,
57
+ parentUrl: (self.options?.parentUrl ?? "") + url,
58
+ });
59
+ self[propKey] = ref;
60
+ }
61
+ return ref;
62
+ };
63
+ const setter = () => {
64
+ throw new Error(`Property '${key}' is readonly`);
65
+ };
66
+ Object.defineProperty(target, key, {
67
+ get: getter,
68
+ set: setter,
69
+ enumerable: true,
70
+ configurable: true,
71
+ });
72
+ };
73
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./api-client.interface";
2
+ export * from "./decorators";
@@ -0,0 +1,3 @@
1
+ // created from 'create-ts-index'
2
+ export * from "./api-client.interface";
3
+ export * from "./decorators";
@@ -0,0 +1,6 @@
1
+ export interface TMakeUrlOptions {
2
+ url: string;
3
+ query: any;
4
+ addQueryParams?: boolean;
5
+ }
6
+ export declare function makeUrl(options: TMakeUrlOptions): TMakeUrlOptions;
@@ -0,0 +1,32 @@
1
+ const isDefined = (x) => !(x === undefined || x === null || Number.isNaN(x));
2
+ const replaceAll = (s, search, replacement) => {
3
+ return s.replace(new RegExp(search, "g"), replacement);
4
+ };
5
+ export function makeUrl(options) {
6
+ const { query: args, addQueryParams } = options;
7
+ let { url } = options;
8
+ const query = {};
9
+ Object.keys(args).forEach((k) => {
10
+ const v = args[k];
11
+ if (isDefined(v)) {
12
+ if (url.includes(`:${k}`)) {
13
+ url = replaceAll(url, `:${k}`, v + "");
14
+ }
15
+ else {
16
+ query[k] = v;
17
+ }
18
+ }
19
+ });
20
+ let qp = "";
21
+ if (addQueryParams) {
22
+ const q = [];
23
+ Object.keys(query).forEach((k) => {
24
+ q.push(`${k}=${encodeURIComponent(query[k] + "")}`);
25
+ });
26
+ qp = q.join("&");
27
+ }
28
+ if (qp) {
29
+ url = `${url}?${qp}`;
30
+ }
31
+ return { url, query };
32
+ }
@@ -3,13 +3,16 @@ export declare class AssertDSL {
3
3
  value: any;
4
4
  constructor(args: any);
5
5
  defined(message?: string): this;
6
- truthly(message?: string): this;
6
+ truthy(message?: string): this;
7
7
  range(a: number, b: number): this;
8
8
  intLike(): this;
9
+ intList(): this;
9
10
  int(compatible?: boolean): this;
10
11
  typeOf(typeName: string): this;
12
+ bool(): this;
11
13
  func(): this;
12
14
  object(): this;
15
+ number(): this;
13
16
  str(): this;
14
17
  anyOf<T>(values: T[]): this;
15
18
  string(): this;
@@ -24,4 +27,3 @@ export declare class AssertDSL {
24
27
  eq(value: any): this;
25
28
  }
26
29
  export declare function assert(arg: any): AssertDSL;
27
- //# sourceMappingURL=assert-dsl.d.ts.map
@@ -1,9 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assert = exports.AssertDSL = void 0;
4
- class AssertDSL {
1
+ export class AssertDSL {
2
+ name = "";
3
+ value;
5
4
  constructor(args) {
6
- this.name = "";
7
5
  if (typeof args === "object") {
8
6
  const keys = Object.keys(args);
9
7
  if (keys.length === 1) {
@@ -24,9 +22,9 @@ class AssertDSL {
24
22
  throw new Error(message || `${this.name} not defined`);
25
23
  return this;
26
24
  }
27
- truthly(message) {
25
+ truthy(message) {
28
26
  if (!this.value)
29
- throw new Error(message || `${this.name} not truthly`);
27
+ throw new Error(message || `${this.name} not truthy`);
30
28
  return this;
31
29
  }
32
30
  range(a, b) {
@@ -37,13 +35,26 @@ class AssertDSL {
37
35
  intLike() {
38
36
  return this.int(true);
39
37
  }
38
+ intList() {
39
+ this.string();
40
+ this.value
41
+ .split(",")
42
+ .filter(Boolean)
43
+ .map((x) => Number(x))
44
+ .forEach((x) => {
45
+ if (!Number.isInteger(x)) {
46
+ throw new Error(`${this.name} is not an integers comma delimited list`);
47
+ }
48
+ });
49
+ return this;
50
+ }
40
51
  int(compatible) {
41
- let candidate = this.value;
52
+ let mayBe = this.value;
42
53
  if (compatible) {
43
- candidate = parseInt(candidate) || candidate;
54
+ mayBe = parseInt(mayBe) ?? mayBe;
44
55
  }
45
- if (!Number.isInteger(candidate))
46
- throw new Error(`${this.name} is '${`${candidate}`}', but not an integer number`);
56
+ if (!Number.isInteger(mayBe))
57
+ throw new Error(`${this.name} is '${`${mayBe}`}', but not an integer number`);
47
58
  return this;
48
59
  }
49
60
  typeOf(typeName) {
@@ -52,12 +63,18 @@ class AssertDSL {
52
63
  throw new Error(`${this.name} is not an ${typeName}, but '${t}'`);
53
64
  return this;
54
65
  }
66
+ bool() {
67
+ return this.typeOf("boolean");
68
+ }
55
69
  func() {
56
70
  return this.typeOf("function");
57
71
  }
58
72
  object() {
59
73
  return this.typeOf("object");
60
74
  }
75
+ number() {
76
+ return this.typeOf("number");
77
+ }
61
78
  str() {
62
79
  return this.string();
63
80
  }
@@ -74,7 +91,7 @@ class AssertDSL {
74
91
  ;
75
92
  (props || []).forEach((prop) => {
76
93
  if (!this.value.hasOwnProperty(prop))
77
- throw new Error(`Property ${this.name}.${prop} not defined`);
94
+ throw new Error(`Property ${this.name}.${String(prop)} not defined`);
78
95
  });
79
96
  return this;
80
97
  }
@@ -137,9 +154,6 @@ class AssertDSL {
137
154
  return this;
138
155
  }
139
156
  }
140
- exports.AssertDSL = AssertDSL;
141
- function assert(arg) {
157
+ export function assert(arg) {
142
158
  return new AssertDSL(arg);
143
159
  }
144
- exports.assert = assert;
145
- //# sourceMappingURL=assert-dsl.js.map
@@ -0,0 +1 @@
1
+ export declare function awaitIfAsync(value: unknown): Promise<void>;
@@ -0,0 +1,5 @@
1
+ export async function awaitIfAsync(value) {
2
+ if (typeof value?.then === "function") {
3
+ await value;
4
+ }
5
+ }
@@ -1,4 +1,3 @@
1
1
  export interface IDisposable<T> {
2
2
  dispose: () => T;
3
3
  }
4
- //# sourceMappingURL=disposable.d.ts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,8 @@
1
+ export * from "./api-client";
1
2
  export * from "./assert-dsl";
3
+ export * from "./await-if-async";
2
4
  export * from "./disposable";
3
5
  export * from "./optional";
4
6
  export * from "./promise-race";
5
7
  export * from "./runnable";
6
8
  export * from "./safe-call";
7
- //# sourceMappingURL=index.d.ts.map