Skip to content

Commit 6917338

Browse files
authored
feat(model): add method wrapper to define the api response wrapper / overrides (#281)
- Added wrap, _unwrap, wrappedBy, and nowrap methods to Model.js to mimic Laravel's resource data wrapping. This makes for `$first`, `$find`, `$get`, and `$all` obsolete but I kept them for backward compatibility. The the overloadable `wrap()` allows you to automatically unwrap using the standard `first`, `find`, `get`, `all` methods but also change what the wrapper key is in case you don't want to use the standard 'data' property - Added unit tests for the new wrap() method - Added new `wrap()` type - Added new `wrappedBy(wrap)` and `nowrap()` builder methods to change / disable wrapping - Updated documentation for new `wrap()` type - Updated documentation for new `wrappedBy()` and `nowrap()` methods
1 parent 2bb3f4b commit 6917338

File tree

7 files changed

+316
-47
lines changed

7 files changed

+316
-47
lines changed

docs/content/en/api/model-options.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ category: API
77

88
## Global Options
99

10-
It's recommended to define the global options in your [Base Model](/configuration#creating-a-base-model),
10+
It's recommended to define the global options in your [Base Model](/configuration#creating-a-base-model),
1111
in order to abstract configuration from your models.
1212

1313
### `$http`
14+
1415
- Returns: `HTTP Client Instance`
1516

1617
Instance of the HTTP client which is used to make requests.
1718

1819
See [Installation](/installation)
1920

2021
### `baseURL`
22+
2123
- Returns: `string`
2224

2325
Base URL which is used and prepended to make requests.
@@ -31,6 +33,7 @@ baseURL() {
3133
```
3234

3335
### `request`
36+
3437
- Arguments: `(config)`
3538
- Returns: `HTTP Client Request`
3639

@@ -45,6 +48,7 @@ request(config) {
4548
```
4649

4750
### `parameterNames`
51+
4852
- Returns: `object`
4953

5054
This method can be overridden in the model to customize the name of the query parameters.
@@ -66,34 +70,42 @@ parameterNames() {
6670
```
6771

6872
#### `include`
73+
6974
- Default: `include`
7075
- Returns: `string`
7176

7277
#### `filter`
78+
7379
- Default: `filter`
7480
- Returns: `string`
7581

7682
#### `sort`
83+
7784
- Default: `sort`
7885
- Returns: `string`
7986

8087
#### `fields`
88+
8189
- Default: `fields`
8290
- Returns: `string`
8391

8492
#### `append`
93+
8594
- Default: `append`
8695
- Returns: `string`
8796

8897
#### `page`
98+
8999
- Default: `page`
90100
- Returns: `string`
91101

92102
#### `limit`
103+
93104
- Default: `limit`
94105
- Returns: `string`
95106

96107
### `formData`
108+
97109
- Returns: `object`
98110

99111
This method can be overridden in the model to configure `object-to-formdata`.
@@ -112,6 +124,7 @@ formData() {
112124
```
113125

114126
### `stringifyOptions`
127+
115128
- Default: `{ encode: false, arrayFormat: 'comma' }`
116129
- Returns: `object`
117130

@@ -133,6 +146,7 @@ stringifyOptions() {
133146
These are model-related options.
134147

135148
### `resource`
149+
136150
- Returns: `string`
137151

138152
Resource route of the model which is used to build the query.
@@ -146,6 +160,7 @@ resource() {
146160
```
147161

148162
### `primaryKey`
163+
149164
- Default: `id`
150165
- Returns: `string`
151166

@@ -159,10 +174,26 @@ primaryKey() {
159174
}
160175
```
161176

177+
### `wrap`
178+
179+
- Default: `null`
180+
- Returns: `string`
181+
182+
The "data" wrapper that should be checked when retrieving models.
183+
184+
See [Configuration](/configuration#changing-the-wrapper)
185+
186+
```js
187+
wrap() {
188+
return 'data'
189+
}
190+
```
191+
162192
### `relations`
193+
163194
- Returns: `object`
164195

165-
This method can be implemented in the model to apply model instances to eager loaded relationships.
196+
This method can be implemented in the model to apply model instances to eager loaded relationships.
166197
It works for collections too.
167198

168199
It must return an object, which the key is the property of the relationship, and the value is the
@@ -179,6 +210,7 @@ relations() {
179210
```
180211

181212
### `hasMany`
213+
182214
- Arguments: `(model)`
183215
- Returns: `Model`
184216

0 commit comments

Comments
 (0)