-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4d3c97
commit 370df4d
Showing
8 changed files
with
279 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
|
||
var _Collection = require('../classes/collection/Collection'); | ||
|
||
var _Collection2 = _interopRequireDefault(_Collection); | ||
|
||
var _axios = require('axios'); | ||
|
||
var _axios2 = _interopRequireDefault(_axios); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
|
||
var pluralize = require('pluralize'); | ||
|
||
var Query = function () { | ||
|
||
// @todo still thinking about how this class whould work | ||
// Ideally it will be a nice way to interact with an api | ||
// For example User.get(1) should maybe get a particular user from the api | ||
// Any api routes would need to both have structure (CRUD) and also allow for flexible starting route names (not only 'api') | ||
function Query() { | ||
_classCallCheck(this, Query); | ||
|
||
if (new.target === Query) { | ||
throw new TypeError("Cannot construct Query instances directly"); | ||
} | ||
|
||
this.base_url = '/'; | ||
this.api_route = 'api'; | ||
this.route = pluralize.plural(this.constructor.name).toLowerCase(); | ||
} | ||
|
||
// Make sure any class extending this is able to collect data | ||
|
||
|
||
_createClass(Query, [{ | ||
key: 'collect', | ||
value: function collect(data) { | ||
throw new Error('The collect method has not been properly implemented in the Query child class'); | ||
} | ||
|
||
// Get the full base path for all api calls on this model | ||
|
||
}, { | ||
key: 'getFullPath', | ||
value: function getFullPath() { | ||
return this.base_url + this.api_route + '/' + this.route + '/'; | ||
} | ||
|
||
// Do an index api call | ||
|
||
}], [{ | ||
key: 'index', | ||
value: function index(params) { | ||
var _this = this; | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _axios2.default.get(_this.getFullPath, { params: params }).then(function (_ref) { | ||
var data = _ref.data; | ||
|
||
resolve(_this.collect(data)); | ||
}).catch(function (e) { | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
|
||
// Get a particular item from the api | ||
|
||
}, { | ||
key: 'get', | ||
value: function get(id, callback) { | ||
var _this2 = this; | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _axios2.default.post(_this2.getFullPath + 'get', params).then(function (_ref2) { | ||
var data = _ref2.data; | ||
|
||
resolve(new _this2(data)); | ||
}).catch(function (e) { | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
|
||
// Create an item of this model type | ||
|
||
}, { | ||
key: 'create', | ||
value: function create(data, callback) { | ||
var _this3 = this; | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _axios2.default.post(_this3.getFullPath + 'get', params).then(function (_ref3) { | ||
var data = _ref3.data; | ||
|
||
resolve(new _this3(data)); | ||
}).catch(function (e) { | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
|
||
// Update this item containing the primary key | ||
|
||
}, { | ||
key: 'update', | ||
value: function update(data, callback) { | ||
var _this4 = this; | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _axios2.default.post(_this4.getFullPath + 'get', params).then(function (_ref4) { | ||
var data = _ref4.data; | ||
|
||
resolve(new _this4(data)); | ||
}).catch(function (e) { | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
|
||
// Delete an item by id | ||
|
||
}, { | ||
key: 'delete', | ||
value: function _delete(id, callback) { | ||
var _this5 = this; | ||
|
||
return new Promise(function (resolve, reject) { | ||
return _axios2.default.post(_this5.getFullPath + 'delete', params).then(function (_ref5) { | ||
var data = _ref5.data; | ||
|
||
resolve(data); | ||
}).catch(function (e) { | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
}]); | ||
|
||
return Query; | ||
}(); | ||
|
||
exports.default = Query; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Collection from '../classes/collection/Collection' | ||
import axios from 'axios' | ||
let pluralize = require('pluralize') | ||
class Query { | ||
|
||
|
||
// @todo still thinking about how this class whould work | ||
// Ideally it will be a nice way to interact with an api | ||
// For example User.get(1) should maybe get a particular user from the api | ||
// Any api routes would need to both have structure (CRUD) and also allow for flexible starting route names (not only 'api') | ||
constructor() { | ||
if (new.target === Query) { | ||
throw new TypeError("Cannot construct Query instances directly"); | ||
} | ||
|
||
this.base_url = '/' | ||
this.api_route = 'api' | ||
this.route = pluralize.plural(this.constructor.name).toLowerCase() | ||
} | ||
|
||
// Make sure any class extending this is able to collect data | ||
collect(data) { | ||
throw new Error('The collect method has not been properly implemented in the Query child class'); | ||
} | ||
|
||
// Get the full base path for all api calls on this model | ||
getFullPath() { | ||
return this.base_url + this.api_route + '/' + this.route + '/' | ||
} | ||
|
||
// Do an index api call | ||
static index(params) { | ||
return new Promise((resolve, reject) => { | ||
return axios.get(this.getFullPath, {params: params}).then(({data}) => { | ||
resolve(this.collect(data)) | ||
}).catch((e) => { | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
|
||
// Get a particular item from the api | ||
static get(id, callback) { | ||
return new Promise((resolve, reject) => { | ||
return axios.post(this.getFullPath + 'get', params).then(({data}) => { | ||
resolve(new this(data)) | ||
}).catch((e) => { | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
|
||
// Create an item of this model type | ||
static create(data, callback) { | ||
return new Promise((resolve, reject) => { | ||
return axios.post(this.getFullPath + 'get', params).then(({data}) => { | ||
resolve(new this(data)) | ||
}).catch((e) => { | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
|
||
// Update this item containing the primary key | ||
static update(data, callback) { | ||
return new Promise((resolve, reject) => { | ||
return axios.post(this.getFullPath + 'get', params).then(({data}) => { | ||
resolve(new this(data)) | ||
}).catch((e) => { | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
|
||
// Delete an item by id | ||
static delete(id, callback) { | ||
return new Promise((resolve, reject) => { | ||
return axios.post(this.getFullPath + 'delete', params).then(({data}) => { | ||
resolve(data) | ||
}).catch((e) => { | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
export default Query; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
var expect = require('chai').expect | ||
var faker = require('faker') | ||
let Where = require('../dist/classes/collection/Where').default | ||
let axios = require('axios') | ||
let moxios = require('moxios') | ||
let sinon = require('sinon') | ||
let Collection = require('../dist/classes/collection/Collection').default | ||
let Model = require('../dist/models/Model').default | ||
let Query = require('../dist/models/Query').default | ||
|
||
describe('#Query', function() { | ||
|
||
it('it cannot be called as a function', function() { | ||
expect(() => Query()).to.throw(TypeError) | ||
}); | ||
|
||
}); | ||
|
||
class User extends Model { | ||
constructor(data) { | ||
super(data) | ||
} | ||
} |