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

portapi-helper

v1.0.0

Published

Easy-peasy, heavily typed, and zod validated requests for browsers, Node, and React Native.

Downloads

10

Readme

PortAPI

Easy-peasy, heavily typed, and zod validated requests for browsers, Node, and React Native. Inspired by trpc.

npm i portapi-helper #npm
yarn add portapi-helper #yarn
pnpm add portapi-helper #pnpm

Basic Usage

import { createClient } from 'portapi-helper';
import { z } from 'zod';

const client = createClient('https://jsonplaceholder.typicode.com/', {
  headers: {
    'content-type': 'application/json'
  }
});

client.get('todos/1').then((response) => {
  if (!response.success) {
    return console.error(response.error.toString());
  }

  console.log(response.data);
});

// With Zod validation & heavy typing
client
  .get(
    'todos/1',
    z.object({
      id: z.number(),
      title: z.string
    })
  )
  .then((response) => {
    if (!response.success) {
      return console.error(response.error.toString());
    }

    console.log(response.data); // response.data { id: 1, title: 'delectus aut aute' }
  });

Advanced Usage

import { createClient } from 'portapi-helper';
import { z } from 'zod';

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {
    headers: {
      'content-type': 'application/json'
    }
  },
  {
    onSuccess: (data) => {},
    onFailedValidation: (error) => {},
    beforeRequest: (request) => {}
    // other handlers
  }
);

client
  .get(
    'todos/1',
    z.object({
      id: z.number(),
      title: z.string()
    })
  )
  .then((response) => {
    if (!response.success) {
      return console.error(response.error.toString());
    }

    console.log(response.data); // response.data { id: 1, title: 'delectus aut aute' }
  });

Request Handlers

Request handlers intercept request at different stages in the request lifecycle.

beforeRequest

Called before the execution of a request

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    beforeRequest: (request: RequestOptions) => {
      console.log(request.method, request.url, request.body, request.headers);
    }
  }
);

onRequest

Called during the execution of request to mutate the request. Useful for appending the request headers and body.

const protectedClient = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onRequest: (request: RequestOptions) => {
      return {
        headers: {
          Authorization: 'Bearer ' + token
        }
      };
    }
  }
);

onFailedAuthorization

Called when a request returns a 403: Unauthorized status code

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onFailedAuthorization: () => {
      console.log("can't do that!");
    }
  }
);

onFailedAuthentication

Called when a request returns a 401: Unauthenticated status code

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onFailedAuthentication: () => {
      location.replace('https://www.my-awesome.app/login');
    }
  }
);

onFailedRequest

Called when a request returns a 5xx status code

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onFailedRequest: (response: any) => {
      // do something
    }
  }
);

onFailedParse

Called when json.parse() fails

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onFailedParse: () => {
      // do something
    }
  }
);

onFailedValidation

Called when zod validation fails

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onFailedValidation: (error: z.ZodIssue[]) => {
      console.error(error.join('\n'));
    }
  }
);

onSuccess

Called after successful completion of a request

const client = createClient(
  'https://jsonplaceholder.typicode.com/',
  {},
  {
    onSuccess: (data) => {
      // do something
    }
  }
);