vue-api-query
Advanced tools
Comparing version
@@ -102,13 +102,29 @@ 'use strict'; | ||
key: 'for', | ||
value: function _for(object) { | ||
if (object instanceof Model === false) { | ||
throw new Error('The object referenced on for() method is not a valid Model.'); | ||
value: function _for() { | ||
var _this2 = this; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
if (!this.isValidId(object.getPrimaryKey())) { | ||
throw new Error('The object referenced on for() method has a invalid id.'); | ||
if (args.length === 0) { | ||
throw new Error('The for() method takes a minimum of one argument.'); | ||
} | ||
var url = this.baseURL() + '/' + object.resource() + '/' + object.getPrimaryKey() + '/' + this.resource(); | ||
var url = '' + this.baseURL(); | ||
args.forEach(function (object) { | ||
if (object instanceof Model === false) { | ||
throw new Error('The object referenced on for() method is not a valid Model.'); | ||
} | ||
if (!_this2.isValidId(object.getPrimaryKey())) { | ||
throw new Error('The object referenced on for() method has a invalid id.'); | ||
} | ||
url += '/' + object.resource() + '/' + object.getPrimaryKey(); | ||
}); | ||
url += '/' + this.resource(); | ||
this._from(url); | ||
@@ -250,3 +266,3 @@ | ||
value: function find(identifier) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -263,3 +279,3 @@ if (identifier === undefined) { | ||
}).then(function (response) { | ||
return new _this2.constructor(response.data); | ||
return new _this3.constructor(response.data); | ||
}); | ||
@@ -270,3 +286,3 @@ } | ||
value: function get() { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -285,5 +301,5 @@ var base = this._fromResource || this.baseURL() + '/' + this.resource(); | ||
collection = collection.map(function (c) { | ||
var item = new _this3.constructor(c); | ||
var item = new _this4.constructor(c); | ||
Object.defineProperty(item, '_fromResource', { get: function get() { | ||
return _this3._fromResource; | ||
return _this4._fromResource; | ||
} }); | ||
@@ -337,3 +353,3 @@ | ||
value: function _create() { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -345,3 +361,3 @@ return this.request({ | ||
}).then(function (response) { | ||
var self = Object.assign(_this4, response.data); | ||
var self = Object.assign(_this5, response.data); | ||
return self; | ||
@@ -353,3 +369,3 @@ }); | ||
value: function _update() { | ||
var _this5 = this; | ||
var _this6 = this; | ||
@@ -361,3 +377,3 @@ return this.request({ | ||
}).then(function (response) { | ||
var self = Object.assign(_this5, response.data); | ||
var self = Object.assign(_this6, response.data); | ||
return self; | ||
@@ -364,0 +380,0 @@ }); |
{ | ||
"name": "vue-api-query", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "💎 Elegant and simple way to build requests for REST API", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -468,2 +468,16 @@ <p align="center"> | ||
The `for()` method can take multiple objects to build hierarchy levels. | ||
```js | ||
let user = new User({id: 1}) | ||
let post = await user.posts().first() | ||
// Related objects go in order of their appearance in the URL. | ||
let comment = new Comment({text: 'for() takes multiple objects.'}).for(user, post) | ||
// POST /users/1/posts/1/comments | ||
await comment.save() | ||
``` | ||
If you need to get a nested resource, without getting the parent model at first, you can do something like this. | ||
@@ -470,0 +484,0 @@ |
@@ -67,12 +67,22 @@ import Builder from './Builder'; | ||
for (object) { | ||
if (object instanceof Model === false) { | ||
throw new Error('The object referenced on for() method is not a valid Model.') | ||
for (...args) { | ||
if( args.length === 0 ) { | ||
throw new Error('The for() method takes a minimum of one argument.') | ||
} | ||
let url = `${this.baseURL()}`; | ||
args.forEach(object => { | ||
if (object instanceof Model === false) { | ||
throw new Error('The object referenced on for() method is not a valid Model.') | ||
} | ||
if (!this.isValidId(object.getPrimaryKey())) { | ||
throw new Error('The object referenced on for() method has a invalid id.') | ||
} | ||
if (!this.isValidId(object.getPrimaryKey())) { | ||
throw new Error('The object referenced on for() method has a invalid id.') | ||
} | ||
url += `/${object.resource()}/${object.getPrimaryKey()}` | ||
}) | ||
let url = `${this.baseURL()}/${object.resource()}/${object.getPrimaryKey()}/${this.resource()}` | ||
url += `/${this.resource()}` | ||
@@ -79,0 +89,0 @@ this._from(url) |
import Post from './dummy/models/Post' | ||
import User from './dummy/models/User' | ||
import Comment from './dummy/models/Comment' | ||
import { Model } from '../src' | ||
@@ -426,2 +427,13 @@ import axios from 'axios' | ||
test('Calling for() with multiple arguments productes the correct URL', () => { | ||
const user = new User({ id: 1 }) | ||
const post = new Post({ id: 2 }) | ||
const comment = new Comment({ | ||
post_id: 2, | ||
text: 'for() takes more than one argument now!' | ||
}).for(user, post) | ||
expect(comment.endpoint()).toEqual(`http://localhost/users/${user.id}/posts/${post.id}/comments`) | ||
}) | ||
test('it throws a error when for() method does not recieve a instance of Model', () => { | ||
@@ -432,3 +444,3 @@ errorModel = () => { | ||
expect(errorModel).toThrow('The object referenced on for() method is not a valid Model.') | ||
expect(errorModel).toThrow('The for() method takes a minimum of one argument.') | ||
@@ -435,0 +447,0 @@ errorModel = () => { |
228975
0.61%1897
1.5%733
1.95%