js

@js/http-client (1.0.0)

Published 2023-08-27 01:41:53 +00:00 by drone in js/http-client

Installation

@js:registry=
npm install @js/http-client@1.0.0
"@js/http-client": "1.0.0"

About this package

Bare-minimum HTTP client library

Features:

  • Zero dependencies
  • Built-in response cache

Install

# Update project npm config to refer to correct registry for the @js scope
echo '@js:registry=https://gitea.jbrumond.me/api/packages/js/npm/' >> ./.npmrc

npm install --save @js/http-client

# optional - additional supporting typescript definitions
npm install --save-dev @js/types

Usage

import { create_http_client } from '@js/http-client';

const http = create_http_client({
	// Default configuration:
	logger: null,  // (optional) expects a pino logger or compatible
	https_only: false,
	cache: {
		// These settings are based on the recommendations in [RFC 9111], with the
		// exception of not caching POST responses (because its not very commonly
		// useful and is complicated) and with the addition of invalidating caches
		// on successful PATCH requests (because they are fairly common use and
		// are expected to modify the referenced resource)
		// 
		// [RFC 9111]: https://www.rfc-editor.org/rfc/rfc9111.html
		cacheable_methods: [ 'GET', 'HEAD' ],
		cache_invalidating_methods: [ 'POST', 'PUT', 'PATCH', 'DELETE' ],
		cacheable_status_codes: [
			200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501
		],
	},
});

const { status, headers, content } = await http.get('https://example.com', {
	'accept': 'application/json'
});

if (status === 200) {
	const json = JSON.parse(content);
	console.log('received data', json);
}

Building Locally (for development)

npm ci
npm run tsc

Dependencies

Development Dependencies

ID Version
@js/types ^0.1.0
@types/node ^20.5.1
typescript ^5.1.3
Details
npm
2023-08-27 01:41:53 +00:00
34
James Brumond
ISC
latest
9.5 KiB
Assets (1)
Versions (1) View all
1.0.0 2023-08-27