Bomret / funkster-http-headers-content

Adds combinators for parsing and setting the HTTP Content-* headers in Funkster.
0 stars 0 forks source link

funkster-http-headers-content

npm node npm Travis

Icon

Funkster is a compositional server library. This package contains combinators to parse and set the HTTP Content-* headers.

Additional examples of how to build HTTP server apis with funkster can be found here.

Typscript is used to illustrate the examples.

Install

$ npm install funkster-http-headers-accept

Build

$ npm install && npm run build

Test

$ npm run test

Parsing the Accept* headers from a request

This module uses the content-type and content-disposition packages under the hood so essentially the same api applies for the parsing and setting the Content-Type (using mediaType instead of type for readability) and Content-Disposition headers.

The parsed headers object has the following type:

{
  type?: {
    mediaType: string,
    parameters?: Object
  },
  length?: number,
  language?: string,
  encoding?: string,
  location?: string,
  disposition?: {
    type?: string,
    parameters?: Object
  } 
}

Example:

import * as http from 'http';
import { parseContentHeaders } from 'funkster-http-headers-content';
import { asRequestListener, Ok } from 'funkster-http';

const echoEncoding = parseContentHeaders(contentHeaders => Ok(contentHeaders.encoding));
const server = http.createServer(asRequestListener(echoEncoding));

// start the node HTTP server and send e.g. a GET with the Content-Encoding header set to 'gzip'.

Setting the Content-* headers of the response

The following combinators are available for setting the respective content headers.

Example

import * as http from 'http';
import { setContentType } from 'funkster-http-headers-content';
import { asRequestListener, Ok } from 'funkster-http';

const sendJsonContentType = setContentType({ mediaType: "application/json", parameters: { charset: "utf-8" } });
const server = http.createServer(asRequestListener(sendJsonContentType));

// start the node HTTP server and send e.g. a GET.

Meta

Icon funky by iconsmind.com from the Noun Project.