Code Monkey home page Code Monkey logo

negapi's Introduction

negapi

negapi helps with content negotiation by selecting the most appropriate media type for a response based on a request’s Accept header.

Constructing a media type set takes exponential space and time on the highest number of parameters for a type. That number is typically 0, but if something significantly larger is required, this module may not be a good choice. Matching an Accept header against the set takes O(n log n + kn) time, where n is the number of ranges in the header and k is the number of distinct parameter names in the type set.

Usage

const {MediaType, MediaTypeSet} = require('negapi');

const types = new MediaTypeSet([
	new MediaType('text', 'plain', { format: 'flowed' }),
	new MediaType('application', 'json'),
	new MediaType('image', 'png'),
]);

const image = types.select('image/*, */*;q=0.5');
console.log(image.subtype);  // png

const text = types.select('text/plain');
console.log(text.get('format'));  // flowed

const none = types.select('*/*;q=0');
console.log(none);  // null

API

new MediaType(type, subtype, [parameters])

Creates a media type with the given type (string), subtype (string), and parameters (object with parameters as properties). In text/plain; format=flowed, the type is 'text', the subtype is 'plain', and the parameters are { format: 'flowed' }.

MediaType#type

The type, as a string.

MediaType#subtype

The subtype, as a string.

MediaType#get(name)

Gets the lowercase value of a parameter by name, or null if the parameter doesn’t exist. The name is case-insensitive.

new MediaTypeSet(types)

Creates a set of media types. types is an array of MediaTypes ordered by descending preference.

MediaTypeSet#select(accept)

Selects the most client-preferred MediaType of the MediaTypeSet according to the provided Accept header, or null if no type is acceptable.

negapi's People

Contributors

charmander avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.