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: docs/content/en/configuration.md
+47-14Lines changed: 47 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,12 @@ category: Getting Started
9
9
10
10
See the [API reference](/api/model-options) for a list of available options.
11
11
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
14
14
[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).
15
15
16
16
The base model must implement two methods:
17
+
17
18
-`baseURL` - The base url of your REST API.
18
19
-`request` - The default request method.
19
20
@@ -43,9 +44,11 @@ export default class Model extends BaseModel {
43
44
Now let's create our domain models that extends the base model. We can create as many models as we like.
44
45
45
46
Each model must implement:
47
+
46
48
-`resource` - The resource route of the model.
47
49
48
50
We can create a **User** model like this:
51
+
49
52
```js{}[~/models/User.js]
50
53
import Model from './Model'
51
54
@@ -71,7 +74,7 @@ export default class User extends Model {
71
74
}
72
75
73
76
// Computed properties are reactive -> user.fullName
74
-
// Make sure to use "get" prefix
77
+
// Make sure to use "get" prefix
75
78
get fullName () {
76
79
return `${this.firstname} ${this.lastname}`
77
80
}
@@ -90,6 +93,7 @@ export default class User extends Model {
90
93
If we are working on a Typescript project, we can infer the types of the fields, so we have intellisense.
91
94
92
95
#### Directly in Model
96
+
93
97
```ts{}[~/models/User.ts]
94
98
import Model from './Model'
95
99
@@ -103,6 +107,7 @@ export default class User extends Model {
103
107
```
104
108
105
109
#### Using an Interface
110
+
106
111
```ts{}[~/models/User.ts]
107
112
import Model from './Model'
108
113
@@ -149,6 +154,34 @@ export default class Post extends Model {
149
154
150
155
This **Post** model will build the query using the `slug` as primary key: `/posts/{slug}`
151
156
157
+
## Changing the Wrapper
158
+
159
+
<alerttype="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
+
152
185
## Defining Relationships
153
186
154
187
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.
158
191
159
192
See the [API reference](/api/model-options#relations)
160
193
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
162
195
to apply their model instances. It works for collections too.
163
196
164
197
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 {
191
224
}
192
225
```
193
226
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
195
228
using the specified key: `post.user`
196
229
197
230
The `relations` method also support nested keys, by dot notation:
@@ -247,7 +280,7 @@ export default class User extends Model {
247
280
}
248
281
249
282
// Computed properties are reactive -> user.fullName
250
-
// Make sure to use "get" prefix
283
+
// Make sure to use "get" prefix
251
284
get fullName () {
252
285
return `${this.firstname} ${this.lastname}`
253
286
}
@@ -290,7 +323,7 @@ export default class Model extends BaseModel {
290
323
const customParams = {
291
324
include: 'include_custom'
292
325
}
293
-
326
+
294
327
return { ...defaultParams, ...customParams }
295
328
}
296
329
}
@@ -300,10 +333,10 @@ export default class Model extends BaseModel {
300
333
301
334
See the [API reference](/api/model-options#stringifyOptions) and [qs](https://github.com/ljharb/qs#stringifying)
302
335
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
304
337
`spatie/laravel-query-builder`, which uses `comma` array format.
305
338
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`
307
340
by overriding the `stringifyOptions` method.
308
341
309
342
We can globally configure this in the [Base Model](/configuration#creating-a-base-model):
@@ -329,10 +362,10 @@ export default class Model extends BaseModel {
329
362
const customParams = {
330
363
include: 'include_custom'
331
364
}
332
-
365
+
333
366
return { ...defaultParams, ...customParams }
334
367
}
335
-
368
+
336
369
// Configure qs
337
370
stringifyOptions() {
338
371
return {
@@ -373,17 +406,17 @@ export default class Model extends BaseModel {
0 commit comments