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 🙏

© 2026 – Pkg Stats / Ryan Hefner

apidefs

v0.0.6

Published

Create API mappings with ease

Readme

apidefs

Create API mappings with ease

Installation

with NPM

npm i apidefs --save

with YARN

yarn add apidefs

Peer dependencies

  • Install dataloader if you use loader resolver (see example)

Features

  1. REST API supported
  2. GraphQL API supported
  3. Dataloader supported
  4. Middleare supported
  5. Fully Typescript supported

Usages

Defining simple REST API

import { define } from "apidefs";
import { rest } from "apidefs/rest";

const api = define({
  getUserById: rest("https://yourserver.com/api/getUserById/{id}", {
    // convert api payload to URL params
    params: (payload) => ({ id: payload }),
  }),
  // if your API requires user id as query param, just do this
  getUserById2: rest("https://yourserver.com/api/getUserById", {
    // convert api payload to URL params
    query: (id) => ({ id }),
  }),
});

// apidefs will generate an object that has getUserById method
// a API mapped method returns a promise object. The promise object will resolve when request completed and reject if there is any HTTP error
api.getUserById(1);

Defining GraphQL API

import { define } from "apidefs";
import gql from "graphql-tag";
import { graphql } from "apidefs/graphql";

const getMediaByIdQuery = gql`
  query GetMediaById($id: Int!) {
    Media(id: $id) {
      id
      startDate {
        year
        month
        day
      }
      season
      tags {
        id
      }
    }
  }
`;

const api = define({
  // graphql() can retrieve query object or query string
  getMediaById: graphql(getMediaByIdQuery, {
    url: "https://yourserver.com/graphql",
    // map payload to variables
    vars: (payload) => ({ id: payload }),
  }), // this API will return { Media: { id, name, ... } }
  getMediaById2: graphql(
    getMediaByIdQuery,
    // select Media prop of data object that returned from server
    "Media",
    {
      url: "https://yourserver.com/graphql",
      vars: (payload) => ({ id: payload }),
    } // this API will return { id, name, ... }
  ),
});

Using HTTP configs

const api = define({
  configs: {
    // headers builder
    headers(payload) {
      return {
        // pass access token that is saved in localStorage
        authorization: localStorage.getItem("token"),
      };
    },
    http: { baseUrl: "https://yourserver.com/api" },
  },
  // the url will prepend baseUrl from http configs
  getUserById: rest("/getUserById/{id}", { params: (id) => ({ id }) }),
});

Using dataloader

apidefs uses dataloader package for handling batch requests. Let say you have an API get users by id list

import { transform } from "apidefs";
import { loader } from "apidefs/loader";
import { rest } from "apidefs/rest";
const api = define({
  configs: { http: { baseUrl: "https://yourserver.com/api" } },
  // the url will prepend baseUrl from http configs
  getUserById: loader(
    // we are not sure the user list that is returned from server has same other with id list
    // so we use transform resolver to re-order the result
    transform(
      rest("/getUsers", {
        method: "post",
        // passing ids to request body
        body: (ids) => ({ ids }),
      }),
      (users, ids) => ids.map((id) => users.find((x) => x.id === id))
    )
    // passing dataloader options
  ),
});

api.getUserById(1);
api.getUserById(2);
api.getUserById(1); // duplicated
api.getUserById(3);
// all getUserById requests will be delayed in awhile and only one getUsers request sends to server

Handling errors

const api = define({
  configs: {
    // this is global error handler, it will be called by all API providers (rest, graphql)
    onError(e) {
      console.log(e);
    },
    // by default, dismissErrors = false, if you set dismissErrors = true, all errors are dismissed and the API will return the promise that runs forever. The error handlers still recieve errors
    dismissErrors: false,
    ...rest.configs({
      // this is error handler for REST API only
      onError(e) {},
    }),
    ...graphql.configs({
      // this is error handler for GraphQL API only
      onError(e) {},
    }),
  },
});

Using middleware

apidefs provides middleware machanism, where you can control requests working flow with ease

import { use, define } from "apidefs";
import { rest } from "apidefs/rest";

// simple cache middleware
const cache = (keyFactory) => (context) => {
  const cacheStorage = context.__cache || (context.__cache = {});
  return (next) => (payload) => {
    const key = keyFactory(payload);
    if (!cacheStorage[key]) {
      // the origin api and store the result
      cacheStorage[key] = next(payload);
    }
    return cacheStorage[key];
  };
};

const api = define({
  getUserById: use(
    rest("/getUserById"),
    // generate cache key according to api payload
    cache((payload) => `getUserById:${payload}`)
  ),
});

api.getUserById(1);
api.getUserById(1);
api.getUserById(1);
// only one request sent

API References

https://linq2js.github.io/apidefs/