0.0.1 • Published 7 years ago

moot-sdk-browser v0.0.1

Weekly downloads
6
License
VOL
Repository
github
Last release
7 years ago

moot-sdk-browser

Function usable as a moot-api provider to the moot-interface which interacts with a moot REST API.

You'll likely use it like this in your moot app:

// somewhere in the setup of the app
import sdk from 'moot-sdk-browser'
import mannish from 'mannish' // or some other mediator
import fetch from 'whatwg-fetch' // or some other fetch polyfill

const mediator = mannish()
const baseUrl = 'https://your-site.com/api'

mediator.provide('moot-api', sdk({ fetch, baseUrl }))

api

The API of this module looks like:

options(Object<>) => callable(Object<>) => Promise<>

It returns a callable function, which returns a promise that resolves when the API fetch is completed.

options(Object<>)

Create an instance of the SDK by calling the default exported funtion with an object containing the following properties:

  • fetch (function, required): This must be an implementation of fetch, but in practice this has only been tested with the whatwg-fetch polyfill implementation.
  • baseUrl (string, required): The base URL to the moot API that fetch requests will go to. This must not have a trailing slash.
  • apiUsesDifferentDomain (boolean, default false): If the API lives on a different domain, the appropriate CORS options will be set.

callable(Object<>)

Creating an instance of the SDK returns a function which is used to make fetch requests to the API. This callable function takes an object with the following properties:

  • resource (string, required): The resource identifier without parameters included. E.g. /players/:playerId instead of /players/abc123.
  • method (string, required): The method to call on this resource, e.g. the normal capitalizes HTTP methods, GET, POST, etc.
  • parameters (object): This key-value map will be used to fill in the resource identifier parameters.
  • filter (object): Filter the results returned from the API using these properties.
  • include (array of strings): Types of resources to include in the API response.
  • fields (object): This key->array map is used to limit the fields included in the API response.
  • body (object): The data passed to the API, e.g. in a POST or PUT request. If this is a JavaScript object, it will be JSON-stringified and the appropriate headers set.
  • files (object): This key->FormData map will upload binary files to the API.

license

This project published and released under the Very Open License.