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

@api-blueprints/pathmaker

v1.3.0

Published

Structure and send API calls with ease

Downloads

9

Readme

API Pathmaker

Structure and send API calls that make sense in JS

NPM version

Imagine a scenario...

You need to add an API into your app. There isn't a JS client for it, so you'll have to use messy HTTP requests instead of clean JavaScript code. You could...

Construct a messy fetch function to send your request

import fetch from 'node-fetch';

fetch('https://cat-photos-api.xyz/find_cat_photo', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer UwU-uwu-oWo-0W0',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    limit: 100
  })
}).then(response => await response.json()).then(json => {
  // now you can do something
});

fetch('https://cat-photos-api.xyz/upload_cat_photo', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer UwU-uwu-oWo-0W0',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    url: 'https://my-cat-photos.uwucdn.com/my-cat-photo-no-238'
  })
}).then(response => await response.json()).then(json => {
  // now you can do something
});

OR construct a clean API template that works with as many API calls as you need

import API from '@api-blueprints/pathmaker';

const api = new API({
    headers: {
        'Authorization': 'Bearer UwU-uwu-oWo-0W0',
        'Content-Type': 'application/json'
    },
    baseUrl: 'https://cat-photos-api.xyz'
});

api.find_cat_photo.post({ limit: 100 }).then(data => { /* do something */ });
api.upload_cat_photo.post({ url: 'https://my-cat-photos.uwucdn.com/my-cat-photo-no-238' }).then(data => { /* do something */ });

Documentation

default class API ( object )

  • object.headers: Default headers. Common ones include Authorization and Content-Type
  • object.baseUrl: Base URL for this API. Should not have a / at the end
  • object.inputParser: A function to convert the input into a server-readable format. Defaults to JSON.stringify
  • object.outputParser: A function to parse the server output. Defaults to JSON.parse

Constructing a URL

To construct a URL, start by accessing a property on the API instance. Each property acts as a path. To access <baseURL>/boo/bar, use api.foo.bar. To specify an unknown parameter, use the JavaScript [] syntax. For example, you can access <baseURL>/documents/:documentNumber, use api.documents[documentNumber].

Search params

New in version 1.2.0, you can use the .searchParams() method to attach query params to the end of a url. Just pass in an object of keys and values.

Sending a request

To send a request that you've constructed, just run .get(), .post(), .patch(), etc. API Pathmaker works with GET, HEAD, POST, PUT, DELETE, PATCH, and OPTIONS requests. These are all async functions where you can pass in data to send to the API. You can also get the URL of the request without sending it by accessing the ._url property.

URLs containing HTTP method names

You might have noticed that if you try to access <baseURL>/route/get by using api.route.get.get(), it won't work. You can work around this by using api.route._absolute('get').