Pretty Cache Header Utility

1.0.0 · active · verified Tue Apr 21

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.

Warnings

Install

Imports

Quickstart

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();

view raw JSON →