Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
deserialize (mapper, response, opts) {
opts || (opts = {})
if (utils.isFunction(opts.deserialize)) {
return opts.deserialize(mapper, response, opts)
}
if (utils.isFunction(mapper.deserialize)) {
return mapper.deserialize(mapper, response, opts)
}
if (response && response.hasOwnProperty('data')) {
return response.data
}
return response
},
serialize (mapper, data, opts) {
opts || (opts = {})
if (utils.isFunction(opts.serialize)) {
return opts.serialize(mapper, data, opts)
}
if (utils.isFunction(mapper.serialize)) {
return mapper.serialize(mapper, data, opts)
}
return data
},
queryTransform (mapper, params, opts) {
opts || (opts = {})
if (utils.isFunction(opts.queryTransform)) {
return opts.queryTransform(mapper, params, opts)
}
if (utils.isFunction(mapper.queryTransform)) {
return mapper.queryTransform(mapper, params, opts)
}
return params
},
export function Router (component, config = {}) {
if (!(component instanceof Mapper) && !(component instanceof Container)) {
throw new Error('You must provide an instance of JSData.Container, JSData.DataStore, or JSData.Mapper!')
}
const router = this.router = express.Router()
router.use(bodyParser.json())
router.use(bodyParser.urlencoded({
extended: true
}))
if (utils.isFunction(config.request)) {
router.use(config.request)
config.request = undefined
}
if (component instanceof Container) {
utils.forOwn(component._mappers, (mapper, name) => {
let endpoint = `/${mapper.endpoint || name}`
if (utils.isFunction(config.getEndpoint)) {
endpoint = config.getEndpoint(mapper)
}
router.use(endpoint, new Router(mapper, config).router)
})
} else if (component instanceof Mapper) {
const createManyHandler = makeHandler('createMany', component, config)
const createHandler = makeHandler('create', component, config)
const updateManyHandler = makeHandler('updateMany', component, config)
defaultRequestHandler(req, res, (err) => {
if (err) {
return next(err)
}
if (utils.isFunction(config[method].response)) {
config[method].response(req, res, next)
} else {
defaultResponseHandler(req, res, next)
}
})
})
utils.forOwn(component._mappers, (mapper, name) => {
let endpoint = `/${mapper.endpoint || name}`
if (utils.isFunction(config.getEndpoint)) {
endpoint = config.getEndpoint(mapper)
}
router.use(endpoint, new Router(mapper, config).router)
})
} else if (component instanceof Mapper) {
function makeHandler (method, component, config = {}) {
config[method] || (config[method] = {})
const userRequestHandler = utils.isFunction(config[method].request) ? config[method].request : handlerNoop
const defaultRequestHandler = handlers.makeRequestHandler(method, component, config)
const defaultResponseHandler = handlers.makeResponseHandler(method, component, config)
return (req, res, next) => {
userRequestHandler(req, res, (err) => {
if (err) {
return next(err)
}
defaultRequestHandler(req, res, (err) => {
if (err) {
return next(err)
}
if (utils.isFunction(config[method].response)) {
config[method].response(req, res, next)
} else {
defaultResponseHandler(req, res, next)