Registry / web-framework / pretty-cache-header

pretty-cache-header

JSON →
library1.0.0jsnpmunverified

pretty-cache-header is a JavaScript/TypeScript utility designed to simplify the creation of `Cache-Control` HTTP headers by parsing human-readable time strings into seconds. It aims to reduce errors by leveraging TypeScript's Template Literal Types to provide strong type-safety for time string formats, ensuring developers pass valid patterns like '1week' or '30days'. The package is currently at version 1.0.0, indicating its initial stable release. As a lightweight utility with a focused purpose, its release cadence is expected to be driven by bug fixes or minor enhancements rather than frequent major updates. Its key differentiator is the combination of human-readable time strings with robust type-checking, making it particularly useful in TypeScript-heavy projects where maintainability and type safety are priorities, especially when configuring server responses or CDN caching policies.

npm install pretty-cache-header
INSTALL
IMPORT
SIG · PRETTY-CACHE-HEADE
P
pretty-cache-header
web-frameworkjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cacheHeader
import { cacheHeader } from 'pretty-cache-header';
const { cacheHeader } = require('pretty-cache-header');
The library is primarily designed for ESM usage, as demonstrated in the documentation. While CommonJS might work via transpilation or specific Node.js settings, direct ESM import is preferred. TypeScript users benefit from strong typing for the `maxAge` and `staleWhileRevalidate` parameters.

Demonstrates how to import and use `cacheHeader` to generate a `Cache-Control` string with human-readable time values and common directives.

import { cacheHeader } from 'pretty-cache-header'; function createResponseWithCaching() { const cacheControlHeader = cacheHeader({ public: true, maxAge: '1week', sMaxage: '1hour', staleWhileRevalidate: '1year', immutable: true }); // In a real application, this would be part of an HTTP response. // For demonstration, we'll just log the header value. console.log('Generated Cache-Control Header:', cacheControlHeader); // Example of how it might be used in a web framework (e.g., Express.js or a Web Fetch API response) // return new Response('Content', { // headers: { // 'Content-Type': 'text/plain', // 'Cache-Control': cacheControlHeader // } // }); return cacheControlHeader; } createResponseWithCaching();
Debug
Known issues
gotchaThe library enforces strict `TimeString` formats for `maxAge`, `staleWhileRevalidate`, etc., using TypeScript's Template Literal Types. For example, '1 week' (with a space) is invalid, while '1week' is valid. Non-TypeScript users might only discover format issues at runtime if the string parsing fails, or they might not get the expected cache duration.
fix
Refer to the `TimeString format` section in the documentation for exact valid patterns. Always use a number immediately followed by a unit keyword (e.g., '5days', '30mins').
affects: >=1.0.0
gotchaWhen migrating from older cache-control header implementations, be aware that `pretty-cache-header` automatically converts time strings to seconds. Ensure any existing hardcoded second values are correctly expressed as human-readable strings if desired, or passed as numbers directly for properties like `maxAge` (though the primary benefit is the string parsing).
fix
Review existing cache header logic and refactor time durations to use the `TimeString` format (e.g., '1day' instead of `86400`) where applicable, or pass numerical seconds for directives that accept them directly without string parsing.
affects: >=1.0.0
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
pretty-cache-header — npm install pretty-cache-header · libregistry