ordercloud-js-sdk
v0.0.5
Published
ordercloud-js-sdk
Downloads
1
Readme
OrderCloud Javascript SDK
This is the OrderCloud Javascript (browser-side) and Node (server-side) SDK. The same distributed JS file can be used for both cases. If you are using angular, we have an SDK specifically for that framework. The npm library is called ordercoud-angular-sdk. If you are using any other SPA framework or web app configuration (such as Backbone, Ember, Jquery, Coffeescript, or just plain-old vanilla javascript), this is the SDK for you!
Install
This library is supported for both npm and bower. For browser-side, we recommend bower. For server-side, we recommend npm.
bower install ordercloud-js-sdk --save
npm install ordercloud-js-sdk --save
Using The OrderCloud Javascript SDK in the Browser
After you succesfully install the SDK, you will need to include the library in your index.html file. We have created two files, one minified and one standard.
<script src=path/to/ordercloud-js-sdk.min.js></script>
or
<script src=path/to/ordercloud-js-sdk.js></script>
We recommend that you use either gulp or grunt to create a build that puts all of your third-party app dependencies in one location. This will make it much easier to inject each of the bower or npm main scripts into your index.html file.
After you have successfully injected the ordercloud-js-sdk, it will be available in the global namespace of your application
under OrderCloud
. Ensure that any reference you make to OrderCloud come after your inject the script (from above). An
example will look like this,
<script src=path/to/ordercloud-js-sdk.min.js></script>
<script>
OrderCloud.Buyers.Create({
"ID": "buyer_1",
"Name": "My First Buyer",
"Active": true
}
</script>
The OrderCloud Javascript SDK uses the q promise library to handle all asynchronous tasks. And since OrderCloud is an API and every method call you make to the server is asynchronous, you will be using this promise library A LOT. The good news, however, is that q is an extremely useful and easy to use library that will simplify your codebase and make it much more readable. An example of how q is used within this SDK is below,
OrderCloud.Categories.Get('category_id_1')
.then(function(data) {
//successful
console.log(data);
},
function(ex) {
//failure
console.log(ex);
});
Instead of using a callback function as you may have used previously with asynchronous functions, q has a .then
method
that you call on the same asynchronous method you wish to handle. Inside of the .then
method, you provide it with two
functions, the first being the function that handles a success, and the second being the function that handles an exception.
Additionally, you can chain the .then
methods together, making it quite easy to handle a flow of function handlers.
This is just a basic introduction to how the q promise library works. If you would like to learn more about how you can use q, head over to the documentation here
Beyond the standard OrderCloud API resources and methods available to you in this SDK, there are also a few configuration settings and helper functions that are meant to help you develop without any need to configure how the SDK is being used yourself. Below is a list of these configurations that you will use time and time again,
- OrderCloud.Credentials
Has two methods to handle logging in and logging out.
Method | Params | Action
:---------: | -------- | --------------------
Get | credentials | Authenticates user and returns an access-token (and refresh-token if applicable)
Delete | | Removes access-token from cookiesNote that the Get method does not save the access-token for you. You must handle the response appropriately using OrderCloud.Auth.SetToken method (down below)
- OrderCloud.Auth
Provides all necessary methods to work with authentication in OrderCloud (excluding authenticating itself, as shown above using OrderCloud.Credentials)
Utilizes cookies to store tokens in the browser upon user-reentry into the application
Method | Params | Action
:---------: | -------- | --------------------
GetToken | | Gets the stored access-token in cookies
SetToken | token | Sets access token, typically used after with OrderCloud.Credentials.Get