Plugin for Elysia for content negotiation based on Accept headers based on @tinyhttp/accepts
which is based on old good negotiator
.
bun a elysia-accepts
import { accepts } from "elysia-accepts";
const app = new Elysia()
.use(accepts());
No options currently available and plugin is always registered in global scope.
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.type("text/html")); // Returns "html" if acceptable
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.type(["text/html", "application/json"]));
// Returns the first acceptable or false if none
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.type(["html", "json"]));
// Can pass file extension. In this case "html" returned if acceptable.
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.types); // Returns the list of all acceptable encodings
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.encoding("gzip")); // Returns "gzip" if acceptable
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.encoding(["gzip", "deflate"])); // Returns the first acceptable or false if none
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.encodings); // Returns the list of all acceptable encodings
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.language("en")); // Returns "en" if acceptable
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.language(["en", "ru"])); // Returns the first acceptable or false if none
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.languages); // Returns the list of all acceptable languages
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => 'hi', { types: ['text/plain'] });
// Returns 406 if plain text is not acceptable
const app = new Elysia()
.use(accepts())
.guard({ types: ['text/plain'] })
.get("/", (ctx) => 'hi');
// Returns 406 if plain text is not acceptable