elysia-accepts
v1.0.0
Published
Elysia plugin for accept headers parsing and content negotiation
Downloads
17
Readme
elysia-accepts
Plugin for Elysia for content negotiation based on Accept headers based on @tinyhttp/accepts which is based on old good negotiator.
Installation
bun a elysia-acceptsimport { accepts } from "elysia-accepts";
const app = new Elysia()
.use(accepts());No options currently available and plugin is always registered in global scope.
Context
Type
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.type("text/html")); // Returns "html" if acceptableconst app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.type(["text/html", "application/json"]));
// Returns the first acceptable or false if noneconst 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 encodingsEncoding
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.encoding("gzip")); // Returns "gzip" if acceptableconst app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.encoding(["gzip", "deflate"])); // Returns the first acceptable or false if noneconst app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.encodings); // Returns the list of all acceptable encodingsLanguages
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.language("en")); // Returns "en" if acceptableconst app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.language(["en", "ru"])); // Returns the first acceptable or false if noneconst app = new Elysia()
.use(accepts())
.get("/", (ctx) => ctx.languages); // Returns the list of all acceptable languagesGuard
Per Route
const app = new Elysia()
.use(accepts())
.get("/", (ctx) => 'hi', { types: ['text/plain'] });
// Returns 406 if plain text is not acceptableGlobal
const app = new Elysia()
.use(accepts())
.guard({ types: ['text/plain'] })
.get("/", (ctx) => 'hi');
// Returns 406 if plain text is not acceptable