- Purpose of the Client
- Getting Started
- Authentication
- Creating a Resource
- Finding Resources
- Getting a Resource
- To enable consumption of multiple JSON:API services.
- To be a factory for Resource objects.
- When creating new Resources.
- When retrieving Resources from an API.
First up, if you're working in a browser you'll need to drop the library onto your page:
<script type="text/javascript" src="jsonapi-client.min.js"></script>
From here on, the implementation (be it in NodeJS or the browser) is the same. Start by pulling in the library:
var JsonapiClient = require("jsonapi-client");
Next, we create a new instance of jsonapi-client and tell it where our target API is located:
var client = new JsonapiClient("http://localhost:16006/rest");
Now we're good to go!
jsonapi-server currently supports two basic forms of authentication - cookies and headers.
Custom headers:
var client = new JsonapiClient("http://localhost:16006/rest", {
header: {
myHeaderName: "my-header-value"
}
});
Cookie:
var client = new JsonapiClient("http://localhost:16006/rest", {
cookie: {
myCookieName: "my-cookie-value"
}
});
Creating a resource begins with a synchronous call to client.create
- it will immediately return a new (empty) instance of a Resource. Next up is an opportunity to set the attributes on your new resource, for example you'll need to assign values to all mandatory attributes before trying to push it up to the remote API.
var newResource = client.create("resource-type"); // synchronous
Once the newResource is populated and ready to be pushed, it needs to be sync
d. You can read more about sync
in the Resource documentation.
newResource.sync(function(err) { /* accepts a callback */ });
newResource.sync(); // without a callback, it returns a promise
To search for resources, use client.find
. The first argument must be the resource-type you want to search for. The second argument is optional, it represents any URL parameters you may want to include - filter, include, sort, etc. The third argument is an optional callback. If no callback is provided, a promise will be returned.
The function will produce either an error or an array of Resources.
var optionalParams = { /* url-params go here */ };
client.find("resource-type", optionalParams, function(err, resources) { });
client.find("resource-type", optionalParams); // without a callback, it returns a promise
To find a specific resource, use client.get
. The first argument must be the resource-type you want to search for. The second argument must be the resource-id of the resource you want to retrieve from the remote API. The third argument is optional, it represents any URL parameters you may want to include - filter, include, sort, etc. The fourth argument is an optional callback. If no callback is provided, a promise will be returned.
The function will produce either an error or a single Resource.
var optionalParams = { /* url-params go here */ };
client.get("resource-type", "resource-id", optionalParams, function(err, resources) { });
var promise = client.get("resource-type", "resource-id", optionalParams);