| 1234567891011121314151617181920212223 |
- const response = require('../utils/responseHandler')
- module.exports = (fromField, callback, nameVariable = null) => async (req, res, next) => {
- const field = fromField.toString().split('.').pop()
- let data = null
- try {
- data = await callback(fromField(req))
- if (!Object.keys(data).length) {
- return response.error(res, {
- message: `${field} tidak ditemukan`,
- code: 404
- })
- }
- } catch (e) {
- return response.error(res, {
- message: e.message,
- code: e.response?.status || 500
- })
- }
- if (nameVariable) req.data[nameVariable] = data
- else req.data[field] = data
- return next()
- }
|