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

make-axios-request

v1.1.1

Published

Create 100% type-safe Axios requests with Zod, with other features like retrying and queueing

Downloads

11

Readme

make-axios-request

Axios wrapper

Installation

yarn add make-axios-request

Usage

import { makeAxiosRequest } from "make-axios-request";
import { z } from "zod";

const outputSchema = z.object({
	response: z.object({
		access_token: z.string(),
		token_type: z.string(),
		expires_in: z.number(),
		scope: z.string(),
		user_id: z.coerce.number(),
		refresh_token: z.string(),
	}),
});

// res is typed:
// {
//     access_token: string;
//     refresh_token: string;
//     token_type: string;
//     expires_in: number;
//     scope: string;
//     user_id: number;
// }
const res = await makeAxiosRequest({
	method: "post",
	baseURL: "http://api.example.com/",
	url: "/oauth/token",
	queue: {
		// All makeAxiosRequest calls with this key will enter a queue with a 100ms delay (default)
		key: "example_api_queue_key",
	},
	dataSchema: z.object({
		grant_type: z.literal("refresh_token"),
		client_id: z.string(),
		client_secret: z.string(),
		refresh_token: z.string(),
		user_id: z.string().optional(),
	}),
	// errors if not valid z.input of dataSchema
	data: {
		grant_type: "refresh_token",
		client_id: "123",
		client_secret: "123",
		refresh_token: "token",
		// user_id will error if removeNulls: true is not supplied, as the input schema is .optional()
		user_id: null,
	},
	paramsSchema: z.object({
		timestamp: z.number(),
		signature: z.string().optional(),
	}),
	params: {
		// Type 'string' is not assignable to type 'number'.ts(2322)
		timestamp: "error",
		signature: null,
	},
	removeNulls: true,
	outputSchema,
	postProcessor: (data: z.output<typeof outputSchema>) => data.response,
	retry: true,
});

API

Input (Object)

  • url: The URL to make the request
  • method: The HTTP method like get, post, put etc.
  • baseURL : The base url for the HTTP request
  • data : The request body
  • dataSchema: Zod schema for request body parsing.
  • params: The URL parameters to send with request.
  • paramsSchema: Zod schema for params parsing.
  • outputSchema: Zod schema for response parsing.
  • extraBody: Additional body parameters to send with the request.
  • headers: The headers for the request.
  • extraParams: Additional parameters to send with the request.
  • preProcessor: A function that receives the raw axios return body and pre-processes it. useful if you only need to extract part of the response and do not want to create a schema for the the whole response. Not type-safe.
  • postProcessor: A function that receives the response of the outputSchema (z.output) and returns adjusts the return of the makeAxiosRequest function, type-safe.
  • errorHandler: Handles AxiosError.
  • axiosInstance: Custom Axios instance to use for the request.
  • retry: To retry the failed request. Uses axios-retry, max of 3 times.
  • queue: If provided, queues the task.
    • queue.key: Unique key to the queue
    • queue.delay: Delay between tasks in the queue, in ms (100ms by default).
  • responseType: Response data type
  • removeNulls: Remove null values from params and body, useful if the input schema is has .optional() properties and your data can contain nulls that should be ignored.

License

MIT