HTTP Link Header Parser & Formatter

1.1.3 · abandoned · verified Tue Apr 21

http-link-header is a JavaScript library designed for parsing and formatting HTTP Link headers as defined by RFC 8288 (which supersedes RFC 5988). The current stable version is 1.1.3, last published in 2018, indicating a low or absent release cadence. A key characteristic is its explicit deviation from RFC 8288 regarding relative URI-References: the library does *not* automatically resolve them, leaving this responsibility to the user. This design choice is critical for developers to be aware of, as it means the parsed URIs might not be absolute without further processing. The library provides methods for parsing single or multiple headers, checking for specific references, retrieving references by attribute, and setting/uniquely setting new references, along with stringifying Link objects back into HTTP header format.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a complex HTTP Link header string (including extended attributes), accessing individual references, filtering by relation type, adding new unique references, and stringifying the Link object back to a header format.

const LinkHeader = require('http-link-header');

// Example HTTP Link header string with multiple relations and extended attributes
const headerString = '<https://example.com/next>; rel="next", ' +
                     '<https://example.com/prev>; rel="prev"; title="Previous Page", ' +
                     '<https://example.com/image.png>; rel="preload"; as="image"; type="image/png", ' +
                     '</extended-attr-example>; rel=start; title*=UTF-8\'en\'%E2%91%A0%E2%93%AB%E2%85%93%E3%8F%A8%E2%99%B3%F0%9D%84%9E%CE%BB';

// Parse the header string
const link = LinkHeader.parse(headerString);

console.log('Parsed Link Header:', JSON.stringify(link, null, 2));
console.log('First reference URI:', link.refs[0].uri);
console.log('All "prev" references:', link.rel('prev'));
console.log('Extended title value:', link.get('rel', 'start')[0]['title*'].value);

// Add a new unique reference
link.setUnique({
  uri: 'https://example.com/canonical',
  rel: 'canonical'
});
console.log('\nAfter adding canonical:', JSON.stringify(link.refs, null, 2));

// Stringify back to header format
const formattedHeader = link.toString();
console.log('\nFormatted Header:', formattedHeader);

// Check for a specific relation
console.log('\nHas "preload" relation?', link.has('rel', 'preload'));

view raw JSON →