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
+49-31Lines changed: 49 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,67 +14,57 @@ Install the package:
14
14
npm install adonis-api-resources
15
15
```
16
16
17
-
18
17
Configure the package:
19
18
20
19
```sh
21
20
node ace configure adonis-api-resources
22
21
```
23
22
24
-
25
-
## Usage
26
-
27
-
28
-
Generate a new resource:
23
+
Generate a new resource (i.e. for a user):
29
24
30
25
```sh
31
26
node ace make:resource user
32
27
```
33
28
34
-
35
-
Edit newly generated app/resources/users_resource.ts to 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:
36
-
37
-
```typescript
38
-
...
39
-
return {
40
-
full_name: data.firstName+''+data.lastName,
41
-
}
42
-
...
43
-
```
44
-
45
-
46
29
Import your generated resource before using it, i.e. in a controller:
47
30
48
31
```typescript
49
32
importUserResourcefrom'#resources/user_resource'
50
33
```
51
34
52
-
53
35
Remove old endpoint return declaration:
54
36
55
37
```typescript
56
38
returnuser
57
39
```
58
40
59
-
60
-
Use the your generated resource instead:
41
+
Use the your generated resource instead (either of them):
61
42
62
43
```typescript
63
44
returnnewUserResource(user).refine()
64
45
```
65
46
47
+
## redefine() and refine() - manually defined map
66
48
67
-
You may also use arrays of models, with resources:
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.
50
+
51
+
Edit newly generated app/resources/user_resource.ts to 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:
68
52
69
53
```typescript
70
-
returnnewUserResource(users).refine()
54
+
...
55
+
return {
56
+
full_name: data.firstName+''+data.lastName,
57
+
}
58
+
...
71
59
```
72
60
73
-
That's it. Enjoy yourself!
74
-
61
+
You may also use arrays of models, with resources:
75
62
76
-
## Expected output examples
63
+
```typescript
64
+
returnnewUserResource(users).refine()
65
+
```
77
66
67
+
### Output examples
78
68
79
69
Model:
80
70
@@ -84,7 +74,6 @@ Model:
84
74
}
85
75
```
86
76
87
-
88
77
Array of models:
89
78
90
79
```json
@@ -98,17 +87,46 @@ Array of models:
98
87
]
99
88
```
100
89
90
+
## paginate() and refinePaginate()
91
+
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()
0 commit comments