@tsed/json-mapper 7.34.1 → 7.35.0-beta.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.
Files changed (60) hide show
  1. package/jest.config.js +3 -3
  2. package/lib/browser/json-mapper.umd.min.js +1 -1
  3. package/lib/browser/json-mapper.umd.min.js.map +1 -1
  4. package/lib/cjs/components/ArrayMapper.js.map +1 -1
  5. package/lib/cjs/components/DateMapper.js +1 -1
  6. package/lib/cjs/components/DateMapper.js.map +1 -1
  7. package/lib/cjs/components/MapMapper.js.map +1 -1
  8. package/lib/cjs/components/PrimitiveMapper.js.map +1 -1
  9. package/lib/cjs/components/SetMapper.js.map +1 -1
  10. package/lib/cjs/components/SymbolMapper.js.map +1 -1
  11. package/lib/cjs/domain/JsonMapperCompiler.js +156 -0
  12. package/lib/cjs/domain/JsonMapperCompiler.js.map +1 -0
  13. package/lib/cjs/domain/JsonSerializer.js +214 -0
  14. package/lib/cjs/domain/JsonSerializer.js.map +1 -0
  15. package/lib/cjs/domain/JsonSerializerOptions.js +3 -0
  16. package/lib/cjs/domain/JsonSerializerOptions.js.map +1 -0
  17. package/lib/cjs/domain/Writer.js +111 -0
  18. package/lib/cjs/domain/Writer.js.map +1 -0
  19. package/lib/cjs/hooks/alterOnSerialize.js +8 -0
  20. package/lib/cjs/hooks/alterOnSerialize.js.map +1 -0
  21. package/lib/cjs/index.js +6 -0
  22. package/lib/cjs/index.js.map +1 -1
  23. package/lib/cjs/utils/getObjectProperties.js +9 -0
  24. package/lib/cjs/utils/getObjectProperties.js.map +1 -0
  25. package/lib/cjs/utils/serialize.js +5 -143
  26. package/lib/cjs/utils/serialize.js.map +1 -1
  27. package/lib/esm/components/ArrayMapper.js.map +1 -1
  28. package/lib/esm/components/DateMapper.js +1 -1
  29. package/lib/esm/components/DateMapper.js.map +1 -1
  30. package/lib/esm/components/MapMapper.js.map +1 -1
  31. package/lib/esm/components/PrimitiveMapper.js.map +1 -1
  32. package/lib/esm/components/SetMapper.js.map +1 -1
  33. package/lib/esm/components/SymbolMapper.js.map +1 -1
  34. package/lib/esm/domain/JsonMapperCompiler.js +152 -0
  35. package/lib/esm/domain/JsonMapperCompiler.js.map +1 -0
  36. package/lib/esm/domain/JsonSerializer.js +210 -0
  37. package/lib/esm/domain/JsonSerializer.js.map +1 -0
  38. package/lib/esm/domain/JsonSerializerOptions.js +2 -0
  39. package/lib/esm/domain/JsonSerializerOptions.js.map +1 -0
  40. package/lib/esm/domain/Writer.js +108 -0
  41. package/lib/esm/domain/Writer.js.map +1 -0
  42. package/lib/esm/hooks/alterOnSerialize.js +4 -0
  43. package/lib/esm/hooks/alterOnSerialize.js.map +1 -0
  44. package/lib/esm/index.js +6 -0
  45. package/lib/esm/index.js.map +1 -1
  46. package/lib/esm/utils/getObjectProperties.js +5 -0
  47. package/lib/esm/utils/getObjectProperties.js.map +1 -0
  48. package/lib/esm/utils/serialize.js +4 -141
  49. package/lib/esm/utils/serialize.js.map +1 -1
  50. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  51. package/lib/tsconfig.tsbuildinfo +1 -1
  52. package/lib/types/domain/JsonMapperCompiler.d.ts +52 -0
  53. package/lib/types/domain/JsonSerializer.d.ts +20 -0
  54. package/lib/types/domain/JsonSerializerOptions.d.ts +13 -0
  55. package/lib/types/domain/Writer.d.ts +34 -0
  56. package/lib/types/hooks/alterOnSerialize.d.ts +2 -0
  57. package/lib/types/index.d.ts +6 -0
  58. package/lib/types/utils/getObjectProperties.d.ts +1 -0
  59. package/lib/types/utils/serialize.d.ts +2 -18
  60. package/package.json +11 -8
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonMapperCompiler = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ class JsonMapperCompiler {
6
+ /**
7
+ * Cached mappers metadata
8
+ * @protected
9
+ */
10
+ cache = new Map();
11
+ /**
12
+ * Cached executable mappers by his id
13
+ * @protected
14
+ */
15
+ mappers = {};
16
+ /**
17
+ * Cached schemas
18
+ * @protected
19
+ */
20
+ schemes = {};
21
+ /**
22
+ * Global variables available in the mapper
23
+ * @protected
24
+ */
25
+ globals = {
26
+ isCollection: core_1.isCollection,
27
+ isClass: core_1.isClass,
28
+ isObject: core_1.isObject,
29
+ classOf: core_1.classOf,
30
+ nameOf: core_1.nameOf,
31
+ hasJsonMethod: core_1.hasJsonMethod,
32
+ isMongooseObject: core_1.isMongooseObject,
33
+ isNil: core_1.isNil,
34
+ isDate: core_1.isDate,
35
+ objectKeys: core_1.objectKeys,
36
+ isMomentObject: core_1.isMomentObject
37
+ };
38
+ constructor() {
39
+ this.addGlobal("alterIgnore", this.alterIgnore.bind(this));
40
+ this.addGlobal("alterValue", this.alterValue.bind(this));
41
+ this.addGlobal("execMapper", this.execMapper.bind(this));
42
+ }
43
+ addTypeMapper(model, fn) {
44
+ const id = (0, core_1.nameOf)(model);
45
+ this.cache.set(model, new Map().set("typeMapper", {
46
+ id,
47
+ fn
48
+ }));
49
+ this.mappers[id] = fn;
50
+ return this;
51
+ }
52
+ removeTypeMapper(model) {
53
+ const store = this.cache.get(model);
54
+ if (store) {
55
+ const { id } = store.get("typeMapper");
56
+ delete this.mappers[id];
57
+ this.cache.delete(model);
58
+ }
59
+ }
60
+ addGlobal(key, value) {
61
+ this.globals[key] = value;
62
+ return this;
63
+ }
64
+ eval(mapper, { id, groupsId, model }) {
65
+ this.addGlobal("cache", this.cache);
66
+ const { globals } = this;
67
+ const injectGlobals = Object.keys(globals)
68
+ .map((name) => {
69
+ return `const ${name} = globals.${name};`;
70
+ })
71
+ .join("\n");
72
+ eval(`${injectGlobals};
73
+
74
+ cache.get(model).set(groupsId, { id: '${id}', fn: ${mapper} })`);
75
+ const store = this.cache.get(model).get(groupsId);
76
+ this.mappers[id] = store.fn;
77
+ return store;
78
+ }
79
+ createContext(options) {
80
+ const { mappers, globals, cache } = this;
81
+ return {
82
+ ...options,
83
+ cache
84
+ };
85
+ }
86
+ compile(model, groups) {
87
+ model = this.getType(model);
88
+ const groupsId = this.getGroupsId(groups);
89
+ let storeGroups = this.cache.get(model) || this.cache.get((0, core_1.nameOf)(model));
90
+ if (!storeGroups) {
91
+ storeGroups = new Map();
92
+ this.cache.set(model, storeGroups);
93
+ }
94
+ if (storeGroups.has("typeMapper")) {
95
+ return storeGroups.get("typeMapper");
96
+ }
97
+ // generate mapper for the given groups
98
+ if (!storeGroups.has(groupsId)) {
99
+ const id = this.getId(model, groupsId);
100
+ // prevent circular dependencies
101
+ storeGroups.set(groupsId, {
102
+ id
103
+ });
104
+ const mapper = this.createMapper(model, id, groups);
105
+ try {
106
+ console.log(mapper);
107
+ return this.eval(mapper, { id, model, groupsId });
108
+ }
109
+ catch (err) {
110
+ throw new Error(`Fail to compile mapper for ${(0, core_1.nameOf)(model)}. See the error above: ${err.message}.\n${mapper}`);
111
+ }
112
+ }
113
+ return storeGroups.get(groupsId);
114
+ }
115
+ execMapper(id, value, options) {
116
+ return this.mappers[id](value, options);
117
+ }
118
+ getType(model) {
119
+ if (!model) {
120
+ return Object;
121
+ }
122
+ if ((0, core_1.isClass)(model) && !(0, core_1.isCollection)(model)) {
123
+ const type = [Array, Map, Set].find((t) => (0, core_1.ancestorsOf)(model).includes(t));
124
+ if (type) {
125
+ return type;
126
+ }
127
+ }
128
+ return model;
129
+ }
130
+ alterIgnore(id, options) {
131
+ let result = this.schemes[id]?.$hooks?.alter("ignore", false, [options]);
132
+ if (result) {
133
+ return result;
134
+ }
135
+ }
136
+ alterGroups(schema, groups) {
137
+ if (groups !== false) {
138
+ return schema.$hooks.alter("groups", false, [groups]);
139
+ }
140
+ return false;
141
+ }
142
+ getGroupsId(groups) {
143
+ if (groups === false) {
144
+ return "default";
145
+ }
146
+ if (groups.length === 0) {
147
+ return "-";
148
+ }
149
+ return groups.join(",");
150
+ }
151
+ getId(model, groupsId) {
152
+ return `${(0, core_1.nameOf)(model)}:${(0, core_1.getRandomId)()}:${groupsId}`;
153
+ }
154
+ }
155
+ exports.JsonMapperCompiler = JsonMapperCompiler;
156
+ //# sourceMappingURL=JsonMapperCompiler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonMapperCompiler.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperCompiler.ts"],"names":[],"mappings":";;;AAAA,qCAeoB;AAWpB,MAAsB,kBAAkB;IACtC;;;OAGG;IACO,KAAK,GAAG,IAAI,GAAG,EAAuD,CAAC;IACjF;;;OAGG;IACO,OAAO,GAAgD,EAAE,CAAC;IACpE;;;OAGG;IACO,OAAO,GAAwB,EAAE,CAAC;IAC5C;;;OAGG;IACO,OAAO,GAAwB;QACvC,YAAY,EAAZ,mBAAY;QACZ,OAAO,EAAP,cAAO;QACP,QAAQ,EAAR,eAAQ;QACR,OAAO,EAAP,cAAO;QACP,MAAM,EAAN,aAAM;QACN,aAAa,EAAb,oBAAa;QACb,gBAAgB,EAAhB,uBAAgB;QAChB,KAAK,EAAL,YAAK;QACL,MAAM,EAAN,aAAM;QACN,UAAU,EAAV,iBAAU;QACV,cAAc,EAAd,qBAAc;KACf,CAAC;IAEF;QACE,IAAI,CAAC,SAAS,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC3D,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,aAAa,CAAC,KAAyB,EAAE,EAAO;QAC9C,MAAM,EAAE,GAAG,IAAA,aAAM,EAAC,KAAK,CAAC,CAAC;QAEzB,IAAI,CAAC,KAAK,CAAC,GAAG,CACZ,KAAK,EACL,IAAI,GAAG,EAAE,CAAC,GAAG,CAAC,YAAY,EAAE;YAC1B,EAAE;YAC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EAAE;YACpB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;SACvD;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAES,WAAW,CAAC,MAAwB;QAC5C,IAAI,MAAM,KAAK,KAAK,EAAE;YACpB,OAAO,SAAS,CAAC;SAClB;QAED,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;YACvB,OAAO,GAAG,CAAC;SACZ;QAED,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAES,KAAK,CAAC,KAAgB,EAAE,QAAgB;QAChD,OAAO,GAAG,IAAA,aAAM,EAAC,KAAK,CAAC,IAAI,IAAA,kBAAW,GAAE,IAAI,QAAQ,EAAE,CAAC;IACzD,CAAC;CACF;AAtMD,gDAsMC"}
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonSerializer = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ const schema_1 = require("@tsed/schema");
6
+ const alterOnSerialize_1 = require("../hooks/alterOnSerialize");
7
+ const getObjectProperties_1 = require("../utils/getObjectProperties");
8
+ const JsonMapperCompiler_1 = require("./JsonMapperCompiler");
9
+ const JsonMapperTypesContainer_1 = require("./JsonMapperTypesContainer");
10
+ const Writer_1 = require("./Writer");
11
+ function getBestType(type, obj) {
12
+ const dataType = (0, core_1.classOf)(obj);
13
+ if (dataType && !(0, core_1.isClassObject)(dataType)) {
14
+ return dataType;
15
+ }
16
+ return type || Object;
17
+ }
18
+ class JsonSerializer extends JsonMapperCompiler_1.JsonMapperCompiler {
19
+ constructor() {
20
+ super();
21
+ this.addTypeMapper(Object, this.mapObject.bind(this));
22
+ this.addTypeMapper(Array, this.mapArray.bind(this));
23
+ this.addTypeMapper(Map, this.mapMap.bind(this));
24
+ this.addTypeMapper(Set, this.mapSet.bind(this));
25
+ this.addGlobal("mapJSON", this.mapJSON.bind(this));
26
+ }
27
+ map(input, options = {}) {
28
+ if ((0, core_1.isNil)(input)) {
29
+ return input;
30
+ }
31
+ options = this.mapOptions(options);
32
+ const model = getBestType(options.type, input);
33
+ const mapper = this.compile(model, options.groups);
34
+ console.log(input, model);
35
+ return mapper.fn(input, this.createContext(options));
36
+ }
37
+ alterValue(schemaId, value, options) {
38
+ return (0, alterOnSerialize_1.alterOnSerialize)(this.schemes[schemaId], value, options);
39
+ }
40
+ createMapper(model, id, groups) {
41
+ const entity = schema_1.JsonEntityStore.from(model);
42
+ const properties = new Set();
43
+ const schemaProperties = [...(0, schema_1.getPropertiesStores)(entity).values()];
44
+ const writer = new Writer_1.Writer().arrow("input", "options");
45
+ writer.if("isNil(input)").return("input");
46
+ // prepare options
47
+ writer.const("obj", "{}");
48
+ writer.set("options", "{...options, self: input}");
49
+ // detect some special case
50
+ writer.add(this.mapPrecondition(id));
51
+ if (!schemaProperties.length) {
52
+ return writer.return("input").root().toString();
53
+ }
54
+ // properties
55
+ writer.add(...schemaProperties.flatMap((propertyStore) => {
56
+ properties.add(propertyStore.propertyKey);
57
+ if (propertyStore.schema?.$hooks?.has("groups") && this.alterGroups(propertyStore.schema, groups)) {
58
+ return;
59
+ }
60
+ return this.mapProperty(propertyStore, id, groups);
61
+ }));
62
+ // discriminator
63
+ writer.add(this.mapDiscriminator(entity));
64
+ // additional properties
65
+ writer.add(this.mapAdditionalProperties(entity, properties));
66
+ return writer.return("obj").root().toString();
67
+ }
68
+ mapOptions({ groups = false, useAlias = true, types, ...options }) {
69
+ const customMappers = {};
70
+ types = types || (0, JsonMapperTypesContainer_1.getJsonMapperTypes)();
71
+ types.forEach((mapper, model) => {
72
+ if (![Array, Set, Map].includes(model)) {
73
+ this.addTypeMapper(model, mapper.serialize.bind(mapper));
74
+ }
75
+ });
76
+ return {
77
+ ...options,
78
+ groups,
79
+ useAlias,
80
+ customMappers
81
+ };
82
+ }
83
+ mapProperty(propertyStore, id, groups) {
84
+ const key = String(propertyStore.propertyKey);
85
+ const aliasKey = String(propertyStore.parent.schema.getAliasOf(key) || key);
86
+ const schemaId = `${id}:${key}`;
87
+ let writer = new Writer_1.Writer().add(`// Map ${key} ${id} ${groups || ""}`);
88
+ // ignore hook (deprecated)
89
+ if (propertyStore.schema?.$hooks?.has("ignore")) {
90
+ this.schemes[schemaId] = propertyStore.schema;
91
+ writer = writer.if(`!alterIgnore('${schemaId}', {...options, self: input})`);
92
+ }
93
+ writer = writer.add(`let ${key} = input.${key};`).if(`${key} !== undefined`);
94
+ const format = propertyStore.itemSchema.get("format");
95
+ const formatOpts = format && `options: {format: '${format}'}`;
96
+ // pre hook
97
+ const hasSerializer = propertyStore.schema?.$hooks?.has("onSerialize");
98
+ if (hasSerializer) {
99
+ this.schemes[schemaId] = propertyStore.schema;
100
+ const opts = Writer_1.Writer.options(formatOpts);
101
+ writer.set(key, `alterValue('${schemaId}', ${key}, ${opts})`);
102
+ }
103
+ if (propertyStore.isCollection) {
104
+ const type = propertyStore.getBestType();
105
+ const nestedMapper = this.compile(type, groups);
106
+ writer.callMapper((0, core_1.nameOf)(propertyStore.collectionType), key, `id: '${nestedMapper.id}'`, formatOpts);
107
+ }
108
+ else {
109
+ const type = propertyStore.getBestType();
110
+ const nestedMapper = this.compile(type, groups);
111
+ const fill = (writer) => writer.callMapper(nestedMapper.id, key, formatOpts);
112
+ if (hasSerializer) {
113
+ fill(writer.if(`${key} === input.${key}`));
114
+ }
115
+ else {
116
+ fill(writer);
117
+ }
118
+ }
119
+ if (aliasKey !== key) {
120
+ writer.set(`obj[options.useAlias ? '${aliasKey}' : '${key}']`, key);
121
+ }
122
+ else {
123
+ writer.set(`obj.${key}`, key);
124
+ }
125
+ return writer.root();
126
+ }
127
+ mapPrecondition(id) {
128
+ const writer = new Writer_1.Writer();
129
+ writer.if("input && isCollection(input)").return(Writer_1.Writer.mapperFrom("input", `{...options, id: '${id}'}`));
130
+ writer.if("hasJsonMethod(input)").return(`mapJSON(input, {...options, id: '${id}'})`);
131
+ return writer;
132
+ }
133
+ mapDiscriminator(entity) {
134
+ if (entity.discriminatorAncestor) {
135
+ const discriminator = entity.discriminatorAncestor.schema.discriminator();
136
+ const type = discriminator.getDefaultValue(entity.target);
137
+ if (type) {
138
+ const writer = new Writer_1.Writer();
139
+ writer.if(`!obj.${discriminator.propertyName}`).set(`obj.${discriminator.propertyName}`, `'${type}'`);
140
+ return writer;
141
+ }
142
+ }
143
+ }
144
+ mapAdditionalProperties(entity, properties) {
145
+ const additionalProperties = !!entity.schema.get("additionalProperties");
146
+ if (additionalProperties) {
147
+ const exclude = [...properties.values()].map((key) => `'${key}'`).join(", ");
148
+ const writer = new Writer_1.Writer();
149
+ writer.add("// add additional properties");
150
+ writer.each("objectKeys(input)", ["key"]).if(`![${exclude}].includes(key)`).set("obj[key]", "input[key]");
151
+ return writer;
152
+ }
153
+ }
154
+ mapObject(input, { type, ...options }) {
155
+ if (input && (0, core_1.isCollection)(input)) {
156
+ return this.execMapper((0, core_1.nameOf)((0, core_1.classOf)(input)), input, options);
157
+ }
158
+ if ((0, core_1.hasJsonMethod)(input)) {
159
+ return this.mapJSON(input, options);
160
+ }
161
+ return (0, getObjectProperties_1.getObjectProperties)(input)
162
+ .filter(([, value]) => value !== undefined)
163
+ .reduce((obj, [key, value]) => {
164
+ if ((0, core_1.isNil)(value)) {
165
+ return {
166
+ ...obj,
167
+ [key]: value
168
+ };
169
+ }
170
+ const mapper = this.compile((0, core_1.classOf)(value), options.groups);
171
+ return {
172
+ ...obj,
173
+ [key]: mapper.fn(value, options)
174
+ };
175
+ }, {});
176
+ }
177
+ mapSet(input, options) {
178
+ return [...input.values()].map((item) => {
179
+ return this.mapItem(item, options);
180
+ });
181
+ }
182
+ mapArray(input, options) {
183
+ return [].concat(input).map((item) => {
184
+ return this.mapItem(item, options);
185
+ });
186
+ }
187
+ mapMap(input, options) {
188
+ return [...input.entries()].reduce((obj, [key, item]) => {
189
+ return {
190
+ ...obj,
191
+ [key]: this.mapItem(item, options)
192
+ };
193
+ }, {});
194
+ }
195
+ mapItem(input, { id, ...options }) {
196
+ if (!id && input) {
197
+ return this.compile((0, core_1.classOf)(input), options.groups).fn(input, options);
198
+ }
199
+ return id ? this.execMapper(id, input, options) : input;
200
+ }
201
+ mapJSON(input, { id, ...options }) {
202
+ if ((0, core_1.isMongooseObject)(input)) {
203
+ return input.toJSON({ ...options, id });
204
+ }
205
+ id = id || (0, core_1.nameOf)((0, core_1.classOf)(input));
206
+ if (this.mappers[id] && ((0, core_1.isDate)(input) || (0, core_1.isMomentObject)(input))) {
207
+ return this.execMapper(id, input, options);
208
+ }
209
+ input = input.toJSON();
210
+ return (0, core_1.isObject)(input) ? this.execMapper(id, input, options) : input;
211
+ }
212
+ }
213
+ exports.JsonSerializer = JsonSerializer;
214
+ //# sourceMappingURL=JsonSerializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSerializer.js","sourceRoot":"","sources":["../../../src/domain/JsonSerializer.ts"],"names":[],"mappings":";;;AAAA,qCAYoB;AACpB,yCAAqG;AACrG,gEAA2D;AAC3D,sEAAiE;AACjE,6DAAwD;AACxD,yEAA8D;AAE9D,qCAAgC;AAEhC,SAAS,WAAW,CAAC,IAAe,EAAE,GAAQ;IAC5C,MAAM,QAAQ,GAAG,IAAA,cAAO,EAAC,GAAG,CAAC,CAAC;IAE9B,IAAI,QAAQ,IAAI,CAAC,IAAA,oBAAa,EAAC,QAAQ,CAAC,EAAE;QACxC,OAAO,QAAQ,CAAC;KACjB;IAED,OAAO,IAAI,IAAI,MAAM,CAAC;AACxB,CAAC;AAED,MAAa,cAAe,SAAQ,uCAAyC;IAC3E;QACE,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACtD,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,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C,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACvE,CAAC;CACF;AA9PD,wCA8PC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=JsonSerializerOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSerializerOptions.js","sourceRoot":"","sources":["../../../src/domain/JsonSerializerOptions.ts"],"names":[],"mappings":""}
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Writer = void 0;
4
+ class Writer {
5
+ static object = {
6
+ assign(...args) {
7
+ return `{ ${args.filter(Boolean).join(", ")} }`;
8
+ }
9
+ };
10
+ _root;
11
+ body = [];
12
+ _indent;
13
+ constructor(root) {
14
+ this._root = root;
15
+ }
16
+ static indent(lines) {
17
+ return lines.map((p) => `\t${p}`);
18
+ }
19
+ static mapper(mapperId, key, options) {
20
+ return `execMapper('${mapperId}', ${key}, ${options})`;
21
+ }
22
+ static mapperFrom(key, options) {
23
+ return `execMapper(nameOf(classOf(${key})), ${key}, ${options})`;
24
+ }
25
+ static options(...args) {
26
+ args = args.filter(Boolean);
27
+ return !args.length ? "options" : Writer.object.assign("...options", ...args);
28
+ }
29
+ add(...lines) {
30
+ lines = lines.filter((line) => line !== undefined);
31
+ lines.length && this.body.push(...lines);
32
+ return this;
33
+ }
34
+ arrow(...args) {
35
+ this.add(`(${args}) => {`);
36
+ const writer = this.new();
37
+ this.add("}");
38
+ return writer;
39
+ }
40
+ build() {
41
+ const result = this.body.flatMap((line) => {
42
+ return line instanceof Writer ? line.build() : line;
43
+ });
44
+ return this._indent ? result.map((item) => "\t" + item) : result;
45
+ }
46
+ callMapper(id, key, ...options) {
47
+ return this.set(key, Writer.mapper(id, key, Writer.options(...options)));
48
+ }
49
+ const(name, line) {
50
+ this.add(`const ${name} = ${line};`);
51
+ return this;
52
+ }
53
+ each(iterable, args = []) {
54
+ const writer = this.add(iterable + ".forEach((" + args.join(", ") + ") => {").new();
55
+ this.add("});");
56
+ return writer;
57
+ }
58
+ if(condition) {
59
+ const writer = new IfWriter(condition, this);
60
+ this.add(writer);
61
+ return writer;
62
+ }
63
+ indent(indent) {
64
+ this._indent = indent;
65
+ return this;
66
+ }
67
+ new(indent = true) {
68
+ const writer = new Writer(this.root());
69
+ writer.indent(indent);
70
+ this.add(writer);
71
+ return writer;
72
+ }
73
+ return(line) {
74
+ this.add(`return ${line};`);
75
+ return this.root();
76
+ }
77
+ root() {
78
+ return this._root || this;
79
+ }
80
+ set(left, right) {
81
+ this.add(`${left} = ${right};`);
82
+ return this;
83
+ }
84
+ toString() {
85
+ return this.build().join("\n");
86
+ }
87
+ }
88
+ exports.Writer = Writer;
89
+ class IfWriter extends Writer {
90
+ condition;
91
+ elseWriter;
92
+ constructor(condition, root) {
93
+ super();
94
+ this.condition = condition;
95
+ this._root = root;
96
+ }
97
+ else() {
98
+ const writer = new Writer(this._root);
99
+ this.elseWriter = writer;
100
+ return writer;
101
+ }
102
+ build() {
103
+ return [
104
+ `if (${this.condition}) {`,
105
+ ...Writer.indent(super.build()),
106
+ "}",
107
+ this.elseWriter ? [`else {`, ...Writer.indent(this.elseWriter.build()), "}"] : []
108
+ ].flat();
109
+ }
110
+ }
111
+ //# sourceMappingURL=Writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Writer.js","sourceRoot":"","sources":["../../../src/domain/Writer.ts"],"names":[],"mappings":";;;AAAA,MAAa,MAAM;IACjB,MAAM,CAAC,MAAM,GAAG;QACd,MAAM,CAAC,GAAG,IAAc;YACtB,OAAO,KAAK,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAClD,CAAC;KACF,CAAC;IAEQ,KAAK,CAAU;IACf,IAAI,GAAwB,EAAE,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,SAAS,KAAK;YAC1B,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YAC/B,GAAG;YACH,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE;SAClF,CAAC,IAAI,EAAE,CAAC;IACX,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.alterOnSerialize = void 0;
4
+ function alterOnSerialize(schema, value, options) {
5
+ return schema.$hooks.alter("onSerialize", value, [options]);
6
+ }
7
+ exports.alterOnSerialize = alterOnSerialize;
8
+ //# sourceMappingURL=alterOnSerialize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alterOnSerialize.js","sourceRoot":"","sources":["../../../src/hooks/alterOnSerialize.ts"],"names":[],"mappings":";;;AAEA,SAAgB,gBAAgB,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IACvF,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC;AAFD,4CAEC"}
package/lib/cjs/index.js CHANGED
@@ -15,12 +15,18 @@ tslib_1.__exportStar(require("./decorators/beforeDeserialize"), exports);
15
15
  tslib_1.__exportStar(require("./decorators/jsonMapper"), exports);
16
16
  tslib_1.__exportStar(require("./decorators/onDeserialize"), exports);
17
17
  tslib_1.__exportStar(require("./decorators/onSerialize"), exports);
18
+ tslib_1.__exportStar(require("./domain/JsonMapperCompiler"), exports);
18
19
  tslib_1.__exportStar(require("./domain/JsonMapperContext"), exports);
19
20
  tslib_1.__exportStar(require("./domain/JsonMapperSettings"), exports);
20
21
  tslib_1.__exportStar(require("./domain/JsonMapperTypesContainer"), exports);
22
+ tslib_1.__exportStar(require("./domain/JsonSerializer"), exports);
23
+ tslib_1.__exportStar(require("./domain/JsonSerializerOptions"), exports);
24
+ tslib_1.__exportStar(require("./domain/Writer"), exports);
21
25
  tslib_1.__exportStar(require("./hooks/alterAfterDeserialize"), exports);
22
26
  tslib_1.__exportStar(require("./hooks/alterBeforeDeserialize"), exports);
27
+ tslib_1.__exportStar(require("./hooks/alterOnSerialize"), exports);
23
28
  tslib_1.__exportStar(require("./interfaces/JsonMapperMethods"), exports);
24
29
  tslib_1.__exportStar(require("./utils/deserialize"), exports);
30
+ tslib_1.__exportStar(require("./utils/getObjectProperties"), exports);
25
31
  tslib_1.__exportStar(require("./utils/serialize"), exports);
26
32
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,mEAAyC;AACzC,kEAAwC;AACxC,iEAAuC;AACvC,uEAA6C;AAC7C,iEAAuC;AACvC,oEAA0C;AAC1C,wEAA8C;AAC9C,yEAA+C;AAC/C,kEAAwC;AACxC,qEAA2C;AAC3C,mEAAyC;AACzC,qEAA2C;AAC3C,sEAA4C;AAC5C,4EAAkD;AAClD,wEAA8C;AAC9C,yEAA+C;AAC/C,yEAA+C;AAC/C,8DAAoC;AACpC,4DAAkC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,mEAAyC;AACzC,kEAAwC;AACxC,iEAAuC;AACvC,uEAA6C;AAC7C,iEAAuC;AACvC,oEAA0C;AAC1C,wEAA8C;AAC9C,yEAA+C;AAC/C,kEAAwC;AACxC,qEAA2C;AAC3C,mEAAyC;AACzC,sEAA4C;AAC5C,qEAA2C;AAC3C,sEAA4C;AAC5C,4EAAkD;AAClD,kEAAwC;AACxC,yEAA+C;AAC/C,0DAAgC;AAChC,wEAA8C;AAC9C,yEAA+C;AAC/C,mEAAyC;AACzC,yEAA+C;AAC/C,8DAAoC;AACpC,sEAA4C;AAC5C,4DAAkC"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getObjectProperties = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ function getObjectProperties(obj) {
6
+ return Object.entries(obj).filter(([, value]) => !(0, core_1.isFunction)(value));
7
+ }
8
+ exports.getObjectProperties = getObjectProperties;
9
+ //# sourceMappingURL=getObjectProperties.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getObjectProperties.js","sourceRoot":"","sources":["../../../src/utils/getObjectProperties.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAEtC,SAAgB,mBAAmB,CAAC,GAAQ;IAC1C,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,CAAC;AAFD,kDAEC"}