155 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-03-17 16:54:37 +01:00
'use strict';
2022-05-05 12:32:22 +02:00
const _ = require('lodash');
2022-03-17 16:54:37 +01:00
const cleanSchemaAttributes = require('./utils/clean-schema-attributes');
const loopContentTypeNames = require('./utils/loop-content-type-names');
const pascalCase = require('./utils/pascal-case');
2022-05-05 12:32:22 +02:00
const { hasFindMethod, isLocalizedPath } = require('./utils/routes');
2022-03-17 16:54:37 +01:00
/**
2022-04-05 10:57:35 +02:00
* @decription Get all open api schema objects for a given content type
2022-03-17 16:54:37 +01:00
*
* @param {object} apiInfo
* @property {string} apiInfo.uniqueName - Api name | Api name + Content type name
* @property {object} apiInfo.attributes - Attributes on content type
* @property {object} apiInfo.routeInfo - The routes for the api
*
* @returns {object} Open API schemas
*/
2022-04-01 18:10:59 +02:00
const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
2022-03-17 16:54:37 +01:00
// Store response and request schemas in an object
let schemas = {};
// Get all the route methods
const routeMethods = routeInfo.routes.map(route => route.method);
2022-05-05 12:32:22 +02:00
// Check for localized paths
const hasLocalizationPath = routeInfo.routes.filter(route => isLocalizedPath(route.path)).length;
2022-04-05 10:57:35 +02:00
// When the route methods contain any post or put requests
2022-03-17 16:54:37 +01:00
if (routeMethods.includes('POST') || routeMethods.includes('PUT')) {
2022-05-05 12:32:22 +02:00
const attributesToOmit = [
'createdAt',
'updatedAt',
'publishedAt',
'publishedBy',
'updatedBy',
'createdBy',
'localizations',
];
const attributesForRequest = _.omit(attributes, attributesToOmit);
// Get a list of required attribute names
const requiredAttributes = Object.entries(attributesForRequest).reduce((acc, attribute) => {
const [attributeKey, attributeValue] = attribute;
2022-03-17 16:54:37 +01:00
if (attributeValue.required) {
acc.push(attributeKey);
}
return acc;
}, []);
2022-03-17 16:54:37 +01:00
2022-04-29 10:05:29 +02:00
if (hasLocalizationPath) {
schemas = {
...schemas,
[`${pascalCase(uniqueName)}LocalizationRequest`]: {
required: [...requiredAttributes, 'locale'],
2022-04-29 10:05:29 +02:00
type: 'object',
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true }),
2022-04-29 10:05:29 +02:00
},
};
}
2022-04-05 10:57:35 +02:00
// Build the request schema
2022-03-17 16:54:37 +01:00
schemas = {
...schemas,
2022-04-05 10:57:35 +02:00
[`${pascalCase(uniqueName)}Request`]: {
2022-03-17 16:54:37 +01:00
type: 'object',
required: ['data'],
2022-03-17 16:54:37 +01:00
properties: {
data: {
required: requiredAttributes,
2022-03-17 16:54:37 +01:00
type: 'object',
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true }),
2022-03-17 16:54:37 +01:00
},
},
},
};
}
2022-05-02 11:09:43 +02:00
if (hasLocalizationPath) {
schemas = {
...schemas,
[`${pascalCase(uniqueName)}LocalizationResponse`]: {
type: 'object',
properties: {
id: { type: 'string' },
...cleanSchemaAttributes(attributes),
},
},
};
}
2022-04-05 10:57:35 +02:00
// Check for routes that need to return a list
const hasListOfEntities = routeInfo.routes.filter(route => hasFindMethod(route.handler)).length;
2022-04-05 10:57:35 +02:00
if (hasListOfEntities) {
// Build the list response schema
schemas = {
...schemas,
[`${pascalCase(uniqueName)}ListResponse`]: {
properties: {
data: {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes) },
},
},
},
meta: {
type: 'object',
properties: {
pagination: {
properties: {
page: { type: 'integer' },
pageSize: { type: 'integer', minimum: 25 },
pageCount: { type: 'integer', maximum: 1 },
total: { type: 'integer' },
},
},
},
},
},
},
};
}
// Build the response schema
2022-03-17 16:54:37 +01:00
schemas = {
...schemas,
2022-04-05 10:57:35 +02:00
[`${pascalCase(uniqueName)}Response`]: {
properties: {
data: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes) },
},
},
meta: { type: 'object' },
},
},
2022-03-17 16:54:37 +01:00
};
return schemas;
};
const buildComponentSchema = api => {
2022-04-06 17:10:27 +02:00
// A reusable loop for building paths and component schemas
// Uses the api param to build a new set of params for each content type
// Passes these new params to the function provided
2022-03-17 16:54:37 +01:00
return loopContentTypeNames(api, getAllSchemasForContentType);
};
module.exports = buildComponentSchema;