UNPKG

cannery-adapter-rest

Version:
60 lines (51 loc) 1.54 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 responses = { GET: { 'foo/bar/baz': [{ id: 100 }], 'cars/1/parts': [{ id: 1, name: 'Engine' }, { id: 2, name: 'Steering Wheel' }], 'api/v1/cars/1/parts': [{ id: 1, name: 'Starter' }], 'cars/1/parts?type=chevy': [{ id: 3, name: 'Radio' }], 'cars/1/parts/2/part_makers': [{ id: 3, name: 'Big John' }] } }; 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/1/parts?type=chevy']; } if (options.envelope) { return JSON.stringify(_defineProperty({}, options.envelope, response)); } return JSON.stringify(response); } }); }); };