{"id":15165,"library":"pretty-cache-header","title":"Pretty Cache Header Utility","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/jjenzz/pretty-cache-header","tags":["javascript","typescript"],"install":[{"cmd":"npm install pretty-cache-header","lang":"bash","label":"npm"},{"cmd":"yarn add pretty-cache-header","lang":"bash","label":"yarn"},{"cmd":"pnpm add pretty-cache-header","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const { cacheHeader } = require('pretty-cache-header');","symbol":"cacheHeader","correct":"import { cacheHeader } from 'pretty-cache-header';"}],"quickstart":{"code":"import { cacheHeader } from 'pretty-cache-header';\n\nfunction createResponseWithCaching() {\n  const cacheControlHeader = cacheHeader({\n    public: true,\n    maxAge: '1week',\n    sMaxage: '1hour',\n    staleWhileRevalidate: '1year',\n    immutable: true\n  });\n\n  // In a real application, this would be part of an HTTP response.\n  // For demonstration, we'll just log the header value.\n  console.log('Generated Cache-Control Header:', cacheControlHeader);\n\n  // Example of how it might be used in a web framework (e.g., Express.js or a Web Fetch API response)\n  // return new Response('Content', {\n  //   headers: {\n  //     'Content-Type': 'text/plain',\n  //     'Cache-Control': cacheControlHeader\n  //   }\n  // });\n  return cacheControlHeader;\n}\n\ncreateResponseWithCaching();","lang":"typescript","description":"Demonstrates how to import and use `cacheHeader` to generate a `Cache-Control` string with human-readable time values and common directives."},"warnings":[{"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').","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"When 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).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[],"ecosystem":"npm"}