@art-ws/common 1.0.3-rc.6 → 2.0.1
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/{cancellation → es/cancellation}/cancellation-token-source.d.ts +1 -2
- package/dist/es/cancellation/cancellation-token-source.js +24 -0
- package/dist/{cancellation → es/cancellation}/cancellation-token.d.ts +1 -2
- package/dist/{cancellation → es/cancellation}/cancellation-token.js +4 -8
- package/dist/{cancellation → es/cancellation}/index.d.ts +0 -1
- package/dist/es/cancellation/index.js +4 -0
- package/dist/{cancellation → es/cancellation}/interfaces.d.ts +1 -2
- package/dist/es/cancellation/interfaces.js +5 -0
- package/dist/{heart-beat → es/heart-beat}/heart-beat.class.d.ts +1 -2
- package/dist/{heart-beat → es/heart-beat}/heart-beat.class.js +10 -15
- package/dist/{heart-beat → es/heart-beat}/heart-beat.types.d.ts +0 -1
- package/dist/es/heart-beat/heart-beat.types.js +1 -0
- package/dist/{heart-beat → es/heart-beat}/index.d.ts +0 -1
- package/dist/es/heart-beat/index.js +3 -0
- package/dist/{hr-time.d.ts → es/hr-time.d.ts} +1 -2
- package/dist/es/hr-time.js +9 -0
- package/dist/{index.d.ts → es/index.d.ts} +1 -1
- package/dist/es/index.js +13 -0
- package/dist/es/interpolate.d.ts +5 -0
- package/dist/es/interpolate.js +150 -0
- package/dist/es/json-file-loader.d.ts +25 -0
- package/dist/es/json-file-loader.js +108 -0
- package/dist/es/json-refs-resolver.d.ts +6 -0
- package/dist/{json-refs-resolver.js → es/json-refs-resolver.js} +13 -18
- package/dist/es/managed-pool/index.d.ts +1 -0
- package/dist/es/managed-pool/index.js +2 -0
- package/dist/{managed-pool → es/managed-pool}/managed-pool.d.ts +0 -1
- package/dist/{managed-pool → es/managed-pool}/managed-pool.js +9 -10
- package/dist/{managed-promise.d.ts → es/managed-promise.d.ts} +1 -1
- package/dist/es/managed-promise.js +24 -0
- package/dist/{mem-cache → es/mem-cache}/cache-stat.d.ts +0 -1
- package/dist/{mem-cache → es/mem-cache}/cache-stat.js +5 -12
- package/dist/{mem-cache → es/mem-cache}/index.d.ts +0 -1
- package/dist/es/mem-cache/index.js +3 -0
- package/dist/{mem-cache → es/mem-cache}/memory-cache.d.ts +3 -4
- package/dist/{mem-cache → es/mem-cache}/memory-cache.js +20 -21
- package/dist/{node-zone.d.ts → es/node-zone.d.ts} +3 -4
- package/dist/{node-zone.js → es/node-zone.js} +14 -22
- package/dist/es/object-hash/index.d.ts +1 -0
- package/dist/es/object-hash/index.js +2 -0
- package/dist/es/object-hash/object-hash.d.ts +1 -0
- package/dist/es/object-hash/object-hash.js +3 -0
- package/dist/es/shared/api-client/api-client.interface.d.ts +63 -0
- package/dist/es/shared/api-client/api-client.interface.js +15 -0
- package/dist/es/shared/api-client/decorators.d.ts +8 -0
- package/dist/es/shared/api-client/decorators.js +73 -0
- package/dist/es/shared/api-client/index.d.ts +2 -0
- package/dist/es/shared/api-client/index.js +3 -0
- package/dist/es/shared/api-client/make-url.d.ts +6 -0
- package/dist/es/shared/api-client/make-url.js +32 -0
- package/dist/{shared → es/shared}/assert-dsl.d.ts +4 -2
- package/dist/{shared → es/shared}/assert-dsl.js +30 -16
- package/dist/es/shared/await-if-async.d.ts +1 -0
- package/dist/es/shared/await-if-async.js +5 -0
- package/dist/{shared → es/shared}/disposable.d.ts +0 -1
- package/dist/es/shared/disposable.js +1 -0
- package/dist/{shared → es/shared}/index.d.ts +2 -1
- package/dist/es/shared/index.js +9 -0
- package/dist/{shared → es/shared}/optional.d.ts +0 -1
- package/dist/{shared → es/shared}/optional.js +6 -10
- package/dist/es/shared/promise-race.d.ts +7 -0
- package/dist/{shared → es/shared}/promise-race.js +10 -18
- package/dist/{shared → es/shared}/runnable.d.ts +1 -2
- package/dist/es/shared/runnable.js +6 -0
- package/dist/{shared → es/shared}/safe-call.d.ts +1 -2
- package/dist/es/shared/safe-call.js +8 -0
- package/package.json +37 -63
- package/dist/cancellation/cancellation-token-source.d.ts.map +0 -1
- package/dist/cancellation/cancellation-token-source.js +0 -31
- package/dist/cancellation/cancellation-token-source.js.map +0 -1
- package/dist/cancellation/cancellation-token.d.ts.map +0 -1
- package/dist/cancellation/cancellation-token.js.map +0 -1
- package/dist/cancellation/index.d.ts.map +0 -1
- package/dist/cancellation/index.js +0 -8
- package/dist/cancellation/index.js.map +0 -1
- package/dist/cancellation/interfaces.d.ts.map +0 -1
- package/dist/cancellation/interfaces.js +0 -10
- package/dist/cancellation/interfaces.js.map +0 -1
- package/dist/heart-beat/heart-beat.class.d.ts.map +0 -1
- package/dist/heart-beat/heart-beat.class.js.map +0 -1
- package/dist/heart-beat/heart-beat.types.d.ts.map +0 -1
- package/dist/heart-beat/heart-beat.types.js +0 -3
- package/dist/heart-beat/heart-beat.types.js.map +0 -1
- package/dist/heart-beat/index.d.ts.map +0 -1
- package/dist/heart-beat/index.js +0 -7
- package/dist/heart-beat/index.js.map +0 -1
- package/dist/hr-time.d.ts.map +0 -1
- package/dist/hr-time.js +0 -15
- package/dist/hr-time.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -16
- package/dist/index.js.map +0 -1
- package/dist/interpolate.d.ts +0 -3
- package/dist/interpolate.d.ts.map +0 -1
- package/dist/interpolate.js +0 -88
- package/dist/interpolate.js.map +0 -1
- package/dist/json-file-loader.d.ts +0 -17
- package/dist/json-file-loader.d.ts.map +0 -1
- package/dist/json-file-loader.js +0 -83
- package/dist/json-file-loader.js.map +0 -1
- package/dist/json-refs-resolver.d.ts +0 -7
- package/dist/json-refs-resolver.d.ts.map +0 -1
- package/dist/json-refs-resolver.js.map +0 -1
- package/dist/managed-pool/index.d.ts +0 -2
- package/dist/managed-pool/index.d.ts.map +0 -1
- package/dist/managed-pool/index.js +0 -6
- package/dist/managed-pool/index.js.map +0 -1
- package/dist/managed-pool/managed-pool.d.ts.map +0 -1
- package/dist/managed-pool/managed-pool.js.map +0 -1
- package/dist/managed-promise.d.ts.map +0 -1
- package/dist/managed-promise.js +0 -26
- package/dist/managed-promise.js.map +0 -1
- package/dist/mem-cache/cache-stat.d.ts.map +0 -1
- package/dist/mem-cache/cache-stat.js.map +0 -1
- package/dist/mem-cache/index.d.ts.map +0 -1
- package/dist/mem-cache/index.js +0 -7
- package/dist/mem-cache/index.js.map +0 -1
- package/dist/mem-cache/memory-cache.d.ts.map +0 -1
- package/dist/mem-cache/memory-cache.js.map +0 -1
- package/dist/node-zone.d.ts.map +0 -1
- package/dist/node-zone.js.map +0 -1
- package/dist/shared/assert-dsl.d.ts.map +0 -1
- package/dist/shared/assert-dsl.js.map +0 -1
- package/dist/shared/disposable.d.ts.map +0 -1
- package/dist/shared/disposable.js +0 -3
- package/dist/shared/disposable.js.map +0 -1
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/index.js +0 -11
- package/dist/shared/index.js.map +0 -1
- package/dist/shared/optional.d.ts.map +0 -1
- package/dist/shared/optional.js.map +0 -1
- package/dist/shared/promise-race.d.ts +0 -8
- package/dist/shared/promise-race.d.ts.map +0 -1
- package/dist/shared/promise-race.js.map +0 -1
- package/dist/shared/runnable.d.ts.map +0 -1
- package/dist/shared/runnable.js +0 -12
- package/dist/shared/runnable.js.map +0 -1
- package/dist/shared/safe-call.d.ts.map +0 -1
- package/dist/shared/safe-call.js +0 -13
- 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
|
+
}
|
@@ -1,11 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
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 :
|
23
|
-
miss_percent: this.total ? (miss * 100) / this.total :
|
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,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
|
4
|
-
export
|
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
|
-
|
2
|
-
|
3
|
-
|
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)
|
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
|
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?:
|
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():
|
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
|
-
|
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 =
|
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
|
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]
|
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 @@
|
|
1
|
+
export declare const getObjectHash: (o: Record<string, any>) => string;
|
@@ -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,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
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
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
|
-
|
25
|
+
truthy(message) {
|
28
26
|
if (!this.value)
|
29
|
-
throw new Error(message || `${this.name} not
|
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
|
52
|
+
let mayBe = this.value;
|
42
53
|
if (compatible) {
|
43
|
-
|
54
|
+
mayBe = parseInt(mayBe) ?? mayBe;
|
44
55
|
}
|
45
|
-
if (!Number.isInteger(
|
46
|
-
throw new Error(`${this.name} is '${`${
|
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
|
-
|
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 @@
|
|
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
|