Skip to content

Commit d36bc0f

Browse files
committed
docs: more on readme
1 parent e082667 commit d36bc0f

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

README.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ class User extends BaseEntity {
2626

2727
// we never want to give this back in API responses
2828
// maybe it's private, or maybe we don't want consumers to depend on it
29-
privateField: number;
30-
31-
@ApiField()
3229
firstName: string;
30+
lastName: string;
31+
32+
@ApiField() // works just fine on getters
33+
get fullName() {
34+
return `${this.firstName} ${this.lastName}`;
35+
}
3336

3437
// here, we only want the API Fields of Permission in the nested field
3538
@ApiField(() => Permission)
@@ -39,7 +42,12 @@ class User extends BaseEntity {
3942
}
4043
```
4144

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`.
4351

4452
```typescript
4553
import { getApiFields } from '@lcdev/api-fields';
@@ -52,14 +60,40 @@ const extraction = getApiFields(User);
5260
const trimmedFields = extract(fullFields, extraction);
5361
```
5462

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.
6084

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+
```
6397

6498
### Alternatives
6599
- [class-transformer](https://github.com/typestack/class-transformer)

0 commit comments

Comments
 (0)