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

apitool

v0.1.3

Published

<p align="center"><img width="256" src="https://raw.githubusercontent.com/eunjae-lee/apitool/master/logo.png"></p>

Downloads

6

Readme

Build Status npm version GitHub stars Twitter

Introduction

apitool is a wrapper of axios. It provides an organized way to work with APIs.

This goes smoothly with your TypeScript-based project since it's written in TypeScript.

Table of contents

Install

npm install apitool --save

Getting Started

Performing a simple GET request

import Api from 'apitool';

const result = await new Api().request("get", url);
// or
const result = await new Api().get(url);

You can, of course, put params like the following:

const params = {
  ...
}

const result = await new Api().request("get", url, params);
// or
const result = await new Api().get(url, params);

Performing a simple POST request

As you guess,

const data = {
  ...
}

const result = await new Api().request("post", url, data);
// or
const result = await new Api().post(url, data);

apitool provides get, post, put and delete.

result Schema

The request returns a result. It looks like the following:

{
  // It indicates if error has occurred
  error: boolean;

  // It indicates what kind of error has occurred
  errorType?: ErrorType;

  // It holds an error code or any error-related data
  errorCode?: any;

  // A response object which has been transformed by your transformers.
  response?: T | undefined;

  // An original axios response object
  orgResponse?: AxiosResponse;
}

So after executing api call, you can handle error like this:

const result = await new Api().get(url);
if (result.error) {
  // handleError with result.errorType and result.errorCode
} else {
  // do something with result.response
}

Or with object destructuring,

const { error, errorType, errorCode, response } = await new Api().get(url);
if (error) {
  // handleError
} else {
  // do something
}

Getting deeper

So far it doesn't seem to be different from axios. Here's an real life example to help you understand what apitool really exists for.

const myApi = new Api().extend({
  baseURL: MY_DOMAIN,
  before: [
    () => showLoader()
  ],
  after: [
    () => hideLoader()
  ]
  transformData: [
    (data) => decamelizeKeys(data),
  ],
  transformResponse: [
    (response) => camelizeKeys(response),
  ]
})

In JavaScript people usually use camelCase and in rails or in some server-side languages they usually use snake_case. With transformData and transformResponse, you can convert cases easily. And unlike axios, transformData applies to all methods including get.

before and after helps you execute things before request and things after request.

With apitool, you can extend this.

const myAuthApi = myApi.extend({
  headers: {
    Authorization: () => getAuthToken()
  },
  responseValidations: [
    async (response, context, orgResponse) => {
      if (!invalidAuth(orgResponse)) {
        return;
      }

      if (needToRefreshToken(orgResponse)) {
        await refreshToken();
        context.retry();
      } else {
        context.cancelAll();
        sendEventToRedirectToLogin();
      }
    },
    (response, context, orgResponse) => {
      if (!isOkay(orgResponse)) {
        context.error("not okay");
      }
    }
  ]
});

You can put headers. Each value could be a string or a function returning a string. When it's a function, it shouldn't be async.

Next, we see responseValidations. You can put an array of functions and they might be async or sync. Each function takes three arguments:

  • response : A response object which has been transformed by your transformResponse.
  • context : A context object with functions to be called when it's not successful.
    • error(errorCode?: any) : It returns an error. Once any of error, retry or cancelAll is called, then it will not execute next validation functions. However after callbacks will be still executed.
    • retry(retryNum = 1) : If you want to retry, call this function. The result from the retried request will be returned.
    • cancelAll() : It cancels all the other ongoing requests. For example, you can call this when user needs to be logged out due to expired token.
  • orgResponse : An original axios response object
const { error, errorType, errorCode, response, orgResponse } = await myAuthApi.get(path);

ErrorType Schema

enum ErrorType {
  // `retry()` has been called, it retried, but eventually failed
  // `errorCode` won't contain anything
  RETRY_DONE_FAILED,

  // `cancelAll()` has been called from this request
  // `errorCode` won't contain anything
  CANCELED_ALL,

  // `cancelAll()` has been called from other request, so this request has got canceled
  // `errorCode` won't contain anything
  GOT_CANCELED,

  // axios has thrown an exception
  // `errorCode` will contain exception object
  EXCEPTION,

  // `error()` has been called
  // `errorCode` will contain whatever you passed at `error(whatever)`
  USER_DEFINED_ERROR
}

Combining configs

You can extend api objects like above, however there's another approach. You can combine configs and use it like the following:

import { mergeConfigs } from "apitool";

const config1 = {
  baseURL: ...
  headers: {},
  transformData: []
  transformResponse: []
  before: []
  after: []
  responseValidations: []
};
const config2 = {...};
const config3 = {...};
const config = mergeConfigs(config1, config2, config3)

const api = new Api(config);
const result = await api.get(path);

Importing things

All you can import from apitool is the following:

import Api, { Response, Context, ErrorType, mergeConfigs } from "apitool";

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

Eunjae Lee, Released under the MIT License.