Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
@tinyhttp/accepts
Advanced tools
accepts
rewrite in TypeScript.
Higher level content negotiation based on negotiator. Extracted from koa for general use.
In addition to negotiator, it allows:
(['text/html', 'application/json'])
as well as
('text/html', 'application/json')
.json
.false
when no types match*
pnpm i @tinyhttp/accepts
import { Accepts } from '@tinyhttp/accepts'
Create a new Accepts
object for the given req
.
.charset(charsets)
Return the first accepted charset. If nothing in charsets
is accepted, then
false
is returned.
.charsets()
Return the charsets that the request accepts, in the order of the client's preference (most preferred first).
.encoding(encodings)
Return the first accepted encoding. If nothing in encodings
is accepted, then
false
is returned.
.encodings()
Return the encodings that the request accepts, in the order of the client's preference (most preferred first).
.language(languages)
Return the first accepted language. If nothing in languages
is accepted, then
false
is returned.
.languages()
Return the languages that the request accepts, in the order of the client's preference (most preferred first).
.type(types)
Return the first accepted type (and it is returned as the same text as what
appears in the types
array). If nothing in types
is accepted, then false
is returned.
The types
array can contain full MIME types or file extensions. Any value that
is not a full MIME types is passed to require('mime-types').lookup
.
.types()
Return the types that the request accepts, in the order of the client's preference (most preferred first).
This simple example shows how to use accepts
to return a different typed
respond body based on what the client wants to accept. The server lists it's
preferences in order and will get back the best match between the client and
server.
import Accepts from '@tinyhttp/accepts'
import { createServer } from 'node:http'
createServer((req, res) => {
const accept = new Accepts(req)
// the order of this list is significant; should be server preferred order
switch (accept.type(['json', 'html'])) {
case 'json':
res.setHeader('Content-Type', 'application/json')
res.write('{"hello":"world!"}')
break
case 'html':
res.setHeader('Content-Type', 'text/html')
res.write('<b>hello, world!</b>')
break
default:
// the fallback is text/plain, so no need to specify it above
res.setHeader('Content-Type', 'text/plain')
res.write('hello, world!')
break
}
res.end()
}).listen(3000)
You can test this out with the cURL program:
curl -I -H 'Accept: text/html' http://localhost:3000/
FAQs
accepts rewrite in TypeScript
We found that @tinyhttp/accepts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.