1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @file js-data-http.js
4
- * @version 1.0.0-alpha.3 - Homepage <http://www.js-data.io/docs/dshttpadapter>
4
+ * @version 1.0.0-alpha.4 - Homepage <http://www.js-data.io/docs/dshttpadapter>
5
5
* @copyright (c) 2014 Jason Dobry
6
6
* @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE>
7
7
*
@@ -1398,7 +1398,7 @@ if (!JSData) {
1398
1398
throw new Error ( 'js-data must be loaded!' ) ;
1399
1399
}
1400
1400
1401
- var makePath = JSData . DSUtils . makePath ;
1401
+ var DSUtils = JSData . DSUtils ;
1402
1402
var deepMixIn = JSData . DSUtils . deepMixIn ;
1403
1403
var http = require ( 'axios' ) ;
1404
1404
@@ -1443,19 +1443,24 @@ function DSHttpAdapter(options) {
1443
1443
1444
1444
var dsHttpAdapterPrototype = DSHttpAdapter . prototype ;
1445
1445
1446
- dsHttpAdapterPrototype . getIdPath = function ( resourceConfig , options , id ) {
1447
- return makePath ( options . basePath || this . defaults . basePath || resourceConfig . basePath , resourceConfig . getEndpoint ( id , options ) , id ) ;
1448
- } ;
1449
-
1450
- dsHttpAdapterPrototype . getAllPath = function ( resourceConfig , options ) {
1451
- return makePath ( options . basePath || this . defaults . basePath || resourceConfig . basePath , resourceConfig . getEndpoint ( null , options ) ) ;
1446
+ dsHttpAdapterPrototype . getPath = function ( method , resourceConfig , id , options ) {
1447
+ var _this = this ;
1448
+ options = options || { } ;
1449
+ var args = [
1450
+ options . basePath || _this . defaults . basePath || resourceConfig . basePath ,
1451
+ resourceConfig . getEndpoint ( ( DSUtils . isString ( id ) || DSUtils . isNumber ( id ) || method === 'create' ) ? id : null , options )
1452
+ ] ;
1453
+ if ( method === 'find' || method === 'update' || method === 'destroy' ) {
1454
+ args . push ( id ) ;
1455
+ }
1456
+ return DSUtils . makePath . apply ( DSUtils , args ) ;
1452
1457
} ;
1453
1458
1454
1459
dsHttpAdapterPrototype . HTTP = function ( config ) {
1455
1460
var _this = this ;
1456
1461
var start = new Date ( ) ;
1457
1462
config = deepMixIn ( config , _this . defaults . httpConfig ) ;
1458
- if ( _this . defaults . forceTrailingSlash && config . url [ config . url . length ] !== '/' ) {
1463
+ if ( _this . defaults . forceTrailingSlash && config . url [ config . url . length - 1 ] !== '/' ) {
1459
1464
config . url += '/' ;
1460
1465
}
1461
1466
@@ -1523,7 +1528,7 @@ dsHttpAdapterPrototype.find = function (resourceConfig, id, options) {
1523
1528
var _this = this ;
1524
1529
options = options || { } ;
1525
1530
return _this . GET (
1526
- _this . getIdPath ( resourceConfig , options , id ) ,
1531
+ _this . getPath ( 'find' , resourceConfig , id , options ) ,
1527
1532
options
1528
1533
) . then ( function ( data ) {
1529
1534
return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
@@ -1539,7 +1544,7 @@ dsHttpAdapterPrototype.findAll = function (resourceConfig, params, options) {
1539
1544
deepMixIn ( options . params , params ) ;
1540
1545
}
1541
1546
return _this . GET (
1542
- _this . getAllPath ( resourceConfig , options ) ,
1547
+ _this . getPath ( 'findAll' , resourceConfig , params , options ) ,
1543
1548
options
1544
1549
) . then ( function ( data ) {
1545
1550
return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
@@ -1550,7 +1555,7 @@ dsHttpAdapterPrototype.create = function (resourceConfig, attrs, options) {
1550
1555
var _this = this ;
1551
1556
options = options || { } ;
1552
1557
return _this . POST (
1553
- makePath ( options . basePath || this . defaults . basePath || resourceConfig . basePath , resourceConfig . getEndpoint ( attrs , options ) ) ,
1558
+ _this . getPath ( 'create' , resourceConfig , attrs , options ) ,
1554
1559
options . serialize ? options . serialize ( resourceConfig , attrs ) : _this . defaults . serialize ( resourceConfig , attrs ) ,
1555
1560
options
1556
1561
) . then ( function ( data ) {
@@ -1562,7 +1567,7 @@ dsHttpAdapterPrototype.update = function (resourceConfig, id, attrs, options) {
1562
1567
var _this = this ;
1563
1568
options = options || { } ;
1564
1569
return _this . PUT (
1565
- _this . getIdPath ( resourceConfig , options , id ) ,
1570
+ _this . getPath ( 'update' , resourceConfig , id , options ) ,
1566
1571
options . serialize ? options . serialize ( resourceConfig , attrs ) : _this . defaults . serialize ( resourceConfig , attrs ) ,
1567
1572
options
1568
1573
) . then ( function ( data ) {
@@ -1579,7 +1584,7 @@ dsHttpAdapterPrototype.updateAll = function (resourceConfig, attrs, params, opti
1579
1584
deepMixIn ( options . params , params ) ;
1580
1585
}
1581
1586
return this . PUT (
1582
- _this . getAllPath ( resourceConfig , options ) ,
1587
+ _this . getPath ( 'updateAll' , resourceConfig , attrs , options ) ,
1583
1588
options . serialize ? options . serialize ( resourceConfig , attrs ) : _this . defaults . serialize ( resourceConfig , attrs ) ,
1584
1589
options
1585
1590
) . then ( function ( data ) {
@@ -1591,7 +1596,7 @@ dsHttpAdapterPrototype.destroy = function (resourceConfig, id, options) {
1591
1596
var _this = this ;
1592
1597
options = options || { } ;
1593
1598
return _this . DEL (
1594
- _this . getIdPath ( resourceConfig , options , id ) ,
1599
+ _this . getPath ( 'destroy' , resourceConfig , id , options ) ,
1595
1600
options
1596
1601
) . then ( function ( data ) {
1597
1602
return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
@@ -1607,7 +1612,7 @@ dsHttpAdapterPrototype.destroyAll = function (resourceConfig, params, options) {
1607
1612
deepMixIn ( options . params , params ) ;
1608
1613
}
1609
1614
return this . DEL (
1610
- _this . getAllPath ( resourceConfig , options ) ,
1615
+ _this . getPath ( 'destroyAll' , resourceConfig , params , options ) ,
1611
1616
options
1612
1617
) . then ( function ( data ) {
1613
1618
return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
0 commit comments