cannery-adapter-rest
Version:
The Cannery REST adapter
57 lines (49 loc) • 1.47 kB
JavaScript
;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var responses = {
GET: {
'foo/bar/baz': [{
id: 100
}],
cars: [{
id: 1,
make: 'Ford',
model: 'Tauras'
}, {
id: 2,
make: 'Honda',
model: 'Accord'
}],
'api/v1/cars': [{
id: 1,
make: 'Pontiac',
model: 'Grand Prix'
}],
'cars?type=chevy': [{
make: 'Chevy'
}]
}
};
module.exports = function (method, url, options) {
return new Promise(function (resolve, reject) {
if (options.throwError) {
return reject({
statusCode: 500
});
}
return resolve({
statusCode: 200,
headers: options.headers,
getBody: function getBody() {
var response = responses[method][url];
if (options.qs) {
response = responses[method]['cars?type=chevy'];
}
if (options.envelope) {
return JSON.stringify(_defineProperty({}, options.envelope, response));
}
return JSON.stringify(response);
}
});
});
};