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

@studiokeywi/papi

v0.1.7

Published

PAPI - The Proxy API tool to make API calls friendlier

Downloads

6

Readme

PAPI

The Proxy API tool from studioKeywi for better DX. Zero dependencies. Built with ❤️ and Bun, Material for MkDocs, TypeDoc, and Vitest

NPM Version NPM Type Definitions npm bundle size (scoped) NPM Unpacked Size NPM License

Installation

PAPI is available to install through the NPM registry and can be installed by any runtime that is compatible with NPM installs:

Node

Node projects can install PAPI as a dependency:

npm install @studiokeywi/papi

Bun

Bun projects can install PAPI as a dependency:

bun add @studiokeywi/papi

Deno

Deno projects can import PAPI using the npm: specifier:

import { papi } from 'npm:@studiokeywi/papi';

Or by adding to the import map of deno.json:

{
  "imports": {
    "papi": "npm:@studiokeywi/papi"
  }
}
import { papi } from 'papi';

PAPI is a TypeScript first implementation. Along with providing and exporting its own types, exports are provided for the Bun and Deno runtimes as well as an explicit @studiokeywi/papi/ts export entry

Under the hood, PAPI is a simple builder syntax combined with a custom JS Proxy object used to wrap around the Web API fetch method

PAPI performs no validation on API responses, nor does PAPI provide any integrated security features. When building an API caller, PAPI allows an API key/token value to be provided automatically in the Authorization header in Bearer: [token] format in plaintext. This could be detected by any software that can read or intercept network requests

Beyond this, PAPI allows any standard option that can be provided to the second argument of your runtime's implementation of the Web API fetch method. If you are concerned about the security/validity of your network calls, we strongly suggest utilizing other libraries in addition to/instead of PAPI to perform your security/validation

Usage

PAPI is designed with a chainable builder approach to defining the shape of any API, combined with a chained property approach to calling the various API endpoints. The combination allows for a natural and expressive way to declare and request expected API responses

PAPI in 4 lines

import { papi } from '@studiokeywi/papi';

const url = 'https://jsonplaceholder.typicode.com';
const albumShape = { userId: 0, id: 0, title: '' };
const allAlbums = await papi(url)                     /* 1. Define a base URL */
  .path('albums', albums => albums.get([albumShape])) /* 2. Define API endpoints */
  .build()                                            /* 3. Create API caller */
  .albums.get();                                      /* 4. Make an API request */
// typeof allAlbums = { userId: number; id: number; title: string }[]

API Builder

PAPI allows API endpoints to be defined as simply or robustly as needed. An expanded example of the JSONPlaceholder API's /albums endpoint is demonstrated below:

Example Endpoint

import { papi } from '@studiokeywi/papi';

const albumShape = { userId: 0, id: 0, title: '' };
const errShape = {};
const photoShape = { albumId: 0, id: 0, title: '', url: '', thumbnailUrl: '' };

const apiCaller = papi('https://jsonplaceholder.typicode.com')
  .path('albums', $albums =>
    $albums
      .get([albumShape], $get => $get.error(errShape).queryOpt(albumShape))
      .post(albumShape, $post => $post.bodyOpt(albumShape).error(errShape))
      .slug($albumId =>
        $albumId
          .get(albumShape, $get => $get.error(errShape))
          .delete({}, $del => $del.error(errShape))
          .patch(albumShape, $patch => $patch.bodyOpt(albumShape).error(errShape))
          .put(albumShape, $put => $put.bodyOpt(albumShape).error(errShape))
          .path('photos', $photos => $photos.get([photoShape], $get => $get.error(errShape)))
      )
  )
  .build();

See the builder docs for more

API Caller

The API Caller tool allows arbitrary API endpoints to be called. Following from the example above:

Example Caller

const data = { title: "studioKeywi's Loudest Hits!" };
const newAlbum = await apiCaller.albums.post({ data });
if (!('id' in newAlbum)) throw new Error(`API Post failed and returned ${newAlbum}`);

const photos = await apiCaller.albums[newAlbum.id].photos.get();
if (!('length' in photos)) throw new Error(`API Get failed and returned ${photos}`);

photos.forEach(photo => {
  // ...
});

See the caller docs for more

API Documentation

Generated by TypeDoc: API Documentation

Future

Think PAPI is missing a feature? Open an issue!

Links

PAPI Docs
PAPI NPMJS
PAPI Repo