Skip to content

Utilities for content negotiation described in Section 5.3 of RFC 7231.

License

Notifications You must be signed in to change notification settings

nlibjs/negotiate

Repository files navigation

@nlib/negotiate

.github/workflows/test.yml codecov

RFC-compliant HTTP content negotiation, made simple with TypeScript.

@nlib/negotiate parses Accept, Accept-Language, Accept-Encoding, and other HTTP request headers to help your server select the best response format for each client.

  • RFC 7231 compliant — faithful implementation of Section 5.3 of RFC 7231
  • Wildcard support — handles *, type/*, */subtype, and */* patterns
  • Extensible — plug in a custom matcher function to control how values are compared
  • Fault-tolerantloose mode silently skips malformed header values to gracefully handle real-world client quirks
  • Efficient — exits early once a quality-1.0 match is found
  • Dual package — ships both ESM and CJS builds, ready for any modern JS environment

Utilities for content negotiation described in Section 5.3 of RFC 7231.

negotiate

const supported = ['text/html', 'image/webp'];
const accept = 'text/html,image/avif,image/webp,image/apng,*/*;q=0.8';
negotiate(supported, accept); // → 'text/html'
negotiate([], accept); // → null

parseAcceptStatements

const generator = parseAcceptStatements('v1,v2;q=0.9,v3;q=0.8');
generator.next().value; // → {value: 'v1', q: 1}
generator.next().value; // → {value: 'v2', q: 0.9}
generator.next().value; // → {value: 'v3', q: 0.8}
generator.next(); // → {value: undefined, done: true}

parseNegotiationItem

parseNegotiationItem('text/html;charset=utf-8;foo=1');
// → {value: 'text/html', parameters: {charset: 'utf-8', foo: '1'}}

About

Utilities for content negotiation described in Section 5.3 of RFC 7231.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •