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

@richardpickett/api-lib

v1.1.6

Published

A library of notion wrapper classes

Downloads

257

Readme

api lib

Add to your packages.json:

npm i @richardpickett/api-lib

Usage

Extend to make your own api class:

import { Api } from "@richardpickett/api-lib";

class MyApi extends Api {
  constructor() {
    super({ baseUrl: "https://myApi.com/vi" });
  }

  async mySpecificApiCall(body) {
    return this._fetch({ path: "/my/api/call/path", body });
  }
}

const myApi = new MyAPI();

export default myApi;

Quck start: use your API class

import myApi from "./myApi.js";

myApi.mySpecificApiCall({ var: "val" }).then((jsonResult) => {
  console.log(JSON.stringify(jsonResult, null, 2));
});

The constructor

constructor({ baseUrl = "", headers = { "Content-Type": "application/json" } })

Override baseUrl with your api's base Url, and override the headers as needed. The headers set here are used in all subsequent calls unless the header is deleted with deleteHeader(name).

Calling your Api

_fetch({ path = "/", body = false, method = "POST", headers = this.headers, redirect = "follow" })

Typically you will override the path and body, overriding the method, headers, and redirect as needed.

path: appended to the baseUrl body: JSON encoded data for POST (build your own URI parameters and append to path if using GET with variables) method: all common HTTP methods are supported redirect: passed through to node's fetch()

Using specific headers

The headers for every call can be set in the super() constructor call, like this:

import Api from "@richardpickett/api-lib";

class MyApi extends Api {
    constructor() {
        super( { baseUrl: "https://myApi.com/vi", headers: { "HeaderName": "HeaderValue" } } )
    }
...

You can set specific headers (Auth tokens, etc) via the constructor, or for specific calls when you call _fetch(). Please note, if you supply headers to the _fetch() call, it will not use the headers set in the constructor or via setHeader()

The default header set in the constructor is: { "Content-Type": "application/json" }

Setting headers for every call after constructor

Use the following to set/unset the default headers for every call:

setHeader(name, value);
deleteHeader(name);

Using a cookie jar

If your api sets cookies and you need to use them on subsequent calls, use _cookieFetch() instead of _fetch(), all the same parameters apply, with the addition that each call will use a cookie jar for each call.