34 lines
883 B
JavaScript
34 lines
883 B
JavaScript
// Hero profile routes: GET /api/heroes/:name (public).
|
|
const zod = require('zod');
|
|
const { AppError } = require('../utils/errors');
|
|
const { getHeroProfile } = require('../services/heroProfileService');
|
|
|
|
function wrap(fn) {
|
|
return (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
|
}
|
|
|
|
/**
|
|
* GET /api/heroes/:name — public hero profile.
|
|
*/
|
|
async function handleHeroProfile(req, res) {
|
|
const parsed = zod.strictObject({
|
|
name: zod.string().min(1).trim(),
|
|
}).safeParse(req.params);
|
|
if (!parsed.success) {
|
|
throw new AppError('VALIDATION_ERROR', 'Request validation failed.', 400);
|
|
}
|
|
|
|
const profile = getHeroProfile(parsed.data.name);
|
|
if (!profile) {
|
|
throw new AppError('NOT_FOUND', 'Hero not found.', 404);
|
|
}
|
|
|
|
res.json({ ok: true, data: profile });
|
|
}
|
|
|
|
module.exports = {
|
|
routes: {
|
|
getHeroProfile: wrap(handleHeroProfile),
|
|
},
|
|
};
|