106 lines
2.2 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
/**
* @description - Builds the Swagger response object for a given api
*
2022-04-05 17:24:39 +02:00
* @param {object} name - Name of the api or plugin
2021-09-02 11:25:24 +02:00
* @param {object} route - The current route
* @param {boolean} isListOfEntities - Checks for a list of entitities
*
* @returns The Swagger responses
*/
2022-05-05 12:32:22 +02:00
const getApiResponse = ({
uniqueName,
route,
isListOfEntities = false,
isLocalizationPath = false,
}) => {
2022-04-05 10:57:35 +02:00
const getSchema = () => {
if (route.method === 'DELETE') {
return {
type: 'integer',
format: 'int64',
};
}
2022-05-05 12:32:22 +02:00
if (isLocalizationPath) {
return { $ref: `#/components/schemas/${pascalCase(uniqueName)}LocalizationResponse` };
}
2022-04-05 10:57:35 +02:00
if (isListOfEntities) {
2022-05-05 12:32:22 +02:00
return { $ref: `#/components/schemas/${pascalCase(uniqueName)}ListResponse` };
2022-04-05 10:57:35 +02:00
}
2022-05-05 12:32:22 +02:00
return { $ref: `#/components/schemas/${pascalCase(uniqueName)}Response` };
2022-04-05 10:57:35 +02:00
};
const schema = getSchema();
2021-09-02 11:25:24 +02:00
return {
responses: {
2022-05-05 12:32:22 +02:00
200: {
description: 'OK',
2021-09-02 11:25:24 +02:00
content: {
'application/json': {
schema,
},
},
},
2022-05-05 12:32:22 +02:00
400: {
description: 'Bad Request',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
2022-05-05 12:32:22 +02:00
401: {
description: 'Unauthorized',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
2022-05-05 12:32:22 +02:00
403: {
2021-09-02 11:25:24 +02:00
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
},
},
},
2022-05-05 12:32:22 +02:00
404: {
description: 'Not Found',
content: {
'application/json': {
2022-03-17 16:54:37 +01:00
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
2022-05-05 12:32:22 +02:00
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
},
},
},
},
};
};
2022-04-05 10:57:35 +02:00
module.exports = getApiResponse;