mirror of
https://github.com/strapi/strapi.git
synced 2025-10-14 09:34:32 +00:00
restructure component schemas
This commit is contained in:
parent
c3b7613deb
commit
487b06f46d
@ -122,7 +122,7 @@ const getPaths = ({ routeInfo, uniqueName, contentTypeInfo }) => {
|
|||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
schema: {
|
schema: {
|
||||||
$ref: `#/components/schemas/New${pascalCase(uniqueName)}`,
|
$ref: `#/components/schemas/${pascalCase(uniqueName)}Request`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const cleanSchemaAttributes = require('./utils/clean-schema-attributes');
|
const cleanSchemaAttributes = require('./utils/clean-schema-attributes');
|
||||||
const getSchemaData = require('./utils/get-schema-data');
|
|
||||||
const loopContentTypeNames = require('./utils/loop-content-type-names');
|
const loopContentTypeNames = require('./utils/loop-content-type-names');
|
||||||
const pascalCase = require('./utils/pascal-case');
|
const pascalCase = require('./utils/pascal-case');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @decription Gets all open api schema objects for a given content type
|
* @decription Get all open api schema objects for a given content type
|
||||||
*
|
*
|
||||||
* @param {object} apiInfo
|
* @param {object} apiInfo
|
||||||
* @property {string} apiInfo.getter - api | plugin
|
|
||||||
* @property {array} apiInfo.ctNames - All contentType names on the api
|
|
||||||
* @property {string} apiInfo.uniqueName - Api name | Api name + Content type name
|
* @property {string} apiInfo.uniqueName - Api name | Api name + Content type name
|
||||||
* @property {object} apiInfo.attributes - Attributes on content type
|
* @property {object} apiInfo.attributes - Attributes on content type
|
||||||
* @property {object} apiInfo.routeInfo - The routes for the api
|
* @property {object} apiInfo.routeInfo - The routes for the api
|
||||||
@ -20,11 +17,10 @@ const pascalCase = require('./utils/pascal-case');
|
|||||||
const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
||||||
// Store response and request schemas in an object
|
// Store response and request schemas in an object
|
||||||
let schemas = {};
|
let schemas = {};
|
||||||
// Set flag false since schemas are always objects
|
|
||||||
const isListOfEntities = false;
|
|
||||||
// Get all the route methods
|
// Get all the route methods
|
||||||
const routeMethods = routeInfo.routes.map(route => route.method);
|
const routeMethods = routeInfo.routes.map(route => route.method);
|
||||||
|
|
||||||
|
// When the route methods contain any post or put requests
|
||||||
if (routeMethods.includes('POST') || routeMethods.includes('PUT')) {
|
if (routeMethods.includes('POST') || routeMethods.includes('PUT')) {
|
||||||
const requiredAttributes = Object.entries(attributes)
|
const requiredAttributes = Object.entries(attributes)
|
||||||
.filter(([, attribute]) => attribute.required)
|
.filter(([, attribute]) => attribute.required)
|
||||||
@ -37,9 +33,10 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
? Object.assign({}, ...requiredAttributes)
|
? Object.assign({}, ...requiredAttributes)
|
||||||
: attributes;
|
: attributes;
|
||||||
|
|
||||||
|
// Build the request schema
|
||||||
schemas = {
|
schemas = {
|
||||||
...schemas,
|
...schemas,
|
||||||
[`New${pascalCase(uniqueName)}`]: {
|
[`${pascalCase(uniqueName)}Request`]: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
data: {
|
data: {
|
||||||
@ -51,9 +48,60 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for routes that need to return a list
|
||||||
|
const hasListOfEntities = routeInfo.routes.filter(
|
||||||
|
route => route.handler.split('.').pop() === 'find'
|
||||||
|
).length;
|
||||||
|
|
||||||
|
if (hasListOfEntities) {
|
||||||
|
// Build the list response schema
|
||||||
schemas = {
|
schemas = {
|
||||||
...schemas,
|
...schemas,
|
||||||
[pascalCase(uniqueName)]: getSchemaData(isListOfEntities, cleanSchemaAttributes(attributes)),
|
[`${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
|
||||||
|
schemas = {
|
||||||
|
...schemas,
|
||||||
|
[`${pascalCase(uniqueName)}Response`]: {
|
||||||
|
properties: {
|
||||||
|
data: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: { type: 'string' },
|
||||||
|
attributes: { type: 'object', properties: cleanSchemaAttributes(attributes) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
meta: { type: 'object' },
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return schemas;
|
return schemas;
|
||||||
|
@ -1,55 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const pascalCase = require('./pascal-case');
|
const pascalCase = require('./pascal-case');
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {boolean} isSingleEntity - Checks for a single entity
|
|
||||||
* @returns {object} The correctly formatted meta object
|
|
||||||
*/
|
|
||||||
const getMeta = isListOfEntities => {
|
|
||||||
if (isListOfEntities) {
|
|
||||||
return {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pagination: {
|
|
||||||
properties: {
|
|
||||||
page: { type: 'integer' },
|
|
||||||
pageSize: { type: 'integer', minimum: 25 },
|
|
||||||
pageCount: { type: 'integer', maximum: 1 },
|
|
||||||
total: { type: 'integer' },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return { type: 'object' };
|
|
||||||
};
|
|
||||||
|
|
||||||
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),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description - Builds the Swagger response object for a given api
|
* @description - Builds the Swagger response object for a given api
|
||||||
@ -60,17 +11,24 @@ const getSchemaAsArrayOrObject = (isListOfEntities, name) => {
|
|||||||
*
|
*
|
||||||
* @returns The Swagger responses
|
* @returns The Swagger responses
|
||||||
*/
|
*/
|
||||||
module.exports = (name, route, isListOfEntities = false) => {
|
const getApiResponse = (name, route, isListOfEntities = false) => {
|
||||||
let schema;
|
const getSchema = () => {
|
||||||
if (route.method === 'DELETE') {
|
if (route.method === 'DELETE') {
|
||||||
schema = {
|
return {
|
||||||
type: 'integer',
|
type: 'integer',
|
||||||
format: 'int64',
|
format: 'int64',
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
schema = getSchemaAsArrayOrObject(isListOfEntities, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isListOfEntities) {
|
||||||
|
return { $ref: `#/components/schemas/${pascalCase(name)}ListResponse` };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { $ref: `#/components/schemas/${pascalCase(name)}Response` };
|
||||||
|
};
|
||||||
|
|
||||||
|
const schema = getSchema();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
responses: {
|
responses: {
|
||||||
'200': {
|
'200': {
|
||||||
@ -134,3 +92,5 @@ module.exports = (name, route, isListOfEntities = false) => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = getApiResponse;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user