137 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-09-02 11:25:24 +02:00
'use strict';
2022-03-17 16:54:37 +01:00
const pascalCase = require('./pascal-case');
2021-09-02 11:25:24 +02:00
/**
*
* @param {boolean} isSingleEntity - Checks for a single entity
* @returns {object} The correctly formatted meta object
*/
const getMeta = isListOfEntities => {
if (isListOfEntities) {
return {
type: 'object',
2021-09-02 11:25:24 +02:00
properties: {
pagination: {
properties: {
page: { type: 'integer' },
pageSize: { type: 'integer', minimum: 25 },
pageCount: { type: 'integer', maximum: 1 },
total: { type: 'integer' },
},
},
},
};
}
return { type: 'object' };
};
2022-03-17 16:54:37 +01:00
const getSchemaAsArrayOrObject = (isListOfEntities, name) => {
if (isListOfEntities) {
return {
properties: {
data: {
type: 'array',
items: {
$ref: `#/components/schemas/${pascalCase(name)}`,
},
},
meta: getMeta(isListOfEntities),
},
};
}
return {
properties: {
data: {
$ref: `#/components/schemas/${pascalCase(name)}`,
},
meta: getMeta(isListOfEntities),
},
};
};
2021-09-02 11:25:24 +02:00
/**
* @description - Builds the Swagger response object for a given api
*
* @param {object} attributes - The attributes found on a contentType
* @param {object} route - The current route
* @param {boolean} isListOfEntities - Checks for a list of entitities
*
* @returns The Swagger responses
*/
2022-03-17 16:54:37 +01:00
module.exports = (name, route, isListOfEntities = false) => {
2021-09-02 11:25:24 +02:00
let schema;
if (route.method === 'DELETE') {
schema = {
type: 'integer',
format: 'int64',
};
} else {
2022-03-17 16:54:37 +01:00
schema = getSchemaAsArrayOrObject(isListOfEntities, name);
2021-09-02 11:25:24 +02:00
}
return {
responses: {
'200': {
description: 'OK',
2021-09-02 11:25:24 +02:00
content: {
'application/json': {
schema,
},
},
},
'400': {
description: 'Bad Request',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
'401': {
description: 'Unauthorized',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
2021-09-02 11:25:24 +02:00
'403': {
description: 'Forbidden',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
2021-09-02 11:25:24 +02:00
},
},
},
'404': {
description: 'Not Found',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
'500': {
description: 'Internal Server Error',
2021-09-02 11:25:24 +02:00
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
2021-09-02 11:25:24 +02:00
},
},
},
},
};
};