1.1.1 • Published 9 months ago

rx-express-middleware v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Express Middleware

An opinionated set of middleware to use on an express API.

Use

import attachMiddleware, { handleError } from 'rx-express-middleware';
const app = express();
attachMiddleware(app, {
  maxBodySize: string | undefined; // Max body size (default: 50mb)
  cookieSecret: string | undefined; // Secret for cookie parser
  cookieOptions: CookieParseOptions | undefined; // Cookie options
} | undefined);

app.use(baseRouter);

app.use(handleError({
  prisma: boolean | undefined; // Handle Prisma errors
}))

Error Handling

If you want to specify a specific error code, you can set the error cause to it. For example:

throw new Error('unauthenticated', { cause: new Error('401') });

Formatting

Formatting functions are provided through import

Prisma Pagination

Pagination can be imported through rx-express-middleware/dist/prismaPagination

For example:

// index.ts
import { PrismaClient } from '@prisma/client';
import { initiatePagination } from 'rx-express-middleware/dist/prismaPagination';

export const prisma = new PrismaClient();
export const paginate = initiatePagination(10, prisma);

// controller.ts
import { paginate } from './index.ts';
import { RequestHandler } from 'express';

export const handler: RequestHandler = async (req, res) => {
  res.json(
    await paginate(req.stringQuery, 'user', {
      where: {
        name: { contains: req.stringQuery.name },
      },
      select: {
        name: true,
      },
    } as Prisma.UserFindManyArgs),
  );
};
1.1.1

9 months ago

1.0.2

11 months ago

1.1.0

9 months ago

1.0.9

10 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.10

10 months ago

1.0.1

1 year ago

1.0.0

1 year ago