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

fast-client

v1.0.2

Published

Quickly create a client for an api

Downloads

30

Readme

Fast Client

This library is intended to be a facilitator when defining which API calls you want to make. The library behaves in a way that allows you to define all calls in one place, in a simple structure.

How to use

Let's assume you have an API with the endpoint /search. First, create an instance of the FastClient:

const client = createFastClient({
  base: 'https://api.example.com',
});

Now you can define function your endpoints using the client function, and then call it:

const search = client(search: {
  method: 'GET',
  href: '/search',
});

// res: Promise<Response>
const res = search({
  query: { q: 'teste', limit: 10 },
});

You can also define path params using {} in the href:

const getUser = client({
  method: 'GET',
  href: '/user/{id}',
});

This will allow you to pass the id parameter in the request:

// res: Promise<Response>
// 'path' with property 'id' is required here
const res = getUser({ path: { id: 123 } });

Parsers

To automatically convert the return type, just pass a parser function to the endpoint definition:

type User = { id: string; name: string };

const getUser = client({
  method: 'GET',
  href: '/user/{id}',
  parser: (res) => res.json() as Promise<User>,
});

For simplicity you can create a custom helper to parse json responses with types:

const asJson = <T>(res: Response) => res.json() as Promise<T>;

type User = { id: string; name: string };

const getUser = client({
  method: 'GET',
  href: '/users/{id}',
  parser: asJson<User>, // much better now
});

Now, when you call getUser, the result will parsed and the return type will be of type Promise<User>:

// res will be of type Promise<User>
const res = getUsers({
  query: { name: 'matheus', limit: 10, order: 'asc' },
});

Middlewares

Use a middleware to define a function to intercept the request and response of all endpoints:

const client = createFastClient({
  base: 'https://api.example.com',
  async middleware(req, next) {
    // here you can modify the request
    req.headers.append('X-Application-Key', 'ABC123');

    // call next to continue the pipeline
    const res = await next(req);

    // here you can modify the response

    // return the response
    return res;
  },
});

This pattern can help you add Authorization header for each request.

Interceptors

You can also intercept requests and responses on any endpoint you want:

const unsubscribe = client.on('response', (res) => {
  if (res.status === 401) navigate('/login');
  return res;
});

The function return a unsubscribe function for removing the event and stop it from being called. This pattern, for example, is super handy when used inside a useEffect in react.