@tsed/json-mapper 6.87.2 → 6.90.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.
- package/lib/{components → cjs/components}/ArrayMapper.d.ts +0 -0
- package/lib/cjs/components/ArrayMapper.js +23 -0
- package/lib/cjs/components/ArrayMapper.js.map +1 -0
- package/lib/{components → cjs/components}/DateMapper.d.ts +0 -0
- package/lib/cjs/components/DateMapper.js +30 -0
- package/lib/cjs/components/DateMapper.js.map +1 -0
- package/lib/{components → cjs/components}/MapMapper.d.ts +0 -0
- package/lib/cjs/components/MapMapper.js +30 -0
- package/lib/cjs/components/MapMapper.js.map +1 -0
- package/lib/{components → cjs/components}/PrimitiveMapper.d.ts +0 -0
- package/lib/cjs/components/PrimitiveMapper.js +45 -0
- package/lib/cjs/components/PrimitiveMapper.js.map +1 -0
- package/lib/{components → cjs/components}/SetMapper.d.ts +0 -0
- package/lib/cjs/components/SetMapper.js +30 -0
- package/lib/cjs/components/SetMapper.js.map +1 -0
- package/lib/{components → cjs/components}/SymbolMapper.d.ts +0 -0
- package/lib/cjs/components/SymbolMapper.js +24 -0
- package/lib/cjs/components/SymbolMapper.js.map +1 -0
- package/lib/{components → cjs/components}/index.d.ts +0 -0
- package/lib/cjs/components/index.js +10 -0
- package/lib/cjs/components/index.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/afterDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/afterDeserialize.js +39 -0
- package/lib/cjs/decorators/afterDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/beforeDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/beforeDeserialize.js +39 -0
- package/lib/cjs/decorators/beforeDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/jsonMapper.d.ts +0 -0
- package/lib/cjs/decorators/jsonMapper.js +18 -0
- package/lib/cjs/decorators/jsonMapper.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/onDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/onDeserialize.js +29 -0
- package/lib/cjs/decorators/onDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/onSerialize.d.ts +0 -0
- package/lib/cjs/decorators/onSerialize.js +29 -0
- package/lib/cjs/decorators/onSerialize.js.map +1 -0
- package/lib/{domain → cjs/domain}/JsonMapperContext.d.ts +0 -0
- package/lib/cjs/domain/JsonMapperContext.js +23 -0
- package/lib/cjs/domain/JsonMapperContext.js.map +1 -0
- package/lib/{domain → cjs/domain}/JsonMapperTypesContainer.d.ts +0 -0
- package/lib/cjs/domain/JsonMapperTypesContainer.js +23 -0
- package/lib/cjs/domain/JsonMapperTypesContainer.js.map +1 -0
- package/lib/{hooks → cjs/hooks}/alterAfterDeserialize.d.ts +0 -0
- package/lib/cjs/hooks/alterAfterDeserialize.js +9 -0
- package/lib/cjs/hooks/alterAfterDeserialize.js.map +1 -0
- package/lib/{hooks → cjs/hooks}/alterBeforeDeserialize.d.ts +0 -0
- package/lib/cjs/hooks/alterBeforeDeserialize.js +9 -0
- package/lib/cjs/hooks/alterBeforeDeserialize.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/{interfaces → cjs/interfaces}/JsonMapperMethods.d.ts +0 -0
- package/lib/cjs/interfaces/JsonMapperMethods.js +3 -0
- package/lib/cjs/interfaces/JsonMapperMethods.js.map +1 -0
- package/lib/{utils → cjs/utils}/deserialize.d.ts +0 -0
- package/lib/cjs/utils/deserialize.js +140 -0
- package/lib/cjs/utils/deserialize.js.map +1 -0
- package/lib/{utils → cjs/utils}/serialize.d.ts +0 -0
- package/lib/cjs/utils/serialize.js +112 -0
- package/lib/cjs/utils/serialize.js.map +1 -0
- package/lib/esm/components/ArrayMapper.js +20 -0
- package/lib/esm/components/ArrayMapper.js.map +1 -0
- package/lib/esm/components/DateMapper.js +27 -0
- package/lib/esm/components/DateMapper.js.map +1 -0
- package/lib/esm/components/MapMapper.js +27 -0
- package/lib/esm/components/MapMapper.js.map +1 -0
- package/lib/esm/components/PrimitiveMapper.js +42 -0
- package/lib/esm/components/PrimitiveMapper.js.map +1 -0
- package/lib/esm/components/SetMapper.js +27 -0
- package/lib/esm/components/SetMapper.js.map +1 -0
- package/lib/esm/components/SymbolMapper.js +21 -0
- package/lib/esm/components/SymbolMapper.js.map +1 -0
- package/lib/esm/components/index.js +7 -0
- package/lib/esm/components/index.js.map +1 -0
- package/lib/esm/decorators/afterDeserialize.js +35 -0
- package/lib/esm/decorators/afterDeserialize.js.map +1 -0
- package/lib/esm/decorators/beforeDeserialize.js +35 -0
- package/lib/esm/decorators/beforeDeserialize.js.map +1 -0
- package/lib/esm/decorators/jsonMapper.js +14 -0
- package/lib/esm/decorators/jsonMapper.js.map +1 -0
- package/lib/esm/decorators/onDeserialize.js +25 -0
- package/lib/esm/decorators/onDeserialize.js.map +1 -0
- package/lib/esm/decorators/onSerialize.js +25 -0
- package/lib/esm/decorators/onSerialize.js.map +1 -0
- package/lib/esm/domain/JsonMapperContext.js +19 -0
- package/lib/esm/domain/JsonMapperContext.js.map +1 -0
- package/lib/esm/domain/JsonMapperTypesContainer.js +18 -0
- package/lib/esm/domain/JsonMapperTypesContainer.js.map +1 -0
- package/lib/esm/hooks/alterAfterDeserialize.js +5 -0
- package/lib/esm/hooks/alterAfterDeserialize.js.map +1 -0
- package/lib/esm/hooks/alterBeforeDeserialize.js +5 -0
- package/lib/esm/hooks/alterBeforeDeserialize.js.map +1 -0
- package/lib/esm/index.js +12 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/JsonMapperMethods.js +2 -0
- package/lib/esm/interfaces/JsonMapperMethods.js.map +1 -0
- package/lib/esm/utils/deserialize.js +135 -0
- package/lib/esm/utils/deserialize.js.map +1 -0
- package/lib/esm/utils/serialize.js +107 -0
- package/lib/esm/utils/serialize.js.map +1 -0
- package/lib/types/components/ArrayMapper.d.ts +10 -0
- package/lib/types/components/DateMapper.d.ts +11 -0
- package/lib/types/components/MapMapper.d.ts +12 -0
- package/lib/types/components/PrimitiveMapper.d.ts +10 -0
- package/lib/types/components/SetMapper.d.ts +10 -0
- package/lib/types/components/SymbolMapper.d.ts +11 -0
- package/lib/types/components/index.d.ts +6 -0
- package/lib/types/decorators/afterDeserialize.d.ts +33 -0
- package/lib/types/decorators/beforeDeserialize.d.ts +33 -0
- package/lib/types/decorators/jsonMapper.d.ts +7 -0
- package/lib/types/decorators/onDeserialize.d.ts +23 -0
- package/lib/types/decorators/onSerialize.d.ts +23 -0
- package/lib/types/domain/JsonMapperContext.d.ts +20 -0
- package/lib/types/domain/JsonMapperTypesContainer.d.ts +10 -0
- package/lib/types/hooks/alterAfterDeserialize.d.ts +4 -0
- package/lib/types/hooks/alterBeforeDeserialize.d.ts +4 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/interfaces/JsonMapperMethods.d.ts +25 -0
- package/lib/types/utils/deserialize.d.ts +41 -0
- package/lib/types/utils/serialize.d.ts +16 -0
- package/package.json +18 -14
- package/tsconfig.compile.esm.json +23 -0
- package/lib/index.js +0 -719
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -708
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deserialize = exports.plainObjectToClass = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
require("../components");
|
|
7
|
+
const JsonMapperContext_1 = require("../domain/JsonMapperContext");
|
|
8
|
+
const JsonMapperTypesContainer_1 = require("../domain/JsonMapperTypesContainer");
|
|
9
|
+
const alterAfterDeserialize_1 = require("../hooks/alterAfterDeserialize");
|
|
10
|
+
const alterBeforeDeserialize_1 = require("../hooks/alterBeforeDeserialize");
|
|
11
|
+
function isDeserializable(obj, options) {
|
|
12
|
+
if ((!!options.collectionType && (0, core_1.isNil)(obj)) || obj === undefined) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return !((0, core_1.isEmpty)(options.type) || (options.type === Object && !options.collectionType));
|
|
16
|
+
}
|
|
17
|
+
function alterValue(schema, value, options) {
|
|
18
|
+
return schema.$hooks.alter("onDeserialize", value, [options]);
|
|
19
|
+
}
|
|
20
|
+
function transformCollection(src, options) {
|
|
21
|
+
var _a;
|
|
22
|
+
const { types, type = Object, collectionType } = options;
|
|
23
|
+
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
24
|
+
type,
|
|
25
|
+
collectionType,
|
|
26
|
+
options,
|
|
27
|
+
next: (data, { collectionType, ...options }) => deserialize(data, options)
|
|
28
|
+
});
|
|
29
|
+
return (_a = types === null || types === void 0 ? void 0 : types.get(options.collectionType)) === null || _a === void 0 ? void 0 : _a.deserialize(src, context);
|
|
30
|
+
}
|
|
31
|
+
function transformType(src, options) {
|
|
32
|
+
var _a;
|
|
33
|
+
const { types, type = Object } = options;
|
|
34
|
+
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
35
|
+
type,
|
|
36
|
+
options,
|
|
37
|
+
next: (data, { type, ...options }) => deserialize(data, options)
|
|
38
|
+
});
|
|
39
|
+
return (_a = types === null || types === void 0 ? void 0 : types.get(type)) === null || _a === void 0 ? void 0 : _a.deserialize(src, context);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Transform given plain object to class.
|
|
43
|
+
* @param src
|
|
44
|
+
* @param options
|
|
45
|
+
*/
|
|
46
|
+
function plainObjectToClass(src, options) {
|
|
47
|
+
if ((0, core_1.isEmpty)(src)) {
|
|
48
|
+
return src;
|
|
49
|
+
}
|
|
50
|
+
const { type, store = schema_1.JsonEntityStore.from(type), ...next } = options;
|
|
51
|
+
const propertiesMap = (0, schema_1.getProperties)(store, { ...options, withIgnoredProps: true });
|
|
52
|
+
let keys = (0, core_1.objectKeys)(src);
|
|
53
|
+
const additionalProperties = propertiesMap.size ? !!store.schema.get("additionalProperties") || options.additionalProperties : true;
|
|
54
|
+
src = (0, alterBeforeDeserialize_1.alterBeforeDeserialize)(src, store.schema, options);
|
|
55
|
+
const out = new type(src);
|
|
56
|
+
propertiesMap.forEach((propStore) => {
|
|
57
|
+
const key = options.useAlias
|
|
58
|
+
? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName
|
|
59
|
+
: propStore.propertyName;
|
|
60
|
+
keys = keys.filter((k) => k !== key);
|
|
61
|
+
next.type = propStore.computedType;
|
|
62
|
+
if ((0, schema_1.alterIgnore)(propStore.itemSchema, options)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
let value = alterValue(propStore.schema, src[key], { ...options, self: src });
|
|
66
|
+
if (propStore.schema.hasGenerics) {
|
|
67
|
+
next.nestedGenerics = propStore.schema.nestedGenerics;
|
|
68
|
+
}
|
|
69
|
+
else if (propStore.schema.isGeneric && options.nestedGenerics) {
|
|
70
|
+
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
71
|
+
const genericLabels = propStore.parent.schema.genericLabels || [];
|
|
72
|
+
next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
|
|
73
|
+
if (next.type instanceof schema_1.JsonSchema) {
|
|
74
|
+
next.type = next.type.getTarget();
|
|
75
|
+
}
|
|
76
|
+
next.nestedGenerics = nestedGenerics;
|
|
77
|
+
}
|
|
78
|
+
value = deserialize(value, {
|
|
79
|
+
...next,
|
|
80
|
+
type: value === src[key] ? next.type : undefined,
|
|
81
|
+
collectionType: propStore.collectionType
|
|
82
|
+
});
|
|
83
|
+
if (value !== undefined) {
|
|
84
|
+
out[propStore.propertyName] = value;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (additionalProperties) {
|
|
88
|
+
keys.forEach((key) => {
|
|
89
|
+
out[key] = src[key];
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return (0, alterAfterDeserialize_1.alterAfterDeserialize)(out, store.schema, options);
|
|
93
|
+
}
|
|
94
|
+
exports.plainObjectToClass = plainObjectToClass;
|
|
95
|
+
function buildOptions(options) {
|
|
96
|
+
if (options.store instanceof schema_1.JsonEntityStore) {
|
|
97
|
+
if (options.store.parameter && options.store.parameter.nestedGenerics.length) {
|
|
98
|
+
options.nestedGenerics = options.store.parameter.nestedGenerics;
|
|
99
|
+
}
|
|
100
|
+
options.type = options.store.computedType;
|
|
101
|
+
options.collectionType = options.store.collectionType;
|
|
102
|
+
delete options.store;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
groups: false,
|
|
106
|
+
useAlias: true,
|
|
107
|
+
...options,
|
|
108
|
+
type: options.type ? options.type : undefined,
|
|
109
|
+
types: options.types ? options.types : (0, JsonMapperTypesContainer_1.getJsonMapperTypes)()
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Transform given source to class base on the given `options.type`.
|
|
114
|
+
*
|
|
115
|
+
* @param src
|
|
116
|
+
* @param options
|
|
117
|
+
*/
|
|
118
|
+
function deserialize(src, options = {}) {
|
|
119
|
+
var _a, _b;
|
|
120
|
+
options = buildOptions(options);
|
|
121
|
+
if (!isDeserializable(src, options)) {
|
|
122
|
+
return src;
|
|
123
|
+
}
|
|
124
|
+
if (!options.collectionType && (0, core_1.isArray)(src)) {
|
|
125
|
+
options.collectionType = Array;
|
|
126
|
+
}
|
|
127
|
+
if (options.collectionType) {
|
|
128
|
+
if (!((_a = options.types) === null || _a === void 0 ? void 0 : _a.has(options.collectionType))) {
|
|
129
|
+
throw new Error(`${(0, core_1.nameOf)(options.collectionType)} is not supported by JsonMapper.`);
|
|
130
|
+
}
|
|
131
|
+
return transformCollection(src, options);
|
|
132
|
+
}
|
|
133
|
+
if ((_b = options.types) === null || _b === void 0 ? void 0 : _b.has(options.type)) {
|
|
134
|
+
return transformType(src, options);
|
|
135
|
+
}
|
|
136
|
+
// class converter
|
|
137
|
+
return plainObjectToClass(src, options);
|
|
138
|
+
}
|
|
139
|
+
exports.deserialize = deserialize;
|
|
140
|
+
//# sourceMappingURL=deserialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":";;;AAAA,qCAA4F;AAC5F,yCAAsG;AACtG,yBAAuB;AACvB,mEAA8D;AAC9D,iFAAsE;AACtE,0EAAqE;AACrE,4EAAuE;AA6BvE,SAAS,gBAAgB,CAAC,GAAQ,EAAE,OAAgC;IAClE,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,IAAI,IAAA,YAAK,EAAC,GAAG,CAAC,CAAC,IAAI,GAAG,KAAK,SAAS,EAAE;QACjE,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CAAC,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAChE,CAAC;AAED,SAAS,mBAAmB,CAAU,GAAQ,EAAE,OAA0C;;IACxF,MAAM,EAAC,KAAK,EAAE,IAAI,GAAG,MAAM,EAAE,cAAc,EAAC,GAAG,OAAO,CAAC;IAEvD,MAAM,OAAO,GAAG,IAAI,qCAAiB,CAAC;QACpC,IAAI;QACJ,cAAc;QACd,OAAO;QACP,IAAI,EAAE,CAAC,IAAI,EAAE,EAAC,cAAc,EAAE,GAAG,OAAO,EAAC,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC;KACzE,CAAC,CAAC;IAEH,OAAO,MAAA,KAAK,aAAL,KAAK,uBAAL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,OAAO,CAAC,CAAC;AAC1C,CAAC;AAzBD,kCAyBC","sourcesContent":["import {isArray, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getProperties, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {alterAfterDeserialize} from \"../hooks/alterAfterDeserialize\";\nimport {alterBeforeDeserialize} from \"../hooks/alterBeforeDeserialize\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: WeakMap<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n /**\n * Accept additionalProperties or ignore it\n */\n additionalProperties?: boolean;\n /**\n * Use the store which have all metadata to deserialize correctly the model. This\n * property is useful when you deal with metadata parameters.\n */\n store?: JsonEntityStore;\n /**\n *\n */\n groups?: string[] | false;\n\n [key: string]: any;\n}\n\nfunction isDeserializable(obj: any, options: JsonDeserializerOptions) {\n if ((!!options.collectionType && isNil(obj)) || obj === undefined) {\n return false;\n }\n\n return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onDeserialize\", value, [options]);\n}\n\nfunction transformCollection<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object, collectionType} = options;\n\n const context = new JsonMapperContext({\n type,\n collectionType,\n options,\n next: (data, {collectionType, ...options}) => deserialize(data, options)\n });\n\n return types?.get(options.collectionType)?.deserialize<T>(src, context);\n}\n\nfunction transformType<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object} = options;\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data, {type, ...options}) => deserialize(data, options)\n });\n\n return types?.get(type)?.deserialize<T>(src, context);\n}\n\n/**\n * Transform given plain object to class.\n * @param src\n * @param options\n */\nexport function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T {\n if (isEmpty(src)) {\n return src;\n }\n\n const {type, store = JsonEntityStore.from(type), ...next} = options;\n\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = objectKeys(src);\n const additionalProperties = propertiesMap.size ? !!store.schema.get(\"additionalProperties\") || options.additionalProperties : true;\n src = alterBeforeDeserialize(src, store.schema, options);\n const out: any = new type(src);\n\n propertiesMap.forEach((propStore) => {\n const key = options.useAlias\n ? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName\n : propStore.propertyName;\n\n keys = keys.filter((k) => k !== key);\n\n next.type = propStore.computedType;\n\n if (alterIgnore(propStore.itemSchema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n if (propStore.schema.hasGenerics) {\n next.nestedGenerics = propStore.schema.nestedGenerics;\n } else if (propStore.schema.isGeneric && options.nestedGenerics) {\n const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;\n const genericLabels = propStore.parent.schema.genericLabels || [];\n\n next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (next.type instanceof JsonSchema) {\n next.type = next.type.getTarget();\n }\n\n next.nestedGenerics = nestedGenerics;\n }\n\n value = deserialize(value, {\n ...next,\n type: value === src[key] ? next.type : undefined,\n collectionType: propStore.collectionType\n });\n\n if (value !== undefined) {\n out[propStore.propertyName] = value;\n }\n });\n\n if (additionalProperties) {\n keys.forEach((key) => {\n out[key] = src[key];\n });\n }\n\n return alterAfterDeserialize(out, store.schema, options);\n}\n\nfunction buildOptions(options: JsonDeserializerOptions<any, any>): any {\n if (options.store instanceof JsonEntityStore) {\n if (options.store.parameter && options.store.parameter.nestedGenerics.length) {\n options.nestedGenerics = options.store.parameter.nestedGenerics;\n }\n\n options.type = options.store.computedType;\n options.collectionType = options.store.collectionType;\n\n delete options.store;\n }\n\n return {\n groups: false,\n useAlias: true,\n ...options,\n type: options.type ? options.type : undefined,\n types: options.types ? options.types : getJsonMapperTypes()\n };\n}\n\n/**\n * Transform given source to class base on the given `options.type`.\n *\n * @param src\n * @param options\n */\nexport function deserialize<T = any>(src: any, options: JsonDeserializerOptions = {}): T {\n options = buildOptions(options);\n\n if (!isDeserializable(src, options)) {\n return src;\n }\n\n if (!options.collectionType && isArray(src)) {\n options.collectionType = Array;\n }\n\n if (options.collectionType) {\n if (!options.types?.has(options.collectionType)) {\n throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);\n }\n\n return transformCollection(src, options);\n }\n\n if (options.types?.has(options.type)) {\n return transformType(src, options);\n }\n\n // class converter\n return plainObjectToClass(src, options);\n}\n"]}
|
|
File without changes
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serialize = exports.classToPlainObject = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
require("../components");
|
|
7
|
+
const JsonMapperContext_1 = require("../domain/JsonMapperContext");
|
|
8
|
+
const JsonMapperTypesContainer_1 = require("../domain/JsonMapperTypesContainer");
|
|
9
|
+
function alterValue(schema, value, options) {
|
|
10
|
+
return schema.$hooks.alter("onSerialize", value, [options]);
|
|
11
|
+
}
|
|
12
|
+
function getSchemaProperties(storedJson, obj) {
|
|
13
|
+
const stores = Array.from((0, schema_1.getPropertiesStores)(storedJson).entries());
|
|
14
|
+
if (!stores.length) {
|
|
15
|
+
// fallback to auto discovering field from obj
|
|
16
|
+
(0, core_1.objectKeys)(obj).forEach((key) => {
|
|
17
|
+
const propStore = schema_1.JsonEntityStore.from(storedJson.target, key);
|
|
18
|
+
stores.push([key, propStore]);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return stores;
|
|
22
|
+
}
|
|
23
|
+
function getObjectProperties(obj) {
|
|
24
|
+
return Object.entries(obj).filter(([, value]) => !(0, core_1.isFunction)(value));
|
|
25
|
+
}
|
|
26
|
+
function getType(propStore, value) {
|
|
27
|
+
if ((0, core_1.isNil)(value) || propStore.type === Object || (0, core_1.isCollection)(propStore.type)) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return propStore.type;
|
|
31
|
+
}
|
|
32
|
+
function classToPlainObject(obj, options) {
|
|
33
|
+
const { useAlias = true, type, ...props } = options;
|
|
34
|
+
const entity = schema_1.JsonEntityStore.from(type || obj);
|
|
35
|
+
return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {
|
|
36
|
+
const schema = propStore.schema;
|
|
37
|
+
if ((0, schema_1.alterIgnore)(schema, { useAlias, ...props, self: obj })) {
|
|
38
|
+
return newObj;
|
|
39
|
+
}
|
|
40
|
+
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
41
|
+
value = serialize(value, {
|
|
42
|
+
useAlias,
|
|
43
|
+
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
44
|
+
collectionType: propStore.collectionType,
|
|
45
|
+
...props
|
|
46
|
+
});
|
|
47
|
+
if (value === undefined) {
|
|
48
|
+
return newObj;
|
|
49
|
+
}
|
|
50
|
+
key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;
|
|
51
|
+
return {
|
|
52
|
+
...newObj,
|
|
53
|
+
[key]: value
|
|
54
|
+
};
|
|
55
|
+
}, {});
|
|
56
|
+
}
|
|
57
|
+
exports.classToPlainObject = classToPlainObject;
|
|
58
|
+
function toObject(obj, options) {
|
|
59
|
+
return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
|
|
60
|
+
...newObj,
|
|
61
|
+
[key]: serialize(value, options)
|
|
62
|
+
}), {});
|
|
63
|
+
}
|
|
64
|
+
function getBestType(type, obj) {
|
|
65
|
+
const dataType = (0, core_1.classOf)(obj);
|
|
66
|
+
if (dataType && !(0, core_1.isClassObject)(dataType)) {
|
|
67
|
+
return dataType;
|
|
68
|
+
}
|
|
69
|
+
return type || Object;
|
|
70
|
+
}
|
|
71
|
+
function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
72
|
+
var _a;
|
|
73
|
+
if ((0, core_1.isEmpty)(obj)) {
|
|
74
|
+
return obj;
|
|
75
|
+
}
|
|
76
|
+
const types = options.types ? options.types : (0, JsonMapperTypesContainer_1.getJsonMapperTypes)();
|
|
77
|
+
options.groups = groups;
|
|
78
|
+
// FIX custom serialization function from @tsed/mongoose and bson
|
|
79
|
+
if ((typeof obj.toJSON === "function" && obj.$isMongooseModelPrototype) || obj._bsontype) {
|
|
80
|
+
return obj.toJSON(options);
|
|
81
|
+
}
|
|
82
|
+
if (typeof obj.toJSON === "function") {
|
|
83
|
+
return serialize(obj.toJSON(), { ...options, type: (0, core_1.classOf)(obj) });
|
|
84
|
+
}
|
|
85
|
+
if (!(0, core_1.isCollection)(obj)) {
|
|
86
|
+
options.type = type = getBestType(type, obj);
|
|
87
|
+
}
|
|
88
|
+
else if (!options.collectionType) {
|
|
89
|
+
type = (0, core_1.classOf)(obj);
|
|
90
|
+
options.collectionType = type;
|
|
91
|
+
}
|
|
92
|
+
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
93
|
+
type,
|
|
94
|
+
options,
|
|
95
|
+
next: (data) => serialize(data, {
|
|
96
|
+
...options,
|
|
97
|
+
collectionType: undefined,
|
|
98
|
+
type: options.type
|
|
99
|
+
})
|
|
100
|
+
});
|
|
101
|
+
if (types.has(type)) {
|
|
102
|
+
const jsonMapper = types.get(type);
|
|
103
|
+
return jsonMapper.serialize(obj, context);
|
|
104
|
+
}
|
|
105
|
+
if ((0, core_1.isArray)(obj)) {
|
|
106
|
+
// Serialize Array class like
|
|
107
|
+
return (_a = types.get(Array)) === null || _a === void 0 ? void 0 : _a.serialize(obj, context);
|
|
108
|
+
}
|
|
109
|
+
return !(0, core_1.isClassObject)(type) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
110
|
+
}
|
|
111
|
+
exports.serialize = serialize;
|
|
112
|
+
//# sourceMappingURL=serialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":";;;AAAA,qCAAsI;AACtI,yCAA4G;AAC5G,yBAAuB;AACvB,mEAA8D;AAC9D,iFAAsE;AAgBtE,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED,SAAS,mBAAmB,CAAC,UAA2B,EAAE,GAAQ;IAChE,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,IAAA,4BAAmB,EAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;IAErE,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;QAClB,8CAA8C;QAC9C,IAAA,iBAAU,EAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC9B,MAAM,SAAS,GAAG,wBAAe,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YAC/D,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC;QAChC,CAAC,CAAC,CAAC;KACJ;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,mBAAmB,CAAC,GAAQ;IACnC,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,IAAA,iBAAU,EAAC,KAAK,CAAC,CAAC,CAAC;AACvE,C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,KAAK,CAAC,0CAAE,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAClD;IAED,OAAO,CAAC,IAAA,oBAAa,EAAC,IAAI,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAC1F,CAAC;AA/CD,8BA+CC","sourcesContent":["import {classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, MetadataTypes, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n value = serialize(value, {\n useAlias,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if ((typeof obj.toJSON === \"function\" && obj.$isMongooseModelPrototype) || obj._bsontype) {\n return obj.toJSON(options);\n }\n\n if (typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = type = getBestType(type, obj);\n } else if (!options.collectionType) {\n type = classOf(obj);\n options.collectionType = type;\n }\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n if (types.has(type)) {\n const jsonMapper = types.get(type)!;\n\n return jsonMapper.serialize(obj, context);\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(type) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { JsonMapper } from "../decorators/jsonMapper";
|
|
3
|
+
/**
|
|
4
|
+
* Mapper for `Array` type.
|
|
5
|
+
* @jsonmapper
|
|
6
|
+
* @component
|
|
7
|
+
*/
|
|
8
|
+
let ArrayMapper = class ArrayMapper {
|
|
9
|
+
deserialize(data, options) {
|
|
10
|
+
return [].concat(data).map((item) => options.next(item));
|
|
11
|
+
}
|
|
12
|
+
serialize(data, options) {
|
|
13
|
+
return [].concat(data).map((item) => options.next(item));
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
ArrayMapper = __decorate([
|
|
17
|
+
JsonMapper(Array)
|
|
18
|
+
], ArrayMapper);
|
|
19
|
+
export { ArrayMapper };
|
|
20
|
+
//# sourceMappingURL=ArrayMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ArrayMapper.js","sourceRoot":"","sources":["../../../src/components/ArrayMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AAEH,IAAa,WAAW,GAAxB,MAAa,WAAW;IACtB,WAAW,CAAU,IAAS,EAAE,OAAsB;QACpD,OAAO,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,SAAS,CAAC,IAAW,EAAE,OAAsB;QAC3C,OAAO,EAAE,CAAC,MAAM,CAAC,IAAW,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAClE,CAAC;CACF,CAAA;AARY,WAAW;IADvB,UAAU,CAAC,KAAK,CAAC;GACL,WAAW,CAQvB;SARY,WAAW","sourcesContent":["import {JsonMapper} from \"../decorators/jsonMapper\";\nimport {JsonMapperCtx, JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\n/**\n * Mapper for `Array` type.\n * @jsonmapper\n * @component\n */\n@JsonMapper(Array)\nexport class ArrayMapper implements JsonMapperMethods {\n deserialize<T = any>(data: any, options: JsonMapperCtx): T[] {\n return [].concat(data).map((item) => options.next(item));\n }\n\n serialize(data: any[], options: JsonMapperCtx): any {\n return [].concat(data as any).map((item) => options.next(item));\n }\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { isBoolean } from "@tsed/core";
|
|
3
|
+
import { JsonMapper } from "../decorators/jsonMapper";
|
|
4
|
+
/**
|
|
5
|
+
* Mapper for `Date` type.
|
|
6
|
+
* @jsonmapper
|
|
7
|
+
* @component
|
|
8
|
+
*/
|
|
9
|
+
let DateMapper = class DateMapper {
|
|
10
|
+
deserialize(data) {
|
|
11
|
+
// don't convert unexpected data. In normal case, Ajv reject unexpected data.
|
|
12
|
+
// But by default, we have to skip data deserialization and let user to apply
|
|
13
|
+
// the right mapping
|
|
14
|
+
if (isBoolean(data) || data === null || data === undefined) {
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
return new Date(data);
|
|
18
|
+
}
|
|
19
|
+
serialize(object) {
|
|
20
|
+
return new Date(object).toISOString();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
DateMapper = __decorate([
|
|
24
|
+
JsonMapper(Date)
|
|
25
|
+
], DateMapper);
|
|
26
|
+
export { DateMapper };
|
|
27
|
+
//# sourceMappingURL=DateMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DateMapper.js","sourceRoot":"","sources":["../../../src/components/DateMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AACrC,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AAEH,IAAa,UAAU,GAAvB,MAAa,UAAU;IAGrB,WAAW,CAAC,IAAS;QACnB,6EAA6E;QAC7E,6EAA6E;QAC7E,oBAAoB;QACpB,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,EAAE;YAC1D,OAAO,IAAI,CAAC;SACb;QAED,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,SAAS,CAAC,MAAY;QACpB,OAAO,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;IACxC,CAAC;CACF,CAAA;AAjBY,UAAU;IADtB,UAAU,CAAC,IAAI,CAAC;GACJ,UAAU,CAiBtB;SAjBY,UAAU","sourcesContent":["import {isBoolean} from \"@tsed/core\";\nimport {JsonMapper} from \"../decorators/jsonMapper\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\n/**\n * Mapper for `Date` type.\n * @jsonmapper\n * @component\n */\n@JsonMapper(Date)\nexport class DateMapper implements JsonMapperMethods {\n deserialize(data: string | number): Date;\n deserialize(data: boolean | null | undefined): boolean | null | undefined;\n deserialize(data: any): any {\n // don't convert unexpected data. In normal case, Ajv reject unexpected data.\n // But by default, we have to skip data deserialization and let user to apply\n // the right mapping\n if (isBoolean(data) || data === null || data === undefined) {\n return data;\n }\n\n return new Date(data);\n }\n\n serialize(object: Date): any {\n return new Date(object).toISOString();\n }\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { objectKeys } from "@tsed/core";
|
|
3
|
+
import { JsonMapper } from "../decorators/jsonMapper";
|
|
4
|
+
/**
|
|
5
|
+
* Mapper for `Map` type.
|
|
6
|
+
* @jsonmapper
|
|
7
|
+
* @component
|
|
8
|
+
*/
|
|
9
|
+
let MapMapper = class MapMapper {
|
|
10
|
+
deserialize(data, ctx) {
|
|
11
|
+
const obj = new Map();
|
|
12
|
+
objectKeys(data).forEach((key) => {
|
|
13
|
+
obj.set(key, ctx.next(data[key]));
|
|
14
|
+
});
|
|
15
|
+
return obj;
|
|
16
|
+
}
|
|
17
|
+
serialize(data, ctx) {
|
|
18
|
+
const obj = {};
|
|
19
|
+
data.forEach((value, key) => (obj[key] = ctx.next(value)));
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
MapMapper = __decorate([
|
|
24
|
+
JsonMapper(Map)
|
|
25
|
+
], MapMapper);
|
|
26
|
+
export { MapMapper };
|
|
27
|
+
//# sourceMappingURL=MapMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MapMapper.js","sourceRoot":"","sources":["../../../src/components/MapMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AACtC,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AAEH,IAAa,SAAS,GAAtB,MAAa,SAAS;IACpB,WAAW,CAA8B,IAA0B,EAAE,GAAwB;QAC3F,MAAM,GAAG,GAAG,IAAI,GAAG,EAAa,CAAC;QAEjC,UAAU,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC/B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAM,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACb,CAAC;IAED,SAAS,CAAI,IAAoB,EAAE,GAAkB;QACnD,MAAM,GAAG,GAAQ,EAAE,CAAC;QAEpB,IAAI,CAAC,OAAO,CAAC,CAAC,KAAQ,EAAE,GAAW,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAEtE,OAAO,GAAG,CAAC;IACb,CAAC;CACF,CAAA;AAlBY,SAAS;IADrB,UAAU,CAAC,GAAG,CAAC;GACH,SAAS,CAkBrB;SAlBY,SAAS","sourcesContent":["import {objectKeys} from \"@tsed/core\";\nimport {JsonMapper} from \"../decorators/jsonMapper\";\nimport {JsonMapperCtx, JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\n/**\n * Mapper for `Map` type.\n * @jsonmapper\n * @component\n */\n@JsonMapper(Map)\nexport class MapMapper implements JsonMapperMethods {\n deserialize<T = any, C = Map<string, T>>(data: {[key: string]: any}, ctx: JsonMapperCtx<T, C>): Map<string, T> {\n const obj = new Map<string, T>();\n\n objectKeys(data).forEach((key) => {\n obj.set(key, ctx.next(data[key]) as T);\n });\n\n return obj;\n }\n\n serialize<T>(data: Map<string, T>, ctx: JsonMapperCtx): any {\n const obj: any = {};\n\n data.forEach((value: T, key: string) => (obj[key] = ctx.next(value)));\n\n return obj;\n }\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { BadRequest } from "@tsed/exceptions";
|
|
3
|
+
import { JsonMapper } from "../decorators/jsonMapper";
|
|
4
|
+
/**
|
|
5
|
+
* Mapper for the `String`, `Number` and `Boolean` types.
|
|
6
|
+
* @jsonmapper
|
|
7
|
+
* @component
|
|
8
|
+
*/
|
|
9
|
+
let PrimitiveMapper = class PrimitiveMapper {
|
|
10
|
+
deserialize(data, ctx) {
|
|
11
|
+
switch (ctx.type) {
|
|
12
|
+
case String:
|
|
13
|
+
return data === null ? null : "" + data;
|
|
14
|
+
case Number:
|
|
15
|
+
if ([null, "null"].includes(data))
|
|
16
|
+
return null;
|
|
17
|
+
const n = +data;
|
|
18
|
+
if (isNaN(n)) {
|
|
19
|
+
throw new BadRequest("Cast error. Expression value is not a number.");
|
|
20
|
+
}
|
|
21
|
+
return n;
|
|
22
|
+
case Boolean:
|
|
23
|
+
if (["true", "1", true].includes(data))
|
|
24
|
+
return true;
|
|
25
|
+
if (["false", "0", false].includes(data))
|
|
26
|
+
return false;
|
|
27
|
+
if ([null, "null"].includes(data))
|
|
28
|
+
return null;
|
|
29
|
+
if (data === undefined)
|
|
30
|
+
return undefined;
|
|
31
|
+
return !!data;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
serialize(object) {
|
|
35
|
+
return object;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
PrimitiveMapper = __decorate([
|
|
39
|
+
JsonMapper(String, Number, Boolean)
|
|
40
|
+
], PrimitiveMapper);
|
|
41
|
+
export { PrimitiveMapper };
|
|
42
|
+
//# sourceMappingURL=PrimitiveMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrimitiveMapper.js","sourceRoot":"","sources":["../../../src/components/PrimitiveMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AAEH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAC1B,WAAW,CAAI,IAAS,EAAE,GAAkB;QAC1C,QAAQ,GAAG,CAAC,IAAI,EAAE;YAChB,KAAK,MAAM;gBACT,OAAO,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC;YAE1C,KAAK,MAAM;gBACT,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;oBAAE,OAAO,IAAI,CAAC;gBAE/C,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;gBAEhB,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE;oBACZ,MAAM,IAAI,UAAU,CAAC,+CAA+C,CAAC,CAAC;iBACvE;gBAED,OAAO,CAAC,CAAC;YAEX,KAAK,OAAO;gBACV,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;oBAAE,OAAO,IAAI,CAAC;gBACpD,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;oBAAE,OAAO,KAAK,CAAC;gBACvD,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;oBAAE,OAAO,IAAI,CAAC;gBAC/C,IAAI,IAAI,KAAK,SAAS;oBAAE,OAAO,SAAS,CAAC;gBAEzC,OAAO,CAAC,CAAC,IAAI,CAAC;SACjB;IACH,CAAC;IAED,SAAS,CAAC,MAAiC;QACzC,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAA;AA9BY,eAAe;IAD3B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC;GACvB,eAAe,CA8B3B;SA9BY,eAAe","sourcesContent":["import {BadRequest} from \"@tsed/exceptions\";\nimport {JsonMapper} from \"../decorators/jsonMapper\";\nimport {JsonMapperCtx, JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\n/**\n * Mapper for the `String`, `Number` and `Boolean` types.\n * @jsonmapper\n * @component\n */\n@JsonMapper(String, Number, Boolean)\nexport class PrimitiveMapper implements JsonMapperMethods {\n deserialize<T>(data: any, ctx: JsonMapperCtx): string | number | boolean | void | null {\n switch (ctx.type) {\n case String:\n return data === null ? null : \"\" + data;\n\n case Number:\n if ([null, \"null\"].includes(data)) return null;\n\n const n = +data;\n\n if (isNaN(n)) {\n throw new BadRequest(\"Cast error. Expression value is not a number.\");\n }\n\n return n;\n\n case Boolean:\n if ([\"true\", \"1\", true].includes(data)) return true;\n if ([\"false\", \"0\", false].includes(data)) return false;\n if ([null, \"null\"].includes(data)) return null;\n if (data === undefined) return undefined;\n\n return !!data;\n }\n }\n\n serialize(object: string | number | boolean): string | number | boolean {\n return object;\n }\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { objectKeys } from "@tsed/core";
|
|
3
|
+
import { JsonMapper } from "../decorators/jsonMapper";
|
|
4
|
+
/**
|
|
5
|
+
* Mapper for the `Set` Type.
|
|
6
|
+
* @jsonmapper
|
|
7
|
+
* @component
|
|
8
|
+
*/
|
|
9
|
+
let SetMapper = class SetMapper {
|
|
10
|
+
deserialize(data, ctx) {
|
|
11
|
+
const obj = new Set();
|
|
12
|
+
objectKeys(data).forEach((key) => {
|
|
13
|
+
obj.add(ctx.next(data[key]));
|
|
14
|
+
});
|
|
15
|
+
return obj;
|
|
16
|
+
}
|
|
17
|
+
serialize(data, ctx) {
|
|
18
|
+
const array = [];
|
|
19
|
+
data.forEach((value) => array.push(ctx.next(value)));
|
|
20
|
+
return array;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
SetMapper = __decorate([
|
|
24
|
+
JsonMapper(Set)
|
|
25
|
+
], SetMapper);
|
|
26
|
+
export { SetMapper };
|
|
27
|
+
//# sourceMappingURL=SetMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetMapper.js","sourceRoot":"","sources":["../../../src/components/SetMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AACtC,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AAEH,IAAa,SAAS,GAAtB,MAAa,SAAS;IACpB,WAAW,CAAI,IAAS,EAAE,GAAkB;QAC1C,MAAM,GAAG,GAAG,IAAI,GAAG,EAAK,CAAC;QAEzB,UAAU,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC/B,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACb,CAAC;IAED,SAAS,CAAI,IAAY,EAAE,GAAkB;QAC3C,MAAM,KAAK,GAAU,EAAE,CAAC;QAExB,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAErD,OAAO,KAAK,CAAC;IACf,CAAC;CACF,CAAA;AAlBY,SAAS;IADrB,UAAU,CAAC,GAAG,CAAC;GACH,SAAS,CAkBrB;SAlBY,SAAS","sourcesContent":["import {objectKeys} from \"@tsed/core\";\nimport {JsonMapper} from \"../decorators/jsonMapper\";\nimport {JsonMapperCtx, JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\n/**\n * Mapper for the `Set` Type.\n * @jsonmapper\n * @component\n */\n@JsonMapper(Set)\nexport class SetMapper implements JsonMapperMethods {\n deserialize<T>(data: any, ctx: JsonMapperCtx): Set<T> {\n const obj = new Set<T>();\n\n objectKeys(data).forEach((key) => {\n obj.add(ctx.next(data[key]));\n });\n\n return obj;\n }\n\n serialize<T>(data: Set<T>, ctx: JsonMapperCtx): any[] {\n const array: any[] = [];\n\n data.forEach((value) => array.push(ctx.next(value)));\n\n return array;\n }\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { JsonMapper } from "../decorators/jsonMapper";
|
|
3
|
+
/**
|
|
4
|
+
* Mapper for the `Symbol` type.
|
|
5
|
+
*
|
|
6
|
+
* @jsonmapper
|
|
7
|
+
* @component
|
|
8
|
+
*/
|
|
9
|
+
let SymbolMapper = class SymbolMapper {
|
|
10
|
+
deserialize(data) {
|
|
11
|
+
return Symbol.for(data);
|
|
12
|
+
}
|
|
13
|
+
serialize(object) {
|
|
14
|
+
return object.toString().replace("Symbol(", "").replace(")", "");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
SymbolMapper = __decorate([
|
|
18
|
+
JsonMapper(Symbol)
|
|
19
|
+
], SymbolMapper);
|
|
20
|
+
export { SymbolMapper };
|
|
21
|
+
//# sourceMappingURL=SymbolMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SymbolMapper.js","sourceRoot":"","sources":["../../../src/components/SymbolMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;;GAKG;AAEH,IAAa,YAAY,GAAzB,MAAa,YAAY;IACvB,WAAW,CAAC,IAAY;QACtB,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,SAAS,CAAC,MAAc;QACtB,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACF,CAAA;AARY,YAAY;IADxB,UAAU,CAAC,MAAM,CAAC;GACN,YAAY,CAQxB;SARY,YAAY","sourcesContent":["import {JsonMapper} from \"../decorators/jsonMapper\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\n/**\n * Mapper for the `Symbol` type.\n *\n * @jsonmapper\n * @component\n */\n@JsonMapper(Symbol)\nexport class SymbolMapper implements JsonMapperMethods {\n deserialize(data: string): symbol {\n return Symbol.for(data);\n }\n\n serialize(object: Symbol): any {\n return object.toString().replace(\"Symbol(\", \"\").replace(\")\", \"\");\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC","sourcesContent":["export * from \"./ArrayMapper\";\nexport * from \"./DateMapper\";\nexport * from \"./MapMapper\";\nexport * from \"./PrimitiveMapper\";\nexport * from \"./SetMapper\";\nexport * from \"./SymbolMapper\";\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { JsonEntityFn } from "@tsed/schema";
|
|
2
|
+
/**
|
|
3
|
+
* Call the function after JsonMapper.
|
|
4
|
+
*
|
|
5
|
+
* ## Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @AfterDeserialize((data: Model) => { // After deserialize get the instance class
|
|
9
|
+
* if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {
|
|
10
|
+
* throw BadRequest('MyMessage'); // or throw a ValidationError
|
|
11
|
+
* } else {
|
|
12
|
+
* data.prop2 = 'VALUE2';
|
|
13
|
+
* return data;
|
|
14
|
+
* }
|
|
15
|
+
* })
|
|
16
|
+
* class Model {
|
|
17
|
+
* @Property()
|
|
18
|
+
* prop1: string;
|
|
19
|
+
* @Property()
|
|
20
|
+
* prop2: string;
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @returns {Function}
|
|
25
|
+
* @decorator
|
|
26
|
+
* @schema
|
|
27
|
+
* @property
|
|
28
|
+
* @param cb
|
|
29
|
+
*/
|
|
30
|
+
export function AfterDeserialize(cb) {
|
|
31
|
+
return JsonEntityFn((storedJson) => {
|
|
32
|
+
storedJson.schema.$hooks.on("afterDeserialize", cb);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=afterDeserialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"afterDeserialize.js","sourceRoot":"","sources":["../../../src/decorators/afterDeserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAkB,MAAM,cAAc,CAAC;AAM3D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAA4B;IAC3D,OAAO,YAAY,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {JsonEntityFn, JsonHookContext} from \"@tsed/schema\";\n\nexport interface AfterDeserializeCallback {\n (value: any, ctx: JsonHookContext): any;\n}\n\n/**\n * Call the function after JsonMapper.\n *\n * ## Example\n *\n * ```typescript\n * @AfterDeserialize((data: Model) => { // After deserialize get the instance class\n * if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {\n * throw BadRequest('MyMessage'); // or throw a ValidationError\n * } else {\n * data.prop2 = 'VALUE2';\n * return data;\n * }\n * })\n * class Model {\n * @Property()\n * prop1: string;\n * @Property()\n * prop2: string;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n * @schema\n * @property\n * @param cb\n */\nexport function AfterDeserialize(cb: AfterDeserializeCallback): ClassDecorator {\n return JsonEntityFn((storedJson) => {\n storedJson.schema.$hooks.on(\"afterDeserialize\", cb);\n });\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { JsonEntityFn } from "@tsed/schema";
|
|
2
|
+
/**
|
|
3
|
+
* Call the function before JsonMapper.
|
|
4
|
+
*
|
|
5
|
+
* ## Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @BeforeDeserialize((data: Record<string, unknown>) => { // Before deserialize get the data
|
|
9
|
+
* if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {
|
|
10
|
+
* throw BadRequest('MyMessage'); // or throw a ValidationError
|
|
11
|
+
* } else {
|
|
12
|
+
* data.prop2 = 'VALUE2';
|
|
13
|
+
* return data;
|
|
14
|
+
* }
|
|
15
|
+
* })
|
|
16
|
+
* class Model {
|
|
17
|
+
* @Property()
|
|
18
|
+
* prop1: string;
|
|
19
|
+
* @Property()
|
|
20
|
+
* prop2: string;
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @returns {Function}
|
|
25
|
+
* @decorator
|
|
26
|
+
* @schema
|
|
27
|
+
* @property
|
|
28
|
+
* @param cb
|
|
29
|
+
*/
|
|
30
|
+
export function BeforeDeserialize(cb) {
|
|
31
|
+
return JsonEntityFn((storedJson) => {
|
|
32
|
+
storedJson.schema.$hooks.on("beforeDeserialize", cb);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=beforeDeserialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beforeDeserialize.js","sourceRoot":"","sources":["../../../src/decorators/beforeDeserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAkB,MAAM,cAAc,CAAC;AAM3D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAA6B;IAC7D,OAAO,YAAY,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {JsonEntityFn, JsonHookContext} from \"@tsed/schema\";\n\nexport interface BeforeDeserializeCallback {\n (value: any, ctx: JsonHookContext): any;\n}\n\n/**\n * Call the function before JsonMapper.\n *\n * ## Example\n *\n * ```typescript\n * @BeforeDeserialize((data: Record<string, unknown>) => { // Before deserialize get the data\n * if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {\n * throw BadRequest('MyMessage'); // or throw a ValidationError\n * } else {\n * data.prop2 = 'VALUE2';\n * return data;\n * }\n * })\n * class Model {\n * @Property()\n * prop1: string;\n * @Property()\n * prop2: string;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n * @schema\n * @property\n * @param cb\n */\nexport function BeforeDeserialize(cb: BeforeDeserializeCallback): ClassDecorator {\n return JsonEntityFn((storedJson) => {\n storedJson.schema.$hooks.on(\"beforeDeserialize\", cb);\n });\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { registerJsonTypeMapper } from "../domain/JsonMapperTypesContainer";
|
|
2
|
+
/**
|
|
3
|
+
* Register a new JsonMapper Type
|
|
4
|
+
* @param types
|
|
5
|
+
* @constructor
|
|
6
|
+
*/
|
|
7
|
+
export function JsonMapper(...types) {
|
|
8
|
+
return (target) => {
|
|
9
|
+
types.forEach((type) => {
|
|
10
|
+
registerJsonTypeMapper(type, target);
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=jsonMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonMapper.js","sourceRoot":"","sources":["../../../src/decorators/jsonMapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,sBAAsB,EAAC,MAAM,oCAAoC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAG,KAAqB;IACjD,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,sBAAsB,CAAC,IAAI,EAAE,MAAa,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerJsonTypeMapper} from \"../domain/JsonMapperTypesContainer\";\n\n/**\n * Register a new JsonMapper Type\n * @param types\n * @constructor\n */\nexport function JsonMapper(...types: (any | Type)[]): ClassDecorator {\n return (target) => {\n types.forEach((type) => {\n registerJsonTypeMapper(type, target as any);\n });\n };\n}\n"]}
|