Skip to content

Commit 1ad80d5

Browse files
committed
Added types and docs for new wrap() method
1 parent aaad116 commit 1ad80d5

File tree

4 files changed

+91
-18
lines changed

4 files changed

+91
-18
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

docs/content/en/configuration.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ category: Getting Started
99

1010
See the [API reference](/api/model-options) for a list of available options.
1111

12-
The first step is to create a base model to define the default options, in order to abstract configuration
13-
from your models. It should extend the
12+
The first step is to create a base model to define the default options, in order to abstract configuration
13+
from your models. It should extend the
1414
[Base Model](https://github.com/robsontenorio/vue-api-query/blob/master/src/Model.js) of [vue-api-query](https://github.com/robsontenorio/vue-api-query).
1515

1616
The base model must implement two methods:
17+
1718
- `baseURL` - The base url of your REST API.
1819
- `request` - The default request method.
1920

@@ -43,9 +44,11 @@ export default class Model extends BaseModel {
4344
Now let's create our domain models that extends the base model. We can create as many models as we like.
4445

4546
Each model must implement:
47+
4648
- `resource` - The resource route of the model.
4749

4850
We can create a **User** model like this:
51+
4952
```js{}[~/models/User.js]
5053
import Model from './Model'
5154
@@ -71,7 +74,7 @@ export default class User extends Model {
7174
}
7275
7376
// Computed properties are reactive -> user.fullName
74-
// Make sure to use "get" prefix
77+
// Make sure to use "get" prefix
7578
get fullName () {
7679
return `${this.firstname} ${this.lastname}`
7780
}
@@ -90,6 +93,7 @@ export default class User extends Model {
9093
If we are working on a Typescript project, we can infer the types of the fields, so we have intellisense.
9194

9295
#### Directly in Model
96+
9397
```ts{}[~/models/User.ts]
9498
import Model from './Model'
9599
@@ -103,6 +107,7 @@ export default class User extends Model {
103107
```
104108

105109
#### Using an Interface
110+
106111
```ts{}[~/models/User.ts]
107112
import Model from './Model'
108113
@@ -149,6 +154,34 @@ export default class Post extends Model {
149154

150155
This **Post** model will build the query using the `slug` as primary key: `/posts/{slug}`
151156

157+
## Changing the Wrapper
158+
159+
<alert type="info">By default, the `wrap` is set to `null`.</alert>
160+
161+
See the [API reference](/api/model-options#wrap)
162+
163+
It's possible to change the wrapper of a model response by implementing the `wrap` method.
164+
This way, the specified key will be used to unwrap the data.
165+
166+
Let's create a **Post** model and set its wrap key to `data`.
167+
168+
```js{}[~/models/Post.js]
169+
import Model from './Model'
170+
171+
export default class Post extends Model {
172+
// Set the resource route of the model
173+
resource() {
174+
return 'posts'
175+
}
176+
177+
// Define the primary key of the model
178+
wrap() {
179+
return 'data'
180+
}
181+
```
182+
183+
This **Post** model will now look inside the API response top level `data` property when retrieving data and initializing your models.
184+
152185
## Defining Relationships
153186

154187
It's also possible to define the relationships of our models. By doing this, model instances will be automatically
@@ -158,7 +191,7 @@ applied to relationships, giving you access to all of their features.
158191

159192
See the [API reference](/api/model-options#relations)
160193

161-
For relationships that have been eager loaded, we only need to implement the `relations` method
194+
For relationships that have been eager loaded, we only need to implement the `relations` method
162195
to apply their model instances. It works for collections too.
163196

164197
The `relations` method must return an object, which the key is the property of the relationship, and the value is the
@@ -191,7 +224,7 @@ export default class Post extends Model {
191224
}
192225
```
193226

194-
Now we can easily access an instance of the **User** model containing the eager loaded data
227+
Now we can easily access an instance of the **User** model containing the eager loaded data
195228
using the specified key: `post.user`
196229

197230
The `relations` method also support nested keys, by dot notation:
@@ -247,7 +280,7 @@ export default class User extends Model {
247280
}
248281
249282
// Computed properties are reactive -> user.fullName
250-
// Make sure to use "get" prefix
283+
// Make sure to use "get" prefix
251284
get fullName () {
252285
return `${this.firstname} ${this.lastname}`
253286
}
@@ -290,7 +323,7 @@ export default class Model extends BaseModel {
290323
const customParams = {
291324
include: 'include_custom'
292325
}
293-
326+
294327
return { ...defaultParams, ...customParams }
295328
}
296329
}
@@ -300,10 +333,10 @@ export default class Model extends BaseModel {
300333

301334
See the [API reference](/api/model-options#stringifyOptions) and [qs](https://github.com/ljharb/qs#stringifying)
302335

303-
We may also need to configure the parser to match our needs. By default, it is configured to match
336+
We may also need to configure the parser to match our needs. By default, it is configured to match
304337
`spatie/laravel-query-builder`, which uses `comma` array format.
305338

306-
If we want, for example, to change this behaviour to `indices`, we can configure the stringify options of `qs`
339+
If we want, for example, to change this behaviour to `indices`, we can configure the stringify options of `qs`
307340
by overriding the `stringifyOptions` method.
308341

309342
We can globally configure this in the [Base Model](/configuration#creating-a-base-model):
@@ -329,10 +362,10 @@ export default class Model extends BaseModel {
329362
const customParams = {
330363
include: 'include_custom'
331364
}
332-
365+
333366
return { ...defaultParams, ...customParams }
334367
}
335-
368+
336369
// Configure qs
337370
stringifyOptions() {
338371
return {
@@ -373,17 +406,17 @@ export default class Model extends BaseModel {
373406
const customParams = {
374407
include: 'include_custom'
375408
}
376-
409+
377410
return { ...defaultParams, ...customParams }
378411
}
379-
412+
380413
// Configure qs
381414
stringifyOptions() {
382415
return {
383416
arrayFormat: 'indices'
384417
}
385418
}
386-
419+
387420
// Configure object-to-formadata
388421
formData() {
389422
return {

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ export class Model extends StaticModel {
368368
*/
369369
primaryKey (): string
370370

371+
/**
372+
* The "data" wrapper that should be checked when retrieving models
373+
*
374+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/model-options#wrap|API Reference}
375+
* @see {@link https://robsontenorio.github.io/vue-api-query/configuration#changing-the-wrapper|Configuration}
376+
*/
377+
wrap (): string
378+
371379
/**
372380
* This method can be used to lazy load relationships of a model and apply model instances to them.
373381
*

src/Model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export default class Model extends StaticModel {
5757
}
5858

5959
/**
60-
* The "data" wrapper that should be applied.
60+
* The "data" wrapper that should be checked when retrieving models
6161
*
6262
* @return {string|null}
6363
*/
6464
wrap() {
65-
return ''
65+
return null
6666
}
6767

6868
/**

0 commit comments

Comments
 (0)