This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ProgBlog/app/base/ApiModel.js

104 lines
2.4 KiB
JavaScript

'use strict';
const container = require('../Container'),
axios = require('axios');
const Model = container.get('base/Model');
/**
* Base class for API Models
*
* Wraps convenience methods for making API requests.
*
* @param {string} [baseUrl] - a base url for api requests
* @extends Model
*/
class ApiModel extends Model {
/**
* Create a new APIModel
*
* @constructor
* @param {string} [baseUrl] - a base url for api requests
*/
constructor(baseUrl) {
super();
let apiConfig = {};
if (baseUrl) {
apiConfig.baseUrl = baseUrl;
}
this.client = axios(apiConfig);
}
/**
* Make a 'HEAD' HTTP request
*
* @param {string} url - the url to request
* @param {Object} [config] - options for the current request
* @return {Promise} - promise wrapping the request
*/
head(url, config) {
return this.client.head(url, config);
}
/**
* Make a 'GET' HTTP request
*
* @param {string} url - the url to request
* @param {Object} [config] - options for the current request
* @return {Promise} - promise wrapping the request
*/
get(url, config) {
return this.client.get(url, config);
}
/**
* Make a 'DELETE' HTTP request
*
* @param {string} url - the url to request
* @param {Object} [config] - options for the current request
* @return {Promise} - promise wrapping the request
*/
delete(url, config) {
return this.client.delete(url, config);
}
/**
* Make a 'POST' HTTP request
*
* @param {string} url - the url to request
* @param {Object} data - the data for the current request
* @param {Object} [config] - options for the current request
* @return {Promise} - promise wrapping the request
*/
post(url, data, config) {
return this.client.post(url, data, config);
}
/**
* Make a 'PUT' HTTP request
*
* @param {string} url - the url to request
* @param {Object} data - the data for the current request
* @param {Object} [config] - options for the current request
* @return {Promise} - promise wrapping the request
*/
put(url, data, config) {
return this.client.put(url, data, config);
}
/**
* Make a 'PATCH' HTTP request
*
* @param {string} url - the url to request
* @param {Object} data - the data for the current request
* @param {Object} [config] - options for the current request
* @return {Promise} - promise wrapping the request
*/
patch(url, data, config) {
return this.client.patch(url, data, config);
}
}
module.exports = ApiModel;