Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a middleware function before sequelize call #78

Open
wattry opened this issue Aug 19, 2022 · 2 comments
Open

Adding a middleware function before sequelize call #78

wattry opened this issue Aug 19, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@wattry
Copy link

wattry commented Aug 19, 2022

Hi there

I was wondering if you've thought about adding an option to allow users to provide a middleware function. For instance a permission check on a given route.

import authCheck from './authCheck';
const ROLES = ['ROLE_1', 'ROLE_2']

express.Router()
    .get('/some/resource', authCheck(ROLES), (req, res, next) => {})

As far as I can see there is no ability to add any validator or permission-checking middleware.

@nicgirault
Copy link
Owner

Hello @wattry. Sorry for the delay to answer. I am now maintaining this library and I share your concern. Do you have any suggestion of what the API should looks like?

@nicgirault nicgirault added the enhancement New feature or request label Aug 19, 2023
@wattry
Copy link
Author

wattry commented Nov 19, 2024

Hi there I lost track of this though it would be nice to have this feature. This is a proposal, it's not perfect though it works.

import { Router, Request, Response, NextFunction, RequestHandler } from 'express'
import bodyParser from 'body-parser'
import { getMany, Get, GetListOptions } from './getList'
import { getOne } from './getOne'
import { create, Create } from './create'
import { update, Update } from './update'
import { destroy, Destroy } from './delete'
import { populateReference, populateReferenceMany, populateReferenceManyCount, populateReferenceOne } from './additionalAttributeHelpers'

export interface Actions<I extends string | number, R> {
  get: Get<R> | null
  create: Create<I, R> | null
  destroy: Destroy | null
  update: Update<R> | null
}

export interface Middlewares {
  get?: RequestHandler[]
  create?: RequestHandler[]
  destroy?: RequestHandler[]
  update?: RequestHandler[]
}

export { Create, Destroy, Update, Get, populateReference, populateReferenceMany, populateReferenceManyCount, populateReferenceOne }

interface Options<R> extends GetListOptions<R> {
  middlewares?: Partial<Middlewares>
};

export const crud = <I extends string | number, R>(
  path: string,
  actions: Partial<Actions<I, R>>,
  options?: Partial<Options<R>>
) => {
  const router = Router()

  router.use(bodyParser.json())

  if (actions.get) {
    const middlewares = options?.middlewares?.get ? options?.middlewares?.get : [];
    router.get(
      path,
      ...middlewares,
      getMany(
        actions.get,
        options
      )
    )
    router.get(`${path}/:id`, ...middlewares, getOne(actions.get, options))
  }

  if (actions.create) {
    const middlewares = options?.middlewares?.create ? options?.middlewares?.create : [];

    router.post(path, ...middlewares, create(actions.create))
  }

  if (actions.update) {
    if (!actions.get) {
      throw new Error('You cannot define update without defining getOne')
    }
    const middlewares = options?.middlewares?.update ? options?.middlewares?.update : [];
    router.put(`${path}/:id`, ...middlewares, update(actions.update, actions.get))
  }

  if (actions.destroy) {
    const middlewares = options?.middlewares?.destroy ? options?.middlewares?.destroy : [];
    router.delete(`${path}/:id`, ...middlewares, destroy(actions.destroy))
  }

  return router
}

export default crud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants