1.1.1 • Published 2 years ago

rx-express-middleware v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years 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

2 years ago

1.0.2

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.10

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago