From 06076fe392580485c2234687441e504aa80109ff Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Sat, 20 Sep 2014 21:05:13 -0600 Subject: [PATCH] Stable Version 0.2.0. --- CHANGELOG.md | 7 +- bower.json | 2 +- dist/js-data-http.js | 407 +++++++-------------------------------- dist/js-data-http.min.js | 4 +- package.json | 2 +- src/index.js | 405 +++++++------------------------------- 6 files changed, 147 insertions(+), 680 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fb0741..91f4065 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +##### 0.2.0 - 2 September 2014 + +###### Backwards compatible API changes +- Added deserialize and serialize. + ##### 0.1.0 - 17 September 2014 -Initial release +- Initial release diff --git a/bower.json b/bower.json index 3ddb023..88fd44b 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "js-data-http", "description": "http adapter for js-data.", - "version": "0.1.0", + "version": "0.2.0", "homepage": "http://www.js-data.io/js-data-http", "repository": { "type": "git", diff --git a/dist/js-data-http.js b/dist/js-data-http.js index 104a7bb..b90abfb 100644 --- a/dist/js-data-http.js +++ b/dist/js-data-http.js @@ -1,7 +1,7 @@ /** * @author Jason Dobry * @file js-data-http.js -* @version 0.1.0 - Homepage +* @version 0.2.0 - Homepage * @copyright (c) 2014 Jason Dobry * @license MIT * @@ -1304,141 +1304,65 @@ function Defaults() { } -/** - * @doc property - * @id DSHttpAdapter.properties:defaults.queryTransform - * @name defaults.queryTransform - * @description - * Transform the js-data query to something your server understands. You might just do this on the server instead. - * - * ## Example: - * ```js - * DSHttpAdapter.defaults.queryTransform = function (resourceName, params) { - * if (params && params.userId) { - * params.user_id = params.userId; - * delete params.userId; - * } - * return params; - * }; - * ``` - * - * @param {string} resourceName The name of the resource. - * @param {object} params Params that will be passed to `http`. - * @returns {*} By default just returns `params` as-is. - */ -Defaults.prototype.queryTransform = function (resourceName, params) { +var defaultsPrototype = Defaults.prototype; + +defaultsPrototype.queryTransform = function (resourceName, params) { return params; }; -/** - * @doc property - * @id DSHttpAdapter.properties:defaults.httpConfig - * @name defaults.httpConfig - * @description - * Default http configuration options used whenever `DSHttpAdapter` uses `http`. - * - * ## Example: - * ```js - * var dsHttpAdapter = new DSHttpAdapter({ - * httpConfig: { - * headers: { - * Authorization: 'Basic YmVlcDpib29w' - * }, - * timeout: 20000 - * }); - * }); - * ``` - */ -Defaults.prototype.httpConfig = {}; +defaultsPrototype.httpConfig = {}; + +defaultsPrototype.log = console ? console.log : function () { +}; + +defaultsPrototype.deserialize = function (resourceName, data) { + return data ? ('data' in data ? data.data : data) : data; +}; + +defaultsPrototype.serialize = function (resourceName, data) { + return data; +}; -/** - * @doc constructor - * @id DSHttpAdapter - * @name DSHttpAdapter - * @description - * Default adapter used by js-data. This adapter uses AJAX and JSON to send/retrieve data to/from a server. - * Developers may provide custom adapters that implement the adapter interface. - */ function DSHttpAdapter(options) { - /** - * @doc property - * @id DSHttpAdapter.properties:defaults - * @name defaults - * @description - * Reference to [DSHttpAdapter.defaults](/documentation/api/api/DSHttpAdapter.properties:defaults). - */ this.defaults = new Defaults(); deepMixIn(this.defaults, options); } -/** - * @doc method - * @id DSHttpAdapter.methods:HTTP - * @name HTTP - * @description - * A wrapper for `http()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.HTTP(config) - * ``` - * - * @param {object} config Configuration object. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.HTTP = function (config) { +var dsHttpAdapterPrototype = DSHttpAdapter.prototype; + +dsHttpAdapterPrototype.HTTP = function (config, resourceConfig) { + var _this = this; var start = new Date().getTime(); + var deserialize; + if (config.deserialize) { + deserialize = config.deserialize; + delete config.deserialize; + } else { + deserialize = _this.defaults.deserialize; + } + resourceConfig = resourceConfig || {}; config = deepMixIn(config, this.defaults.httpConfig); return http(config).then(function (data) { - console.log(data.config.method + ' request:' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', arguments); - return data.data; + if (_this.defaults.log) { + var args = Array.prototype.slice.call(arguments); + args.unshift(data.config.method + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms'); + _this.defaults.log.apply(_this.defaults.log, args); + } + return deserialize(resourceConfig.name, data); }); }; -/** - * @doc method - * @id DSHttpAdapter.methods:GET - * @name GET - * @description - * A wrapper for `http.get()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.GET(url[, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.GET = function (url, config) { +dsHttpAdapterPrototype.GET = function (url, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'get'; } return this.HTTP(deepMixIn(config, { url: url - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:POST - * @name POST - * @description - * A wrapper for `http.post()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.POST(url[, attrs][, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} attrs Request payload. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.POST = function (url, attrs, config) { +dsHttpAdapterPrototype.POST = function (url, attrs, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'post'; @@ -1446,27 +1370,10 @@ DSHttpAdapter.prototype.POST = function (url, attrs, config) { return this.HTTP(deepMixIn(config, { url: url, data: attrs - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:PUT - * @name PUT - * @description - * A wrapper for `http.put()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.PUT(url[, attrs][, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} attrs Request payload. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.PUT = function (url, attrs, config) { +dsHttpAdapterPrototype.PUT = function (url, attrs, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'put'; @@ -1474,92 +1381,29 @@ DSHttpAdapter.prototype.PUT = function (url, attrs, config) { return this.HTTP(deepMixIn(config, { url: url, data: attrs || {} - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:DEL - * @name DEL - * @description - * A wrapper for `http.delete()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.DEL(url[, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.DEL = function (url, config) { +dsHttpAdapterPrototype.DEL = function (url, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'delete'; } return this.HTTP(deepMixIn(config, { url: url - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:find - * @name find - * @description - * Retrieve a single entity from the server. - * - * Makes a `GET` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.find(resourceConfig, id[, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {string|number} id Primary key of the entity to update. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.find = function (resourceConfig, id, options) { +dsHttpAdapterPrototype.find = function (resourceConfig, id, options) { options = options || {}; return this.GET( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), - options + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:findAll - * @name findAll - * @description - * Retrieve a collection of entities from the server. - * - * Makes a `GET` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.findAll(resourceConfig[, params][, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object=} params Search query parameters. See the [query guide](/documentation/guide/queries/index). - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.findAll = function (resourceConfig, params, options) { +dsHttpAdapterPrototype.findAll = function (resourceConfig, params, options) { options = options || {}; options.params = options.params || {}; if (params) { @@ -1568,103 +1412,35 @@ DSHttpAdapter.prototype.findAll = function (resourceConfig, params, options) { } return this.GET( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), - options + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:create - * @name create - * @description - * Create a new entity on the server. - * - * Makes a `POST` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.create(resourceConfig, attrs[, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object} attrs The attribute payload. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.create = function (resourceConfig, attrs, options) { +dsHttpAdapterPrototype.create = function (resourceConfig, attrs, options) { + var _this = this; options = options || {}; return this.POST( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(attrs, options)), - attrs, - options + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:update - * @name update - * @description - * Update an entity on the server. - * - * Makes a `PUT` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.update(resourceConfig, id, attrs[, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {string|number} id Primary key of the entity to update. - * @param {object} attrs The attribute payload. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.update = function (resourceConfig, id, attrs, options) { +dsHttpAdapterPrototype.update = function (resourceConfig, id, attrs, options) { + var _this = this; options = options || {}; return this.PUT( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), - attrs, - options + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:updateAll - * @name updateAll - * @description - * Update a collection of entities on the server. - * - * Makes a `PUT` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.updateAll(resourceConfig, attrs[, params][, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object} attrs The attribute payload. - * @param {object=} params Search query parameters. See the [query guide](/documentation/guide/queries/index). - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.updateAll = function (resourceConfig, attrs, params, options) { +dsHttpAdapterPrototype.updateAll = function (resourceConfig, attrs, params, options) { + var _this = this; options = options || {}; options.params = options.params || {}; if (params) { @@ -1673,68 +1449,22 @@ DSHttpAdapter.prototype.updateAll = function (resourceConfig, attrs, params, opt } return this.PUT( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), - attrs, - options + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:destroy - * @name destroy - * @description - * Delete an entity on the server. - * - * Makes a `DELETE` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.destroy(resourceConfig, id[, options) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {string|number} id Primary key of the entity to update. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.destroy = function (resourceConfig, id, options) { +dsHttpAdapterPrototype.destroy = function (resourceConfig, id, options) { options = options || {}; return this.DEL( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), - options + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:destroyAll - * @name destroyAll - * @description - * Delete a collection of entities on the server. - * - * Makes `DELETE` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.destroyAll(resourceConfig[, params][, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object=} params Search query parameters. See the [query guide](/documentation/guide/queries/index). - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.destroyAll = function (resourceConfig, params, options) { +dsHttpAdapterPrototype.destroyAll = function (resourceConfig, params, options) { options = options || {}; options.params = options.params || {}; if (params) { @@ -1743,7 +1473,8 @@ DSHttpAdapter.prototype.destroyAll = function (resourceConfig, params, options) } return this.DEL( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), - options + options, + resourceConfig ); }; diff --git a/dist/js-data-http.min.js b/dist/js-data-http.min.js index 03304c4..1f3bf20 100644 --- a/dist/js-data-http.min.js +++ b/dist/js-data-http.min.js @@ -1,10 +1,10 @@ /** * @author Jason Dobry * @file js-data-http.min.js -* @version 0.1.0 - Homepage +* @version 0.2.0 - Homepage * @copyright (c) 2014 Jason Dobry * @license MIT * * @overview My Adapter. */ -!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.DSHttpAdapter=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g=200&&m.status<300?a:b)(d),m=null}};var n=h(j.url)?d.read(j.xsrfCookieName||e.xsrfCookieName):void 0;if(n&&(l[j.xsrfHeaderName||e.xsrfHeaderName]=n),i.forEach(l,function(a,b){k||"content-type"!==b.toLowerCase()?m.setRequestHeader(b,a):delete l[b]}),j.withCredentials&&(m.withCredentials=!0),j.responseType)try{m.responseType=j.responseType}catch(o){if("json"!==m.responseType)throw o}m.send(k)}},{"./../buildUrl":4,"./../cookies":5,"./../defaults":6,"./../parseHeaders":7,"./../transformData":9,"./../urlIsSameOrigin":10,"./../utils":11}],3:[function(a,b){(function(c){function d(){h.forEach(arguments,function(a){j[a]=function(b,c){return j(h.merge(c||{},{method:a,url:b}))}})}function e(){h.forEach(arguments,function(a){j[a]=function(b,c,d){return j(h.merge(d||{},{method:a,url:b,data:c}))}})}var f=a("es6-promise").Promise,g=a("./defaults"),h=a("./utils"),i=a("./spread"),j=b.exports=function(b){b=h.merge({method:"get",transformRequest:g.transformRequest,transformResponse:g.transformResponse},b),b.withCredentials=b.withCredentials||g.withCredentials;var d=new f(function(d,e){try{"undefined"!=typeof window?a("./adapters/xhr")(d,e,b):"undefined"!=typeof c&&a("./adapters/http")(d,e,b)}catch(f){e(f)}});return d.success=function(a){return d.then(function(b){a(b.data,b.status,b.headers,b.config)}),d},d.error=function(a){return d.then(null,function(b){a(b.data,b.status,b.headers,b.config)}),d},d};j.defaults=g,j.all=function(a){return f.all(a)},j.spread=i,d("delete","get","head"),e("post","put","patch")}).call(this,a("_process"))},{"./adapters/http":2,"./adapters/xhr":2,"./defaults":6,"./spread":8,"./utils":11,_process:22,"es6-promise":12}],4:[function(a,b){"use strict";function c(a){return encodeURIComponent(a).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+")}var d=a("./utils");b.exports=function(a,b){if(!b)return a;var e=[];return d.forEach(b,function(a,b){null!==a&&"undefined"!=typeof a&&(d.isArray(a)||(a=[a]),d.forEach(a,function(a){d.isDate(a)?a=a.toISOString():d.isObject(a)&&(a=JSON.stringify(a)),e.push(c(b)+"="+c(a))}))}),e.length>0&&(a+=(-1===a.indexOf("?")?"?":"&")+e.join("&")),a}},{"./utils":11}],5:[function(a,b){"use strict";var c=a("./utils");b.exports={write:function(a,b,d,e,f,g){var h=[];h.push(a+"="+encodeURIComponent(b)),c.isNumber(d)&&h.push("expires="+new Date(d).toGMTString()),c.isString(e)&&h.push("path="+e),c.isString(f)&&h.push("domain="+f),g===!0&&h.push("secure"),document.cookie=h.join("; ")},read:function(a){var b=document.cookie.match(new RegExp("(^|;\\s*)("+a+")=([^;]*)"));return b?decodeURIComponent(b[3]):null},remove:function(a){this.write(a,"",Date.now()-864e5)}}},{"./utils":11}],6:[function(a,b){"use strict";var c=a("./utils"),d=/^\s*(\[|\{[^\{])/,e=/[\}\]]\s*$/,f=/^\)\]\}',?\n/,g={"Content-Type":"application/json;charset=utf-8"};b.exports={transformRequest:[function(a){return!c.isObject(a)||c.isFile(a)||c.isBlob(a)?a:JSON.stringify(a)}],transformResponse:[function(a){return"string"==typeof a&&(a=a.replace(f,""),d.test(a)&&e.test(a)&&(a=JSON.parse(a))),a}],headers:{common:{Accept:"application/json, text/plain, */*"},patch:c.merge(g),post:c.merge(g),put:c.merge(g)},xsrfCookieName:"XSRF-TOKEN",xsrfHeaderName:"X-XSRF-TOKEN"}},{"./utils":11}],7:[function(a,b){"use strict";var c=a("./utils");b.exports=function(a){var b,d,e,f={};return a?(c.forEach(a.split("\n"),function(a){e=a.indexOf(":"),b=c.trim(a.substr(0,e)).toLowerCase(),d=c.trim(a.substr(e+1)),b&&(f[b]=f[b]?f[b]+", "+d:d)}),f):f}},{"./utils":11}],8:[function(a,b){b.exports=function(a){return function(b){a.apply(null,b)}}},{}],9:[function(a,b){"use strict";var c=a("./utils");b.exports=function(a,b,d){return c.forEach(d,function(c){a=c(a,b)}),a}},{"./utils":11}],10:[function(a,b){"use strict";function c(a){var b=a;return d&&(f.setAttribute("href",b),b=f.href),f.setAttribute("href",b),{href:f.href,protocol:f.protocol?f.protocol.replace(/:$/,""):"",host:f.host,search:f.search?f.search.replace(/^\?/,""):"",hash:f.hash?f.hash.replace(/^#/,""):"",hostname:f.hostname,port:f.port,pathname:"/"===f.pathname.charAt(0)?f.pathname:"/"+f.pathname}}var d=/(msie|trident)/i.test(navigator.userAgent),e=a("./utils"),f=document.createElement("a"),g=c(window.location.href);b.exports=function(a){var b=e.isString(a)?c(a):a;return b.protocol===g.protocol&&b.host===g.host}},{"./utils":11}],11:[function(a,b){function c(a){return"[object Array]"===m.call(a)}function d(a){return"string"==typeof a}function e(a){return"number"==typeof a}function f(a){return null!==a&&"object"==typeof a}function g(a){return"[object Date]"===m.call(a)}function h(a){return"[object File]"===m.call(a)}function i(a){return"[object Blob]"===m.call(a)}function j(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")}function k(a,b){if(null!==a&&"undefined"!=typeof a){var c=a.constructor===Array||"function"==typeof a.callee;if("object"==typeof a||c||(a=[a]),c)for(var d=0,e=a.length;e>d;d++)b.call(null,a[d],d,a);else for(var f in a)a.hasOwnProperty(f)&&b.call(null,a[f],f,a)}}function l(){var a={};return k(arguments,function(b){k(b,function(b,c){a[c]=b})}),a}var m=Object.prototype.toString;b.exports={isArray:c,isString:d,isNumber:e,isObject:f,isDate:g,isFile:h,isBlob:i,forEach:k,merge:l,trim:j}},{}],12:[function(a,b,c){"use strict";var d=a("./promise/promise").Promise,e=a("./promise/polyfill").polyfill;c.Promise=d,c.polyfill=e},{"./promise/polyfill":16,"./promise/promise":17}],13:[function(a,b,c){"use strict";function d(a){var b=this;if(!e(a))throw new TypeError("You must pass an array to all.");return new b(function(b,c){function d(a){return function(b){e(a,b)}}function e(a,c){h[a]=c,0===--i&&b(h)}var g,h=[],i=a.length;0===i&&b([]);for(var j=0;j0)){var d=c.shift();d()}},!0),function(a){c.push(a),window.postMessage("process-tick","*")}}return function(a){setTimeout(a,0)}}(),d.title="browser",d.browser=!0,d.env={},d.argv=[],d.on=c,d.addListener=c,d.once=c,d.off=c,d.removeListener=c,d.removeAllListeners=c,d.emit=c,d.binding=function(){throw new Error("process.binding is not supported")},d.cwd=function(){return"/"},d.chdir=function(){throw new Error("process.chdir is not supported")}},{}],23:[function(a,b){function c(){}function d(a){this.defaults=new c,g(this.defaults,a)}var e;e=!window&&"undefined"!=typeof b&&b.exports?a("js-data"):window.JSData;var f=e.DSUtils.makePath,g=e.DSUtils.deepMixIn,h=a("axios");c.prototype.queryTransform=function(a,b){return b},c.prototype.httpConfig={},d.prototype.HTTP=function(a){var b=(new Date).getTime();return a=g(a,this.defaults.httpConfig),h(a).then(function(a){return console.log(a.config.method+" request:"+a.config.url+" Time taken: "+((new Date).getTime()-b)+"ms",arguments),a.data})},d.prototype.GET=function(a,b){return b=b||{},"method"in b||(b.method="get"),this.HTTP(g(b,{url:a}))},d.prototype.POST=function(a,b,c){return c=c||{},"method"in c||(c.method="post"),this.HTTP(g(c,{url:a,data:b}))},d.prototype.PUT=function(a,b,c){return c=c||{},"method"in c||(c.method="put"),this.HTTP(g(c,{url:a,data:b||{}}))},d.prototype.DEL=function(a,b){return b=b||{},"method"in b||(b.method="delete"),this.HTTP(g(b,{url:a}))},d.prototype.find=function(a,b,c){return c=c||{},this.GET(f(c.baseUrl||a.baseUrl,a.getEndpoint(b,c),b),c)},d.prototype.findAll=function(a,b,c){return c=c||{},c.params=c.params||{},b&&(b=this.defaults.queryTransform(a.name,b),g(c.params,b)),this.GET(f(c.baseUrl||a.baseUrl,a.getEndpoint(null,c)),c)},d.prototype.create=function(a,b,c){return c=c||{},this.POST(f(c.baseUrl||a.baseUrl,a.getEndpoint(b,c)),b,c)},d.prototype.update=function(a,b,c,d){return d=d||{},this.PUT(f(d.baseUrl||a.baseUrl,a.getEndpoint(b,d),b),c,d)},d.prototype.updateAll=function(a,b,c,d){return d=d||{},d.params=d.params||{},c&&(c=this.defaults.queryTransform(a.name,c),g(d.params,c)),this.PUT(f(d.baseUrl||a.baseUrl,a.getEndpoint(null,d)),b,d)},d.prototype.destroy=function(a,b,c){return c=c||{},this.DEL(f(c.baseUrl||a.baseUrl,a.getEndpoint(b,c),b),c)},d.prototype.destroyAll=function(a,b,c){return c=c||{},c.params=c.params||{},b&&(b=this.defaults.queryTransform(a.name,b),g(c.params,b)),this.DEL(f(c.baseUrl||a.baseUrl,a.getEndpoint(null,c)),c)},b.exports=d},{axios:1,"js-data":"js-data"}]},{},[23])(23)}); \ No newline at end of file +!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.DSHttpAdapter=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g=200&&m.status<300?a:b)(d),m=null}};var n=h(j.url)?d.read(j.xsrfCookieName||e.xsrfCookieName):void 0;if(n&&(l[j.xsrfHeaderName||e.xsrfHeaderName]=n),i.forEach(l,function(a,b){k||"content-type"!==b.toLowerCase()?m.setRequestHeader(b,a):delete l[b]}),j.withCredentials&&(m.withCredentials=!0),j.responseType)try{m.responseType=j.responseType}catch(o){if("json"!==m.responseType)throw o}m.send(k)}},{"./../buildUrl":4,"./../cookies":5,"./../defaults":6,"./../parseHeaders":7,"./../transformData":9,"./../urlIsSameOrigin":10,"./../utils":11}],3:[function(a,b){(function(c){function d(){h.forEach(arguments,function(a){j[a]=function(b,c){return j(h.merge(c||{},{method:a,url:b}))}})}function e(){h.forEach(arguments,function(a){j[a]=function(b,c,d){return j(h.merge(d||{},{method:a,url:b,data:c}))}})}var f=a("es6-promise").Promise,g=a("./defaults"),h=a("./utils"),i=a("./spread"),j=b.exports=function(b){b=h.merge({method:"get",transformRequest:g.transformRequest,transformResponse:g.transformResponse},b),b.withCredentials=b.withCredentials||g.withCredentials;var d=new f(function(d,e){try{"undefined"!=typeof window?a("./adapters/xhr")(d,e,b):"undefined"!=typeof c&&a("./adapters/http")(d,e,b)}catch(f){e(f)}});return d.success=function(a){return d.then(function(b){a(b.data,b.status,b.headers,b.config)}),d},d.error=function(a){return d.then(null,function(b){a(b.data,b.status,b.headers,b.config)}),d},d};j.defaults=g,j.all=function(a){return f.all(a)},j.spread=i,d("delete","get","head"),e("post","put","patch")}).call(this,a("_process"))},{"./adapters/http":2,"./adapters/xhr":2,"./defaults":6,"./spread":8,"./utils":11,_process:22,"es6-promise":12}],4:[function(a,b){"use strict";function c(a){return encodeURIComponent(a).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+")}var d=a("./utils");b.exports=function(a,b){if(!b)return a;var e=[];return d.forEach(b,function(a,b){null!==a&&"undefined"!=typeof a&&(d.isArray(a)||(a=[a]),d.forEach(a,function(a){d.isDate(a)?a=a.toISOString():d.isObject(a)&&(a=JSON.stringify(a)),e.push(c(b)+"="+c(a))}))}),e.length>0&&(a+=(-1===a.indexOf("?")?"?":"&")+e.join("&")),a}},{"./utils":11}],5:[function(a,b){"use strict";var c=a("./utils");b.exports={write:function(a,b,d,e,f,g){var h=[];h.push(a+"="+encodeURIComponent(b)),c.isNumber(d)&&h.push("expires="+new Date(d).toGMTString()),c.isString(e)&&h.push("path="+e),c.isString(f)&&h.push("domain="+f),g===!0&&h.push("secure"),document.cookie=h.join("; ")},read:function(a){var b=document.cookie.match(new RegExp("(^|;\\s*)("+a+")=([^;]*)"));return b?decodeURIComponent(b[3]):null},remove:function(a){this.write(a,"",Date.now()-864e5)}}},{"./utils":11}],6:[function(a,b){"use strict";var c=a("./utils"),d=/^\s*(\[|\{[^\{])/,e=/[\}\]]\s*$/,f=/^\)\]\}',?\n/,g={"Content-Type":"application/json;charset=utf-8"};b.exports={transformRequest:[function(a){return!c.isObject(a)||c.isFile(a)||c.isBlob(a)?a:JSON.stringify(a)}],transformResponse:[function(a){return"string"==typeof a&&(a=a.replace(f,""),d.test(a)&&e.test(a)&&(a=JSON.parse(a))),a}],headers:{common:{Accept:"application/json, text/plain, */*"},patch:c.merge(g),post:c.merge(g),put:c.merge(g)},xsrfCookieName:"XSRF-TOKEN",xsrfHeaderName:"X-XSRF-TOKEN"}},{"./utils":11}],7:[function(a,b){"use strict";var c=a("./utils");b.exports=function(a){var b,d,e,f={};return a?(c.forEach(a.split("\n"),function(a){e=a.indexOf(":"),b=c.trim(a.substr(0,e)).toLowerCase(),d=c.trim(a.substr(e+1)),b&&(f[b]=f[b]?f[b]+", "+d:d)}),f):f}},{"./utils":11}],8:[function(a,b){b.exports=function(a){return function(b){a.apply(null,b)}}},{}],9:[function(a,b){"use strict";var c=a("./utils");b.exports=function(a,b,d){return c.forEach(d,function(c){a=c(a,b)}),a}},{"./utils":11}],10:[function(a,b){"use strict";function c(a){var b=a;return d&&(f.setAttribute("href",b),b=f.href),f.setAttribute("href",b),{href:f.href,protocol:f.protocol?f.protocol.replace(/:$/,""):"",host:f.host,search:f.search?f.search.replace(/^\?/,""):"",hash:f.hash?f.hash.replace(/^#/,""):"",hostname:f.hostname,port:f.port,pathname:"/"===f.pathname.charAt(0)?f.pathname:"/"+f.pathname}}var d=/(msie|trident)/i.test(navigator.userAgent),e=a("./utils"),f=document.createElement("a"),g=c(window.location.href);b.exports=function(a){var b=e.isString(a)?c(a):a;return b.protocol===g.protocol&&b.host===g.host}},{"./utils":11}],11:[function(a,b){function c(a){return"[object Array]"===m.call(a)}function d(a){return"string"==typeof a}function e(a){return"number"==typeof a}function f(a){return null!==a&&"object"==typeof a}function g(a){return"[object Date]"===m.call(a)}function h(a){return"[object File]"===m.call(a)}function i(a){return"[object Blob]"===m.call(a)}function j(a){return a.replace(/^\s*/,"").replace(/\s*$/,"")}function k(a,b){if(null!==a&&"undefined"!=typeof a){var c=a.constructor===Array||"function"==typeof a.callee;if("object"==typeof a||c||(a=[a]),c)for(var d=0,e=a.length;e>d;d++)b.call(null,a[d],d,a);else for(var f in a)a.hasOwnProperty(f)&&b.call(null,a[f],f,a)}}function l(){var a={};return k(arguments,function(b){k(b,function(b,c){a[c]=b})}),a}var m=Object.prototype.toString;b.exports={isArray:c,isString:d,isNumber:e,isObject:f,isDate:g,isFile:h,isBlob:i,forEach:k,merge:l,trim:j}},{}],12:[function(a,b,c){"use strict";var d=a("./promise/promise").Promise,e=a("./promise/polyfill").polyfill;c.Promise=d,c.polyfill=e},{"./promise/polyfill":16,"./promise/promise":17}],13:[function(a,b,c){"use strict";function d(a){var b=this;if(!e(a))throw new TypeError("You must pass an array to all.");return new b(function(b,c){function d(a){return function(b){e(a,b)}}function e(a,c){h[a]=c,0===--i&&b(h)}var g,h=[],i=a.length;0===i&&b([]);for(var j=0;j0)){var d=c.shift();d()}},!0),function(a){c.push(a),window.postMessage("process-tick","*")}}return function(a){setTimeout(a,0)}}(),d.title="browser",d.browser=!0,d.env={},d.argv=[],d.on=c,d.addListener=c,d.once=c,d.off=c,d.removeListener=c,d.removeAllListeners=c,d.emit=c,d.binding=function(){throw new Error("process.binding is not supported")},d.cwd=function(){return"/"},d.chdir=function(){throw new Error("process.chdir is not supported")}},{}],23:[function(a,b){function c(){}function d(a){this.defaults=new c,g(this.defaults,a)}var e;e=!window&&"undefined"!=typeof b&&b.exports?a("js-data"):window.JSData;var f=e.DSUtils.makePath,g=e.DSUtils.deepMixIn,h=a("axios"),i=c.prototype;i.queryTransform=function(a,b){return b},i.httpConfig={},i.log=console?console.log:function(){},i.deserialize=function(a,b){return b&&"data"in b?b.data:b},i.serialize=function(a,b){return b};var j=d.prototype;j.HTTP=function(a,b){var c,d=this,e=(new Date).getTime();return a.deserialize?(c=a.deserialize,delete a.deserialize):c=d.defaults.deserialize,b=b||{},a=g(a,this.defaults.httpConfig),h(a).then(function(a){if(d.defaults.log){var f=Array.prototype.slice.call(arguments);f.unshift(a.config.method+" request: "+a.config.url+" Time taken: "+((new Date).getTime()-e)+"ms"),d.defaults.log.apply(d.defaults.log,f)}return c(b.name,a)})},j.GET=function(a,b,c){return b=b||{},"method"in b||(b.method="get"),this.HTTP(g(b,{url:a}),c)},j.POST=function(a,b,c,d){return c=c||{},"method"in c||(c.method="post"),this.HTTP(g(c,{url:a,data:b}),d)},j.PUT=function(a,b,c,d){return c=c||{},"method"in c||(c.method="put"),this.HTTP(g(c,{url:a,data:b||{}}),d)},j.DEL=function(a,b,c){return b=b||{},"method"in b||(b.method="delete"),this.HTTP(g(b,{url:a}),c)},j.find=function(a,b,c){return c=c||{},this.GET(f(c.baseUrl||a.baseUrl,a.getEndpoint(b,c),b),c,a)},j.findAll=function(a,b,c){return c=c||{},c.params=c.params||{},b&&(b=this.defaults.queryTransform(a.name,b),g(c.params,b)),this.GET(f(c.baseUrl||a.baseUrl,a.getEndpoint(null,c)),c,a)},j.create=function(a,b,c){var d=this;return c=c||{},this.POST(f(c.baseUrl||a.baseUrl,a.getEndpoint(b,c)),c.serialize?c.serialize(a.name,b):d.defaults.serialize(a.name,b),c,a)},j.update=function(a,b,c,d){var e=this;return d=d||{},this.PUT(f(d.baseUrl||a.baseUrl,a.getEndpoint(b,d),b),d.serialize?d.serialize(a.name,c):e.defaults.serialize(a.name,c),d,a)},j.updateAll=function(a,b,c,d){var e=this;return d=d||{},d.params=d.params||{},c&&(c=this.defaults.queryTransform(a.name,c),g(d.params,c)),this.PUT(f(d.baseUrl||a.baseUrl,a.getEndpoint(null,d)),d.serialize?d.serialize(a.name,b):e.defaults.serialize(a.name,b),d,a)},j.destroy=function(a,b,c){return c=c||{},this.DEL(f(c.baseUrl||a.baseUrl,a.getEndpoint(b,c),b),c,a)},j.destroyAll=function(a,b,c){return c=c||{},c.params=c.params||{},b&&(b=this.defaults.queryTransform(a.name,b),g(c.params,b)),this.DEL(f(c.baseUrl||a.baseUrl,a.getEndpoint(null,c)),c,a)},b.exports=d},{axios:1,"js-data":"js-data"}]},{},[23])(23)}); \ No newline at end of file diff --git a/package.json b/package.json index 565a502..8fde7b7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "js-data-http", "description": "http adapter for js-data.", - "version": "0.1.0", + "version": "0.2.0", "homepage": "http://www.js-data.io/js-data-http", "repository": { "type": "git", diff --git a/src/index.js b/src/index.js index 880e589..97fba39 100644 --- a/src/index.js +++ b/src/index.js @@ -13,141 +13,65 @@ function Defaults() { } -/** - * @doc property - * @id DSHttpAdapter.properties:defaults.queryTransform - * @name defaults.queryTransform - * @description - * Transform the js-data query to something your server understands. You might just do this on the server instead. - * - * ## Example: - * ```js - * DSHttpAdapter.defaults.queryTransform = function (resourceName, params) { - * if (params && params.userId) { - * params.user_id = params.userId; - * delete params.userId; - * } - * return params; - * }; - * ``` - * - * @param {string} resourceName The name of the resource. - * @param {object} params Params that will be passed to `http`. - * @returns {*} By default just returns `params` as-is. - */ -Defaults.prototype.queryTransform = function (resourceName, params) { +var defaultsPrototype = Defaults.prototype; + +defaultsPrototype.queryTransform = function (resourceName, params) { return params; }; -/** - * @doc property - * @id DSHttpAdapter.properties:defaults.httpConfig - * @name defaults.httpConfig - * @description - * Default http configuration options used whenever `DSHttpAdapter` uses `http`. - * - * ## Example: - * ```js - * var dsHttpAdapter = new DSHttpAdapter({ - * httpConfig: { - * headers: { - * Authorization: 'Basic YmVlcDpib29w' - * }, - * timeout: 20000 - * }); - * }); - * ``` - */ -Defaults.prototype.httpConfig = {}; +defaultsPrototype.httpConfig = {}; + +defaultsPrototype.log = console ? console.log : function () { +}; + +defaultsPrototype.deserialize = function (resourceName, data) { + return data ? ('data' in data ? data.data : data) : data; +}; + +defaultsPrototype.serialize = function (resourceName, data) { + return data; +}; -/** - * @doc constructor - * @id DSHttpAdapter - * @name DSHttpAdapter - * @description - * Default adapter used by js-data. This adapter uses AJAX and JSON to send/retrieve data to/from a server. - * Developers may provide custom adapters that implement the adapter interface. - */ function DSHttpAdapter(options) { - /** - * @doc property - * @id DSHttpAdapter.properties:defaults - * @name defaults - * @description - * Reference to [DSHttpAdapter.defaults](/documentation/api/api/DSHttpAdapter.properties:defaults). - */ this.defaults = new Defaults(); deepMixIn(this.defaults, options); } -/** - * @doc method - * @id DSHttpAdapter.methods:HTTP - * @name HTTP - * @description - * A wrapper for `http()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.HTTP(config) - * ``` - * - * @param {object} config Configuration object. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.HTTP = function (config) { +var dsHttpAdapterPrototype = DSHttpAdapter.prototype; + +dsHttpAdapterPrototype.HTTP = function (config, resourceConfig) { + var _this = this; var start = new Date().getTime(); + var deserialize; + if (config.deserialize) { + deserialize = config.deserialize; + delete config.deserialize; + } else { + deserialize = _this.defaults.deserialize; + } + resourceConfig = resourceConfig || {}; config = deepMixIn(config, this.defaults.httpConfig); return http(config).then(function (data) { - console.log(data.config.method + ' request:' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', arguments); - return data.data; + if (_this.defaults.log) { + var args = Array.prototype.slice.call(arguments); + args.unshift(data.config.method + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms'); + _this.defaults.log.apply(_this.defaults.log, args); + } + return deserialize(resourceConfig.name, data); }); }; -/** - * @doc method - * @id DSHttpAdapter.methods:GET - * @name GET - * @description - * A wrapper for `http.get()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.GET(url[, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.GET = function (url, config) { +dsHttpAdapterPrototype.GET = function (url, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'get'; } return this.HTTP(deepMixIn(config, { url: url - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:POST - * @name POST - * @description - * A wrapper for `http.post()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.POST(url[, attrs][, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} attrs Request payload. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.POST = function (url, attrs, config) { +dsHttpAdapterPrototype.POST = function (url, attrs, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'post'; @@ -155,27 +79,10 @@ DSHttpAdapter.prototype.POST = function (url, attrs, config) { return this.HTTP(deepMixIn(config, { url: url, data: attrs - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:PUT - * @name PUT - * @description - * A wrapper for `http.put()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.PUT(url[, attrs][, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} attrs Request payload. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.PUT = function (url, attrs, config) { +dsHttpAdapterPrototype.PUT = function (url, attrs, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'put'; @@ -183,92 +90,29 @@ DSHttpAdapter.prototype.PUT = function (url, attrs, config) { return this.HTTP(deepMixIn(config, { url: url, data: attrs || {} - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:DEL - * @name DEL - * @description - * A wrapper for `http.delete()`. - * - * ## Signature: - * ```js - * DSHttpAdapter.DEL(url[, config]) - * ``` - * - * @param {string} url The url of the request. - * @param {object=} config Optional configuration. - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.DEL = function (url, config) { +dsHttpAdapterPrototype.DEL = function (url, config, resourceConfig) { config = config || {}; if (!('method' in config)) { config.method = 'delete'; } return this.HTTP(deepMixIn(config, { url: url - })); + }), resourceConfig); }; -/** - * @doc method - * @id DSHttpAdapter.methods:find - * @name find - * @description - * Retrieve a single entity from the server. - * - * Makes a `GET` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.find(resourceConfig, id[, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {string|number} id Primary key of the entity to update. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.find = function (resourceConfig, id, options) { +dsHttpAdapterPrototype.find = function (resourceConfig, id, options) { options = options || {}; return this.GET( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), - options + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:findAll - * @name findAll - * @description - * Retrieve a collection of entities from the server. - * - * Makes a `GET` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.findAll(resourceConfig[, params][, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object=} params Search query parameters. See the [query guide](/documentation/guide/queries/index). - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.findAll = function (resourceConfig, params, options) { +dsHttpAdapterPrototype.findAll = function (resourceConfig, params, options) { options = options || {}; options.params = options.params || {}; if (params) { @@ -277,103 +121,35 @@ DSHttpAdapter.prototype.findAll = function (resourceConfig, params, options) { } return this.GET( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), - options + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:create - * @name create - * @description - * Create a new entity on the server. - * - * Makes a `POST` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.create(resourceConfig, attrs[, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object} attrs The attribute payload. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.create = function (resourceConfig, attrs, options) { +dsHttpAdapterPrototype.create = function (resourceConfig, attrs, options) { + var _this = this; options = options || {}; return this.POST( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(attrs, options)), - attrs, - options + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:update - * @name update - * @description - * Update an entity on the server. - * - * Makes a `PUT` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.update(resourceConfig, id, attrs[, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {string|number} id Primary key of the entity to update. - * @param {object} attrs The attribute payload. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.update = function (resourceConfig, id, attrs, options) { +dsHttpAdapterPrototype.update = function (resourceConfig, id, attrs, options) { + var _this = this; options = options || {}; return this.PUT( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), - attrs, - options + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:updateAll - * @name updateAll - * @description - * Update a collection of entities on the server. - * - * Makes a `PUT` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.updateAll(resourceConfig, attrs[, params][, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object} attrs The attribute payload. - * @param {object=} params Search query parameters. See the [query guide](/documentation/guide/queries/index). - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.updateAll = function (resourceConfig, attrs, params, options) { +dsHttpAdapterPrototype.updateAll = function (resourceConfig, attrs, params, options) { + var _this = this; options = options || {}; options.params = options.params || {}; if (params) { @@ -382,68 +158,22 @@ DSHttpAdapter.prototype.updateAll = function (resourceConfig, attrs, params, opt } return this.PUT( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), - attrs, - options + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:destroy - * @name destroy - * @description - * Delete an entity on the server. - * - * Makes a `DELETE` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.destroy(resourceConfig, id[, options) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {string|number} id Primary key of the entity to update. - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.destroy = function (resourceConfig, id, options) { +dsHttpAdapterPrototype.destroy = function (resourceConfig, id, options) { options = options || {}; return this.DEL( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), - options + options, + resourceConfig ); }; -/** - * @doc method - * @id DSHttpAdapter.methods:destroyAll - * @name destroyAll - * @description - * Delete a collection of entities on the server. - * - * Makes `DELETE` request. - * - * ## Signature: - * ```js - * DSHttpAdapter.destroyAll(resourceConfig[, params][, options]) - * ``` - * - * @param {object} resourceConfig DS resource definition object: - * @param {object=} params Search query parameters. See the [query guide](/documentation/guide/queries/index). - * @param {object=} options Optional configuration. Also passed along to `http([config])`. Properties: - * - * - `{string=}` - `baseUrl` - Override the default base url. - * - `{string=}` - `endpoint` - Override the default endpoint. - * - `{object=}` - `params` - Additional query string parameters to add to the url. - * - * @returns {Promise} Promise. - */ -DSHttpAdapter.prototype.destroyAll = function (resourceConfig, params, options) { +dsHttpAdapterPrototype.destroyAll = function (resourceConfig, params, options) { options = options || {}; options.params = options.params || {}; if (params) { @@ -452,7 +182,8 @@ DSHttpAdapter.prototype.destroyAll = function (resourceConfig, params, options) } return this.DEL( makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), - options + options, + resourceConfig ); };