js-data

Respect Your Data

Give your data the treatment it deserves with a framework-agnostic, datastore-agnostic JavaScript ORM built for ease of use and peace of mind. Works in Node.js and in the Browser.

Get Started

DSHttpAdapter

http adapter for js-data

js-data-http Slack Status npm version Circle CI npm downloads Coverage Status

Quick Start

bower install --save js-data js-data-http or npm install --save js-data js-data-http.

Load js-data-http.js after js-data.js.

var store = new JSData.DS();
store.registerAdapter('http', new DSHttpAdapter(), { default: true });

// "store" will now use the http adapter for all async operations

Community & Links

API

new DSHttpAdapter([options])

Need help?

Want more examples or have a question? Post on the Slack channel or the mailing list then we'll get your question answered and probably update this wiki.

Default Settings

When you create a new instance of DSHttpAdapter it will come with some defaults.

Setting defaults
// during construction
var httpAdapter = new DSHttpAdapter({
  suffix: '.json'
});
// during runtime
httpAdapter.defaults.basePath = '/api';
// during a method call
httpAdapter.find(User, 1, { basePath: '/otherApi' }).then(...);

serialize

Type: function

Serialize the payload into something your server will understand, if necessary. resourceConfig is the resource for this adapter operation. data is the payload from the data store.

Default:

function serialize(resourceConfig, data) {
  return data;
}

deserialize

Type: function

Take the response from your server and transform it into something the data store will understand, if necessary. resourceConfig is the resource for this adapter operation. data is the response from axios.

Default:

function deserialize(resourceConfig, data) {
  return data ? ('data' in data ? data.data : data) : data;
}

queryTransform

Type: function

Transform the query string parameters before the request is made. resourceConfig is the resource for this adapter operation. params is the object that will be serialized to the query string.

Default:

function queryTransform(resourceConfig, params) {
  return params;
}

httpConfig

Type: object

Configuration options for axios, the http library used by js-data-http. Configuration options for $http, if you're using the http adapter in js-data-angular.

Default: {}

suffix

Type: string

A suffix to be added to the url path before making a request.

Default: ""

basePath

Type: string

A prefix to be added to the url path before making a request.

Default: ""

forceTrailingSlash

Type: boolean

Force use of a trailing slash.

Default: false

log

Type: function || false

The logging function to use for successful requests. Set to false to disable.

Default: console.log

error

Type: function || false

The logging function to use for failed requests. Set to false to disable.

Default: console.error

create(resourceConfig, attrs[, options)

Make a POST request to create a single item on the server. Calls DSHttpAdapter#POST, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resouceConfig

object

A resource definition created by DS#defineResource.

attrs

object

The attributes from which to create the item. This will be the body of the request.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#POST and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var Document = store.defineResource('document');

// bypass the data store
adapter.create(Document, { author: 'John' }).then(function (document) {
  document; // { id: 5, author: 'John' }
  
  // The new document has NOT been injected into the data store because we bypassed the data store
  Document.get(document.id); // undefined
});

// Normally you would just go through the data store
Document.create({ author: 'John' }).then(function (document) {
  document; // { id: 5, author: 'John' }
  
  // the new document has been injected into the data store
  Document.get(document.id); // { id: 5, author: 'John' }
});

DEL(url[, options])

Make a DEL request. A wrapper for axios.delete or $http.delete.

Returns a promise.

Argument
Type
Description

url

string

Url of the request.

options (optional)

object

Configuration options. Passed through to axios or $http.delete.

Examples
var adapter = new DSHttpAdapter();

adapter.DEL('/user/1').then(function (data) {
  data.data; // 1
  data.headers; // {...}
  data.status; // 204
  data.config; {...}
});

destroy(resourceConfig, id[, options])

Make a DELETE request to destroy a single item on the server. Calls DSHttpAdapter#DEL, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resourceConfig

object

A resource definition created by DS#defineResource.

id

string or number

The primary key of the item to destroy.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#DEL and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var Document = store.defineResource('document');

Document.inject({ id: 5, author: 'John' });

// bypass the data store
adapter.destroy(Document, 5).then(function () {
  // the document is still in the data store because we bypassed the data store
  Document.get(document.id); // { id: 5, author: 'John' }
});

// Normally you would just go through the data store
Document.destroy(5).then(function () {
  // the document has been ejected from the data store
  Document.get(document.id); // undefined
});

destroyAll(resourceConfig, params[, options])

Make a DELETE request to destroy a collection of items on the server. Calls DSHttpAdapter#DEL, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resourceConfig

object

A resource definition created by DS#defineResource.

params

object

Query parameters for selecting the items to destroy. Will be serialized to the query string. Default: {}.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#DEL and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var Document = store.defineResource('document');

var params = {
  author: 'John'
};

// bypass the data store
adapter.destroyAll(Document, params).then(function () {
  // the documents have NOT been ejected from the data store because we bypassed the data store
  Document.filter(params); // [{...}, {...}, ...]
});

// normally you would go through the data store
Document.destroyAll(params).then(function () {
  // the documents have been ejected from the data store
  Document.filter(params); // []
});

find(resourceConfig, id[, options])

Make a GET request to retrieve a single item from the server. Calls DSHttpAdapter#GET, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resourceConfig

object

A resource definition created by DS#defineResource.

id

string or number

The primary key of the item to retrieve.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#GET and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var Document = store.defineResource('document');

// bypass the data store
adapter.find(Document, 5).then(function (document) {
  document; // { id: 5, author: 'John Anderson' }
  
  // the document has NOT been injected into the data store because we bypassed the data store
  Document.get(document.id); // undefined
});

// Normally you would just go through the data store
Document.find(5).then(function (document) {
  document; // { id: 5, author: 'John Anderson' }
  
  // the document has been injected into the data store
  Document.get(document.id); // { id: 5, author: 'John Anderson' }
});

findAll(resourceConfig, params[, options])

Make a GET request to retrieve a collection of items from the server. Calls DSHttpAdapter#GET, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resourceConfig

object

A resource definition created by DS#defineResource.

params

object

Query parameters for selecting the items to retrieve. Will be serialized to the query string. Default: {}.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#GET and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var User = store.defineResource('user');

var params = {
  age: {
    '>': 30
  }
};

// bypass the data store
adapter.findAll(User, params).then(function (users) {
  // users[0].age; 55 // etc., etc.
  
  // the users have NOT been injected into the data store because we bypassed the data store
  User.filter(params); // []
});

// normally you would go through the data store
User.findAll(params).then(function (users) {
  // users[0].age; 55 // etc., etc.
  
  // the users have been injected into the data store
  User.filter(params); // [{...}, {...}, ...]
});

GET(url[, options])

Make a GET request. A wrapper for axios.get or $http.get.

Returns a promise.

Argument
Type
Description

url

string

Url of the request.

options (optional)

object

Configuration options. Passed through to axios.get or $http.get.

Examples
var adapter = new DSHttpAdapter();

adapter.GET('/user/1').then(function (data) {
  data.data; // { id: 1, ... }
  data.headers; // {...}
  data.status; // 200
  data.config; {...}
});

HTTP(config)

Make an HTTP request. A wrapper for axios or $http.

Returns a promise.

Argument
Type
Description

config

object

Configuration options. Passed through to axios or $http.

Examples
var adapter = new DSHttpAdapter();

adapter.HTTP({ url: '/user/1', method: 'put', data: { name: 'Johnny' }).then(function (data) {
  data.data; // { id: 1, name: 'Johnny', ... }
  data.headers; // {...}
  data.status; // 200
  data.config; {...}
});

POST(url, data[, options])

Make a POST request. A wrapper for axios.post or $http.post.

Returns a promise.

Argument
Type
Description

url

string

Url of the request.

data

object

Request body.

options (optional)

object

Configuration options. Passed through to axios.post or $http.post.

Examples
var adapter = new DSHttpAdapter();

adapter.POST('/user/1', { name: 'John' }).then(function (data) {
  data.data; // { id: 1, name: 'John', ... }
  data.headers; // {...}
  data.status; // 200
  data.config; {...}
});

PUT(url, data[, options])

Make a PUT request. A wrapper for axios.put or $http.put.

Returns a promise.

Argument
Type
Description

url

string

Url of the request.

data

object

Request body.

options (optional)

object

Configuration options. Passed through to axios.put or $http.put.

Examples
var adapter = new DSHttpAdapter();

adapter.PUT('/user/1', { name: 'Johnny' }).then(function (data) {
  data.data; // { id: 1, name: 'Johnny', ... }
  data.headers; // {...}
  data.status; // 200
  data.config; {...}
});

update(resourceConfig, id, attrs[, options])

Make a PUT request to update a single item on the server. Calls DSHttpAdapter#PUT, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resourceConfig

object

A resource definition created by DS#defineResource.

id

string or number

The primary key of the item to update.

attrs

object

Request body.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#PUT and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var Document = store.defineResource('document');

Document.inject({ id: 5, author: 'John' });

// bypass the data store
adapter.update(Document, 5, { author: 'Johnny' }).then(function (document) {
  document; // { id: 5, author: 'Johnny' }
  
  // The updated document has NOT been injected into the data store because we bypassed the data store
  Document.get(document.id); // { id: 5, author: 'John' }
});

// Normally you would just go through the data store
Document.update(5, { author: 'Johnny' }).then(function (document) {
  document; // { id: 5, author: 'Johnny' }
  
  // the updated document has been injected into the data store
  Document.get(document.id); // { id: 5, author: 'Johnny' }
});

updateAll(resourceConfig, attrs, params[, options])

Make a PUT request to update a collection of items on the server. Calls DSHttpAdapter#PUT, which calls DSHttpAdapter#HTTP which delegates either to axios or $http, so all of the options for those methods are supported here.

Returns a promise, which will resolve with the result of calling deserialize on whatever DSHttpAdapter#HTTP resolves with.

Argument
Type
Description

resourceConfig

object

A resource definition created by DS#defineResource.

attrs

object

Request body.

params

object

Query parameters for selecting the items to update. Will be serialized to the query string. Default: {}.

options (optional)

object

Configuration options. Also passed through to DSHttpAdapter#PUT and subsequently to DSHttpAdapter#HTTP, and finally to axios or $http, depending on which underlying http library is being used. Options supported by axios (or $http) are also supported here.

options.suffix

string

Override the default suffix.

options.serialize

function

Override the default serialize hook.

options.deserialize

function

Override the default deserialize hook.

Examples
var adapter = new DSHttpAdapter();
var store = new JSData.DS();
store.registerAdapter('http', adapter, { default: true });

var Document = store.defineResource('document');

Document.inject({ id: 5, author: 'John' });
Document.inject({ id: 6, author: 'John' });

// bypass the data store
adapter.updateAll(Document, { author: 'Johnny' }, { author: 'John' }).then(function (documents) {
  documents[0]; // { id: 5, author: 'Johnny' }
  
  // The updated documents have NOT been injected into the data store because we bypassed the data store
  Document.filter({ author: 'John' }); // [{...}, {...}]
  Document.filter({ author: 'Johnny' }); // []
});

// Normally you would just go through the data store
Document.updateAll({ author: 'Johnny' }, { author: 'John' }).then(function (documents) {
  documents[0]; // { id: 5, author: 'Johnny' }
  
  // the updated documents have been injected into the data store
  Document.filter({ author: 'John' }); // []
  Document.filter({ author: 'Johnny' }); // [{...}, {...}]
});

License

The MIT License (MIT)

Copyright (c) 2014-2015 Jason Dobry

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

DSHttpAdapter

http adapter for js-data