@xcvzmoon/express-utils
v0.1.1
Published
A collection of utilities for Express
Downloads
16
Maintainers
Readme
@xcvzmoon/express-utils
A collection of utilities for Express. More will be added over time.
Current utilities
Request validation with Zod:
readValidatedBody— validate and typereq.bodywith a Zod schemareadValidatedParams— validate and typereq.paramswith a Zod schemareadValidatedQuery— validate and typereq.querywith a Zod schema
Multipart form data:
readMultipartFormData— parsemultipart/form-dataand get uploaded files as{ name, filename, mimeType, buffer }[], ornullif the request is not multipart (uses busboy)
Server-Sent Events (SSE):
createServerSentEvent— set up an Express response for SSE and get{ push, pushComment, close, closed }to send events, comments, and close the connection
Install
bun add @xcvzmoon/express-utils express zod
# or
npm i @xcvzmoon/express-utils express zodUsage
Zod validation:
import {
readValidatedBody,
readValidatedParams,
readValidatedQuery,
} from '@xcvzmoon/express-utils';
import { z } from 'zod';
const bodySchema = z.object({ name: z.string() });
const paramsSchema = z.object({ id: z.string() });
const querySchema = z.object({ page: z.coerce.number().optional() });
app.post('/users/:id', (req, res) => {
const body = readValidatedBody(req, bodySchema);
const params = readValidatedParams(req, paramsSchema);
const query = readValidatedQuery(req, querySchema);
// body, params, query are typed and validated
});Multipart form data:
import { readMultipartFormData } from '@xcvzmoon/express-utils';
app.post('/upload', async (req, res) => {
const files = await readMultipartFormData(req);
if (files === null) return res.status(400).send('Expected multipart/form-data');
for (const { name, filename, mimeType, buffer } of files) {
// handle each uploaded file
}
});Server-Sent Events:
import { createServerSentEvent } from '@xcvzmoon/express-utils';
app.get('/events', (req, res) => {
const sse = createServerSentEvent(res);
sse.push('data: Connected\n\n');
const heartbeat = setInterval(() => sse.pushComment('heartbeat'), 10000);
req.on('close', () => {
clearInterval(heartbeat);
sse.close();
});
});Peer dependencies
- express ^5.2.1
- zod ^4.3.6
