11/**
22* @author Jason Dobry <[email protected] > 33* @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>
55* @copyright (c) 2014 Jason Dobry
66* @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE>
77*
@@ -1398,7 +1398,7 @@ if (!JSData) {
13981398 throw new Error ( 'js-data must be loaded!' ) ;
13991399}
14001400
1401- var makePath = JSData . DSUtils . makePath ;
1401+ var DSUtils = JSData . DSUtils ;
14021402var deepMixIn = JSData . DSUtils . deepMixIn ;
14031403var http = require ( 'axios' ) ;
14041404
@@ -1443,19 +1443,24 @@ function DSHttpAdapter(options) {
14431443
14441444var dsHttpAdapterPrototype = DSHttpAdapter . prototype ;
14451445
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 ) ;
14521457} ;
14531458
14541459dsHttpAdapterPrototype . HTTP = function ( config ) {
14551460 var _this = this ;
14561461 var start = new Date ( ) ;
14571462 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 ] !== '/' ) {
14591464 config . url += '/' ;
14601465 }
14611466
@@ -1523,7 +1528,7 @@ dsHttpAdapterPrototype.find = function (resourceConfig, id, options) {
15231528 var _this = this ;
15241529 options = options || { } ;
15251530 return _this . GET (
1526- _this . getIdPath ( resourceConfig , options , id ) ,
1531+ _this . getPath ( 'find' , resourceConfig , id , options ) ,
15271532 options
15281533 ) . then ( function ( data ) {
15291534 return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
@@ -1539,7 +1544,7 @@ dsHttpAdapterPrototype.findAll = function (resourceConfig, params, options) {
15391544 deepMixIn ( options . params , params ) ;
15401545 }
15411546 return _this . GET (
1542- _this . getAllPath ( resourceConfig , options ) ,
1547+ _this . getPath ( 'findAll' , resourceConfig , params , options ) ,
15431548 options
15441549 ) . then ( function ( data ) {
15451550 return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
@@ -1550,7 +1555,7 @@ dsHttpAdapterPrototype.create = function (resourceConfig, attrs, options) {
15501555 var _this = this ;
15511556 options = options || { } ;
15521557 return _this . POST (
1553- makePath ( options . basePath || this . defaults . basePath || resourceConfig . basePath , resourceConfig . getEndpoint ( attrs , options ) ) ,
1558+ _this . getPath ( 'create' , resourceConfig , attrs , options ) ,
15541559 options . serialize ? options . serialize ( resourceConfig , attrs ) : _this . defaults . serialize ( resourceConfig , attrs ) ,
15551560 options
15561561 ) . then ( function ( data ) {
@@ -1562,7 +1567,7 @@ dsHttpAdapterPrototype.update = function (resourceConfig, id, attrs, options) {
15621567 var _this = this ;
15631568 options = options || { } ;
15641569 return _this . PUT (
1565- _this . getIdPath ( resourceConfig , options , id ) ,
1570+ _this . getPath ( 'update' , resourceConfig , id , options ) ,
15661571 options . serialize ? options . serialize ( resourceConfig , attrs ) : _this . defaults . serialize ( resourceConfig , attrs ) ,
15671572 options
15681573 ) . then ( function ( data ) {
@@ -1579,7 +1584,7 @@ dsHttpAdapterPrototype.updateAll = function (resourceConfig, attrs, params, opti
15791584 deepMixIn ( options . params , params ) ;
15801585 }
15811586 return this . PUT (
1582- _this . getAllPath ( resourceConfig , options ) ,
1587+ _this . getPath ( 'updateAll' , resourceConfig , attrs , options ) ,
15831588 options . serialize ? options . serialize ( resourceConfig , attrs ) : _this . defaults . serialize ( resourceConfig , attrs ) ,
15841589 options
15851590 ) . then ( function ( data ) {
@@ -1591,7 +1596,7 @@ dsHttpAdapterPrototype.destroy = function (resourceConfig, id, options) {
15911596 var _this = this ;
15921597 options = options || { } ;
15931598 return _this . DEL (
1594- _this . getIdPath ( resourceConfig , options , id ) ,
1599+ _this . getPath ( 'destroy' , resourceConfig , id , options ) ,
15951600 options
15961601 ) . then ( function ( data ) {
15971602 return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
@@ -1607,7 +1612,7 @@ dsHttpAdapterPrototype.destroyAll = function (resourceConfig, params, options) {
16071612 deepMixIn ( options . params , params ) ;
16081613 }
16091614 return this . DEL (
1610- _this . getAllPath ( resourceConfig , options ) ,
1615+ _this . getPath ( 'destroyAll' , resourceConfig , params , options ) ,
16111616 options
16121617 ) . then ( function ( data ) {
16131618 return ( options . deserialize ? options . deserialize : _this . defaults . deserialize ) ( resourceConfig , data ) ;
0 commit comments