0.0.0-alpha.0 • Published 6 years ago

@motionpicture/kwskfs-api-javascript-client v0.0.0-alpha.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

KWSKFS API client library for Javascript

npm (scoped) CircleCI Coverage Status Dependency Status Known Vulnerabilities NSP Status npm

Javascript client library for using Sasaki APIs. Support for authorization and authentication with OAuth 2.0.

Table of Contents

Installation

This library is distributed on npm. In order to add it as a dependency, run the following command:

npm install @motionpicture/kwskfs-api-javascript-client

CommonJS

Include the library in your JavaScript file with

const kwskfs = require("@motionpicture/kwskfs-api-javascript-client");

Browser

Include lib/browser.js in your page.

<script type="text/javascript" src="./node_modules/@motionpicture/kwskfs-api-javascript-client/lib/browser.js"></script>

Usage

Example: Creates a URL Shortener client and retrieves the long url of the given short url:

var kwskfs = require('@motionpicture/kwskfs-api-javascript-client');
var events = new kwskfs.service.Event({
    endpoint: 'endpoint'.
    auth: auth
});

events.search({
    eventType: kwskfs.factory.eventType.FoodEvent
}).then(function (events) {
    console.log('events:', events);
}).catch(function (err) {
    console.error(err);
});

Authorizing and authenticating

OAuth2 client

This client comes with an OAuth2 client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly if you also provide an expiry_date and the token is expired.

In the following examples, you may need a CLIENT_ID, REDIRECT_URI and LOGOUT_URI. You can ask these to the provider.

For more information about OAuth2 and how it works, see here.

A complete sample application that authorizes and authenticates with the OAuth2 client is available at samples/browser/index.html.

Generating an OAuth2 client

redirect them to a consent page. To redirect them a consent page URL:

var kwskfs = require('@motionpicture/kwskfs-api-javascript-client');

// generate a url that asks permissions for Sasai API scopes
var scopes = [
    'phone', 'openid', 'email', 'aws.cognito.signin.user.admin', 'profile',
    'https://IDENTIFIER/events.read-only'
];

var oauth2Client = new kwskfs.auth.Implicit({
    domain: DOMAIN,
    clientId: CLIENT_ID,
    responseType: 'token',
    redirectUri: REDIRECT_URI,
    logoutUri: LOGOUT_URI,
    scope: scopes.join(' '),
    state: '12345'
});
Authorize
oauth2Client.authorize().then(function (credentials) {
    console.log('authorize result:', credentials);
}).catch(function (err) {
    console.error(err);
});
Setting service-level auth

You can set the auth as a service-level option.

Example: Setting a service-level auth option.

var kwskfs = require('@motionpicture/kwskfs-api-javascript-client');

// generate a url that asks permissions for Sasai API scopes
var scopes = [
    'phone', 'openid', 'email', 'aws.cognito.signin.user.admin', 'profile',
    'https://IDENTIFIER/events.read-only'
];

var oauth2Client = new kwskfs.auth.Implicit({
    domain: DOMAIN,
    clientId: CLIENT_ID,
    responseType: 'token',
    redirectUri: REDIRECT_URI,
    logoutUri: LOGOUT_URI,
    scope: scopes.join(' '),
    state: '12345'
});

oauth2Client.authorize().then(function (credentials) {
    console.log('authorize result:', credentials);

    events.search({
        eventType: kwskfs.factory.eventType.FoodEvent
    }).then(function (events) {
        console.log('events:', events);
    }).catch(function (err) {
        console.error(err);
    });
}).catch(function (err) {
    console.error(err);
});
Manually refreshing access token

If you need to manually refresh the access_token associated with your OAuth2 client, make sure you have a refresh_token set in your credentials first and then call:

oauth2Client.refreshToken().then(function (result) {
    console.log('refreshToken result:', result);
}).catch(function (err) {
    console.error(err);
});

License

ISC