-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom middleware functionallity
- Loading branch information
1 parent
f8b7ae9
commit b9d25b8
Showing
4 changed files
with
52 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,37 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import {Router} from 'express'; | ||
import {getListOptions, getOneOptions, pureModelType} from '../types'; | ||
import {buildOptionsFromConfig} from '../middleware/config'; | ||
import {getOneOptions, pureModelType} from '../types'; | ||
import { | ||
buildOptionsFromConfig, | ||
runCustomMiddleware, | ||
} from '../middleware/config'; | ||
|
||
const getOneRoute = ( | ||
model: pureModelType, | ||
router: Router, | ||
config: getOneOptions | ||
) => | ||
router.get('/:resourceId', async (req, res) => { | ||
const {middleware, ...sequelizeOptions} = config; | ||
try { | ||
const options = await buildOptionsFromConfig(sequelizeOptions, req, res); | ||
router.get( | ||
'/:resourceId', | ||
runCustomMiddleware(config.middleware), | ||
async (req, res) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const {middleware, ...sequelizeOptions} = config; | ||
try { | ||
const options = await buildOptionsFromConfig( | ||
sequelizeOptions, | ||
req, | ||
res | ||
); | ||
|
||
const data = await model.findByPk(req.params.resourceId, { | ||
...options, | ||
}); | ||
res.json(data); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send(error); | ||
const data = await model.findByPk(req.params.resourceId, { | ||
...options, | ||
}); | ||
res.json(data); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send(error); | ||
} | ||
} | ||
}); | ||
); | ||
|
||
export default getOneRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters