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

api-atlas

v0.0.6

Published

An easy to use api mapper

Downloads

3

Readme

api atlas

Api atlas is a very lightweight library for mapping an api with a simple configuration. It creates a client that handles caching and parsing for you. It can also be integrated with react using react-api-atlas.

Installation

With npm

$ npm install --save api-atlas

With yarn

$ yarn add api-atlas

Getting started

Atlas export three functions:

  • AtlasClient
  • AtlasMap
  • createNetworkInterface
import { AtlasClient, AtlasMap, createNetworkInterface } from 'api-atlas';

>>>> FOR A VERY QUICK START, READ STEP 1 AND 5 <<<<

Step 1

Create an atlas configuration. The configuration is a js object with the following shape:

const apiConfig = {
    host: 'your api host', 
    options: {}, // root level options
    resources: { // each resource is an object with a path and end-points
        Users: { // The resource name
            path: '/users', // The resource path
            options: {}, // resource level options
            endPoints: { // Each end-point is an object with path and options (fetchOptions - see below)
                getUsers: {
                    path: '/{username}/repos',
                    options: { // options is all fetch options (see below), plus cache and params (see below)
                        cache: false, // turn cache on or off for this request
                        params: { // default parameters
                            username: 'gugamm', // used if no username is supplied
                        },
                    },
                },
            },
        },
    },
};

Note: fetchOptions are options supported by the fetch api. For more information access fetch

Note 2: You can provide options at resource and root level. You could for example turn off cache for the entire api, or just for a resource, however, inner options override outer options.

Step 2

create an api map

import { AtlasMap } from 'api-atlas';

const apiConfig = /* ... */;
const apiMap = AtlasMap(apiConfig); // This is the map of your api

The map of your api provide functions to build a request definition. This request definition is passed to the client so it can solve the request based on your configurations. That's why you need a map. We gonna see how to use it in the next steps.

Step 3

create a network interface

import { AtlasMap, createNetworkInterface } from 'api-atlas';

const apiConfig = /* ... */;
const apiMap = AtlasMap(apiConfig); // This is the map of your api
const networkInterface = createNetworkInterface({
    responseParser: response => response.json(), // you can provide a custom parser (optional)
});
networkInterface.setBeforeRequestListener(
    (url, options) => new Promise(resolve => setTimeout(resolve, 5000)),
); // You can provide a middleware that must return a promise that will resolve. You can override the url and the options by resolving with a object of { url, options }.
// Example: resolve({ url: 'newurl', options: {...options, body: 'newbody' } });

note There is no need to pass a responseParser. The network interface uses a json parser by default.

note 2 You can use setBeforeRequestListener as a middleware to modify and log data about the request. Here you could set headers like authorization by returning new options. (see example below)

networkInterface.setBeforeRequestListener(
    (url, options) => new Promise(resolve => {
        let token = null;
        if (window.localStorage) {
            token = window.localStorage.getItem('TOKEN');
        }
        resolve({ 
            options: {
                headers: {
                    ...options.headers, // <<-- Important! If you don't do this, other headers can be lost.
                    Authorization: token,
                },
            },
        });
    }),
);

Step 4

Finally! Create your client and you are ready to go!

import { AtlasClient, AtlasMap, createNetworkInterface } from 'api-atlas';

const apiConfig = /* ... */;
const apiMap = AtlasMap(apiConfig); // This is the map of your api
const networkInterface = createNetworkInterface(); // You can use the default constructor
const client = AtlasClient({
    networkInterface,
    getIdFromRequest: (url, options) => url, //return a unique id for the request
});

note You must pass a networkInterface

note 2 getIdFromRequest is an optional function used by the cache handler to get an unique id for a request. This way it can look into the cache to see if there is a cached response there. This parameter is optional, but you can override to provide a better implementation. The default one uses the function above (id = url).

Step 5

Using atlas

import { AtlasClient, AtlasMap, createNetworkInterface } from 'api-atlas';

const apiConfig = /* ... */;
const apiMap = AtlasMap(apiConfig); // This is the map of your api
const networkInterface = createNetworkInterface(); // You can use the default constructor
const client = AtlasClient({ networkInterface });
const fetchDefinition = apiMap.Users.getUsers(); //You must call as a function so it return the fetch definition
const fetchOptions = { params: { username: 'gugamm' } }; //You can pass options
client.fetch(fetchDefinition, fetchOptions).then(repos => console.log(repos)); //yay!!

More topics (coming soon)

  • ApiClient docs (updating cache after mutating and clearing cache)

License

MIT