@@ -26,10 +26,13 @@ class User extends BaseEntity {
26
26
27
27
// we never want to give this back in API responses
28
28
// maybe it's private, or maybe we don't want consumers to depend on it
29
- privateField: number ;
30
-
31
- @ApiField ()
32
29
firstName: string ;
30
+ lastName: string ;
31
+
32
+ @ApiField () // works just fine on getters
33
+ get fullName() {
34
+ return ` ${this .firstName } ${this .lastName } ` ;
35
+ }
33
36
34
37
// here, we only want the API Fields of Permission in the nested field
35
38
@ApiField (() => Permission )
@@ -39,7 +42,12 @@ class User extends BaseEntity {
39
42
}
40
43
```
41
44
42
- To reveal the 'Extraction' object that can be used by ` @lcdev/mapper ` :
45
+ Under the hood, this creates a listing of the fields you want to expose. We
46
+ call them "API Fields" because this is usually the way you expose fields in
47
+ JSON API responses.
48
+
49
+ We can get that metadata about any given class with the ` getAPIFields ` function.
50
+ The object returned can actually be used directly in ` @lcdev/mapper ` .
43
51
44
52
``` typescript
45
53
import { getApiFields } from ' @lcdev/api-fields' ;
@@ -52,14 +60,40 @@ const extraction = getApiFields(User);
52
60
const trimmedFields = extract (fullFields , extraction );
53
61
```
54
62
55
- Decorator possibilities:
56
- - ` @ApiField() property ` means take all of ` property `
57
- - ` @ApiField(() => PropertyType) property ` means take ApiFields of ` property `
58
- - ` @ApiField(() => [PropertyType]) property[] ` means take ApiFields of all ` property ` s
59
- - ` @ApiField({ ... }) property ` means take ` { ... } ` from ` property `
63
+ ### Formats
64
+ - ` @ApiField() propName ` : extract ` propName ` as-is
65
+ - ` @ApiField(() => PropertyType) propName ` : extract the ApiFields of ` PropertyType ` as ` propName `
66
+ - ` @ApiField(() => [PropertyType]) propName[] ` : map as array, extracting ApiFields of each element
67
+ - ` @ApiField({ ... }) propName ` : extract fields from ` propName ` (same as ` @lcdev/mapper ` )
68
+ - ` @ApiField(false) propName ` : don't include this field
69
+
70
+ ### Renames
71
+ Renaming a field is supported, in the same way it is in ` @lcdev/mapper ` .
72
+
73
+ ``` typescript
74
+ import { ApiField } from ' @lcdev/api-fields' ;
75
+ import { rename } from ' @lcdev/mapper' ;
76
+
77
+ class ImageUpload {
78
+ @ApiField (rename (' url' ))
79
+ awsURL: string ;
80
+ }
81
+ ```
82
+
83
+ When being extracted, the field will be renamed.
60
84
61
- You might want to create middleware in your router to do this type of extraction for you.
62
- Internally at Launchcode we do just that, and would like to open-source that effort as well.
85
+ ### Transforms
86
+ Transforming a field is supported, in the same way it is in ` @lcdev/mapper ` .
87
+
88
+ ``` typescript
89
+ import { ApiField } from ' @lcdev/api-fields' ;
90
+ import { transform } from ' @lcdev/mapper' ;
91
+
92
+ class ImageUpload {
93
+ @ApiField (transform (v => v .replace (' https:' , ' ' )))
94
+ awsURL: string ;
95
+ }
96
+ ```
63
97
64
98
### Alternatives
65
99
- [ class-transformer] ( https://github.com/typestack/class-transformer )
0 commit comments