Skip to content

yetzt/node-phn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

phn

A lightweight http client adapted from phin and centra.

install

npm i phn

usage

const phn = require("phn");

const res = await phn({
	url: 'https://example.org'
});

options

  • url - URL to request
  • method - HTTP method, default: GET
  • headers - HTTP headers object
  • query - Object to be added to url as query string
  • data - Request body; json, buffer or object containing form data
  • form - object containing form data
  • core - options passed on to http(s).request
  • parse - parse response body as json or string
  • followRedirects - follow redirects if true
  • maxRedirects - maximum number of redirects to follow, default: infinite
  • stream - return stream as res.stream instead of res.body
  • compression - handle compression, accept br, gzip and deflate
  • timeout - request timeout in milliseconds

stream

consume http response as stream

const phn = require("phn");

const stream = await phn({
	url: 'https://example.org/',
	compression: true,
	stream: true,
});

stream.pipe(/* ... */)

custom http(s) options

use a custom agent

const phn = require("phn");
const https = require("https");

const agent = new https.Agent({ keepAlive: true });

await phn({
	url: 'https://example.org/',
	core: { agent },
});

unpromisified

get a classic callback interface

const phn = require("phn").unpromisified;

phn('https://example.org/', (err, res) => {
	if (!err) console.log(res.body);
});

defaults

set options for any subsequent request

const phn = require("phn").defaults({
	method: 'POST',
	parse: 'json',
	timeout: 2000
});

const res = await phn('https://example.org/')

comparison

phn is tiny and comes with no dependencies.

Package Size
node-fetch node-fetch package size
request request package size
phin phin package size
axios axios package size
superagent superagent package size
undici undici package size
got got package size
needle needle package size
isomorphic-fetch isomorphic-fetch package size
r2 r2 package size
slim-fetch slim-fetch package size
phn phn package size

license

MIT

acknowledgement

phn is a fork of phin and centra by Ethan Davis.

About

a lightweight http client

Resources

License

Stars

Watchers

Forks

Packages

No packages published