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

nano-http-client

v6.0.0

Published

A nano http client for node.js and the browser based on fetch

Downloads

9

Readme

NANO API CLIENT

NPM

Github

npm install nano-http-client
# or
pnpm add nano-http-client
# or
yarn add nano-http-client
# or
bund add nano-http-client

First, you need to instantiate the client with a base URL.

const client = new NanoHttpClient({
	baseUrl: 'https://api.example.com',
	headers: {
		'Content-Type': 'application/json',
		Accept: 'application/json',
	},
});

By default, the client will use the fetch API to make requests. You can pass a custom fetch function to the client constructor.

const client = new NanoHttpClient({
	baseUrl: 'https://api.example.com',
	customFetch: (url, options) => myCustomFetch(url, options),
});

Then you can use the client to make requests. The client has methods for all HTTP methods.

const getUsersResult = await client.get({
	url: '/users',
});

const headUserResult = await client.head({
	url: '/users',
});

const postUserResult = await client.post({
	url: '/users',
	body: { name: 'John' },
});

const putUserResult = await client.put({
	url: '/users/1',
	body: { name: 'John' },
});

const patchUserResult = await client.patch({
	url: '/users/1',
	body: { name: 'John' },
});

const deleteUserResult = await client.delete({
	url: '/users/1',
});

You can pass more options to the request.

const getUsersResult = await client.get({
	url: '/users',
	headers: { 'X-Custom-Header': 'Custom value' },
	searchParams: {
		page: 1,
		limit: 10,
	},
	body: {
		name: 'John',
	},
});

The client will automatically serialize the body to JSON if the Content-Type header is set to application/json. You can pass this header in the NanoHttpClient constructor or in the request options.

const postUserResult = await client.post({
	url: '/users',
	headers: { 'Content-Type': 'application/json' },
	body: {
		name: 'John',
	},
});

The client will automatically parse the response body if the Content-Type header is set to application/json. You can pass this header in the NanoHttpClient constructor or in the request options.

const getUsersResult = await client.get({
	url: '/users',
	headers: {
		Accept: 'application/json',
	},
});

By default the client will not throw on error results.

// Safe interface - Does not throw, returns a result object with status code and data or error
const result = await client.get({
	url: '/users',
}); // { ok: true, data: unknown, response: Response } | { ok: false, error: HttpError }

if (result.ok) {
	// result.data is unknown, you need to pass a type guard to narrow it down or use a type assertion
	console.log(result.data); // { id: 1, name: 'John' }
} else {
	// result.error is an HttpError object with status code and data
	console.log(result.error); // { status: 404, data: { message: 'Not found' } }
}

We don't have opinions on how you do your type assertions, but we recommend using a type guard.

// Result<{ ok: true, data: User[], response: Response }, { ok: false, error: HttpError }>
const result = await client.get({
 url: '/users',
 actions: {
  // Type guard for the result data
  typeGuard: (data: unknown): data is User[] => {
   return Array.isArray(data) && data.every((item) => typeof item === 'object'),
  }
 },
});

if (result.ok) {
 // result.data is User[]
 console.log(result.data);
} else {
 // result.error is CustomError
 console.log(result.error);
}

You can also pass a custom error handler to the client constructor.

const client = new NanoHttpClient({
	baseUrl: 'https://api.example.com',
	customErrorHandler: (error) => {
		console.log("I'm a custom error handler", error);
	},
});

You can also just do a raw request with the request method. This is only available in the base client instance.

const result = await client.request({
	url: '/users',
	method: 'GET',
	headers: {
		'Content-Type': 'application/json',
	},
	body: {
		name: 'John',
	},
});

Todo

  • [ ] Add tests to other content types
  • [ ] Add tests to other HTTP methods
  • [ ] Add tests to other HTTP errors
  • [ ] Add tests to the raw request method
  • [ ] Add tests to the custom error handler
  • [ ] Add tests to the custom fetch function