json-api-nestjs
Version:
JsonApi Plugin for NestJs
93 lines • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOne = getOne;
const swagger_1 = require("@nestjs/swagger");
const nestjs_shared_1 = require("../../../../utils/nestjs-shared");
const types_1 = require("../../types");
const utils_1 = require("../utils");
function getOne(controller, descriptor, entity, mapEntity, methodName) {
const entityName = entity.name;
const { props, relations, relationProperty, primaryColumnName, primaryColumnType, } = (0, utils_1.getEntityMapProps)(mapEntity, entity);
const entityRelationStructure = nestjs_shared_1.ObjectTyped.entries(relationProperty).reduce((acum, [name, props]) => {
const relMap = (0, utils_1.getEntityMapProps)(mapEntity, props.entityClass);
acum[name] = relMap.props;
return acum;
}, {});
(0, swagger_1.ApiOperation)({
summary: `Get one item of resource "${entityName}"`,
operationId: `${controller.constructor.name}_${methodName}`,
})(controller, methodName, descriptor);
(0, swagger_1.ApiParam)({
name: 'id',
required: true,
type: primaryColumnType === types_1.TypeField.number ? 'integer' : 'string',
description: `ID of resource "${entityName}"`,
})(controller, methodName, descriptor);
(0, swagger_1.ApiQuery)({
name: 'fields',
required: false,
style: 'deepObject',
schema: {
type: 'object',
},
examples: {
allField: {
summary: 'Select all field',
description: 'Select field for target and relation',
value: {
target: props.join(','),
...nestjs_shared_1.ObjectTyped.entries(entityRelationStructure).reduce((acum, [name, props]) => {
acum[name.toString()] = props.join(',');
return acum;
}, {}),
},
},
selectOnlyIdsTarget: {
summary: 'Select ids for target',
description: 'Select ids for target',
value: {
target: props.filter((i) => i === primaryColumnName).join(','),
},
},
},
description: `Object of field for select field from "${entity.name}" resource`,
})(controller, methodName, descriptor);
if (relations.length > 0) {
(0, swagger_1.ApiQuery)({
name: 'include',
required: false,
enum: relations,
style: 'simple',
isArray: true,
description: `"${entity.name}" resource item has been extended with existing relations`,
examples: {
withInclude: {
summary: 'Add all relation',
description: 'Add all realtion',
value: relations,
},
without: {
summary: 'Without relation',
description: 'Without all relation',
value: [],
},
},
})(controller, methodName, descriptor);
}
(0, swagger_1.ApiResponse)({
status: 404,
description: `Item of resource "${entityName}" not found`,
schema: utils_1.errorSchema,
})(controller, methodName, descriptor);
(0, swagger_1.ApiResponse)({
status: 400,
description: 'Wrong query parameters',
schema: utils_1.errorSchema,
})(controller, methodName, descriptor);
(0, swagger_1.ApiResponse)({
status: 200,
description: 'Resource one item received successfully',
schema: (0, utils_1.jsonSchemaResponse)(entity, mapEntity),
})(controller, methodName, descriptor);
}
//# sourceMappingURL=get-one.js.map