cannery-adapter-rest
Version:
The Cannery REST adapter
54 lines (47 loc) • 1.42 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 requests = {
POST: {
cars: function cars(options) {
if (options.envelope) {
return _defineProperty({}, options.envelope, {
id: 2
});
}
return {
id: 1
};
},
'api/cars': function apiCars(options) {
return {
id: 3
};
},
'foo/bar/baz': function fooBarBaz() {
return {
id: 5000
};
}
}
};
module.exports = function (method, url, options) {
return new Promise(function (resolve, reject) {
if (!options.throwError) {
return resolve({
statusCode: 200,
headers: options.headers,
getBody: function getBody() {
var body = requests[method][url](options);
return JSON.stringify(body);
}
});
}
return reject({
statusCode: 500,
headers: options.headers,
getBody: function getBody() {
return 'Server Error';
}
});
});
};