Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

This package has been deprecated

Author message:

Use the new packages @salling-group/auth and @salling-group/pagination-traverser instead.

sg-base-sdk

1.0.0 • Public • Published

Salling Group API Base SDK

This SDK allows you to easily set up an Axios instance that can query the Salling Group API. Furthermore, it also exports a traverser class that makes it easy to traverse paginated API resources.

Getting Started

This will check if 2017-12-24 is a holiday by querying the Holidays API.

const { SallingGroupAPI } = require('sg-base-sdk');
const instance = SallingGroupAPI.bearer('my_token');
 
instance.get('/v1/holidays/is-holiday', {
  'params': {
    'date': '2017-12-24',
  },
}).then((response) => console.log(response.data));

SallingGroupAPI

The SallingGroupAPI object exposes two authorization methods for setting up an Axios instance. This means that you can use this instance as you would use Axios, and it will handle authorization for you.

The available authorization methods are Bearer and JWT. You can get your credentials on the developer portal.

SallingGroupAPI is included like so:

const { SallingGroupAPI } = require('sg-base-sdk');

Bearer

If you use a Bearer token, you can access the API like this:

const instance = SallingGroupAPI.bearer('my_token');
instance.get('/v1/stores/').then(response => {
  console.log(response.data);
});

JWT

If you use a JWT, you can access the API like so:

const instance = SallingGroupAPI.jwt('my_email', 'my_secret');
instance.get('/v1/stores/').then(response => {
  console.log(response.data);
});

Traverser

Traverser helps you paginate through resources (e.g. the Stores API serves stores in pages).

Traverser is required like this:

const { Traverser } = require('sg-base-sdk');

Traverser has the following asynchronous methods:

Method Description
get() Get the current page.
next() Move the traverser to the next page and get it.
previous() Move the traverser to the previous page and get it.
first() Move the traverser to the first page and get it.
last() Move the traverser to the last page and get it.
goto(page) Move the traverser to the given page and get it.
pageNumber() Get the page that the traverser is on.

Example: Query Stores API

const { SallingGroupAPI, Traverser } = require('sg-base-sdk');
const instance = SallingGroupAPI.jwt('my_email', 'my_secret');
const traverser = new Traverser(instance, '/v1/stores/');
 
traverser.get().then((firstPage) => {
  console.log(firstPage);
  traverser.next().then((secondPage) => {
    console.log(secondPage);
  });
});

Keywords

none

install

npm i sg-base-sdk

Downloadsweekly downloads

14

version

1.0.0

license

none

last publish

collaborators

  • avatar
Report a vulnerability