@edirect/template 1.0.2 → 1.0.6
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/README.md +327 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @edirect/template
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TemplateModule is a library that provides methods to transform a payload based on a given template. The transformation includes mapping fields, applying transformers, setting default values and so on.
|
|
4
4
|
|
|
5
5
|
### Installation
|
|
6
6
|
|
|
@@ -8,6 +8,57 @@ The EDirectInsure template mapper module.
|
|
|
8
8
|
$ npm i --save @edirect/template
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
### Import
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { TemplateModule } from "./TemplateModule";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Methods
|
|
18
|
+
|
|
19
|
+
#### `Constructor`
|
|
20
|
+
|
|
21
|
+
The TemplateModule class does not have a constructor.
|
|
22
|
+
|
|
23
|
+
#### `setContext(context): void`
|
|
24
|
+
|
|
25
|
+
Sets the context object to be used during the transformation.
|
|
26
|
+
|
|
27
|
+
#### `setTemplate(template): void`
|
|
28
|
+
|
|
29
|
+
Sets the template object that describes the transformation to be performed.
|
|
30
|
+
|
|
31
|
+
#### `setTransformers(transformers: ITransformer): void`
|
|
32
|
+
|
|
33
|
+
Sets the transformers to be used during the transformation.
|
|
34
|
+
|
|
35
|
+
#### `verifyTransformer(transformer: ITransformer, methodName: string): boolean`
|
|
36
|
+
|
|
37
|
+
Verifies if a given transformer method exists.
|
|
38
|
+
|
|
39
|
+
#### `runTransformer(transformer: string, value?: unknown): unknown | null`
|
|
40
|
+
|
|
41
|
+
Runs a transformer on a given value.
|
|
42
|
+
|
|
43
|
+
- transformer - The name of the transformer to be used.
|
|
44
|
+
- value - The value to be transformed.
|
|
45
|
+
|
|
46
|
+
#### `checkValue(value: any): boolean`
|
|
47
|
+
|
|
48
|
+
Checks if a given value is not null, undefined, or an empty string.
|
|
49
|
+
|
|
50
|
+
#### `setValueByCondition(object, key: string, value: unknown)`
|
|
51
|
+
|
|
52
|
+
Sets a value in an object after verify using the checkValue() method.
|
|
53
|
+
|
|
54
|
+
- object - The object to be modified.
|
|
55
|
+
- key - The key of the value to be set.
|
|
56
|
+
- value - The value to be set.
|
|
57
|
+
|
|
58
|
+
#### `transformPayload<T, U>(obj: T, template = this.template): U`
|
|
59
|
+
|
|
60
|
+
Transforms a payload object on a given template.
|
|
61
|
+
|
|
11
62
|
### Simple example
|
|
12
63
|
|
|
13
64
|
```javascript
|
|
@@ -63,7 +114,7 @@ To map a value that you'd like to use some transformer feature, you can use thes
|
|
|
63
114
|
- `transformer`: it's a JS function, where you receive value and context as parameters and have all freedom to handle and return a value
|
|
64
115
|
- `defaultValue` it's an option if the engine doesn't find any value in the fields data source array or the transformer doesn't return a value.
|
|
65
116
|
|
|
66
|
-
|
|
117
|
+
##### File name: baseTransformers.ts
|
|
67
118
|
|
|
68
119
|
```javascript
|
|
69
120
|
import { ITransformer, ITransformerParams } from "@edirect/template";
|
|
@@ -89,7 +140,7 @@ const transformers: ITransformer = {
|
|
|
89
140
|
export default transformers;
|
|
90
141
|
```
|
|
91
142
|
|
|
92
|
-
|
|
143
|
+
##### File name: index.ts
|
|
93
144
|
|
|
94
145
|
```javascript
|
|
95
146
|
import { TemplateModule } from "@edirect/template";
|
|
@@ -140,3 +191,276 @@ console.log(result);
|
|
|
140
191
|
// timeZone: "Time zone in Porto (GMT+1)",
|
|
141
192
|
// }
|
|
142
193
|
```
|
|
194
|
+
|
|
195
|
+
### Example with nested object + transformers
|
|
196
|
+
|
|
197
|
+
`Note`: the transformer notation uses three keys to identify an object as being a transformer: fields, transformer, and defaultValue, if there are at least 1 of these keys, the engine will consider the object as being a transformer, on the other hand, if there isn't, the engine will consider as a nested object to mapper all information.
|
|
198
|
+
|
|
199
|
+
##### File name: baseTransformers.ts
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
import { ITransformer, ITransformerParams } from "@edirect/template";
|
|
203
|
+
|
|
204
|
+
const upperCase = ({ value }: ITransformerParams): string | null => {
|
|
205
|
+
return value ? String(value).toUpperCase() : null;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const transformers: ITransformer = {
|
|
209
|
+
upperCase,
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export default transformers;
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
##### File name: index.ts
|
|
216
|
+
|
|
217
|
+
```javascript
|
|
218
|
+
import { TemplateModule } from "@edirect/template";
|
|
219
|
+
import baseTransformers from "./baseTransformers";
|
|
220
|
+
|
|
221
|
+
const template = {
|
|
222
|
+
order: {
|
|
223
|
+
date: "order.date",
|
|
224
|
+
value: "order.value",
|
|
225
|
+
subscriber: {
|
|
226
|
+
name: "sub.name",
|
|
227
|
+
phone: "sub.phone",
|
|
228
|
+
email: {
|
|
229
|
+
fields: ["email", "sub.email"],
|
|
230
|
+
},
|
|
231
|
+
address: {
|
|
232
|
+
street: "sub.add.stt",
|
|
233
|
+
number: "sub.add.num",
|
|
234
|
+
city: {
|
|
235
|
+
fields: ["sub.add.city"],
|
|
236
|
+
transformer: "upperCase",
|
|
237
|
+
},
|
|
238
|
+
state: {
|
|
239
|
+
fields: ["sub.add.stt"],
|
|
240
|
+
transformer: "upperCase",
|
|
241
|
+
},
|
|
242
|
+
zip: {
|
|
243
|
+
fields: ["sub.add.zip"],
|
|
244
|
+
defaultValue: "0000-000",
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const dataSource = {
|
|
252
|
+
order: {
|
|
253
|
+
value: 1000.0,
|
|
254
|
+
date: "2000-01-01",
|
|
255
|
+
},
|
|
256
|
+
sub: {
|
|
257
|
+
name: "name-test",
|
|
258
|
+
phone: "999999999",
|
|
259
|
+
email: "template.service@bolltech.io",
|
|
260
|
+
add: {
|
|
261
|
+
st: "st-test",
|
|
262
|
+
num: 100,
|
|
263
|
+
city: "city-test",
|
|
264
|
+
stt: "state-test",
|
|
265
|
+
zip: "zip-test",
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const templateModule = new TemplateModule();
|
|
271
|
+
|
|
272
|
+
templateModule.setTemplate(template);
|
|
273
|
+
templateModule.setContext(dataSource);
|
|
274
|
+
templateModule.setTransformers(baseTransformers);
|
|
275
|
+
|
|
276
|
+
const result = templateModule.transformPayload(dataSource);
|
|
277
|
+
console.log(result);
|
|
278
|
+
|
|
279
|
+
// {
|
|
280
|
+
// order: {
|
|
281
|
+
// date: "2000-01-01",
|
|
282
|
+
// value: 1000,
|
|
283
|
+
// subscriber: {
|
|
284
|
+
// name: "name-test",
|
|
285
|
+
// phone: "999999999",
|
|
286
|
+
// email: "template.service@bolltech.io",
|
|
287
|
+
// address: {
|
|
288
|
+
// street: "state-test",
|
|
289
|
+
// number: 100,
|
|
290
|
+
// city: "CITY-TEST",
|
|
291
|
+
// state: "STATE-TEST",
|
|
292
|
+
// zip: "zip-test",
|
|
293
|
+
// },
|
|
294
|
+
// },
|
|
295
|
+
// },
|
|
296
|
+
// }
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Example with arrays + transformers
|
|
300
|
+
|
|
301
|
+
`Note`: When it comes to arrays mapper, we need to have in mind that is required use this two keys: arraySource and arrayTemplate.
|
|
302
|
+
|
|
303
|
+
- `arraySource`: source path where the engine will seek the information to mapper
|
|
304
|
+
- `arrayTemplate`: template that will be used for each object within the array
|
|
305
|
+
|
|
306
|
+
##### File name: baseTransformers.ts
|
|
307
|
+
|
|
308
|
+
```javascript
|
|
309
|
+
import { ITransformer, ITransformerParams } from "@edirect/template";
|
|
310
|
+
|
|
311
|
+
const upperCase = ({ value }: ITransformerParams): string | null => {
|
|
312
|
+
return value ? String(value).toUpperCase() : null;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const transformers: ITransformer = {
|
|
316
|
+
upperCase,
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
export default transformers;
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
##### File name: index.ts
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
import { TemplateModule } from "@edirect/template";
|
|
326
|
+
import baseTransformers from "./baseTransformers";
|
|
327
|
+
|
|
328
|
+
const template = {
|
|
329
|
+
quote: {
|
|
330
|
+
orders: {
|
|
331
|
+
arraySource: "order",
|
|
332
|
+
arrayTemplate: {
|
|
333
|
+
value: "value",
|
|
334
|
+
date: "date",
|
|
335
|
+
products: {
|
|
336
|
+
arraySource: "products",
|
|
337
|
+
arrayTemplate: {
|
|
338
|
+
id: "id",
|
|
339
|
+
value: "value",
|
|
340
|
+
description: {
|
|
341
|
+
fields: ["description"],
|
|
342
|
+
transformer: "upperCase",
|
|
343
|
+
defaultValue: "Default description",
|
|
344
|
+
},
|
|
345
|
+
categories: "categories",
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
const dataSource = {
|
|
354
|
+
order: [
|
|
355
|
+
{
|
|
356
|
+
value: 1000.0,
|
|
357
|
+
date: "2000-01-01",
|
|
358
|
+
products: [
|
|
359
|
+
{
|
|
360
|
+
id: "id-test-1",
|
|
361
|
+
value: 1000,
|
|
362
|
+
description: "description-test 1",
|
|
363
|
+
categories: ["category-1"],
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
id: "id-test-2",
|
|
367
|
+
value: 2000,
|
|
368
|
+
description: "description-test 2",
|
|
369
|
+
categories: ["category-1", "category-2"],
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
id: "id-test-3",
|
|
373
|
+
value: 3000,
|
|
374
|
+
categories: ["category-1", "category-2", "category-3"],
|
|
375
|
+
},
|
|
376
|
+
],
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
const templateModule = new TemplateModule();
|
|
382
|
+
|
|
383
|
+
templateModule.setTemplate(template);
|
|
384
|
+
templateModule.setContext(dataSource);
|
|
385
|
+
templateModule.setTransformers(baseTransformers);
|
|
386
|
+
|
|
387
|
+
const result = templateModule.transformPayload(dataSource);
|
|
388
|
+
console.log(result);
|
|
389
|
+
|
|
390
|
+
//{
|
|
391
|
+
// quote: {
|
|
392
|
+
// orders: [
|
|
393
|
+
// {
|
|
394
|
+
// value: 1000,
|
|
395
|
+
// date: "2000-01-01",
|
|
396
|
+
// products: [
|
|
397
|
+
// {
|
|
398
|
+
// id: "id-test-1",
|
|
399
|
+
// value: 1000,
|
|
400
|
+
// description: "DESCRIPTION-TEST 1",
|
|
401
|
+
// categories: ["category-1"],
|
|
402
|
+
// },
|
|
403
|
+
// {
|
|
404
|
+
// id: "id-test-2",
|
|
405
|
+
// value: 2000,
|
|
406
|
+
// description: "DESCRIPTION-TEST 2",
|
|
407
|
+
// categories: ["category-1", "category-2"],
|
|
408
|
+
// },
|
|
409
|
+
// {
|
|
410
|
+
// id: "id-test-3",
|
|
411
|
+
// value: 3000,
|
|
412
|
+
// description: "Default description",
|
|
413
|
+
// categories: ["category-1", "category-2", "category-3"],
|
|
414
|
+
// },
|
|
415
|
+
// ],
|
|
416
|
+
// },
|
|
417
|
+
// ],
|
|
418
|
+
// },
|
|
419
|
+
// }
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### Example inferring types to transformPayload
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
import { TemplateModule } from "@edirect/template";
|
|
426
|
+
|
|
427
|
+
interface DataSource {
|
|
428
|
+
subscriber: {
|
|
429
|
+
firstName: string;
|
|
430
|
+
lastName: string;
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
interface TransformedData {
|
|
435
|
+
edirect_firstname: string;
|
|
436
|
+
edirect_lastname: string;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function transformData(dataSource: DataSource): TransformedData {
|
|
440
|
+
const template = {
|
|
441
|
+
edirect_firstname: "subscriber.firstName",
|
|
442
|
+
edirect_lastname: "subscriber.lastName",
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
const templateModule = new TemplateModule();
|
|
446
|
+
templateModule.setTemplate(template);
|
|
447
|
+
|
|
448
|
+
return templateModule.transformPayload<DataSource, TransformedData>(
|
|
449
|
+
dataSource
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const dataSource = {
|
|
454
|
+
subscriber: {
|
|
455
|
+
firstName: "template",
|
|
456
|
+
lastName: "service",
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
console.log(transformData(dataSource));
|
|
461
|
+
|
|
462
|
+
// {
|
|
463
|
+
// edirect_firstname: "template",
|
|
464
|
+
// edirect_lastname: "service",
|
|
465
|
+
// }
|
|
466
|
+
```
|