@edirect/template 1.0.2 → 1.0.3
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 +229 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,7 +63,7 @@ To map a value that you'd like to use some transformer feature, you can use thes
|
|
|
63
63
|
- `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
64
|
- `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
65
|
|
|
66
|
-
|
|
66
|
+
##### File name: baseTransformers.ts
|
|
67
67
|
|
|
68
68
|
```javascript
|
|
69
69
|
import { ITransformer, ITransformerParams } from "@edirect/template";
|
|
@@ -89,7 +89,7 @@ const transformers: ITransformer = {
|
|
|
89
89
|
export default transformers;
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
##### File name: index.ts
|
|
93
93
|
|
|
94
94
|
```javascript
|
|
95
95
|
import { TemplateModule } from "@edirect/template";
|
|
@@ -140,3 +140,230 @@ console.log(result);
|
|
|
140
140
|
// timeZone: "Time zone in Porto (GMT+1)",
|
|
141
141
|
// }
|
|
142
142
|
```
|
|
143
|
+
|
|
144
|
+
### Example with nested object + transformers
|
|
145
|
+
|
|
146
|
+
`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.
|
|
147
|
+
|
|
148
|
+
##### File name: baseTransformers.ts
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
import { ITransformer, ITransformerParams } from "@edirect/template";
|
|
152
|
+
|
|
153
|
+
const upperCase = ({ value }: ITransformerParams): string | null => {
|
|
154
|
+
return value ? String(value).toUpperCase() : null;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const transformers: ITransformer = {
|
|
158
|
+
upperCase,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export default transformers;
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
##### File name: index.ts
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
import { TemplateModule } from "@edirect/template";
|
|
168
|
+
import baseTransformers from "./baseTransformers";
|
|
169
|
+
|
|
170
|
+
const template = {
|
|
171
|
+
order: {
|
|
172
|
+
date: "order.date",
|
|
173
|
+
value: "order.value",
|
|
174
|
+
subscriber: {
|
|
175
|
+
name: "sub.name",
|
|
176
|
+
phone: "sub.phone",
|
|
177
|
+
email: {
|
|
178
|
+
fields: ["email", "sub.email"],
|
|
179
|
+
},
|
|
180
|
+
address: {
|
|
181
|
+
street: "sub.add.stt",
|
|
182
|
+
number: "sub.add.num",
|
|
183
|
+
city: {
|
|
184
|
+
fields: ["sub.add.city"],
|
|
185
|
+
transformer: "upperCase",
|
|
186
|
+
},
|
|
187
|
+
state: {
|
|
188
|
+
fields: ["sub.add.stt"],
|
|
189
|
+
transformer: "upperCase",
|
|
190
|
+
},
|
|
191
|
+
zip: {
|
|
192
|
+
fields: ["sub.add.zip"],
|
|
193
|
+
defaultValue: "0000-000",
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const dataSource = {
|
|
201
|
+
order: {
|
|
202
|
+
value: 1000.0,
|
|
203
|
+
date: "2000-01-01",
|
|
204
|
+
},
|
|
205
|
+
sub: {
|
|
206
|
+
name: "name-test",
|
|
207
|
+
phone: "999999999",
|
|
208
|
+
email: "template.service@bolltech.io",
|
|
209
|
+
add: {
|
|
210
|
+
st: "st-test",
|
|
211
|
+
num: 100,
|
|
212
|
+
city: "city-test",
|
|
213
|
+
stt: "state-test",
|
|
214
|
+
zip: "zip-test",
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const templateModule = new TemplateModule();
|
|
220
|
+
|
|
221
|
+
templateModule.setTemplate(template);
|
|
222
|
+
templateModule.setContext(dataSource);
|
|
223
|
+
templateModule.setTransformers(baseTransformers);
|
|
224
|
+
|
|
225
|
+
const result = templateModule.transformPayload(dataSource);
|
|
226
|
+
console.log(result);
|
|
227
|
+
|
|
228
|
+
// {
|
|
229
|
+
// order: {
|
|
230
|
+
// date: "2000-01-01",
|
|
231
|
+
// value: 1000,
|
|
232
|
+
// subscriber: {
|
|
233
|
+
// name: "name-test",
|
|
234
|
+
// phone: "999999999",
|
|
235
|
+
// email: "template.service@bolltech.io",
|
|
236
|
+
// address: {
|
|
237
|
+
// street: "state-test",
|
|
238
|
+
// number: 100,
|
|
239
|
+
// city: "CITY-TEST",
|
|
240
|
+
// state: "STATE-TEST",
|
|
241
|
+
// zip: "zip-test",
|
|
242
|
+
// },
|
|
243
|
+
// },
|
|
244
|
+
// },
|
|
245
|
+
// }
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Example with arrays + transformers
|
|
249
|
+
|
|
250
|
+
`Note`: When it comes to arrays mapper, we need to have in mind that is required use this two keys: arraySource and arrayTemplate.
|
|
251
|
+
|
|
252
|
+
- `arraySource`: source path where the engine will seek the information to mapper
|
|
253
|
+
- `arrayTemplate`: template that will be used for each object within the array
|
|
254
|
+
|
|
255
|
+
##### File name: baseTransformers.ts
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
import { ITransformer, ITransformerParams } from "@edirect/template";
|
|
259
|
+
|
|
260
|
+
const upperCase = ({ value }: ITransformerParams): string | null => {
|
|
261
|
+
return value ? String(value).toUpperCase() : null;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const transformers: ITransformer = {
|
|
265
|
+
upperCase,
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export default transformers;
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
##### File name: index.ts
|
|
272
|
+
|
|
273
|
+
```javascript
|
|
274
|
+
import { TemplateModule } from "@edirect/template";
|
|
275
|
+
import baseTransformers from "./baseTransformers";
|
|
276
|
+
|
|
277
|
+
const template = {
|
|
278
|
+
quote: {
|
|
279
|
+
orders: {
|
|
280
|
+
arraySource: "order",
|
|
281
|
+
arrayTemplate: {
|
|
282
|
+
value: "value",
|
|
283
|
+
date: "date",
|
|
284
|
+
products: {
|
|
285
|
+
arraySource: "products",
|
|
286
|
+
arrayTemplate: {
|
|
287
|
+
id: "id",
|
|
288
|
+
value: "value",
|
|
289
|
+
description: {
|
|
290
|
+
fields: ["description"],
|
|
291
|
+
transformer: "upperCase",
|
|
292
|
+
defaultValue: "Default description",
|
|
293
|
+
},
|
|
294
|
+
categories: "categories",
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const dataSource = {
|
|
303
|
+
order: [
|
|
304
|
+
{
|
|
305
|
+
value: 1000.0,
|
|
306
|
+
date: "2000-01-01",
|
|
307
|
+
products: [
|
|
308
|
+
{
|
|
309
|
+
id: "id-test-1",
|
|
310
|
+
value: 1000,
|
|
311
|
+
description: "description-test 1",
|
|
312
|
+
categories: ["category-1"],
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
id: "id-test-2",
|
|
316
|
+
value: 2000,
|
|
317
|
+
description: "description-test 2",
|
|
318
|
+
categories: ["category-1", "category-2"],
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
id: "id-test-3",
|
|
322
|
+
value: 3000,
|
|
323
|
+
categories: ["category-1", "category-2", "category-3"],
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const templateModule = new TemplateModule();
|
|
331
|
+
|
|
332
|
+
templateModule.setTemplate(template);
|
|
333
|
+
templateModule.setContext(dataSource);
|
|
334
|
+
templateModule.setTransformers(baseTransformers);
|
|
335
|
+
|
|
336
|
+
const result = templateModule.transformPayload(dataSource);
|
|
337
|
+
console.log(result);
|
|
338
|
+
|
|
339
|
+
//{
|
|
340
|
+
// quote: {
|
|
341
|
+
// orders: [
|
|
342
|
+
// {
|
|
343
|
+
// value: 1000,
|
|
344
|
+
// date: "2000-01-01",
|
|
345
|
+
// products: [
|
|
346
|
+
// {
|
|
347
|
+
// id: "id-test-1",
|
|
348
|
+
// value: 1000,
|
|
349
|
+
// description: "DESCRIPTION-TEST 1",
|
|
350
|
+
// categories: ["category-1"],
|
|
351
|
+
// },
|
|
352
|
+
// {
|
|
353
|
+
// id: "id-test-2",
|
|
354
|
+
// value: 2000,
|
|
355
|
+
// description: "DESCRIPTION-TEST 2",
|
|
356
|
+
// categories: ["category-1", "category-2"],
|
|
357
|
+
// },
|
|
358
|
+
// {
|
|
359
|
+
// id: "id-test-3",
|
|
360
|
+
// value: 3000,
|
|
361
|
+
// description: "Default description",
|
|
362
|
+
// categories: ["category-1", "category-2", "category-3"],
|
|
363
|
+
// },
|
|
364
|
+
// ],
|
|
365
|
+
// },
|
|
366
|
+
// ],
|
|
367
|
+
// },
|
|
368
|
+
// }
|
|
369
|
+
```
|