-
Notifications
You must be signed in to change notification settings - Fork 33
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
Labels
enhancement
New feature or request
Comments
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? |
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
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.
As far as I can see there is no ability to add any validator or permission-checking middleware.
The text was updated successfully, but these errors were encountered: