@quilted/http
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

@quilted/http

This library provides a collection of HTTP-related types and utilities.

Getting the library

Note: @quilted/quilt/http and @quilted/react-http re-export all of the types from this library. If you already have either @quilted/quilt or @quilted/react-http, you can use the exports from those libraries instead of @quilted/http.

Install this library as a dependency by running the following command:

yarn add @quilted/http

Using the library

This library provides the following helper types, which each represent some aspect of interacting with HTTP:

  • HttpMethod, an enum representing the allowed HTTP methods
  • StatusCode, an enum representing the standard response status codes
  • ResponseType, an enum representing the standard response status code classes
  • ReadonlyHeaders, which is a subset of the Headers type that represents headers that can’t be mutated (like those on some Request objects).
  • Cookies, ReadonlyCookies, and CookieOptions, a set of interfaces that provide an isomorphic pattern for getting and setting cookies
  • ContentSecurityPolicyDirective, ContentSecurityPolicySandboxAllow, and ContentSecurityPolicySpecialSource, enums that provide friendly names for creating a content security policy
  • PermissionsPolicyDirective and PermissionsPolicySpecialSource, enums that provide friendly names for creating a permissions policy

Using this library as types

Many of the types in this library are provided as enums. Enums are convenient for developers, because they let you avoid having plain strings or numbers all over the codebase; that is, many developers prefer:

import {StatusCode} from '@quilted/http';

const statusCode = StatusCode.NotFound;

Over:

const statusCode = 404;

While enums are convenient, they have a runtime cost, because the whole enum is included in the output bundles even if only a single enum value is used. For this reason, we recommend any library that uses @quilted/http use type imports to reference the enums in this package:

import type {StatusCode} from '@quilted/http';
// instead of
// import {StatusCode} from '@quilted/http';

export function myFunctionThatUsesStatusCode(code: StatusCode) {}

This allows consumers of your library to get type safety on the allowed values, but without forcing consumers to include the entire enum in their bundle if they’d prefer to use these types exclusively as types.

import {StatusCode} from '@quilted/http';

// Works when using the enum...
myFunctionThatUsesStatusCode(StatusCode.NotFound);

// And when using static values:
myFunctionThatUsesStatusCode(404);

Readme

Keywords

none

Package Sidebar

Install

npm i @quilted/http

Weekly Downloads

94

Version

0.3.0

License

MIT

Unpacked Size

118 kB

Total Files

48

Last publish

Collaborators

  • lemonmade