Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rest-client-sdk

Package Overview
Dependencies
Maintainers
1
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-client-sdk

Rest Client SDK for API

  • 0.8.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
25
decreased by-66.22%
Maintainers
1
Weekly downloads
 
Created
Source

Mapado Rest Client JS SDK Build Status

Rest client SDK for API for Javascript usage.

This client tries to avoid the complexity of implementing a custom SDK for every API you have. You just have to implements your model and a little configuration and it will hide the complexity for you.

Installation

npm install rest-client-sdk

Usage

Declare your clients

import { AbstractClient } from 'rest-client-sdk';

class SomeEntityClient extends AbstractClient {
  getPathBase() {
    return '/v2/some_entities';
  }

  getName() {
      return 'SomeEntity'; // this will be passed to the entity factory
  }
}

export default SomeEntityClient;

Create the SDK

Create the token storage
import { TokenStorage } from 'rest-client-sdk';

const tokenGeneratorConfig = { path: 'oauth.me', foo: 'bar' };
const tokenGenerator = new SomeTokenGenerator(tokenGeneratorConfig); // Some token generators are defined in `src/TokenGenerator/`
const storage = AsyncStorage; // create a storage instance if you are not on RN. In browser and node, localforage works fine
const tokenStorage = new TokenStorage(tokenGenerator, storage);

The token generator is a class implementing generateToken and refreshToken. Those methods must return an array containing an access_token key.

The storage needs to be a class implementing setItem(key, value), getItem(key) and removeItem(key). Those functions must return a promise.

At Mapado we use localforage in a browser environment and React Native AsyncStorage for React Native.

Configure the SDK
import RestClientSdk from 'rest-client-sdk';

const config = {
    path: 'api.me',
    scheme: 'https',
    port: 443,
    segment: '/my-api',
    authorizationType: 'Bearer', // default to "Bearer", but can be "Basic" or anything
    useDefaultParameters: true,
}; // path and scheme are mandatory

const clients = {
    someEntity: SomeEntityClient,
    // ...
};

const sdk = new RestClientSdk(tokenStorage, config, clients);

You can now call the clients this way:

sdk.someEntity.find(8); // will find the entity with id 8. ie. /v2/some_entities/8

sdk.someEntity.findAll(); // will find all entities. ie. /v2/some_entities

sdk.someEntity.findBy({ foo: 'bar' }); // will find all entities for the request: /v2/some_entities?foo=bar

Custom entity factory

You can inject a custom entity factory to the SDK. All entities will be send to the entityFactory.

The default entity factory is the immutable function fromJS

function entityFactory(input, listOrItem, clientName = null) {
    if (listOrItem === 'list') {
        // do stuff with your list input

        return output;
    } else { // listOrItem === 'item'
        const output = // ... do stuff with your input

        return output;
    }
}

const sdk = new RestClientSdk(tokenStorage, config, clients, entityFactory);

Keywords

FAQs

Package last updated on 02 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc