Skip to content

Commit a7d8e65

Browse files
manomintismanomintis
authored andcommitted
Update documentation
1 parent f6aa583 commit a7d8e65

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11

22
# Change Log
33
All notable changes to this project will be documented in this file.
4+
5+
## [2.0.0] - 2024-06-19
46

7+
### Added
8+
- pick() for picking certain values only
9+
- omit() for excluding certain values from results
10+
- redefine() for composing results according to manually defined map
11+
- paginate() and refinePaginate() for offset-based paginating of the results
12+
- Method chaining support
13+
514
## [1.1.0] - 2024-04-03
615

716
### Added

README.md

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,57 @@ Install the package:
1414
npm install adonis-api-resources
1515
```
1616

17-
1817
Configure the package:
1918

2019
```sh
2120
node ace configure adonis-api-resources
2221
```
2322

24-
25-
## Usage
26-
27-
28-
Generate a new resource:
23+
Generate a new resource (i.e. for a user):
2924

3025
```sh
3126
node ace make:resource user
3227
```
3328

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-
4629
Import your generated resource before using it, i.e. in a controller:
4730

4831
```typescript
4932
import UserResource from '#resources/user_resource'
5033
```
5134

52-
5335
Remove old endpoint return declaration:
5436

5537
```typescript
5638
return user
5739
```
5840

59-
60-
Use the your generated resource instead:
41+
Use the your generated resource instead (either of them):
6142

6243
```typescript
6344
return new UserResource(user).refine()
6445
```
6546

47+
## redefine() and refine() - manually defined map
6648

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:
6852

6953
```typescript
70-
return new UserResource(users).refine()
54+
...
55+
return {
56+
full_name: data.firstName + ' ' + data.lastName,
57+
}
58+
...
7159
```
7260

73-
That's it. Enjoy yourself!
74-
61+
You may also use arrays of models, with resources:
7562

76-
## Expected output examples
63+
```typescript
64+
return new UserResource(users).refine()
65+
```
7766

67+
### Output examples
7868

7969
Model:
8070

@@ -84,7 +74,6 @@ Model:
8474
}
8575
```
8676

87-
8877
Array of models:
8978

9079
```json
@@ -98,17 +87,46 @@ Array of models:
9887
]
9988
```
10089

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:
93+
94+
```typescript
95+
return new UserResource(users).refinePaginate(page, limit)
96+
return new UserResource(users).redefine().paginate(page, limit)
97+
```
10198

102-
## Pagination support
99+
Arguments:
103100

101+
page - optional (default value is 1)
104102

105-
Offset-based pagination is supported. Example usage:
103+
limit - optional (default value is 10)
104+
105+
## pick()
106+
107+
No map definition is needed, once you only want to pick a few values of the object and leave other values behind. Examples:
106108

107109
```typescript
108-
const users = await User.query().paginate(1, 10)
109-
return new UserResource(users).refine()
110+
return new UserResource(users).pick('firstName').get()
111+
return new UserResource(users).pick('firstName', 'lastName', 'email').paginate(1, 20)
110112
```
111113

114+
## omit()
115+
116+
No map definition is needed, once you only want to exclude a few values from the object. Examples:
117+
118+
```typescript
119+
return new UserResource(users).omit('id').paginate()
120+
return new UserResource(users).omit('createdAt', 'updatedAt').get()
121+
```
122+
123+
## get()
124+
125+
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()
126+
127+
## Lucid pagination support
128+
129+
Lucid offset-based pagination is supported.
112130

113131
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/manomintis/adonis-api-resources/test.yml?style=for-the-badge
114132
[gh-workflow-url]: https://github.com/manomintis/adonis-api-resources/actions/workflows/test.yml "Github action"

0 commit comments

Comments
 (0)