|
1 | 1 | /** |
2 | 2 | * @author Jason Dobry <jason.dobry@gmail.com> |
3 | 3 | * @file js-data-http.js |
4 | | -* @version 0.2.0 - Homepage <http://www.js-data.iojs-data-http/> |
| 4 | +* @version 0.3.0 - Homepage <http://www.js-data.iojs-data-http/> |
5 | 5 | * @copyright (c) 2014 Jason Dobry |
6 | 6 | * @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE> |
7 | 7 | * |
@@ -1330,152 +1330,155 @@ function DSHttpAdapter(options) { |
1330 | 1330 |
|
1331 | 1331 | var dsHttpAdapterPrototype = DSHttpAdapter.prototype; |
1332 | 1332 |
|
1333 | | -dsHttpAdapterPrototype.HTTP = function (config, resourceConfig) { |
| 1333 | +dsHttpAdapterPrototype.HTTP = function (config) { |
1334 | 1334 | var _this = this; |
1335 | 1335 | var start = new Date().getTime(); |
1336 | | - var deserialize; |
1337 | | - if (config.deserialize) { |
1338 | | - deserialize = config.deserialize; |
1339 | | - delete config.deserialize; |
1340 | | - } else { |
1341 | | - deserialize = _this.defaults.deserialize; |
1342 | | - } |
1343 | | - resourceConfig = resourceConfig || {}; |
1344 | 1336 | config = deepMixIn(config, this.defaults.httpConfig); |
1345 | 1337 | return http(config).then(function (data) { |
1346 | 1338 | if (_this.defaults.log) { |
1347 | 1339 | var args = Array.prototype.slice.call(arguments); |
1348 | 1340 | args.unshift(data.config.method + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms'); |
1349 | 1341 | _this.defaults.log.apply(_this.defaults.log, args); |
1350 | 1342 | } |
1351 | | - return deserialize(resourceConfig.name, data); |
| 1343 | + return data; |
1352 | 1344 | }); |
1353 | 1345 | }; |
1354 | 1346 |
|
1355 | | -dsHttpAdapterPrototype.GET = function (url, config, resourceConfig) { |
| 1347 | +dsHttpAdapterPrototype.GET = function (url, config) { |
1356 | 1348 | config = config || {}; |
1357 | 1349 | if (!('method' in config)) { |
1358 | 1350 | config.method = 'get'; |
1359 | 1351 | } |
1360 | 1352 | return this.HTTP(deepMixIn(config, { |
1361 | 1353 | url: url |
1362 | | - }), resourceConfig); |
| 1354 | + })); |
1363 | 1355 | }; |
1364 | 1356 |
|
1365 | | -dsHttpAdapterPrototype.POST = function (url, attrs, config, resourceConfig) { |
| 1357 | +dsHttpAdapterPrototype.POST = function (url, attrs, config) { |
1366 | 1358 | config = config || {}; |
1367 | 1359 | if (!('method' in config)) { |
1368 | 1360 | config.method = 'post'; |
1369 | 1361 | } |
1370 | 1362 | return this.HTTP(deepMixIn(config, { |
1371 | 1363 | url: url, |
1372 | 1364 | data: attrs |
1373 | | - }), resourceConfig); |
| 1365 | + })); |
1374 | 1366 | }; |
1375 | 1367 |
|
1376 | | -dsHttpAdapterPrototype.PUT = function (url, attrs, config, resourceConfig) { |
| 1368 | +dsHttpAdapterPrototype.PUT = function (url, attrs, config) { |
1377 | 1369 | config = config || {}; |
1378 | 1370 | if (!('method' in config)) { |
1379 | 1371 | config.method = 'put'; |
1380 | 1372 | } |
1381 | 1373 | return this.HTTP(deepMixIn(config, { |
1382 | 1374 | url: url, |
1383 | 1375 | data: attrs || {} |
1384 | | - }), resourceConfig); |
| 1376 | + })); |
1385 | 1377 | }; |
1386 | 1378 |
|
1387 | | -dsHttpAdapterPrototype.DEL = function (url, config, resourceConfig) { |
| 1379 | +dsHttpAdapterPrototype.DEL = function (url, config) { |
1388 | 1380 | config = config || {}; |
1389 | 1381 | if (!('method' in config)) { |
1390 | 1382 | config.method = 'delete'; |
1391 | 1383 | } |
1392 | 1384 | return this.HTTP(deepMixIn(config, { |
1393 | 1385 | url: url |
1394 | | - }), resourceConfig); |
| 1386 | + })); |
1395 | 1387 | }; |
1396 | 1388 |
|
1397 | 1389 | dsHttpAdapterPrototype.find = function (resourceConfig, id, options) { |
| 1390 | + var _this = this; |
1398 | 1391 | options = options || {}; |
1399 | | - return this.GET( |
| 1392 | + return _this.GET( |
1400 | 1393 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), |
1401 | | - options, |
1402 | | - resourceConfig |
1403 | | - ); |
| 1394 | + options |
| 1395 | + ).then(function (data) { |
| 1396 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1397 | + }); |
1404 | 1398 | }; |
1405 | 1399 |
|
1406 | 1400 | dsHttpAdapterPrototype.findAll = function (resourceConfig, params, options) { |
| 1401 | + var _this = this; |
1407 | 1402 | options = options || {}; |
1408 | 1403 | options.params = options.params || {}; |
1409 | 1404 | if (params) { |
1410 | | - params = this.defaults.queryTransform(resourceConfig.name, params); |
| 1405 | + params = _this.defaults.queryTransform(resourceConfig.name, params); |
1411 | 1406 | deepMixIn(options.params, params); |
1412 | 1407 | } |
1413 | | - return this.GET( |
| 1408 | + return _this.GET( |
1414 | 1409 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), |
1415 | | - options, |
1416 | | - resourceConfig |
1417 | | - ); |
| 1410 | + options |
| 1411 | + ).then(function (data) { |
| 1412 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1413 | + }); |
1418 | 1414 | }; |
1419 | 1415 |
|
1420 | 1416 | dsHttpAdapterPrototype.create = function (resourceConfig, attrs, options) { |
1421 | 1417 | var _this = this; |
1422 | 1418 | options = options || {}; |
1423 | | - return this.POST( |
| 1419 | + return _this.POST( |
1424 | 1420 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(attrs, options)), |
1425 | 1421 | options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), |
1426 | | - options, |
1427 | | - resourceConfig |
1428 | | - ); |
| 1422 | + options |
| 1423 | + ).then(function (data) { |
| 1424 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1425 | + }); |
1429 | 1426 | }; |
1430 | 1427 |
|
1431 | 1428 | dsHttpAdapterPrototype.update = function (resourceConfig, id, attrs, options) { |
1432 | 1429 | var _this = this; |
1433 | 1430 | options = options || {}; |
1434 | | - return this.PUT( |
| 1431 | + return _this.PUT( |
1435 | 1432 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), |
1436 | 1433 | options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), |
1437 | | - options, |
1438 | | - resourceConfig |
1439 | | - ); |
| 1434 | + options |
| 1435 | + ).then(function (data) { |
| 1436 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1437 | + }); |
1440 | 1438 | }; |
1441 | 1439 |
|
1442 | 1440 | dsHttpAdapterPrototype.updateAll = function (resourceConfig, attrs, params, options) { |
1443 | 1441 | var _this = this; |
1444 | 1442 | options = options || {}; |
1445 | 1443 | options.params = options.params || {}; |
1446 | 1444 | if (params) { |
1447 | | - params = this.defaults.queryTransform(resourceConfig.name, params); |
| 1445 | + params = _this.defaults.queryTransform(resourceConfig.name, params); |
1448 | 1446 | deepMixIn(options.params, params); |
1449 | 1447 | } |
1450 | 1448 | return this.PUT( |
1451 | 1449 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), |
1452 | 1450 | options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), |
1453 | | - options, |
1454 | | - resourceConfig |
1455 | | - ); |
| 1451 | + options |
| 1452 | + ).then(function (data) { |
| 1453 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1454 | + }); |
1456 | 1455 | }; |
1457 | 1456 |
|
1458 | 1457 | dsHttpAdapterPrototype.destroy = function (resourceConfig, id, options) { |
| 1458 | + var _this = this; |
1459 | 1459 | options = options || {}; |
1460 | | - return this.DEL( |
| 1460 | + return _this.DEL( |
1461 | 1461 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(id, options), id), |
1462 | | - options, |
1463 | | - resourceConfig |
1464 | | - ); |
| 1462 | + options |
| 1463 | + ).then(function (data) { |
| 1464 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1465 | + }); |
1465 | 1466 | }; |
1466 | 1467 |
|
1467 | 1468 | dsHttpAdapterPrototype.destroyAll = function (resourceConfig, params, options) { |
| 1469 | + var _this = this; |
1468 | 1470 | options = options || {}; |
1469 | 1471 | options.params = options.params || {}; |
1470 | 1472 | if (params) { |
1471 | | - params = this.defaults.queryTransform(resourceConfig.name, params); |
| 1473 | + params = _this.defaults.queryTransform(resourceConfig.name, params); |
1472 | 1474 | deepMixIn(options.params, params); |
1473 | 1475 | } |
1474 | 1476 | return this.DEL( |
1475 | 1477 | makePath(options.baseUrl || resourceConfig.baseUrl, resourceConfig.getEndpoint(null, options)), |
1476 | | - options, |
1477 | | - resourceConfig |
1478 | | - ); |
| 1478 | + options |
| 1479 | + ).then(function (data) { |
| 1480 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 1481 | + }); |
1479 | 1482 | }; |
1480 | 1483 |
|
1481 | 1484 | module.exports = DSHttpAdapter; |
|
0 commit comments