RESTful HTTP client library with composable interceptor architecture for Node.js and browsers
76
The MIME system in rest.js provides automatic serialization and deserialization of request and response entities based on Content-Type headers. It includes built-in converters for common formats and supports custom converter registration.
Parse MIME type strings into structured objects.
const mime = require('rest/mime');
/**
* Parse a MIME type string
* @param {string} mimeType - MIME type to parse
* @returns {object} Parsed MIME type object
*/
function parse(mimeType)Parsed MIME type object:
interface ParsedMimeType {
raw: string; // Original MIME type string
type: string; // Main type (e.g., 'application/json')
suffix: string; // MIME suffix including '+' (e.g., '+json')
params: object; // MIME parameters as key-value pairs
}Usage Example:
const mime = require('rest/mime');
const parsed = mime.parse('application/vnd.api+json; charset=utf-8');
console.log(parsed);
// {
// raw: 'application/vnd.api+json; charset=utf-8',
// type: 'application/vnd.api',
// suffix: '+json',
// params: { charset: 'utf-8' }
// }Central registry for MIME type converters with hierarchical lookup and custom converter support.
const registry = require('rest/mime/registry');
/**
* MIME registry for converter management
*/
interface Registry {
/**
* Look up converter for a MIME type
* @param {string} type - MIME type to find converter for
* @returns {Promise<Converter>} Promise resolving to converter
*/
lookup(type: string): Promise<Converter>;
/**
* Register a custom converter
* @param {string} type - MIME type
* @param {Converter} converter - Converter implementation
* @returns {Promise<Converter>} Promise resolving to registered converter
*/
register(type: string, converter: Converter): Promise<Converter>;
/**
* Create a delegate converter that proxies to another type
* @param {string} type - Target MIME type to delegate to
* @returns {Converter} Delegate converter
*/
delegate(type: string): Converter;
/**
* Create a child registry with local registrations
* @returns {Registry} Child registry
*/
child(): Registry;
}
/**
* MIME converter interface
*/
interface Converter {
/**
* Convert string to object (deserialization)
* @param {string} str - String to convert
* @returns {any} Converted object
*/
read(str: string): any;
/**
* Convert object to string (serialization)
* @param {any} obj - Object to convert
* @returns {string} Converted string
*/
write(obj: any): string;
}Usage Example:
const registry = require('rest/mime/registry');
// Register custom converter
registry.register('application/vnd.myapp+json', {
read: function(str) {
const data = JSON.parse(str);
return { customData: data, timestamp: Date.now() };
},
write: function(obj) {
return JSON.stringify(obj.customData);
}
});
// Use the converter
registry.lookup('application/vnd.myapp+json').then(function(converter) {
const obj = converter.read('{"key": "value"}');
console.log(obj); // { customData: { key: "value" }, timestamp: 1234567890 }
});Handles application/json and +json suffix types.
const jsonConverter = require('rest/mime/type/application/json');
/**
* JSON converter with configurable options
* @param {object} [config] - JSON configuration
* @param {function} [config.replacer] - JSON.stringify replacer
* @param {function} [config.reviver] - JSON.parse reviver
*/Handles application/x-www-form-urlencoded content.
const formConverter = require('rest/mime/type/application/x-www-form-urlencoded');
/**
* Form URL encoding converter
*/
interface FormConverter {
read(str: string): object;
write(obj: object): string;
}Handles multipart/form-data (browser only with FormData support).
const multipartConverter = require('rest/mime/type/multipart/form-data');
/**
* Multipart form data converter (browser only)
*/Handles text/plain content.
const textConverter = require('rest/mime/type/text/plain');
/**
* Plain text converter (pass-through)
*/Handles application/hal+json (Hypertext Application Language).
const halConverter = require('rest/mime/type/application/hal');
/**
* HAL (Hypertext Application Language) converter
* Provides hypermedia navigation capabilities
*/The MIME interceptor automatically applies appropriate converters based on Content-Type headers:
const rest = require('rest');
const mime = require('rest/interceptor/mime');
const client = rest.wrap(mime, {
mime: 'application/json', // Default request MIME type
accept: 'application/json, text/plain', // Accept header
registry: customRegistry, // Custom registry (optional)
permissive: false // Reject unknown MIME types
});
// Automatic JSON serialization
client({
path: '/api/users',
method: 'POST',
entity: { name: 'John', email: 'john@example.com' }
}).then(function(response) {
// response.entity is automatically parsed as JSON
console.log('Created user:', response.entity);
});Create and register custom MIME converters for specialized formats:
const registry = require('rest/mime/registry');
// Custom XML converter example
const xmlConverter = {
read: function(str) {
// Parse XML string to object
// (using a hypothetical XML parser)
return parseXML(str);
},
write: function(obj) {
// Convert object to XML string
return toXML(obj);
}
};
// Register the converter
registry.register('application/xml', xmlConverter);
registry.register('text/xml', xmlConverter);
// Create delegate for related types
registry.register('+xml', registry.delegate('application/xml'));
// Use with MIME interceptor
const mime = require('rest/interceptor/mime');
const client = rest.wrap(mime, {
mime: 'application/xml',
accept: 'application/xml'
});Create isolated MIME registries for different parts of your application:
const registry = require('rest/mime/registry');
// Create child registry
const apiRegistry = registry.child();
// Register converters only for this registry
apiRegistry.register('application/vnd.api+json', customApiConverter);
// Use child registry with interceptor
const mime = require('rest/interceptor/mime');
const apiClient = rest.wrap(mime, {
registry: apiRegistry,
mime: 'application/vnd.api+json'
});The MIME system supports automatic content negotiation through Accept headers:
const mime = require('rest/interceptor/mime');
const client = rest.wrap(mime, {
accept: 'application/json;q=1.0, application/xml;q=0.8, text/plain;q=0.5'
});
// Server can respond with any of the accepted types
// Response will be automatically converted based on Content-TypeInstall with Tessl CLI
npx tessl i tessl/npm-restevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10