npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autonym-client

v0.1.1

Published

A JavaScript client for consuming Autonym APIs.

Downloads

6

Readme

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])

| Argument | Type | Description | Default Value | |----------------------|------------------------|------------------------------------------------------------------------------------------------|---------------| | uri | string | The URI to your Autonym server. | None | | config | object | | {} | | config.serialize | function(attributes) | A function to transform resource attributes before sending it. | None | | config.unserialize | function(attributes) | A function to transform resource attributes received. | None | | config.axiosConfig | object | Additional configuration to pass to the axios instance. | {} |

Autonym#create(route, attributes)

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

| Argument | Type | Description | Default Value | |--------------|----------|---------------------------------|---------------| | route | string | The route for the resource. | None | | attributes | object | The 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.

| Argument | Type | Description | Default Value | |----------|----------|----------------------------------------------------------------------------------------------------------------------------------|---------------| | route | string | The route for the resource. | None | | query | object | An 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.

| Argument | Type | Description | Default Value | |----------|----------|------------------------------------------------------|---------------| | route | string | The route for the resource. | None | | id | string | The 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.

| Argument | Type | Description | Default Value | |--------------|----------|---------------------------------------------------------|---------------| | route | string | The route for the resource. | None | | id | string | The id that uniquely identifies the resource to update. | None | | attributes | object | The 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.

| Argument | Type | Description | Default Value | |----------|----------|---------------------------------------------------------|---------------| | route | string | The route for the resource. | None | | id | string | The 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.

| Argument | Type | Description | Default Value | |----------|----------|-----------------------------------|---------------| | route | string | The 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 };
      }
    }
  }
});