Package Exports
- @polka/send-type
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@polka/send-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@polka/send-type 
An HTTP response helper that detects
Content-Types & handles them accordingly — not limited to Polka!
For a simpler, bare-bones alternative, check out @polka/send instead~
Install
$ npm install --save @polka/send-typeUsage
const { createReadStream } = require('fs');
const send = require('@polka/send-type');
module.exports = function (req, res) {
if (!req.getHeader('authorization')) {
// Objects are converted to JSON
return send(res, 401, { error:'Token required!' });
}
// Streams & Buffers are auto-piped
// Your 'Content-Type' is always used,
// ~> otherwise 'application/octet-stream'
let file = createReadStream('massive.mp4');
send(res, 206, file, { 'Content-Type': 'video/mp4' });
}API
send(res, code, data, headers)
res
Type: ServerReponse
The outgoing HTTP response.
code
Type: Number
Default: 200
The statusCode for your response.
data
Type: String
Default: ''
The body for your response. Defaults to the statusText for the given statusCode.
See Data Detections for special behaviors.
headers
Type: Object
Default: {}
The headers for your response.
The Content-Type header is a little unique – it will be set with the value you provide in headers. However, if you did not set a value explicitly, then send-type will reuse the existing value via res.getHeader.
If neither existed, then the Content-Type will be inferred by the data type.
See Data Detections for special behaviors.
Data Detections
The following operations will be performed for the following data types:
Important: If this is too much magic for you, check out
@polka/sendinstead!
Buffers
- Sets
Content-Typeto'application/octet-stream', unless one exists inheaders - Sets
Content-Length
Objects
- Casts
datato string viaJSON.stringify - Sets
Content-Typeto'application/json; charset=utf-8', unless one exists inheaders - Sets
Content-Length
Streams
- Sets
Content-Typeto'application/octet-stream', unless one exists inheaders - Pipes
datainto theresdirectly
Support
Any issues or questions can be sent to the Polka repo, but please specify that you are using @polka/send-type.
License
MIT © Luke Edwards