Skip to main content

Built and signed on GitHub Actions

Middleware for @planigale/planigale for body parsing

This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
64%
Published
2 months ago (0.2.0)
Package root>parser.ts
import { Middleware } from 'jsr:@planigale/planigale@^0.6.1'; import { qs } from './deps.ts'; export const bodyParser: Middleware = async (req, next) => { const request = req.request; if (!req.route) { return await next(); } req.body = await loadBody(request); return await next(); }; const methods = ['POST', 'PUT', 'PATCH', 'DELETE']; const loadBody = async (request: Request) => { if (!methods.includes(request.method)) { return request.body; } const contentType = request.headers.get('content-type'); if (contentType?.includes('application/json')) { return await request.json(); } else if (contentType?.includes('application/x-www-form-urlencoded')) { return qs.parse(await request.text()); } else if (contentType?.includes('text/plain')) { return await request.text(); } else { return request.body; } };