js-tinyapi
Advanced tools
Comparing version 0.1.0-alpha.3 to 0.1.0-alpha.4
@@ -116,3 +116,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
"use strict"; | ||
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.Batch = exports.Middleware = exports.ApiError = exports.ajaxSettings = exports.postForm = exports.postJson = exports.ajax = undefined;\n\nvar _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _jsonapi = __webpack_require__(/*! ./jsonapi */ \"./src/jsonapi.js\");\n\nvar _utils = __webpack_require__(/*! ./utils */ \"./src/utils.js\");\n\nvar _errors = __webpack_require__(/*! ./errors */ \"./src/errors.js\");\n\nvar _middleware = __webpack_require__(/*! ./middleware */ \"./src/middleware.js\");\n\nvar _middleware2 = _interopRequireDefault(_middleware);\n\nvar _batch = __webpack_require__(/*! ./batch */ \"./src/batch.js\");\n\nvar _batch2 = _interopRequireDefault(_batch);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\n/**\n * Describes an API.\n */\nvar Api = function () {\n\n /**\n * Constructs an Api instance. Accepts an endpoint tree.\n */\n function Api() {\n var endpoints = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n\n _classCallCheck(this, Api);\n\n _initialiseProps.call(this);\n\n this.crud = {};\n this.middlewares = [];\n this.merge(endpoints);\n }\n\n /**\n * Merge an endpoint tree.\n */\n\n\n /**\n * Constructs an endpoint call function and sets it on the object.\n *\n * @param {string} name - The name of the endpoint function.\n * @param {string} path - The URL path to use.\n * @param {string} method - The method to use for this call.\n * @param {func} handler - A custom handler to call.\n */\n\n\n _createClass(Api, [{\n key: 'makeCrudEndpoints',\n\n\n /**\n * Automatically create a set of CRUD endpoint functions.\n */\n value: function makeCrudEndpoints(key, path) {\n var joiner = path[path.length - 1] == '/' ? '' : '/';\n var basePath = path + joiner + key;\n this.crud[key] = {\n list: this.makeEndpoint(key + 'List', basePath, 'GET'),\n create: this.makeEndpoint(key + 'Create', basePath, 'POST', {\n handler: function handler(req, payload) {\n var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n return req(_extends({}, opts, {\n payload: payload\n }));\n }\n }),\n detail: this.makeEndpoint(key + 'Get', basePath + '/{id}', 'GET', {\n handler: function handler(req, id) {\n var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n\n return req(_extends({}, opts, {\n params: { id: id }\n }));\n }\n }),\n update: this.makeEndpoint(key + 'Update', basePath + '/{id}', 'PATCH', {\n handler: function handler(req, id, payload) {\n var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};\n\n return req(_extends({}, opts, {\n params: { id: id },\n payload: payload\n }));\n }\n }),\n remove: this.makeEndpoint(key + 'Remove', basePath + '/{id}', 'DELETE', {\n handler: function handler(req, id) {\n var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n return req(_extends({}, opts, {\n params: { id: id }\n }));\n }\n }),\n options: this.makeEndpoint(key + 'Options', basePath, 'OPTIONS')\n };\n }\n\n /**\n * Perform a request call.\n */\n\n }, {\n key: 'request',\n value: function request(endpoint) {\n var _this = this;\n\n var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};\n var _options$method = options.method,\n method = _options$method === undefined ? (endpoint.method || '').toLowerCase() : _options$method,\n _options$path = options.path,\n path = _options$path === undefined ? endpoint.path : _options$path,\n _options$params = options.params,\n params = _options$params === undefined ? {} : _options$params,\n payload = options.payload,\n _options$type = options.type,\n type = _options$type === undefined ? endpoint.type : _options$type,\n _options$extraHeaders = options.extraHeaders,\n extraHeaders = _options$extraHeaders === undefined ? {} : _options$extraHeaders,\n _options$include = options.include,\n include = _options$include === undefined ? endpoint.include || [] : _options$include,\n _options$filter = options.filter,\n filter = _options$filter === undefined ? endpoint.filter || {} : _options$filter,\n _options$sort = options.sort,\n sort = _options$sort === undefined ? endpoint.sort || [] : _options$sort;\n var urlRoot = options.urlRoot,\n _options$contentType = options.contentType,\n contentType = _options$contentType === undefined ? endpoint.contentType : _options$contentType;\n\n // \"type\" is used a convenient shorthand for the content type.\n // \"contentType\" still trumps it.\n\n if (!!type && !contentType) {\n contentType = _utils.contentTypes[type];\n }\n\n // Process the body. This can end up being a FormData object\n // or a json string.\n var body = void 0;\n var queryString = [];\n if (method != 'get' && method != 'options') {\n if (payload !== undefined) {\n if ((0, _utils.matchContentType)(contentType, 'form')) {\n body = (0, _utils.makeFormData)(payload);\n } else {\n body = JSON.stringify(payload || {});\n }\n }\n } else {\n if (payload !== undefined) {\n for (var k in payload) {\n queryString.push(k + '=' + encodeURIComponent(payload[k]));\n }\n }\n }\n\n // Replace any URL arguments. This is typically just the ID of\n // an object.\n var finalPath = (0, _utils.supplant)(path, params);\n\n // Add any JSONAPI query strings.\n finalPath += (0, _jsonapi.jsonApiQuery)({ initial: queryString, include: include, filter: filter, sort: sort });\n\n // If we've been given an URL root, add it in here. This is useful\n // for writing Node tests.\n if (urlRoot) {\n if (urlRoot[urlRoot.length - 1] == '/') {\n urlRoot = urlRoot.substring(0, urlRoot.length - 1);\n }\n finalPath = urlRoot + finalPath;\n }\n\n // Prepare the request object. This is passed to either \"ajax\"\n // or the middlewares.\n var req = {\n url: finalPath,\n method: method,\n body: body,\n contentType: contentType,\n extraHeaders: extraHeaders\n };\n\n // If there are no middlewares, we are free to fulfill the request\n // now.\n if (!this.middlewares.length) {\n (0, _utils.debug)('API ' + method + ' ' + type + ': ' + finalPath, payload);\n return (0, _utils.ajax)(req);\n }\n\n // Otherwise, we need to pipe through all the middleware. This works\n // by iterating over middleware in order until one returns a promise.\n // We then chain the remaining middleware after that promise. It's the\n // user's responsibility to ensure only one middleware wants to return\n // a promise.\n else {\n req = (0, _utils.makeRequest)(req);\n var ii = 0;\n var obj = this.middlewares[ii++].process(req);\n for (; ii < this.middlewares.length; ++ii) {\n if (Promise.resolve(obj) == obj) {\n var _loop = function _loop() {\n var mw = _this.middlewares[ii];\n obj = obj.then(function (r) {\n return mw.process(r);\n });\n };\n\n for (; ii < this.middlewares.length; ++ii) {\n _loop();\n }\n } else {\n obj = this.middlewares[ii].process(obj);\n }\n }\n return obj;\n }\n }\n }]);\n\n return Api;\n}();\n\nvar _initialiseProps = function _initialiseProps() {\n var _this2 = this;\n\n this.pushMiddleware = function (middleware) {\n if (!Array.isArray(middleware)) {\n middleware = [middleware];\n }\n middleware.forEach(function (m) {\n m.api = _this2;\n _this2.middlewares.push(m);\n });\n };\n\n this.unshiftMiddleware = function (middleware) {\n if (!Array.isArray(middleware)) {\n middleware = [middleware];\n }\n middleware.forEach(function (m) {\n m.api = _this2;\n _this2.middlewares.unshift(m);\n });\n };\n\n this.merge = function (endpoints) {\n var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';\n\n if (!endpoints) {\n throw new Error('Empty endpoint data given to Api.merge.');\n }\n\n var _iteratorNormalCompletion = true;\n var _didIteratorError = false;\n var _iteratorError = undefined;\n\n try {\n for (var _iterator = Object.keys(endpoints)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {\n var key = _step.value;\n\n var ep = endpoints[key];\n\n // Check if we're looking at an endpoint.\n var match = /^(GET|POST|PUT|PATCH|DELETE|CRUD)$/.exec(key);\n if (match) {\n\n // If we matched a CRUD endpoint, perform the setup.\n if (match[1] == 'CRUD') {\n var ii = path.lastIndexOf('/') + 1;\n var crudKey = path.slice(ii);\n var crudPath = path.slice(0, ii);\n _this2.makeCrudEndpoints(crudKey, crudPath);\n }\n\n // The endpoint can be just the name of the function\n // or it can be an object of details.\n else {\n if (!(ep instanceof Object)) {\n ep = { name: ep };\n }\n var _ep = ep,\n name = _ep.name,\n _ep$options = _ep.options,\n options = _ep$options === undefined ? {} : _ep$options;\n\n // Make the endpoint.\n\n _this2.makeEndpoint(name, path + '/', match[1], options);\n }\n }\n\n // If not an endpoint, check for a CRUD shorthand.\n else if (ep == 'CRUD') {\n _this2.makeCrudEndpoints(key, path);\n }\n\n // If not an endpoint or CRUD, continue down the tree.\n else {\n _this2.merge(ep, path + '/' + key);\n }\n }\n } catch (err) {\n _didIteratorError = true;\n _iteratorError = err;\n } finally {\n try {\n if (!_iteratorNormalCompletion && _iterator.return) {\n _iterator.return();\n }\n } finally {\n if (_didIteratorError) {\n throw _iteratorError;\n }\n }\n }\n };\n\n this.makeEndpoint = function (name, path, method) {\n var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};\n\n\n // Fail loudly if we're overriding names.\n if (_this2[name] !== undefined) {\n throw new Error('Duplicate name in Api: ' + name);\n }\n\n // Prepare the context to be passed to the request method. This\n // will be passed to the each invocation of the endpoint call\n // as a set of defaults.\n var ctx = _extends({}, opts, {\n path: (0, _utils.addTrailingSlash)(path),\n method: method\n });\n\n // If we were given a function to call, bind it appropriately.\n // Otherwise just use the standard request.\n var request = function request(opts) {\n return _this2.request(ctx, opts);\n };\n var handler = opts.handler;\n\n if (handler !== undefined) {\n\n // The first argument to the handler will be a function to call\n // the builtin handler. This allows the handler to easily finalise\n // the call after modifying any options.\n var wrapper = function wrapper() {\n for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return handler.apply(undefined, [request].concat(args));\n };\n wrapper.context = ctx;\n _this2[name] = wrapper;\n } else {\n request.context = ctx;\n _this2[name] = request;\n }\n\n return _this2[name];\n };\n};\n\nexports.default = Api;\nexports.ajax = _utils.ajax;\nexports.postJson = _utils.postJson;\nexports.postForm = _utils.postForm;\nexports.ajaxSettings = _utils.ajaxSettings;\nexports.ApiError = _errors.ApiError;\nexports.Middleware = _middleware2.default;\nexports.Batch = _batch2.default;\n\n//# sourceURL=webpack://js-tinyapi/./src/index.js?"); | ||
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.Batch = exports.Middleware = exports.ApiError = exports.ajaxSettings = exports.postForm = exports.postJson = exports.ajax = undefined;\n\nvar _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _jsonapi = __webpack_require__(/*! ./jsonapi */ \"./src/jsonapi.js\");\n\nvar _utils = __webpack_require__(/*! ./utils */ \"./src/utils.js\");\n\nvar _errors = __webpack_require__(/*! ./errors */ \"./src/errors.js\");\n\nvar _middleware = __webpack_require__(/*! ./middleware */ \"./src/middleware.js\");\n\nvar _middleware2 = _interopRequireDefault(_middleware);\n\nvar _batch = __webpack_require__(/*! ./batch */ \"./src/batch.js\");\n\nvar _batch2 = _interopRequireDefault(_batch);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\n/**\n * Describes an API.\n */\nvar Api = function () {\n\n /**\n * Constructs an Api instance. Accepts an endpoint tree.\n */\n function Api() {\n var endpoints = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};\n\n _classCallCheck(this, Api);\n\n _initialiseProps.call(this);\n\n this.crud = {};\n this.middlewares = [];\n this.merge(endpoints);\n }\n\n /**\n * Merge an endpoint tree.\n */\n\n\n /**\n * Constructs an endpoint call function and sets it on the object.\n *\n * @param {string} name - The name of the endpoint function.\n * @param {string} path - The URL path to use.\n * @param {string} method - The method to use for this call.\n * @param {func} handler - A custom handler to call.\n */\n\n\n _createClass(Api, [{\n key: 'makeCrudEndpoints',\n\n\n /**\n * Automatically create a set of CRUD endpoint functions.\n */\n value: function makeCrudEndpoints(key, path) {\n var joiner = path[path.length - 1] == '/' ? '' : '/';\n var basePath = path + joiner + key;\n this.crud[key] = {\n list: this.makeEndpoint(key + 'List', basePath, 'GET'),\n create: this.makeEndpoint(key + 'Create', basePath, 'POST', {\n handler: function handler(req, payload) {\n var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n return req(_extends({}, opts, {\n payload: payload\n }));\n }\n }),\n detail: this.makeEndpoint(key + 'Get', basePath + '/{id}', 'GET', {\n handler: function handler(req, id) {\n var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n\n return req(_extends({}, opts, {\n params: { id: id }\n }));\n }\n }),\n update: this.makeEndpoint(key + 'Update', basePath + '/{id}', 'PATCH', {\n handler: function handler(req, id, payload) {\n var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};\n\n return req(_extends({}, opts, {\n params: { id: id },\n payload: payload\n }));\n }\n }),\n remove: this.makeEndpoint(key + 'Remove', basePath + '/{id}', 'DELETE', {\n handler: function handler(req, id) {\n var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n return req(_extends({}, opts, {\n params: { id: id }\n }));\n }\n }),\n options: this.makeEndpoint(key + 'Options', basePath, 'OPTIONS')\n };\n }\n\n /**\n * Perform a request call.\n */\n\n }, {\n key: 'request',\n value: function request(endpoint) {\n var _this = this;\n\n var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};\n var _options$method = options.method,\n method = _options$method === undefined ? (endpoint.method || '').toLowerCase() : _options$method,\n _options$path = options.path,\n path = _options$path === undefined ? endpoint.path : _options$path,\n _options$params = options.params,\n params = _options$params === undefined ? {} : _options$params,\n payload = options.payload,\n _options$type = options.type,\n type = _options$type === undefined ? endpoint.type : _options$type,\n _options$extraHeaders = options.extraHeaders,\n extraHeaders = _options$extraHeaders === undefined ? {} : _options$extraHeaders,\n _options$include = options.include,\n include = _options$include === undefined ? endpoint.include || [] : _options$include,\n _options$filter = options.filter,\n filter = _options$filter === undefined ? endpoint.filter || {} : _options$filter,\n _options$sort = options.sort,\n sort = _options$sort === undefined ? endpoint.sort || [] : _options$sort,\n _options$fields = options.fields,\n fields = _options$fields === undefined ? endpoint.fields || [] : _options$fields;\n var urlRoot = options.urlRoot,\n _options$contentType = options.contentType,\n contentType = _options$contentType === undefined ? endpoint.contentType : _options$contentType;\n\n // \"type\" is used a convenient shorthand for the content type.\n // \"contentType\" still trumps it.\n\n if (!!type && !contentType) {\n contentType = _utils.contentTypes[type];\n }\n\n // Process the body. This can end up being a FormData object\n // or a json string.\n var body = void 0;\n var queryString = [];\n if (method != 'get' && method != 'options') {\n if (payload !== undefined) {\n if ((0, _utils.matchContentType)(contentType, 'form') || (0, _utils.matchContentType)(contentType, 'multiForm')) {\n body = (0, _utils.makeFormData)(payload);\n } else {\n body = JSON.stringify(payload || {});\n }\n }\n } else {\n if (payload !== undefined) {\n for (var k in payload) {\n queryString.push(k + '=' + encodeURIComponent(payload[k]));\n }\n }\n }\n\n // Replace any URL arguments. This is typically just the ID of\n // an object.\n var finalPath = (0, _utils.supplant)(path, params);\n\n // Add any JSONAPI query strings.\n finalPath += (0, _jsonapi.jsonApiQuery)({ initial: queryString, include: include, filter: filter, sort: sort, fields: fields });\n\n // If we've been given an URL root, add it in here. This is useful\n // for writing Node tests.\n if (urlRoot) {\n if (urlRoot[urlRoot.length - 1] == '/') {\n urlRoot = urlRoot.substring(0, urlRoot.length - 1);\n }\n finalPath = urlRoot + finalPath;\n }\n\n // Prepare the request object. This is passed to either \"ajax\"\n // or the middlewares.\n var req = {\n url: finalPath,\n method: method,\n body: body,\n contentType: contentType,\n extraHeaders: extraHeaders,\n bearer: this.bearer\n };\n\n // If there are no middlewares, we are free to fulfill the request\n // now.\n if (!this.middlewares.length) {\n (0, _utils.debug)('API ' + method + ' ' + type + ': ' + finalPath, payload);\n return (0, _utils.ajax)(req);\n }\n\n // Otherwise, we need to pipe through all the middleware. This works\n // by iterating over middleware in order until one returns a promise.\n // We then chain the remaining middleware after that promise. It's the\n // user's responsibility to ensure only one middleware wants to return\n // a promise.\n else {\n req = (0, _utils.makeRequest)(req);\n var ii = 0;\n var obj = this.middlewares[ii++].process(req);\n for (; ii < this.middlewares.length; ++ii) {\n if (Promise.resolve(obj) == obj) {\n var _loop = function _loop() {\n var mw = _this.middlewares[ii];\n obj = obj.then(function (r) {\n return mw.process(r);\n });\n };\n\n for (; ii < this.middlewares.length; ++ii) {\n _loop();\n }\n } else {\n obj = this.middlewares[ii].process(obj);\n }\n }\n return obj;\n }\n }\n }]);\n\n return Api;\n}();\n\nvar _initialiseProps = function _initialiseProps() {\n var _this2 = this;\n\n this.pushMiddleware = function (middleware) {\n if (!Array.isArray(middleware)) {\n middleware = [middleware];\n }\n middleware.forEach(function (m) {\n m.api = _this2;\n _this2.middlewares.push(m);\n });\n };\n\n this.unshiftMiddleware = function (middleware) {\n if (!Array.isArray(middleware)) {\n middleware = [middleware];\n }\n middleware.forEach(function (m) {\n m.api = _this2;\n _this2.middlewares.unshift(m);\n });\n };\n\n this.merge = function (endpoints) {\n var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';\n\n if (!endpoints) {\n throw new Error('Empty endpoint data given to Api.merge.');\n }\n\n var _iteratorNormalCompletion = true;\n var _didIteratorError = false;\n var _iteratorError = undefined;\n\n try {\n for (var _iterator = Object.keys(endpoints)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {\n var key = _step.value;\n\n var ep = endpoints[key];\n\n // Check if we're looking at an endpoint.\n var match = /^(GET|POST|PUT|PATCH|DELETE|CRUD)$/.exec(key);\n if (match) {\n\n // If we matched a CRUD endpoint, perform the setup.\n if (match[1] == 'CRUD') {\n var ii = path.lastIndexOf('/') + 1;\n var crudKey = path.slice(ii);\n var crudPath = path.slice(0, ii);\n _this2.makeCrudEndpoints(crudKey, crudPath);\n }\n\n // The endpoint can be just the name of the function\n // or it can be an object of details.\n else {\n if (!(ep instanceof Object)) {\n ep = { name: ep };\n }\n var _ep = ep,\n name = _ep.name,\n _ep$options = _ep.options,\n options = _ep$options === undefined ? {} : _ep$options;\n\n // Make the endpoint.\n\n _this2.makeEndpoint(name, path + '/', match[1], options);\n }\n }\n\n // If not an endpoint, check for a CRUD shorthand.\n else if (ep == 'CRUD') {\n _this2.makeCrudEndpoints(key, path);\n }\n\n // If not an endpoint or CRUD, continue down the tree.\n else {\n _this2.merge(ep, path + '/' + key);\n }\n }\n } catch (err) {\n _didIteratorError = true;\n _iteratorError = err;\n } finally {\n try {\n if (!_iteratorNormalCompletion && _iterator.return) {\n _iterator.return();\n }\n } finally {\n if (_didIteratorError) {\n throw _iteratorError;\n }\n }\n }\n };\n\n this.makeEndpoint = function (name, path, method) {\n var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};\n\n\n // Fail loudly if we're overriding names.\n if (_this2[name] !== undefined) {\n throw new Error('Duplicate name in Api: ' + name);\n }\n\n // Prepare the context to be passed to the request method. This\n // will be passed to the each invocation of the endpoint call\n // as a set of defaults.\n var ctx = _extends({}, opts, {\n path: (0, _utils.addTrailingSlash)(path),\n method: method\n });\n\n // If we were given a function to call, bind it appropriately.\n // Otherwise just use the standard request.\n var request = function request(opts) {\n return _this2.request(ctx, opts);\n };\n var handler = opts.handler;\n\n if (handler !== undefined) {\n\n // The first argument to the handler will be a function to call\n // the builtin handler. This allows the handler to easily finalise\n // the call after modifying any options.\n var wrapper = function wrapper() {\n for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return handler.apply(undefined, [request].concat(args));\n };\n wrapper.context = ctx;\n _this2[name] = wrapper;\n } else {\n request.context = ctx;\n _this2[name] = request;\n }\n\n return _this2[name];\n };\n};\n\nexports.default = Api;\nexports.ajax = _utils.ajax;\nexports.postJson = _utils.postJson;\nexports.postForm = _utils.postForm;\nexports.ajaxSettings = _utils.ajaxSettings;\nexports.ApiError = _errors.ApiError;\nexports.Middleware = _middleware2.default;\nexports.Batch = _batch2.default;\n\n//# sourceURL=webpack://js-tinyapi/./src/index.js?"); | ||
@@ -129,3 +129,3 @@ /***/ }), | ||
"use strict"; | ||
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nfunction jsonApiList(values, key) {\n if (values && values.length) {\n return [key + '=' + values.join(',')];\n }\n return [];\n}\n\nfunction jsonApiInclude(values, key) {\n return jsonApiList(values, key || 'include');\n}\n\nfunction jsonApiSort(values, key) {\n return jsonApiList(values, key || 'sort');\n}\n\nfunction jsonApiFilter(values, key) {\n if (values && Object.keys(values).length) {\n var k = key || 'filter';\n return Object.keys(values).map(function (attr) {\n return 'filter[' + attr + ']=' + values[attr];\n });\n }\n return [];\n}\n\nfunction jsonApiQuery(opts) {\n var _ref = opts || {},\n include = _ref.include,\n sort = _ref.sort,\n filter = _ref.filter;\n\n var parts = (opts.initial || []).concat(jsonApiInclude(include).concat(jsonApiFilter(filter).concat(jsonApiSort(sort))));\n if (parts.length > 0) {\n return '?' + parts.join('&');\n }\n return '';\n}\n\nexports.jsonApiQuery = jsonApiQuery;\n\n//# sourceURL=webpack://js-tinyapi/./src/jsonapi.js?"); | ||
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nfunction jsonApiList(values, key) {\n if (!Array.isArray(values)) values = [values];\n if (values && values.length) return [key + '=' + values.join(',')];\n return [];\n}\n\nfunction jsonApiInclude(values, key) {\n return jsonApiList(values, key || 'include');\n}\n\nfunction jsonApiSort(values, key) {\n return jsonApiList(values, key || 'sort');\n}\n\nfunction jsonApiFilter(values, key) {\n if (values && Object.keys(values).length) {\n var k = key || 'filter';\n return Object.keys(values).map(function (attr) {\n return 'filter[' + attr + ']=' + values[attr];\n });\n }\n return [];\n}\n\nfunction jsonApiFields(values, key) {\n if (values && Object.keys(values).length) {\n var k = key || 'filter';\n return Object.keys(values).map(function (res) {\n return 'fields[' + res + ']=' + values[res];\n });\n }\n return [];\n}\n\nfunction jsonApiQuery(opts) {\n var _ref = opts || {},\n include = _ref.include,\n sort = _ref.sort,\n filter = _ref.filter,\n fields = _ref.fields;\n\n var parts = (opts.initial || []).concat(jsonApiInclude(include).concat(jsonApiFilter(filter).concat(jsonApiSort(sort).concat(jsonApiFields(fields)))));\n if (parts.length > 0) return '?' + parts.join('&');\n return '';\n}\n\nexports.jsonApiQuery = jsonApiQuery;\n\n//# sourceURL=webpack://js-tinyapi/./src/jsonapi.js?"); | ||
@@ -154,3 +154,3 @@ /***/ }), | ||
"use strict"; | ||
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.fetchHeaders = exports.ajaxWithRequest = exports.makeRequest = exports.makeFormData = exports.matchContentType = exports.contentTypes = exports.ajaxSettings = exports.postForm = exports.postJson = exports.ajax = exports.addTrailingSlash = exports.debug = undefined;\nexports.supplant = supplant;\nexports.capitalize = capitalize;\n\nvar _jsCookie = __webpack_require__(/*! js-cookie */ \"js-cookie\");\n\nvar _jsCookie2 = _interopRequireDefault(_jsCookie);\n\nvar _errors = __webpack_require__(/*! ./errors */ \"./src/errors.js\");\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }\n\n/**\n * Make it a little easier to use content types.\n */\nvar contentTypes = {\n form: 'application/x-www-form-urlencoded',\n multiForm: 'multipart/form-data',\n json: 'application/json',\n jsonApi: 'application/vnd.api+json'\n};\n\n/**\n *\n */\nfunction debug() {\n if (console.debug) {\n console.debug.apply(null, arguments);\n }\n}\n\nfunction matchContentType(src, id) {\n if (!src) {\n return false;\n }\n var ii = src.indexOf(';');\n if (ii >= 0) {\n src = src.substring(0, ii);\n }\n return src.toLowerCase() == contentTypes[id];\n}\n\nfunction addTrailingSlash(path) {\n return path + (path[path.length - 1] == '/' ? '' : '/');\n}\n\n/**\n * Find and replace terms in a string.\n *\n * Terms are identified by being surrounded by curly braces, such as\n * this: \"a string with a {substitution}\". Here, \"substitution\" will\n * be replaced by looking for the same named key in the supplied\n * mapping.\n *\n * @param {string} text - The string with replacements.\n * @param {object} mapping - The mapping.\n */\nfunction supplant(text, mapping) {\n return text.replace(/{([^{}]*)}/g, function (a, b) {\n var r = mapping[b];\n if (r === undefined) {\n throw new _errors.ApiError('Missing string template: ' + b);\n }\n return typeof r === 'string' || typeof r === 'number' ? r : a;\n });\n}\n\n/**\n * Capitalize a string.\n *\n * @param {string} text - The string to capitalize.\n */\nfunction capitalize(text) {\n return text[0].toUpperCase() + text.slice(1);\n}\n\n/**\n * Global storage for authorization and CSRF.\n *\n * Often when making requests the same credentials or CSRF token needs\n * to be used. This is a place to store these details. Currently\n * accepts \"csrf\" and \"bearer\" values. Upon initialisation cookies\n * are examined for a current value for the CSRF token (looks for\n * a cookie called \"csrftoken\").\n */\nvar ajaxSettings = {\n csrf: _jsCookie2.default ? _jsCookie2.default.get('csrftoken') || '' : '',\n bearer: null\n};\n\n/**\n * Construct headers for a fetch request.\n *\n * Uses the HTML5 Headers object to formulate an appropriate set of\n * headers based on the supplied options.\n *\n * @param {string} method - The request method. Defaults to \"get\".\n * @param {string} contentType - The content type of the request. Defaults to \"application/json\".\n * @param {object} extraHeaders - Custom headers to add.\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction fetchHeaders(opts) {\n var _ref = opts || {},\n _ref$method = _ref.method,\n method = _ref$method === undefined ? 'get' : _ref$method,\n _ref$contentType = _ref.contentType,\n contentType = _ref$contentType === undefined ? contentTypes.json : _ref$contentType,\n extraHeaders = _ref.extraHeaders,\n _ref$useBearer = _ref.useBearer,\n useBearer = _ref$useBearer === undefined ? true : _ref$useBearer;\n\n var headers = new Headers({\n 'X-Requested-With': 'XMLHttpRequest'\n });\n headers.set('Content-Type', contentType);\n if (!/^(GET|HEAD|OPTIONS\\TRACE)$/i.test(method)) {\n headers.set('X-CSRFToken', ajaxSettings.csrf);\n }\n if (useBearer && ajaxSettings.bearer) {\n headers.set('Authorization', 'Bearer ' + ajaxSettings.bearer);\n }\n for (var k in extraHeaders || {}) {\n headers.set(k, extraHeaders[k]);\n }\n return headers;\n}\n\nfunction makeRequest(opts) {\n var method = (opts.method || 'get').toLowerCase();\n\n var _ref2 = opts || {},\n url = _ref2.url,\n body = _ref2.body,\n contentType = _ref2.contentType,\n extraHeaders = _ref2.extraHeaders,\n _ref2$useBearer = _ref2.useBearer,\n useBearer = _ref2$useBearer === undefined ? true : _ref2$useBearer;\n\n var request = {\n url: url,\n method: method,\n headers: fetchHeaders({\n method: method,\n contentType: contentType,\n extraHeaders: extraHeaders,\n useBearer: useBearer\n }),\n credentials: 'same-origin'\n };\n if (method != 'get' && method != 'head' && method != 'options') {\n request.body = body;\n }\n return request;\n}\n\n/**\n * Perform an ajax request.\n *\n * Uses HTML5 fetch to perform an ajax request according to parameters\n * supplied via the options object.\n *\n * @param {string} url - The URL to make the request to.\n * @param {string} method - The request method. Defaults to \"get\".\n * @param {string} body - Data to be sent with the request.\n * @param {string} contentType - The content type of the request. Defaults to \"application/json\".\n * @param {object} extraHeaders - Custom headers to add.\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction ajax(opts) {\n var method = (opts.method || 'get').toLowerCase();\n\n var _ref3 = opts || {},\n url = _ref3.url,\n body = _ref3.body,\n contentType = _ref3.contentType,\n extraHeaders = _ref3.extraHeaders,\n _ref3$useBearer = _ref3.useBearer,\n useBearer = _ref3$useBearer === undefined ? true : _ref3$useBearer;\n\n var requestInit = {\n method: method,\n headers: fetchHeaders({\n method: method,\n contentType: contentType,\n extraHeaders: extraHeaders,\n useBearer: useBearer\n }),\n credentials: 'same-origin'\n };\n if (method != 'get' && method != 'head' && method != 'options') {\n requestInit.body = body;\n }\n\n var request = new Request(url, requestInit);\n return fetch(request).then(function (response) {\n if (!!response.ok) {\n if (response.status == 204) {\n return {};\n }\n if (typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE) {\n return response;\n }\n if (!!response.json) {\n return response.json();\n } else {\n return response;\n }\n }\n if (!!response.json) {\n return response.json().catch(function (e) {\n return Object({ status: response.status });\n }).then(function (e) {\n return Promise.reject(e);\n });\n } else {\n return response;\n }\n });\n}\n\nfunction ajaxWithRequest(opts) {\n var url = opts.url,\n requestInit = _objectWithoutProperties(opts, ['url']);\n\n var request = new Request(url, requestInit);\n return fetch(request).then(function (response) {\n if (!!response.ok) {\n if (response.status == 204) {\n return {};\n }\n if (typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE) {\n return response;\n }\n if (!!response.json) {\n return response.json();\n } else {\n return response;\n }\n }\n if (!!response.json) {\n return response.json().catch(function (e) {\n return Object({ status: response.status });\n }).then(function (e) {\n return Promise.reject(e);\n });\n } else {\n return response;\n }\n });\n}\n\n/**\n * Post JSON data.\n *\n * @param {string} url - The URL to make the request to.\n * @param {object} payload - Data to be sent with the request.\n * @param {string} contentType - The content type of the request. Defaults to \"application/json\".\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction postJson(_ref4) {\n var url = _ref4.url,\n payload = _ref4.payload,\n contentType = _ref4.contentType,\n useBearer = _ref4.useBearer;\n\n return ajax({\n url: url,\n method: 'post',\n body: JSON.stringify(payload || {}),\n contentType: contentType,\n useBearer: useBearer\n });\n}\n\n/**\n * Convert an object into HTML5 FormData.\n */\nfunction makeFormData(payload) {\n var body = new FormData();\n for (var k in payload || {}) {\n body.append(k, payload[k]);\n }\n return body;\n}\n\n/**\n * Post form data.\n *\n * @param {string} url - The URL to make the request to.\n * @param {object} payload - Data to be sent with the request.\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction postForm(_ref5) {\n var url = _ref5.url,\n payload = _ref5.payload,\n useBearer = _ref5.useBearer;\n\n return ajax({\n url: url,\n body: makeFormData(payload),\n method: 'post',\n useBearer: useBearer\n });\n}\n\nexports.debug = debug;\nexports.addTrailingSlash = addTrailingSlash;\nexports.ajax = ajax;\nexports.postJson = postJson;\nexports.postForm = postForm;\nexports.ajaxSettings = ajaxSettings;\nexports.contentTypes = contentTypes;\nexports.matchContentType = matchContentType;\nexports.makeFormData = makeFormData;\nexports.makeRequest = makeRequest;\nexports.ajaxWithRequest = ajaxWithRequest;\nexports.fetchHeaders = fetchHeaders;\n\n//# sourceURL=webpack://js-tinyapi/./src/utils.js?"); | ||
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.fetchHeaders = exports.ajaxWithRequest = exports.makeRequest = exports.makeFormData = exports.matchContentType = exports.contentTypes = exports.ajaxSettings = exports.postForm = exports.postJson = exports.ajax = exports.addTrailingSlash = exports.debug = undefined;\nexports.supplant = supplant;\nexports.capitalize = capitalize;\n\nvar _jsCookie = __webpack_require__(/*! js-cookie */ \"js-cookie\");\n\nvar _jsCookie2 = _interopRequireDefault(_jsCookie);\n\nvar _errors = __webpack_require__(/*! ./errors */ \"./src/errors.js\");\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }\n\n/**\n * Make it a little easier to use content types.\n */\nvar contentTypes = {\n form: 'application/x-www-form-urlencoded',\n multiForm: 'multipart/form-data',\n json: 'application/json',\n jsonApi: 'application/vnd.api+json'\n};\n\n/**\n * TODO: Get rid of this, we can optimise better without it.\n */\nfunction debug() {\n if (console.debug) console.debug.apply(null, arguments);\n}\n\nfunction matchContentType(src, id) {\n if (!src) return false;\n var ii = src.indexOf(';');\n if (ii >= 0) src = src.substring(0, ii);\n return src.toLowerCase() == contentTypes[id];\n}\n\nfunction addTrailingSlash(path) {\n return path + (path[path.length - 1] == '/' ? '' : '/');\n}\n\n/**\n * Find and replace terms in a string.\n *\n * Terms are identified by being surrounded by curly braces, such as\n * this: \"a string with a {substitution}\". Here, \"substitution\" will\n * be replaced by looking for the same named key in the supplied\n * mapping.\n *\n * @param {string} text - The string with replacements.\n * @param {object} mapping - The mapping.\n */\nfunction supplant(text, mapping) {\n return text.replace(/{([^{}]*)}/g, function (a, b) {\n var r = mapping[b];\n if (r === undefined) {\n throw new _errors.ApiError('Missing string template: ' + b);\n }\n return typeof r === 'string' || typeof r === 'number' ? r : a;\n });\n}\n\n/**\n * Capitalize a string.\n *\n * @param {string} text - The string to capitalize.\n */\nfunction capitalize(text) {\n return text[0].toUpperCase() + text.slice(1);\n}\n\n/**\n * Global storage for authorization and CSRF.\n *\n * Often when making requests the same credentials or CSRF token needs\n * to be used. This is a place to store these details. Currently\n * accepts \"csrf\" and \"bearer\" values. Upon initialisation cookies\n * are examined for a current value for the CSRF token (looks for\n * a cookie called \"csrftoken\").\n */\nvar ajaxSettings = {\n csrf: _jsCookie2.default ? _jsCookie2.default.get('csrftoken') || '' : '',\n bearer: null\n};\n\n/**\n * Construct headers for a fetch request.\n *\n * Uses the HTML5 Headers object to formulate an appropriate set of\n * headers based on the supplied options.\n *\n * @param {string} method - The request method. Defaults to \"get\".\n * @param {string} contentType - The content type of the request. Defaults to \"application/json\".\n * @param {object} extraHeaders - Custom headers to add.\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction fetchHeaders(opts) {\n var _ref = opts || {},\n _ref$method = _ref.method,\n method = _ref$method === undefined ? 'get' : _ref$method,\n _ref$contentType = _ref.contentType,\n contentType = _ref$contentType === undefined ? contentTypes.json : _ref$contentType,\n extraHeaders = _ref.extraHeaders,\n _ref$useBearer = _ref.useBearer,\n useBearer = _ref$useBearer === undefined ? true : _ref$useBearer,\n bearer = _ref.bearer;\n\n var headers = new Headers({\n 'X-Requested-With': 'XMLHttpRequest'\n });\n headers.set('Content-Type', contentType);\n if (!/^(GET|HEAD|OPTIONS\\TRACE)$/i.test(method)) {\n headers.set('X-CSRFToken', ajaxSettings.csrf);\n }\n if (!bearer) bearer = ajaxSettings.bearer;\n if (useBearer && bearer) {\n headers.set('Authorization', 'Bearer ' + bearer);\n }\n for (var k in extraHeaders || {}) {\n headers.set(k, extraHeaders[k]);\n }\n return headers;\n}\n\nfunction makeRequest(opts) {\n var method = (opts.method || 'get').toUpperCase();\n\n var _ref2 = opts || {},\n url = _ref2.url,\n body = _ref2.body,\n contentType = _ref2.contentType,\n extraHeaders = _ref2.extraHeaders,\n _ref2$useBearer = _ref2.useBearer,\n useBearer = _ref2$useBearer === undefined ? true : _ref2$useBearer,\n bearer = _ref2.bearer;\n\n var request = {\n url: url,\n method: method,\n headers: fetchHeaders({\n method: method,\n contentType: contentType,\n extraHeaders: extraHeaders,\n useBearer: useBearer,\n bearer: bearer\n }),\n credentials: 'same-origin'\n };\n if (method != 'GET' && method != 'HEAD' && method != 'OPTIONS') {\n request.body = body;\n }\n return request;\n}\n\n/**\n * Perform an ajax request.\n *\n * Uses HTML5 fetch to perform an ajax request according to parameters\n * supplied via the options object.\n *\n * @param {string} url - The URL to make the request to.\n * @param {string} method - The request method. Defaults to \"get\".\n * @param {string} body - Data to be sent with the request.\n * @param {string} contentType - The content type of the request. Defaults to \"application/json\".\n * @param {object} extraHeaders - Custom headers to add.\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction ajax(opts) {\n var method = (opts.method || 'get').toUpperCase();\n\n var _ref3 = opts || {},\n url = _ref3.url,\n body = _ref3.body,\n contentType = _ref3.contentType,\n extraHeaders = _ref3.extraHeaders,\n _ref3$useBearer = _ref3.useBearer,\n useBearer = _ref3$useBearer === undefined ? true : _ref3$useBearer,\n bearer = _ref3.bearer;\n\n var requestInit = {\n method: method,\n headers: fetchHeaders({\n method: method,\n contentType: contentType,\n extraHeaders: extraHeaders,\n useBearer: useBearer,\n bearer: bearer\n }),\n credentials: 'same-origin'\n };\n if (method != 'GET' && method != 'HEAD' && method != 'OPTIONS') {\n requestInit.body = body;\n }\n\n var request = new Request(url, requestInit);\n return fetch(request).then(function (response) {\n if (!!response.ok) {\n if (response.status == 204) {\n return {};\n }\n if (typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE) {\n return response;\n }\n if (!!response.json) {\n return response.json();\n } else {\n return response;\n }\n }\n if (!!response.json) {\n return response.json().catch(function (e) {\n return Object({ status: response.status });\n }).then(function (e) {\n return Promise.reject(e);\n });\n } else {\n return response;\n }\n });\n}\n\nfunction ajaxWithRequest(opts) {\n var url = opts.url,\n requestInit = _objectWithoutProperties(opts, ['url']);\n\n var request = new Request(url, requestInit);\n return fetch(request).then(function (response) {\n if (!!response.ok) {\n if (response.status == 204) {\n return {};\n }\n if (typeof TINYAPI_NODE !== 'undefined' && TINYAPI_NODE) {\n return response;\n }\n if (!!response.json) {\n return response.json();\n } else {\n return response;\n }\n }\n if (!!response.json) {\n return response.json().catch(function (e) {\n return Object({ status: response.status });\n }).then(function (e) {\n return Promise.reject(e);\n });\n } else {\n return response;\n }\n });\n}\n\n/**\n * Post JSON data.\n *\n * @param {string} url - The URL to make the request to.\n * @param {object} payload - Data to be sent with the request.\n * @param {string} contentType - The content type of the request. Defaults to \"application/json\".\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction postJson(_ref4) {\n var url = _ref4.url,\n payload = _ref4.payload,\n contentType = _ref4.contentType,\n useBearer = _ref4.useBearer;\n\n return ajax({\n url: url,\n method: 'post',\n body: JSON.stringify(payload || {}),\n contentType: contentType,\n useBearer: useBearer\n });\n}\n\n/**\n * Convert an object into HTML5 FormData.\n */\nfunction makeFormData(payload) {\n var body = new FormData();\n for (var k in payload || {}) {\n body.append(k, payload[k]);\n }\n return body;\n}\n\n/**\n * Post form data.\n *\n * @param {string} url - The URL to make the request to.\n * @param {object} payload - Data to be sent with the request.\n * @param {boolean} useBearer - Flag indicating whether to include bearer authorization.\n */\nfunction postForm(_ref5) {\n var url = _ref5.url,\n payload = _ref5.payload,\n useBearer = _ref5.useBearer;\n\n return ajax({\n url: url,\n body: makeFormData(payload),\n method: 'post',\n useBearer: useBearer\n });\n}\n\nexports.debug = debug;\nexports.addTrailingSlash = addTrailingSlash;\nexports.ajax = ajax;\nexports.postJson = postJson;\nexports.postForm = postForm;\nexports.ajaxSettings = ajaxSettings;\nexports.contentTypes = contentTypes;\nexports.matchContentType = matchContentType;\nexports.makeFormData = makeFormData;\nexports.makeRequest = makeRequest;\nexports.ajaxWithRequest = ajaxWithRequest;\nexports.fetchHeaders = fetchHeaders;\n\n//# sourceURL=webpack://js-tinyapi/./src/utils.js?"); | ||
@@ -157,0 +157,0 @@ /***/ }), |
{ | ||
"name": "js-tinyapi", | ||
"version": "0.1.0-alpha.3", | ||
"version": "0.1.0-alpha.4", | ||
"description": "A simple and lightweight API helper.", | ||
@@ -15,2 +15,3 @@ "repository": { | ||
"build": "webpack --config webpack.config.js --mode development", | ||
"watch": "webpack --config webpack.config.js --mode development --watch", | ||
"build:prod": "webpack --config webpack.config.js --mode production", | ||
@@ -17,0 +18,0 @@ "prepublish": "npm run build", |
215
README.md
@@ -1,2 +0,2 @@ | ||
# redux-tinyapi | ||
# js-tinyapi | ||
@@ -6,2 +6,213 @@ [![npm version](https://badge.fury.io/js/js-tinyapi.svg)](http://badge.fury.io/js/js-tinyapi) | ||
TODO | ||
## Installing | ||
```sh | ||
yarn add js-tinyapi | ||
``` | ||
## Setup | ||
Create a custom api object containing all the endpoints you create | ||
```javascript | ||
import API from 'js-tinyapi' | ||
const api = new API() | ||
``` | ||
## Example | ||
Perform a `GET` request to an endpoint | ||
```javascript | ||
// make endpoint | ||
api.makeEndpoint('people', '/api/people', 'GET') | ||
// call the endpoint | ||
api.people() | ||
.then(data => { | ||
console.log(data) | ||
}) | ||
.catch(error => { | ||
console.log(error) | ||
}) | ||
``` | ||
Perform a `POST` request an endpoint | ||
```javascript | ||
// make endpoint | ||
api.makeEndpoint('people', '/api/people', 'POST') | ||
// call the endpoint passing in the post payload | ||
api.people({ | ||
payload: {name: 'Mary'} | ||
}) | ||
.then(data => { | ||
console.log(data) | ||
}) | ||
.catch(error => { | ||
console.log(error) | ||
}) | ||
``` | ||
Perform a custom request | ||
```javascript | ||
options = { | ||
method: 'GET', | ||
path: '/api/people', | ||
params: {}, | ||
type: 'json', | ||
payload: undefined, | ||
contentType: undefined, | ||
include: [] | ||
} | ||
// call the endpoint with options | ||
api.request(null, options) | ||
.then(data => { | ||
console.log(data) | ||
}) | ||
.catch(error => { | ||
console.log(error) | ||
}) | ||
``` | ||
## Create CRUD endpoints | ||
```javascript | ||
api.makeCrudEndpoints('people', '/api/') | ||
api.peopleList() // GET /api/people | ||
api.peopleCreate(payload) // POST /api/people with payload | ||
api.peopleDetail(123) // GET /api/people?id=123 | ||
api.peopleUpdate(123, payload) // PATCH /api/people?id=123 with payload | ||
api.peopleRemove(123) // DELETE /api/people?id=123 | ||
api.peopleOptions() // OPTIONS /api/people | ||
``` | ||
## Merge in new endpoints | ||
Merge in `POST` and/or `GET` endpoints | ||
```javascript | ||
api.merge({ | ||
api: { | ||
people: { | ||
GET: { | ||
name: 'peopleGet' | ||
}, | ||
POST: { | ||
name: 'peoplePost' | ||
} | ||
} | ||
} | ||
}) | ||
api.peopleGet() // GET /api/people | ||
api.peoplePost(payload) // POST /api/people with payload | ||
``` | ||
Merge in a `CRUD` endpoint. The result of this merge is equivalent to the above [Create CRUD endpoints](#Create-CRUD-endpoints) example. | ||
```javascript | ||
api.merge({ | ||
api: { | ||
people: { | ||
CRUD: { | ||
name: 'people' | ||
} | ||
} | ||
} | ||
}) | ||
// OR | ||
api.merge({ | ||
api: { | ||
people: 'CRUD' | ||
} | ||
}) | ||
// equivalent to | ||
api.makeCrudEndpoints('people', '/api/') | ||
``` | ||
## Middleware | ||
A middleware layer is provided in order to easily alter the characteristics | ||
of requests made through `js-tinyapi`. Three kinds of middleware may be | ||
created: | ||
1. Request altering middleware. | ||
2. Response altering middleware. | ||
3. Fetch middleware. | ||
The first, request altering middleware, is able to modify a request prior to | ||
being fetched. The second, response altering middleware, is able to modify | ||
a response after having been returned. The last, fetch middleware, is able | ||
to alter how each request is sent to a server. | ||
To create a basic middleware for modifying a request, inherit from the provided | ||
middleware baseclass and override the `process` method: | ||
```javascript | ||
import Middleware from './middleware' | ||
// Add an extra slash to all request URLs. | ||
class AddSlash extends Middleware { | ||
process = request => { | ||
return { | ||
...request, | ||
url: request.url + '/' | ||
} | ||
} | ||
} | ||
``` | ||
The above middleware returns a new request with an extra slash added to the URL. | ||
To create a middleware that performs a fetch, simply return a promise that will | ||
be resolved once the fetch has completed: | ||
```javascript | ||
import Middleware from './middleware' | ||
// Add an extra slash to all request URLs. | ||
class DelayedFetch extends Middleware { | ||
process = request => { | ||
return new Promise( (resolve, reject) => { | ||
setTimeout( () => { | ||
this.submit( request ) | ||
.then( r => resolve( r ) ) | ||
}, 500 ) | ||
}) | ||
} | ||
} | ||
``` | ||
The above will add a 500ms delay to all requests. Notice the use of `this.submit`; | ||
this is a helper method to submit the supplied request object. | ||
As an example, a batching middleware is provided. It can be enabled as such: | ||
```javascript | ||
import API, { Batch } from 'js-tinyapi' | ||
const api = new API() | ||
// prepare API as usual | ||
api.pushMiddleware( | ||
new Batch({ | ||
batchUrl: 'http://your.domain/api/batch/', | ||
timeout: 50 | ||
}) | ||
) | ||
``` | ||
This causes each incoming request to be "held" for up to 50 milliseconds, | ||
waiting for further requests to be made. Once the timeout has expired, all | ||
collected requests are sent to the batch endpoint simultaneously. |
@@ -201,3 +201,4 @@ import { jsonApiQuery } from './jsonapi' | ||
filter = (endpoint.filter || {}), | ||
sort = (endpoint.sort || []) | ||
sort = (endpoint.sort || []), | ||
fields = (endpoint.fields || []) | ||
} = options | ||
@@ -221,3 +222,3 @@ let { | ||
if( payload !== undefined ) { | ||
if( matchContentType( contentType, 'form' ) ) { | ||
if( matchContentType( contentType, 'form' ) || matchContentType( contentType, 'multiForm' ) ) { | ||
body = makeFormData( payload ) | ||
@@ -243,3 +244,3 @@ } | ||
// Add any JSONAPI query strings. | ||
finalPath += jsonApiQuery({ initial: queryString, include, filter, sort }) | ||
finalPath += jsonApiQuery({initial: queryString, include, filter, sort, fields}) | ||
@@ -262,3 +263,4 @@ // If we've been given an URL root, add it in here. This is useful | ||
contentType, | ||
extraHeaders | ||
extraHeaders, | ||
bearer: this.bearer | ||
} | ||
@@ -265,0 +267,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
86958
526
218