|
1 | | -/* global fetch:true Headers:true Request:true */ |
| 1 | +/* global fetch:true Headers:true */ |
2 | 2 |
|
3 | 3 | import {utils} from 'js-data' |
4 | 4 | import axios from '../node_modules/axios/dist/axios' |
@@ -53,7 +53,7 @@ function buildUrl (url, params) { |
53 | 53 | } |
54 | 54 |
|
55 | 55 | val.forEach(function (v) { |
56 | | - if (window.toString.call(v) === '[object Date]') { |
| 56 | + if (toString.call(v) === '[object Date]') { |
57 | 57 | v = v.toISOString() |
58 | 58 | } else if (utils.isObject(v)) { |
59 | 59 | v = utils.toJson(v) |
@@ -558,20 +558,19 @@ Adapter.extend({ |
558 | 558 | * @param {Object} config.headers Headers for the request. |
559 | 559 | * @param {Object} config.params Querystring for the request. |
560 | 560 | * @param {string} config.url Url for the request. |
561 | | - * @param {Object} [opts] Configuration options. |
562 | 561 | */ |
563 | | - fetch (config, opts) { |
| 562 | + fetch (config) { |
564 | 563 | const requestConfig = { |
565 | 564 | method: config.method, |
566 | 565 | // turn the plain headers object into the Fetch Headers object |
567 | | - headers: new Headers(config.headers) |
| 566 | + headers: new Headers(config.headers || {}) |
568 | 567 | } |
569 | 568 |
|
570 | 569 | if (config.data) { |
571 | 570 | requestConfig.body = utils.toJson(config.data) |
572 | 571 | } |
573 | 572 |
|
574 | | - return fetch(new Request(buildUrl(config.url, config.params), requestConfig)) |
| 573 | + return fetch(buildUrl(config.url, config.params), requestConfig) |
575 | 574 | .then((response) => { |
576 | 575 | response.config = { |
577 | 576 | method: config.method, |
@@ -969,7 +968,7 @@ Adapter.extend({ |
969 | 968 | sum (mapper, field, query, opts) { |
970 | 969 | query || (query = {}) |
971 | 970 | opts || (opts = {}) |
972 | | - if (!utils.utils.isString(field)) { |
| 971 | + if (!utils.isString(field)) { |
973 | 972 | throw new Error('field must be a string!') |
974 | 973 | } |
975 | 974 | opts.params = this.getParams(opts) |
@@ -1064,7 +1063,7 @@ Adapter.extend({ |
1064 | 1063 | * |
1065 | 1064 | * // GET /reports/schools/:school_id/teachers |
1066 | 1065 | * addAction('getTeacherReports', { |
1067 | | - * basePath: 'reports/schools', |
| 1066 | + * endpoint: 'reports/schools', |
1068 | 1067 | * pathname: 'teachers', |
1069 | 1068 | * method: 'GET' |
1070 | 1069 | * })(store.getMapper('school')) |
@@ -1098,42 +1097,39 @@ export function addAction (name, opts) { |
1098 | 1097 | opts.response = opts.response || function (response) { return response } |
1099 | 1098 | opts.responseError = opts.responseError || function (err) { return utils.reject(err) } |
1100 | 1099 | mapper[name] = function (id, _opts) { |
| 1100 | + _opts = _opts || {} |
1101 | 1101 | if (utils.isObject(id)) { |
1102 | 1102 | _opts = id |
1103 | 1103 | } |
1104 | | - _opts = _opts || {} |
1105 | | - let adapter = this.getAdapter(opts.adapter || this.defaultAdapter || 'http') |
1106 | | - let config = {} |
1107 | | - utils.fillIn(config, opts) |
1108 | | - if (!_opts.hasOwnProperty('endpoint') && config.endpoint) { |
1109 | | - _opts.endpoint = config.endpoint |
1110 | | - } |
| 1104 | + utils.fillIn(_opts, opts) |
| 1105 | + let adapter = this.getAdapter(_opts.adapter || this.defaultAdapter || 'http') |
| 1106 | + const config = {} |
| 1107 | + config.mapper = this.name |
| 1108 | + utils.deepMixIn(config, _opts) |
| 1109 | + config.method = config.method || 'GET' |
1111 | 1110 | if (typeof _opts.getEndpoint === 'function') { |
1112 | 1111 | config.url = _opts.getEndpoint(this, _opts) |
1113 | 1112 | } else { |
1114 | 1113 | let args = [ |
1115 | 1114 | _opts.basePath || this.basePath || adapter.basePath, |
1116 | | - adapter.getEndpoint(this, utils.isSorN(id) ? id : null, _opts) |
| 1115 | + adapter.getEndpoint(this, id, _opts) |
1117 | 1116 | ] |
1118 | 1117 | if (utils.isSorN(id)) { |
1119 | 1118 | args.push(id) |
1120 | 1119 | } |
1121 | 1120 | args.push(opts.pathname || name) |
1122 | 1121 | config.url = makePath.apply(null, args) |
1123 | 1122 | } |
1124 | | - config.method = config.method || 'GET' |
1125 | | - config.mapper = this.name |
1126 | | - utils.deepMixIn(config, _opts) |
1127 | 1123 | return utils.resolve(config) |
1128 | | - .then(_opts.request || opts.request) |
| 1124 | + .then(_opts.request) |
1129 | 1125 | .then((config) => adapter.HTTP(config)) |
1130 | 1126 | .then((data) => { |
1131 | 1127 | if (data && data.config) { |
1132 | 1128 | data.config.mapper = this.name |
1133 | 1129 | } |
1134 | 1130 | return data |
1135 | 1131 | }) |
1136 | | - .then(_opts.response || opts.response, _opts.responseError || opts.responseError) |
| 1132 | + .then(_opts.response, _opts.responseError) |
1137 | 1133 | } |
1138 | 1134 | return mapper |
1139 | 1135 | } |
|
0 commit comments