fastify-mongoose-rest
Version:
Rest API generator tools for fastify and mongoose
68 lines • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Details = void 0;
const utils_1 = require("../utils");
function Details(basePath, model, options) {
const { tags, findProperty, validationSchema } = options;
let response = {};
if (validationSchema) {
response = (0, utils_1.createResponseSchema)(validationSchema, 'object');
}
return {
method: 'GET',
url: `${basePath}/:id`,
schema: {
summary: `Get details of a single ${model.modelName} resource`,
tags,
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: `Unique identifier of ${model.modelName}`,
},
},
},
querystring: {
type: 'object',
properties: {
populate: {
type: 'string',
description: 'Population options of mongoose',
},
projection: {
type: 'string',
description: 'Projection options of mongoose',
},
select: {
type: 'string',
description: 'Select options of mongoose',
},
},
},
response,
},
handler: async (request, reply) => {
const { populate, projection, select } = request.query;
const query = model.findOne({
[findProperty || '_id']: request.params.id,
});
if (populate) {
query.populate((0, utils_1.parseInput)(populate));
}
if (projection) {
query.projection((0, utils_1.parseInput)(projection));
}
if (select) {
query.select((0, utils_1.parseInput)(select));
}
const resource = await query.lean().exec();
if (!resource) {
return reply.status(404).send(new Error('Resource not found'));
}
return reply.send(resource);
},
};
}
exports.Details = Details;
//# sourceMappingURL=details.js.map