@rsdk/common 5.12.0-next.2 → 5.12.0-next.4
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/functions/create-async-iterator.d.ts +3 -0
- package/dist/functions/create-async-iterator.js +13 -0
- package/dist/functions/create-async-iterator.js.map +1 -0
- package/dist/functions/index.d.ts +5 -0
- package/dist/functions/index.js +13 -1
- package/dist/functions/index.js.map +1 -1
- package/dist/functions/null-to-undefined.d.ts +5 -0
- package/dist/functions/null-to-undefined.js +15 -0
- package/dist/functions/null-to-undefined.js.map +1 -0
- package/dist/functions/stringify-parse-resources.d.ts +6 -0
- package/dist/functions/stringify-parse-resources.js +54 -0
- package/dist/functions/stringify-parse-resources.js.map +1 -0
- package/dist/functions/undefined-to-null.d.ts +5 -0
- package/dist/functions/undefined-to-null.js +15 -0
- package/dist/functions/undefined-to-null.js.map +1 -0
- package/dist/functions/unwrap.d.ts +2 -0
- package/dist/functions/unwrap.js +15 -0
- package/dist/functions/unwrap.js.map +1 -0
- package/package.json +2 -2
- package/src/functions/create-async-iterator.ts +15 -0
- package/src/functions/index.ts +8 -0
- package/src/functions/null-to-undefined.ts +21 -0
- package/src/functions/stringify-parse-resources.ts +52 -0
- package/src/functions/undefined-to-null.ts +21 -0
- package/src/functions/unwrap.ts +13 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAsyncIterator = createAsyncIterator;
|
|
4
|
+
function createAsyncIterator(values) {
|
|
5
|
+
return {
|
|
6
|
+
[Symbol.asyncIterator]: async function* () {
|
|
7
|
+
for (const value of values) {
|
|
8
|
+
yield value;
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=create-async-iterator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-async-iterator.js","sourceRoot":"","sources":["../../src/functions/create-async-iterator.ts"],"names":[],"mappings":";;AAAA,kDAcC;AAdD,SAAgB,mBAAmB,CAAI,MAAW;IAGhD,OAAO;QACL,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,KAAK,SAAS,CAAC;YAKrC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -4,3 +4,8 @@ export * from './delay';
|
|
|
4
4
|
export { isSerializable } from './serializable.type';
|
|
5
5
|
export * from './omit-undefined';
|
|
6
6
|
export { assert } from './assert';
|
|
7
|
+
export { createAsyncIterator } from './create-async-iterator';
|
|
8
|
+
export { nullToUndefined } from './null-to-undefined';
|
|
9
|
+
export { undefinedToNull } from './undefined-to-null';
|
|
10
|
+
export { unwrap, unwrapAndTransform } from './unwrap';
|
|
11
|
+
export { StringifyResources, ParseResources, } from './stringify-parse-resources';
|
package/dist/functions/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.assert = exports.isSerializable = void 0;
|
|
17
|
+
exports.ParseResources = exports.StringifyResources = exports.unwrapAndTransform = exports.unwrap = exports.undefinedToNull = exports.nullToUndefined = exports.createAsyncIterator = exports.assert = exports.isSerializable = void 0;
|
|
18
18
|
__exportStar(require("./misc"), exports);
|
|
19
19
|
__exportStar(require("./obj-normalization"), exports);
|
|
20
20
|
__exportStar(require("./delay"), exports);
|
|
@@ -23,4 +23,16 @@ Object.defineProperty(exports, "isSerializable", { enumerable: true, get: functi
|
|
|
23
23
|
__exportStar(require("./omit-undefined"), exports);
|
|
24
24
|
var assert_1 = require("./assert");
|
|
25
25
|
Object.defineProperty(exports, "assert", { enumerable: true, get: function () { return assert_1.assert; } });
|
|
26
|
+
var create_async_iterator_1 = require("./create-async-iterator");
|
|
27
|
+
Object.defineProperty(exports, "createAsyncIterator", { enumerable: true, get: function () { return create_async_iterator_1.createAsyncIterator; } });
|
|
28
|
+
var null_to_undefined_1 = require("./null-to-undefined");
|
|
29
|
+
Object.defineProperty(exports, "nullToUndefined", { enumerable: true, get: function () { return null_to_undefined_1.nullToUndefined; } });
|
|
30
|
+
var undefined_to_null_1 = require("./undefined-to-null");
|
|
31
|
+
Object.defineProperty(exports, "undefinedToNull", { enumerable: true, get: function () { return undefined_to_null_1.undefinedToNull; } });
|
|
32
|
+
var unwrap_1 = require("./unwrap");
|
|
33
|
+
Object.defineProperty(exports, "unwrap", { enumerable: true, get: function () { return unwrap_1.unwrap; } });
|
|
34
|
+
Object.defineProperty(exports, "unwrapAndTransform", { enumerable: true, get: function () { return unwrap_1.unwrapAndTransform; } });
|
|
35
|
+
var stringify_parse_resources_1 = require("./stringify-parse-resources");
|
|
36
|
+
Object.defineProperty(exports, "StringifyResources", { enumerable: true, get: function () { return stringify_parse_resources_1.StringifyResources; } });
|
|
37
|
+
Object.defineProperty(exports, "ParseResources", { enumerable: true, get: function () { return stringify_parse_resources_1.ParseResources; } });
|
|
26
38
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,sDAAoC;AACpC,0CAAwB;AACxB,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AACvB,mDAAiC;AACjC,mCAAkC;AAAzB,gGAAA,MAAM,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,sDAAoC;AACpC,0CAAwB;AACxB,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AACvB,mDAAiC;AACjC,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,iEAA8D;AAArD,4HAAA,mBAAmB,OAAA;AAC5B,yDAAsD;AAA7C,oHAAA,eAAe,OAAA;AACxB,yDAAsD;AAA7C,oHAAA,eAAe,OAAA;AACxB,mCAAsD;AAA7C,gGAAA,MAAM,OAAA;AAAE,4GAAA,kBAAkB,OAAA;AACnC,yEAGqC;AAFnC,+HAAA,kBAAkB,OAAA;AAClB,2HAAA,cAAc,OAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
type RecursivelyReplaceNullWithUndefined<T> = T extends null ? undefined : T extends (infer U)[] ? RecursivelyReplaceNullWithUndefined<U>[] : T extends Record<string, unknown> ? {
|
|
2
|
+
[K in keyof T]: RecursivelyReplaceNullWithUndefined<T[K]>;
|
|
3
|
+
} : T;
|
|
4
|
+
export declare function nullToUndefined<T>(obj: T): RecursivelyReplaceNullWithUndefined<T>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nullToUndefined = nullToUndefined;
|
|
4
|
+
function nullToUndefined(obj) {
|
|
5
|
+
if (obj === null || obj === undefined) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
if (typeof obj === 'object' && !Buffer.isBuffer(obj)) {
|
|
9
|
+
for (const key in obj) {
|
|
10
|
+
obj[key] = nullToUndefined(obj[key]);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=null-to-undefined.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"null-to-undefined.js","sourceRoot":"","sources":["../../src/functions/null-to-undefined.ts"],"names":[],"mappings":";;AAQA,0CAYC;AAZD,SAAgB,eAAe,CAC7B,GAAM;IAEN,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,OAAO,SAAgB,CAAC;IAC1B,CAAC;IACD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACrD,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;YACtB,GAAG,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAQ,CAAC;QAC9C,CAAC;IACH,CAAC;IACD,OAAO,GAAU,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ParseResources = exports.StringifyResources = void 0;
|
|
4
|
+
const undefined_to_null_1 = require("./undefined-to-null");
|
|
5
|
+
class StringifyResources {
|
|
6
|
+
static fromJson(resources) {
|
|
7
|
+
return JSON.stringify(resources.map(undefined_to_null_1.undefinedToNull), function (key, value) {
|
|
8
|
+
if (this[key] instanceof Map) {
|
|
9
|
+
return {
|
|
10
|
+
dataType: 'Map',
|
|
11
|
+
value: [...this[key]],
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
else if (this[key] instanceof Set) {
|
|
15
|
+
return {
|
|
16
|
+
dataType: 'Set',
|
|
17
|
+
value: [...this[key]],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
else if (this[key] instanceof Date) {
|
|
21
|
+
return {
|
|
22
|
+
dataType: 'Date',
|
|
23
|
+
value: this[key].getTime(),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.StringifyResources = StringifyResources;
|
|
31
|
+
class ParseResources {
|
|
32
|
+
static toJson(content) {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(content.toString(), (_key, value) => {
|
|
35
|
+
if (typeof value === 'object') {
|
|
36
|
+
switch (value?.dataType) {
|
|
37
|
+
case 'Map':
|
|
38
|
+
return new Map(value.value);
|
|
39
|
+
case 'Set':
|
|
40
|
+
return new Set(value.value);
|
|
41
|
+
case 'Date':
|
|
42
|
+
return new Date(value.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new Error(`Cannot parse content`, { cause: error });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ParseResources = ParseResources;
|
|
54
|
+
//# sourceMappingURL=stringify-parse-resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stringify-parse-resources.js","sourceRoot":"","sources":["../../src/functions/stringify-parse-resources.ts"],"names":[],"mappings":";;;AAAA,2DAAsD;AAEtD,MAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,SAAgB;QAC9B,OAAO,IAAI,CAAC,SAAS,CACnB,SAAS,CAAC,GAAG,CAAC,mCAAe,CAAC,EAC9B,UAAU,GAAG,EAAE,KAAK;YAClB,IAAI,IAAI,CAAC,GAAG,CAAC,YAAY,GAAG,EAAE,CAAC;gBAC7B,OAAO;oBACL,QAAQ,EAAE,KAAK;oBACf,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;iBACtB,CAAC;YACJ,CAAC;iBAAM,IAAI,IAAI,CAAC,GAAG,CAAC,YAAY,GAAG,EAAE,CAAC;gBACpC,OAAO;oBACL,QAAQ,EAAE,KAAK;oBACf,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;iBACtB,CAAC;YACJ,CAAC;iBAAM,IAAI,IAAI,CAAC,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC;gBACrC,OAAO;oBACL,QAAQ,EAAE,MAAM;oBAChB,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE;iBAC3B,CAAC;YACJ,CAAC;YAED,OAAO,KAAK,CAAC;QACf,CAAC,CACF,CAAC;IACJ,CAAC;CACF;AA1BD,gDA0BC;AAED,MAAa,cAAc;IACzB,MAAM,CAAC,MAAM,CAAC,OAAe;QAC3B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;gBACpD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;oBAC9B,QAAQ,KAAK,EAAE,QAAQ,EAAE,CAAC;wBACxB,KAAK,KAAK;4BACR,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;wBAC9B,KAAK,KAAK;4BACR,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;wBAC9B,KAAK,MAAM;4BACT,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBACjC,CAAC;gBACH,CAAC;gBAED,OAAO,KAAK,CAAC;YACf,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,sBAAsB,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC;CACF;AArBD,wCAqBC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
type RecursivelyReplaceUndefinedWithNull<T> = T extends undefined ? null : T extends (infer U)[] ? RecursivelyReplaceUndefinedWithNull<U>[] : T extends Record<string, unknown> ? {
|
|
2
|
+
[K in keyof T]: RecursivelyReplaceUndefinedWithNull<T[K]>;
|
|
3
|
+
} : T;
|
|
4
|
+
export declare function undefinedToNull<T>(obj: T): RecursivelyReplaceUndefinedWithNull<T>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.undefinedToNull = undefinedToNull;
|
|
4
|
+
function undefinedToNull(obj) {
|
|
5
|
+
if (obj === null || obj === undefined) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
if (typeof obj === 'object') {
|
|
9
|
+
for (const key in obj) {
|
|
10
|
+
obj[key] = undefinedToNull(obj[key]);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=undefined-to-null.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"undefined-to-null.js","sourceRoot":"","sources":["../../src/functions/undefined-to-null.ts"],"names":[],"mappings":";;AAQA,0CAYC;AAZD,SAAgB,eAAe,CAC7B,GAAM;IAEN,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,OAAO,IAAW,CAAC;IACrB,CAAC;IACD,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;YACtB,GAAG,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAQ,CAAC;QAC9C,CAAC;IACH,CAAC;IACD,OAAO,GAAU,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unwrapAndTransform = exports.unwrap = void 0;
|
|
4
|
+
const unwrap = (t) => {
|
|
5
|
+
if (t === undefined) {
|
|
6
|
+
throw new Error('Malformed service response');
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
exports.unwrap = unwrap;
|
|
11
|
+
const unwrapAndTransform = (t, map) => {
|
|
12
|
+
return map((0, exports.unwrap)(t));
|
|
13
|
+
};
|
|
14
|
+
exports.unwrapAndTransform = unwrapAndTransform;
|
|
15
|
+
//# sourceMappingURL=unwrap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unwrap.js","sourceRoot":"","sources":["../../src/functions/unwrap.ts"],"names":[],"mappings":";;;AAAO,MAAM,MAAM,GAAG,CAAI,CAAgB,EAAK,EAAE;IAC/C,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AALW,QAAA,MAAM,UAKjB;AAEK,MAAM,kBAAkB,GAAG,CAChC,CAAgB,EAChB,GAAgB,EACb,EAAE;IACL,OAAO,GAAG,CAAC,IAAA,cAAM,EAAC,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC,CAAC;AALW,QAAA,kBAAkB,sBAK7B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/common",
|
|
3
|
-
"version": "5.12.0-next.
|
|
3
|
+
"version": "5.12.0-next.4",
|
|
4
4
|
"description": "Useful common classes, functions and types",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -16,5 +16,5 @@
|
|
|
16
16
|
"peerDependencies": {
|
|
17
17
|
"rxjs": "^7.1.0"
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "7d42d139bc812ac8526d6d9848d24165f8e15a20"
|
|
20
20
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function createAsyncIterator<T>(values: T[]): {
|
|
2
|
+
[Symbol.asyncIterator]: () => AsyncGenerator<Awaited<T>, void, unknown>;
|
|
3
|
+
} {
|
|
4
|
+
return {
|
|
5
|
+
[Symbol.asyncIterator]: async function* (): AsyncGenerator<
|
|
6
|
+
Awaited<T>,
|
|
7
|
+
void,
|
|
8
|
+
unknown
|
|
9
|
+
> {
|
|
10
|
+
for (const value of values) {
|
|
11
|
+
yield value;
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
package/src/functions/index.ts
CHANGED
|
@@ -4,3 +4,11 @@ export * from './delay';
|
|
|
4
4
|
export { isSerializable } from './serializable.type';
|
|
5
5
|
export * from './omit-undefined';
|
|
6
6
|
export { assert } from './assert';
|
|
7
|
+
export { createAsyncIterator } from './create-async-iterator';
|
|
8
|
+
export { nullToUndefined } from './null-to-undefined';
|
|
9
|
+
export { undefinedToNull } from './undefined-to-null';
|
|
10
|
+
export { unwrap, unwrapAndTransform } from './unwrap';
|
|
11
|
+
export {
|
|
12
|
+
StringifyResources,
|
|
13
|
+
ParseResources,
|
|
14
|
+
} from './stringify-parse-resources';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type RecursivelyReplaceNullWithUndefined<T> = T extends null
|
|
2
|
+
? undefined
|
|
3
|
+
: T extends (infer U)[]
|
|
4
|
+
? RecursivelyReplaceNullWithUndefined<U>[]
|
|
5
|
+
: T extends Record<string, unknown>
|
|
6
|
+
? { [K in keyof T]: RecursivelyReplaceNullWithUndefined<T[K]> }
|
|
7
|
+
: T;
|
|
8
|
+
|
|
9
|
+
export function nullToUndefined<T>(
|
|
10
|
+
obj: T,
|
|
11
|
+
): RecursivelyReplaceNullWithUndefined<T> {
|
|
12
|
+
if (obj === null || obj === undefined) {
|
|
13
|
+
return undefined as any;
|
|
14
|
+
}
|
|
15
|
+
if (typeof obj === 'object' && !Buffer.isBuffer(obj)) {
|
|
16
|
+
for (const key in obj) {
|
|
17
|
+
obj[key] = nullToUndefined(obj[key]) as any;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return obj as any;
|
|
21
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { undefinedToNull } from './undefined-to-null';
|
|
2
|
+
|
|
3
|
+
export class StringifyResources {
|
|
4
|
+
static fromJson(resources: any[]): string {
|
|
5
|
+
return JSON.stringify(
|
|
6
|
+
resources.map(undefinedToNull),
|
|
7
|
+
function (key, value) {
|
|
8
|
+
if (this[key] instanceof Map) {
|
|
9
|
+
return {
|
|
10
|
+
dataType: 'Map',
|
|
11
|
+
value: [...this[key]],
|
|
12
|
+
};
|
|
13
|
+
} else if (this[key] instanceof Set) {
|
|
14
|
+
return {
|
|
15
|
+
dataType: 'Set',
|
|
16
|
+
value: [...this[key]],
|
|
17
|
+
};
|
|
18
|
+
} else if (this[key] instanceof Date) {
|
|
19
|
+
return {
|
|
20
|
+
dataType: 'Date',
|
|
21
|
+
value: this[key].getTime(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return value;
|
|
26
|
+
},
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class ParseResources {
|
|
32
|
+
static toJson(content: Buffer): any {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(content.toString(), (_key, value) => {
|
|
35
|
+
if (typeof value === 'object') {
|
|
36
|
+
switch (value?.dataType) {
|
|
37
|
+
case 'Map':
|
|
38
|
+
return new Map(value.value);
|
|
39
|
+
case 'Set':
|
|
40
|
+
return new Set(value.value);
|
|
41
|
+
case 'Date':
|
|
42
|
+
return new Date(value.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return value;
|
|
47
|
+
});
|
|
48
|
+
} catch (error: any) {
|
|
49
|
+
throw new Error(`Cannot parse content`, { cause: error });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type RecursivelyReplaceUndefinedWithNull<T> = T extends undefined
|
|
2
|
+
? null
|
|
3
|
+
: T extends (infer U)[]
|
|
4
|
+
? RecursivelyReplaceUndefinedWithNull<U>[]
|
|
5
|
+
: T extends Record<string, unknown>
|
|
6
|
+
? { [K in keyof T]: RecursivelyReplaceUndefinedWithNull<T[K]> }
|
|
7
|
+
: T;
|
|
8
|
+
|
|
9
|
+
export function undefinedToNull<T>(
|
|
10
|
+
obj: T,
|
|
11
|
+
): RecursivelyReplaceUndefinedWithNull<T> {
|
|
12
|
+
if (obj === null || obj === undefined) {
|
|
13
|
+
return null as any;
|
|
14
|
+
}
|
|
15
|
+
if (typeof obj === 'object') {
|
|
16
|
+
for (const key in obj) {
|
|
17
|
+
obj[key] = undefinedToNull(obj[key]) as any;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return obj as any;
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const unwrap = <T>(t: T | undefined): T => {
|
|
2
|
+
if (t === undefined) {
|
|
3
|
+
throw new Error('Malformed service response');
|
|
4
|
+
}
|
|
5
|
+
return t;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const unwrapAndTransform = <T, R>(
|
|
9
|
+
t: T | undefined,
|
|
10
|
+
map: (t: T) => R,
|
|
11
|
+
): R => {
|
|
12
|
+
return map(unwrap(t));
|
|
13
|
+
};
|