A TypeScript-friendly package that provides strongly-typed HTTP constants
npm install http-directivesA TypeScript-friendly package that provides strongly-typed constants for common HTTP headers, directives, status codes,
MIME types, caching policies, CSP rules, and more.
Say goodbye to string typos and make your HTTP code safer, cleaner, and more maintainable.
``sh`
npm i http-directives
The following categories of constants are included in this package:
| Category | Constants examples |
|-----------------------|--------------------------------------------------------------------------------------------------|
| HTTP Status Codes | OK, NOT_FOUND, INTERNAL_SERVER_ERROR, etc. |GET
| HTTP Methods | , POST, PUT, DELETE, etc. |CONTENT_TYPE
| HTTP Headers | , CACHE_CONTROL, STRICT_TRANSPORT_SECURITY, CONTENT_SECURITY_POLICY, etc. |APPLICATION_JSON
| MIME Types | , APPLICATION_JAVASCRIPT, APPLICATION_ZIP, TEXT_PLAIN, TEXT_HTML, etc. |GZIP
| Content Encodings | , BR, etc. |MAX_AGE
| Cache Control | , NO_CACHE, PRIVATE, etc. |UTF-8
| Charsets | , UTF-16, ISO-8859-1, etc. |SCRIPT_SRC
| CSP Directives | , STYLE_SRC, etc. |SELF
| CSP Sources | , UNSAFE_INLINE, nonce(), sha256(), etc. |!
| DNT | , ?, N, T, etc. |HTTP
| Protocols | , HTTPS, SSH, WSS, MQTT, etc. |
`tsx
import {
Headers,
MimeTypes,
ContentEncodings,
Charsets,
StatusCodes,
CacheControl,
CSPDirectives,
CSPSources,
Methods,
DNT,
Protocols
} from 'http-directives';
Headers.STRICT_TRANSPORT_SECURITY // "Strict-Transport-Security"
MimeTypes.APPLICATION_JAVASCRIPT // "application/javascript"
ContentEncodings.GZIP // "gzip"
Charsets.UTF_8 // "UTF-8"
StatusCodes.OK // 200
CacheControl.MAX_AGE // "max-age"
CSPDirectives.BASE_URI // "base-uri"
CSPSources.SELF // "self"
Methods.POST // "POST"
DNT.NOT_TRACKING // "N"
Protocols.HTTPS // "https:"
`
Additionally, exported CSP utility functions:
`ts
import {CSPSources} from 'http-directives';
CSPSources.nonce('abc123') // "'nonce-abc123'"
CSPSources.sha256('hash') // "'sha256-hash'"
CSPSources.sha384('hash') // "'sha384-hash'"
CSPSources.sha512('hash') // "'sha512-hash'"
`
Define and reuse your own constants with full autocompletion and type safety:
`ts
import {Headers} from 'http-directives';
class CustomHeaders extends Headers {
public static readonly CUSTOM_HEADER = 'custom-header';
}
CustomHeaders.CUSTOM_HEADER // "custom-header"
``
* Type-safe: Avoid typos in strings and headers
* Autocompletion: Full IDE support with TypeScript
* Up-to-date: Includes modern headers, directives, and encodings
* Zero dependencies: Lightweight and fast
* Compatible: Supports both ESM and CJS
* Extensible: Easily add custom directives and headers
Please motivate yourself to submit a PR.
MIT