0.1.1 • Published 7 years ago

autonym-client v0.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

autonym-client

A JavaScript client for consuming Autonym APIs.

A class that provides a thin wrapper around axios for making HTTP requests against an Autonym API.

Installation

npm install autonym-client --save

The package can be imported into your application if you are using a build system like Webpack or Browserify.

// es2015
import Autonym from 'autonym-client';

// common-js
const Autonym = require('autonym-client');

Alternatively, you can point a script tag to the dist file. Note that the dist file bundles its dependencies, axios and qs.

<!-- uncompressed -->
<script src="/node_modules/autonym-client/dist/autonym.js"></script>

<!-- compressed -->
<script src="/node_modules/autonym-client/dist/autonym.min.js"></script>

Quick Start

const autonym = new Autonym('https://api.myservice.com');
const peopleApi = autonym.bindToRoute('people');

// Create a new resource
peopleApi.create({ firstName: 'John', lastName: 'Galt' }).then(response => console.log(response));

// Fetch resources
peopleApi.find().then(response => console.log(response));

// Fetch a resource
peopleApi.findOne('42').then(response => console.log(response));

// Update an existing resource
peopleApi.findOneAndUpdate('42', { lastName: 'Doe' }).then(response => console.log(response));

// Delete a resource
peopleApi.findOneAndDelete('42').then(response => console.log(response));

API

Autonym#constructor(uri[, config])

ArgumentTypeDescriptionDefault Value
uristringThe URI to your Autonym server.None
configobject{}
config.serializefunction(attributes)A function to transform resource attributes before sending it.None
config.unserializefunction(attributes)A function to transform resource attributes received.None
config.axiosConfigobjectAdditional configuration to pass to the axios instance.{}

Autonym#create(route, attributes)

Serializes the given attributes and sends a request to create a new resource.

ArgumentTypeDescriptionDefault Value
routestringThe route for the resource.None
attributesobjectThe attributes of the resource.None

Returns a promise that resolves with the unserialized server response.

Autonym#find(route[, query = {}])

Sends a request to fetch resources, optionally passing a query string to filter the result set.

ArgumentTypeDescriptionDefault Value
routestringThe route for the resource.None
queryobjectAn object that will be converted to a query string via qs.stringify() and appended.{}

Returns a promise that resolves with the unserialized server response.

Autonym#findOne(route, id)

Sends a request to fetch a resource.

ArgumentTypeDescriptionDefault Value
routestringThe route for the resource.None
idstringThe id that uniquely identifies the resource to get.None

Returns a promise that resolves with the unserialized server response.

Autonym#findOneAndUpdate(route, id, attributes)

Serializes the given attributes and sends a request to update an existing resource.

ArgumentTypeDescriptionDefault Value
routestringThe route for the resource.None
idstringThe id that uniquely identifies the resource to update.None
attributesobjectThe attributes to update.None

Returns a promise that resolves with the unserialized server response.

Autonym#findOneAndDelete(route, id)

Sends a request to delete a resource.

ArgumentTypeDescriptionDefault Value
routestringThe route for the resource.None
idstringThe id that uniquely identifies the resource to delete.None

Returns a promise that resolves with the unserialized server response.

Autonym#bindToRoute(route)

A convenience method that returns the methods on this class bound to the given route.

ArgumentTypeDescriptionDefault Value
routestringThe route to bind the methods to.None

Returns an object with the methods bound to the given route.

Examples

Serializing and unserializing resources

const autonym = new Autonym('https://api.myservice.com', {
  serialize: attributes => ({
    ...attributes,
    birthdate: attributes.birthdate.getTime()
  }),
  unserialize: attributes => ({
    ...attributes,
    birthdate: new Date(attributes.birthdate)
  })
});

Reading and modifying headers

const autonym = new Autonym('https://api.myservice.com', {
  axiosConfig: {
    transformRequest: (data, headers) => {
      headers['Authorization'] = `Token ${localStorage.getItem('apiToken')}`;
      return data;
    },
    transformResponse: (data, headers) => {
      if (Array.isArray(data)) {
        return { items: data, numberOfPages: parseInt(headers['x-page-count'], 10) };
      } else {
        return { item: data };
      }
    }
  }
});
0.1.1

7 years ago

0.1.0

7 years ago