Comparing version
@@ -26,2 +26,4 @@ 'use strict'; | ||
var _lang = require('lodash/lang'); | ||
var _string = require('lodash/string'); | ||
@@ -188,3 +190,3 @@ | ||
* @param {String} endpoint - The endpoint to hit. | ||
* @param {Object} params - The parameters to substitute into the endpoint string. | ||
* @param {Object|Array.<String|Number>} params - The parameters to substitute into the endpoint string. | ||
* @returns {Promise.<String|TypeError>} Resolves to the API response or a fetch network error. | ||
@@ -201,4 +203,6 @@ */ | ||
key: 'request', | ||
value: function request(endpoint) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
value: function request(endpoint, params) { | ||
if (!Array.isArray(params) && !(0, _lang.isPlainObject)(params)) { | ||
throw new TypeError('Parameters given must be in an array or a plain object.'); | ||
} | ||
@@ -217,3 +221,3 @@ return (0, _isomorphicFetch2.default)(this._buildRequestUrl(endpoint, params)).then(function (response) { | ||
* @param {String} endpoint - The endpoint to hit. | ||
* @param {Object} params - The parameters to substitute into the endpoint string. | ||
* @param {Object|Array.<String|Number>} params - The parameters to substitute into the endpoint string. | ||
* @returns {String} The request URL. | ||
@@ -226,3 +230,5 @@ * @private | ||
value: function _buildRequestUrl(endpoint, params) { | ||
return '' + this.constructor.BASE_API_URL + (0, _sprintfJs.sprintf)((0, _string.trimStart)(endpoint, '/'), params) + '.json?' + _querystring2.default.stringify({ key: this._key }); | ||
var formattedEndpoint = Array.isArray(params) ? (0, _sprintfJs.vsprintf)((0, _string.trimStart)(endpoint.replace(/%\(.+?\)s/g, '%s'), '/'), params) : (0, _sprintfJs.sprintf)((0, _string.trimStart)(endpoint, '/'), params); | ||
return '' + this.constructor.BASE_API_URL + formattedEndpoint + '.json?' + _querystring2.default.stringify({ key: this._key }); | ||
} | ||
@@ -229,0 +235,0 @@ }]); |
{ | ||
"name": "luu", | ||
"version": "0.2.0", | ||
"version": "0.2.2", | ||
"description": "A Node.js client library for the University of Waterloo's API.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -34,3 +34,5 @@ # luu | ||
// request() returns a promise which resolves to the JSON response from the API | ||
client.request('foodservices/menu').then(console.log).catch(console.error); | ||
client.request('foodservices/menu') | ||
.then(console.log) | ||
.catch(console.error); | ||
``` | ||
@@ -53,2 +55,5 @@ | ||
}).then(console.log); | ||
// specify an array of parameters | ||
client.request(Constants.FS_YEAR_WEEK_MENU, [2015, 2]).then(console.log); | ||
``` |
11425
4.17%199
2.05%58
9.43%