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

api-client

v2.0.2

Published

Object Oriented library for HTTP Web API clients

Readme

api-client

node.js fetch wrapper and api endpoint configuration management

Why?

Needed a driver for fetch that clearly separated configuration of web service API endpoints from the code that consumed them.

Installation

npm install api-client

Usage

api-client manages configuration and creation of a set of named api endpoints. Endpoint configuration can be achieved in one of three ways:

  1. The library can load a configuration using the node-config module. (See https://github.com/node-config/node-config)
  2. The configuration can be supplied explicitly to the library by clients
  3. Configuration can be added piecemeal by registering endpoint classes and configuration by calling functions.

In the first case, the configuration should define a single attribute 'endpoints', pointing at an object. The object in turn contains any number of endpoint configuration objects as attributes.

{
  "endpoints": {
    "twitter": {
      "type": "ApiClient",
      "host": "api.twitter.com",
      "options": {
        "protocol": "https"
      }
    },
    "other_api": {
      "host": "other.com"
      ...
    }
  }
}

The above configuration object defines configuration of two named endpoints, 'twitter' and 'other_api'. The configurations can be referred to by name when creating instances of ApiClient for sending requests to the web service api. The configuration may specify a 'type' attribute, whose value is the name of a registered or pre-configured api client object.

Configuration

Each endpoint configuration object has the following layout:

"host": "some.host.com"     # The only required attribute
"port": 232                 # Defaults to 80 or 443, depending on the
                            #   options.protocol attribute
"type": "StringClassName"   # Defaults to 'ApiClient'
"options": {
  "protocol": "http|https", # Either 'http' or 'https', defaults to 'http'
  "base_path": "/apibase",  # The base of all url paths for the service, defaults to '/'
  "username": "user",       # Defaults to null, use to configure HTTP basic auth
  "password": "pass"        # Defaults to null, use to configure HTTP basic auth
}

The url formed by the api-client will therefore be:

"#{options.protocol}://#{host}:#{port}#{base_path}"

Using the default configuration

import { ApiClient } from './lib/api_client.js';
const client = ApiClient.create("dseifert");
const r = await client.get('/', {});
console.log("RESPONSE: ", r);

Client supplied configuration

import { ApiClient } from './lib/api_client.js';
const myConfig = {
  foo_client: {
    host: 'foo.com'
  }
};
ApiClient.load(myConfig);
foo_client = ApiClient.create('foo_client')
foo_client.get({...})

Registering client created ApiClient subclasses

import { ApiClient } from './lib/api_client.js';

class FooClient extends ApiClient {
  test() {
    console.log("Foo request: " + self.url());
  }
}

ApiClient.registerClass('FooClient', FooClient);
ApiClient.registerConfig('fooclient', {
  host: 'foo.com',
  type: 'FooClient',
  options: {
    base_path: '/fooapi'
  }
});
fc = ApiClient.create('fooclient')
fc.test()

License

MIT Licensed

Copyright (c) 2013 Douglas A. Seifert