Skip to content

Commit d567730

Browse files
authored
Merge pull request #6 from mindedge/dev
feat(model): add method wrappedBy and nowrap to define single request wrapper / overrides
2 parents e26d689 + bfe7cd1 commit d567730

File tree

5 files changed

+142
-32
lines changed

5 files changed

+142
-32
lines changed

docs/content/en/api/query-builder-methods.md

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ category: API
66
---
77

88
## `include`
9+
910
- Arguments: `(...args)`
1011
- Returns: `self`
1112

@@ -26,6 +27,7 @@ await Model.include(['user', 'category'])
2627
<alert type="info">`with` is an alias of this method.</alert>
2728

2829
## `append`
30+
2931
- Arguments: `(...args)`
3032
- Returns: `self`
3133

@@ -44,17 +46,20 @@ await Model.append(['likes', 'shares'])
4446
```
4547

4648
## `select`
49+
4750
- Arguments: `(...fields)`
4851
- Returns: `self`
4952

5053
Set the columns to be selected.
5154

5255
#### Single entity
56+
5357
```js
5458
await Model.select(['title', 'content'])
5559
```
5660

5761
#### Related entities
62+
5863
```js
5964
await Post.select({
6065
posts: ['title', 'content'],
@@ -63,6 +68,7 @@ await Post.select({
6368
```
6469

6570
## `where`
71+
6672
- Arguments: `(field, value)`
6773
- Returns: `self`
6874

@@ -89,6 +95,7 @@ await Model.where({ user: { status: 'active' } })
8995
```
9096

9197
## `whereIn`
98+
9299
- Arguments: `(field, array)`
93100
- Returns: `self`
94101

@@ -115,24 +122,26 @@ await Model.where({ user: { id: [1, 2, 3] } })
115122
```
116123

117124
## `orderBy`
125+
118126
- Arguments: `(...args)`
119127
- Returns: `self`
120128

121129
Add an "order by" clause to the query.
122130

123131
```js
124-
await Model.orderBy('-created_at', 'category_id')
132+
await Model.orderBy('-created_at', 'category_id')
125133
```
126134

127135
#### Array
128136

129137
<alert type="success">Available in version >= v1.8.0</alert>
130138

131139
```js
132-
await Model.orderBy(['-created_at', 'category_id'])
140+
await Model.orderBy(['-created_at', 'category_id'])
133141
```
134142

135143
## `page`
144+
136145
- Arguments: `(value)`
137146
- Returns: `self`
138147

@@ -143,6 +152,7 @@ await Model.page(1)
143152
```
144153

145154
## `limit`
155+
146156
- Arguments: `(value)`
147157
- Returns: `self`
148158

@@ -153,6 +163,7 @@ await Model.limit(20)
153163
```
154164

155165
## `params`
166+
156167
- Arguments: `(payload)`
157168
- Returns: `self`
158169

@@ -161,24 +172,25 @@ Add custom parameters to the query.
161172
<code-group>
162173
<code-block Label="Query" active>
163174

164-
```js
165-
await Model.params({
166-
foo: 'bar',
167-
baz: true
168-
})
169-
```
175+
```js
176+
await Model.params({
177+
foo: 'bar',
178+
baz: true
179+
})
180+
```
170181

171182
</code-block>
172183
<code-block Label="Request">
173184

174-
```http request
175-
GET /resource?foo=bar&baz=true
176-
```
185+
```http request
186+
GET /resource?foo=bar&baz=true
187+
```
177188

178189
</code-block>
179190
</code-group>
180191

181192
## `when`
193+
182194
<alert type="success">Available in version >= v1.10.0</alert>
183195

184196
- Arguments: `(value, callback)`
@@ -192,7 +204,33 @@ const search = 'foo'
192204
await Model.when(search, (query, value) => query.where('search', value))
193205
```
194206

207+
## `wrappedBy`
208+
209+
<alert type="success">Available in version >= v1.14.0</alert>
210+
211+
- Arguments: `(value)`
212+
- Returns: `self`
213+
214+
Change the `wrap()` data wrapper for this request.
215+
216+
```js
217+
await Model.wrappedBy('somewrapper').get()
218+
```
219+
220+
## `nowrap`
221+
222+
<alert type="success">Available in version >= v1.14.0</alert>
223+
224+
- Returns: `self`
225+
226+
Remove the `wrap()` data wrapper for this request to return the raw response.
227+
228+
```js
229+
await Model.nowrap().get()
230+
```
231+
195232
## `custom`
233+
196234
- Arguments: `(...args)`
197235
- Returns: `self`
198236

@@ -201,38 +239,39 @@ Build custom endpoints.
201239
<code-group>
202240
<code-block Label="Simple Query" active>
203241

204-
```js
205-
await Post.custom('posts/latest')
206-
```
242+
```js
243+
await Post.custom('posts/latest')
244+
```
207245

208246
</code-block>
209247
<code-block Label="Simple Request">
210248

211-
```http request
212-
GET /posts/latest
213-
```
249+
```http request
250+
GET /posts/latest
251+
```
214252

215253
</code-block>
216254
<code-block Label="Complex Query">
217255

218-
```js
219-
const user = new User({ id: 1 })
220-
const post = new Post()
256+
```js
257+
const user = new User({ id: 1 })
258+
const post = new Post()
221259

222-
await Post.custom(user, post, 'latest')
223-
```
260+
await Post.custom(user, post, 'latest')
261+
```
224262

225263
</code-block>
226264
<code-block Label="Complex Request">
227265

228-
```http request
229-
GET /users/1/posts/latest
230-
```
266+
```http request
267+
GET /users/1/posts/latest
268+
```
231269

232270
</code-block>
233271
</code-group>
234272

235273
## `config`
274+
236275
<alert type="success">Available in version >= v1.8.0</alert>
237276

238277
- Arguments: `(config)`
@@ -243,12 +282,15 @@ Configuration of HTTP Instance.
243282
```js
244283
await Model.config({
245284
method: 'PATCH',
246-
header: { /* ... */ },
285+
header: {
286+
/* ... */
287+
},
247288
data: { foo: 'bar' }
248289
}).save()
249290
```
250291

251292
## `get`
293+
252294
- Returns: `Collection | { data: Collection }`
253295

254296
Execute the query and get all results.
@@ -260,6 +302,7 @@ await Model.get()
260302
<alert type="info">`all` is an alias of this method.</alert>
261303

262304
## `first`
305+
263306
- Returns: `Model | { data: Model }`
264307

265308
Execute the query and get the first result.
@@ -269,6 +312,7 @@ await Model.first()
269312
```
270313

271314
## `find`
315+
272316
- Arguments: `(identifier)`
273317
- Returns: `Model | { data: Model }`
274318

@@ -279,6 +323,7 @@ await Model.find(1)
279323
```
280324

281325
## `$get`
326+
282327
- Returns: `Collection`
283328

284329
Execute the query and get all results.
@@ -293,6 +338,7 @@ They handle and unwrap responses within "data".</alert>
293338
<alert type="info">`$all` is an alias of this method.</alert>
294339

295340
## `$first`
341+
296342
- Returns: `Model`
297343

298344
Execute the query and get the first result.
@@ -301,10 +347,11 @@ Execute the query and get the first result.
301347
await Model.$first()
302348
```
303349

304-
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
350+
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
305351
They handle and unwrap responses within "data".</alert>
306352

307353
## `$find`
354+
308355
- Arguments: `(identifier)`
309356
- Returns: `Model`
310357

@@ -314,5 +361,5 @@ Find a model by its primary key.
314361
await Model.$find(1)
315362
```
316363

317-
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
364+
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
318365
They handle and unwrap responses within "data".</alert>

docs/content/en/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default class Post extends Model {
174174
return 'posts'
175175
}
176176
177-
// Define the primary key of the model
177+
// Define the response wrapper
178178
wrap() {
179179
return 'data'
180180
}

src/Model.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default class Model extends StaticModel {
1212

1313
if (attributes.length === 0) {
1414
this._builder = new Builder(this)
15+
// Set the default data wrapper
16+
this._wrapper = this.wrap()
1517
} else {
1618
Object.assign(this, ...attributes)
1719
this._applyRelations(this)
@@ -71,9 +73,8 @@ export default class Model extends StaticModel {
7173
* @return {object|array} The unwraped response
7274
*/
7375
_unwrap(response) {
74-
const wrapper = this.wrap()
75-
if (wrapper) {
76-
return response[wrapper] || response
76+
if (this._wrapper) {
77+
return response[this._wrapper] || response
7778
} else {
7879
return response
7980
}
@@ -285,6 +286,26 @@ export default class Model extends StaticModel {
285286
return this
286287
}
287288

289+
/**
290+
* The "data" wrapper that will override the wrap() method
291+
*
292+
* @param {string} wrap The new wrapper for this one request
293+
*
294+
* @return {string|null}
295+
*/
296+
wrappedBy(wrap) {
297+
this._wrapper = wrap
298+
return this
299+
}
300+
301+
/**
302+
* Disable wrapping for this one request
303+
*/
304+
nowrap() {
305+
this._wrapper = null
306+
return this
307+
}
308+
288309
/**
289310
* Result
290311
*/

src/StaticModel.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ export default class StaticModel {
8888
return self
8989
}
9090

91+
static wrappedBy(value) {
92+
let self = this.instance()
93+
self.wrappedBy(value)
94+
95+
return self
96+
}
97+
98+
static nowrap() {
99+
let self = this.instance()
100+
self.nowrap()
101+
102+
return self
103+
}
104+
91105
static custom(...args) {
92106
let self = this.instance()
93107
self.custom(...args)

0 commit comments

Comments
 (0)