@edirect/template 1.0.3 → 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 +98 -1
- 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
|
|
@@ -367,3 +418,49 @@ console.log(result);
|
|
|
367
418
|
// },
|
|
368
419
|
// }
|
|
369
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
|
+
```
|