Table of contents
Installing
Node
npm install --save cache-parser
yarn add cache-parser
const { parse, tokenize } = require('cache-parser');
import { parse, tokenize } from 'cache-parser';
Browser
<script
crossorigin
src="https://cdn.jsdelivr.net/npm/cache-parser@1/index.umd.js"
></script>
<script crossorigin src="https://unpkg.com/cache-parser@1/dist/index.umd.js"></script>
const { parse, tokenize } = window.CacheParser;
Getting Started
This library consists of two important functions: parse
and
tokenize
.
This is a pretty straightforward library, so, every documentation needed by every piece of
code is in form of TSDoc
and JSDoc
comments.
Some examples
Simple header parsing:
import { parse } from 'cache-parser';
const rawHeader = 'public, max-age=3600';
const { public, maxAge, immutable } = parse(rawHeader);
console.log(public);
console.log(maxAge);
console.log(typeof maxAge);
console.log(immutable);
Simple header building:
import { tokenize } from 'cache-parser';
const cacheProperties = { public: true, maxAge: 3600 };
const cacheTokens = tokenize(cacheProperties);
response.headers['Cache-Control'] = tokens.join(', ');
License
Licensed under the MIT.