You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-29Lines changed: 30 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
## Description
6
6
7
-
Provides a transformation layer between models and actual API endpont responses in JSON format.
7
+
Provides a transformation layer between a model and the actual API endpoint response in JSON format.
8
8
9
9
## Setup
10
10
@@ -20,35 +20,35 @@ Configure the package:
20
20
node ace configure adonis-api-resources
21
21
```
22
22
23
-
Generate a new resource (i.e. for a user):
23
+
Generate a new resource (e.g., for a `User` model):
24
24
25
25
```sh
26
26
node ace make:resource user
27
27
```
28
28
29
-
Import your generated resource before using it, i.e. in a controller:
29
+
Import the generated resource before using it, for example in a controller:
30
30
31
31
```typescript
32
32
importUserResourcefrom'#resources/user_resource'
33
33
```
34
34
35
-
Remove old endpoint return declaration:
35
+
Remove the old endpoint return declaration:
36
36
37
37
```typescript
38
38
returnuser
39
39
```
40
40
41
-
Use the your generated resource instead:
41
+
Use the generated resource instead:
42
42
43
43
```typescript
44
-
returnnewUserResource(user).refine()
44
+
returnnewUserResource(user).remap()
45
45
```
46
46
47
-
## redefine() and refine() - manually defined map
47
+
## remap() - manually defined map
48
48
49
-
redefine() modifies a model or array of models according to your custom map defined in resource file. Used along with get() method, which indicates that data processing is done and provides the result. You can also use refine(), which is an alias of redefine().get(), see example below.
49
+
`remap()` modifies a model or an array of models according to the custom map defined in the resource file.
50
50
51
-
Edit newly generated app/resources/user_resource.tsto create output you need. This example shows how you may output user's full name even if your implementation of user model has separate fields for the first and last names:
51
+
Edit the newly generated `app/resources/user_resource.ts` file to produce the output you need. This example shows how you can output the user's full name, even if your `User`model has separate fields for the first and last names.
52
52
53
53
```typescript
54
54
...
@@ -58,10 +58,10 @@ Edit newly generated app/resources/user_resource.ts to create output you need. T
58
58
...
59
59
```
60
60
61
-
You may also use arrays of models with resources:
61
+
You can also use arrays of models with resources:
62
62
63
63
```typescript
64
-
returnnewUserResource(users).refine()
64
+
returnnewUserResource(users).remap()
65
65
```
66
66
67
67
### Output examples
@@ -87,41 +87,42 @@ Array of models:
87
87
]
88
88
```
89
89
90
-
## paginate() and refinePaginate()
90
+
## pick()
91
+
92
+
No map definition is needed if you only want to pick a few values from the object and leave the rest out.
91
93
92
-
In case you are dealing with not paginateable array you can still create pagination using this extension. refinePaginate() is an alias of redefine().paginate(), see examples below:
get() finalizes data processing. While pick(), omit() and redefine() modify data, trailing get() or paginate() is needed to complete the response. This requirement is not applied to refine() and refinePaginate()
124
+
-**page** — Number of the page to show (optional, default: 1)
125
+
-**limit** — Items per page (optional, default: 10)
0 commit comments