json-api-nestjs
Version:
JsonApi Plugin for NestJs
203 lines • 7.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAll = getAll;
const swagger_1 = require("@nestjs/swagger");
const nestjs_shared_1 = require("../../../../utils/nestjs-shared");
const utils_1 = require("../utils");
const constants_1 = require("../../../../constants");
function getAll(controller, descriptor, entity, mapEntity, methodName) {
const { props, relations, relationProperty, primaryColumnName } = (0, utils_1.getEntityMapProps)(mapEntity, entity);
const entityRelationStructure = {};
const relationTree = nestjs_shared_1.ObjectTyped.entries(relationProperty).reduce((acum, [name, props]) => {
const relMap = (0, utils_1.getEntityMapProps)(mapEntity, props.entityClass);
acum.push(...relMap.props.map((i) => `${name.toLocaleString()}.${i}`));
entityRelationStructure[name] = relMap.props;
return acum;
}, []);
(0, swagger_1.ApiOperation)({
summary: `Get list items of resource "${entity.name}"`,
operationId: `${controller.constructor.name}_${methodName}`,
servers: undefined,
})(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);
(0, swagger_1.ApiQuery)({
name: 'filter',
required: false,
style: 'deepObject',
schema: {
type: 'object',
},
examples: {
simpleExample: {
summary: 'Several conditional',
description: 'Get if relation is not null',
value: {
[props[0]]: {
in: '1,2,3',
},
[props[1]]: {
lt: '1',
},
[relationTree[0]]: {
eq: 'test',
},
},
},
relationNull: {
summary: 'Get if relation is null',
description: 'Get if relation is null',
value: {
[relations[0]]: {
eq: null,
},
},
},
relationNotNull: {
summary: 'Get if relation is not null',
description: 'Get if relation is not null',
value: {
[relations[0]]: {
ne: null,
},
},
},
getRelationByConditional: {
summary: 'Get if relation field is',
description: 'Get if relation field is',
value: {
[relationTree[0]]: {
eq: 'test',
},
},
},
},
description: `Object of filter for select items from "${entity.name}" resource`,
})(controller, methodName, descriptor);
let sortAscRelation = {};
let sortDescRelation = {};
let sortSeveral = {
summary: 'Sort several field',
description: 'Sort several field',
value: `${props[1]},-${props[0]}`,
};
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);
sortAscRelation = {
summary: 'Sort field relation by ASC',
description: 'Sort field relation by ASC',
value: relationTree[2],
};
sortDescRelation = {
summary: 'Sort field relation by DESC',
description: 'Sort field relation by DESC',
value: `-${relationTree[2]}`,
};
sortSeveral = {
summary: 'Sort several field with relation',
description: 'Sort several field relation',
value: `${props[1]},-${relationTree[2]},${relationTree[1]},-${props[0]}`,
};
}
(0, swagger_1.ApiQuery)({
name: 'sort',
type: 'string',
required: false,
description: `Params for sorting of "${entity.name}"`,
examples: {
sortAsc: {
summary: 'Sort field by ASC',
description: 'Sort field by ASC',
value: props[1],
},
sortDesc: {
summary: 'Sort field by DESC',
description: 'Sort field by DESC',
value: `-${props[1]}`,
},
sortAscRelation,
sortDescRelation,
sortSeveral,
},
})(controller, methodName, descriptor);
(0, swagger_1.ApiQuery)({
name: 'page',
style: 'deepObject',
required: false,
schema: {
type: 'object',
properties: {
number: {
type: 'integer',
minimum: 1,
example: constants_1.DEFAULT_QUERY_PAGE,
},
size: {
type: 'integer',
minimum: 1,
example: constants_1.DEFAULT_PAGE_SIZE,
maximum: 500,
},
},
additionalProperties: false,
},
description: `"${entity.name}" resource has been limit and offset with this params.`,
})(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 list received successfully',
schema: (0, utils_1.jsonSchemaResponse)(entity, mapEntity, true),
})(controller, methodName, descriptor);
}
//# sourceMappingURL=get-all.js.map