@quilted/http
v0.3.0
Published
Provides a collection of HTTP-related types and utilities.
Downloads
188
Readme
@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 methodsStatusCode
, an enum representing the standard response status codesResponseType
, an enum representing the standard response status code classesReadonlyHeaders
, which is a subset of theHeaders
type that represents headers that can’t be mutated (like those on someRequest
objects).Cookies
,ReadonlyCookies
, andCookieOptions
, a set of interfaces that provide an isomorphic pattern for getting and setting cookiesContentSecurityPolicyDirective
,ContentSecurityPolicySandboxAllow
, andContentSecurityPolicySpecialSource
, enums that provide friendly names for creating a content security policyPermissionsPolicyDirective
andPermissionsPolicySpecialSource
, 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);