UNPKG

cannery-adapter-rest

Version:
48 lines (41 loc) 1.38 kB
'use strict'; 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 = { PUT: { 'cars/1': function cars1(options) { if (options.envelope) { return _defineProperty({}, options.envelope, JSON.parse(options.body)); } return JSON.parse(options.body); }, 'api/cars/2': function apiCars2(options) { return JSON.parse(options.body); }, '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'; } }); }); };