mirror of
https://github.com/strapi/strapi.git
synced 2025-10-18 19:43:22 +00:00
Merge pull request #2938 from EpicUsaMan/patch-6
Support array of methods in documentation plugin
This commit is contained in:
commit
d2ea21ef09
@ -369,7 +369,13 @@ module.exports = {
|
|||||||
const routeTagConfig = _.get(current, ['config', 'tag']);
|
const routeTagConfig = _.get(current, ['config', 'tag']);
|
||||||
// Add curly braces between dynamic params
|
// Add curly braces between dynamic params
|
||||||
const endPoint = this.formatApiEndPoint(current.path);
|
const endPoint = this.formatApiEndPoint(current.path);
|
||||||
const verb = current.method.toLowerCase();
|
let verb;
|
||||||
|
|
||||||
|
if (Array.isArray(current.method)) {
|
||||||
|
verb = current.method.map((method) => method.toLowerCase());
|
||||||
|
} else {
|
||||||
|
verb = current.method.toLowerCase();
|
||||||
|
}
|
||||||
// The key corresponds to firsts keys of the returned object
|
// The key corresponds to firsts keys of the returned object
|
||||||
let key;
|
let key;
|
||||||
let tags;
|
let tags;
|
||||||
@ -404,9 +410,16 @@ module.exports = {
|
|||||||
tags: _.isEmpty(tags) ? [_.upperFirst(key)] : [_.upperFirst(tags)],
|
tags: _.isEmpty(tags) ? [_.upperFirst(key)] : [_.upperFirst(tags)],
|
||||||
};
|
};
|
||||||
|
|
||||||
_.set(acc, [key, 'paths', endPoint, verb], verbObject);
|
// Swagger is not support key with ',' symbol, for array of methods need generate documentation for each method
|
||||||
|
if (Array.isArray(verb)) {
|
||||||
|
verb.forEach((method) => {
|
||||||
|
_.set(acc, [key, 'paths', endPoint, method], verbObject);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_.set(acc, [key, 'paths', endPoint, verb], verbObject);
|
||||||
|
}
|
||||||
|
|
||||||
if (verb === 'post' || verb === 'put') {
|
if (verb.includes('post') || verb.includes('put')) {
|
||||||
let requestBody;
|
let requestBody;
|
||||||
|
|
||||||
if (controllerMethod === 'create' || controllerMethod === 'update') {
|
if (controllerMethod === 'create' || controllerMethod === 'update') {
|
||||||
@ -439,14 +452,38 @@ module.exports = {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
_.set(acc, [key, 'paths', endPoint, verb, 'requestBody'], requestBody);
|
if (Array.isArray(verb)) {
|
||||||
|
verb.forEach((method) => {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, method, 'requestBody'],
|
||||||
|
requestBody,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, verb, 'requestBody'],
|
||||||
|
requestBody,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refer to https://swagger.io/specification/#pathItemObject
|
// Refer to https://swagger.io/specification/#pathItemObject
|
||||||
const parameters = this.generateVerbParameters(verb, controllerMethod, current.path);
|
const parameters = this.generateVerbParameters(verb, controllerMethod, current.path);
|
||||||
|
|
||||||
if (verb !== 'post') {
|
if (!verb.includes('post')) {
|
||||||
_.set(acc, [key, 'paths', endPoint, verb, 'parameters'], parameters);
|
if (Array.isArray(verb)) {
|
||||||
|
verb.forEach((method) => {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, method, 'parameters'],
|
||||||
|
parameters,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_.set(acc, [key, 'paths', endPoint, verb, 'parameters'], parameters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
@ -555,7 +592,14 @@ module.exports = {
|
|||||||
prefix === undefined
|
prefix === undefined
|
||||||
? this.formatApiEndPoint(`/${pluginName}${current.path}`)
|
? this.formatApiEndPoint(`/${pluginName}${current.path}`)
|
||||||
: this.formatApiEndPoint(`${prefix}${current.path}`);
|
: this.formatApiEndPoint(`${prefix}${current.path}`);
|
||||||
const verb = current.method.toLowerCase();
|
let verb;
|
||||||
|
|
||||||
|
if (Array.isArray(current.method)) {
|
||||||
|
verb = current.method.map((method) => method.toLowerCase());
|
||||||
|
} else {
|
||||||
|
verb = current.method.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
const actionType = _.get(current, ['config', 'tag', 'actionType'], '');
|
const actionType = _.get(current, ['config', 'tag', 'actionType'], '');
|
||||||
let key;
|
let key;
|
||||||
let tags;
|
let tags;
|
||||||
@ -591,11 +635,25 @@ module.exports = {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (_.isEmpty(defaultDocumentation)) {
|
if (_.isEmpty(defaultDocumentation)) {
|
||||||
if (verb !== 'post') {
|
if (!verb.includes('post')) {
|
||||||
_.set(acc, [key, 'paths', endPoint, verb, 'parameters'], parameters);
|
if (Array.isArray(verb)) {
|
||||||
|
verb.forEach((method) => {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, method, 'parameters'],
|
||||||
|
parameters,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, verb, 'parameters'],
|
||||||
|
parameters,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verb === 'post' || verb === 'put') {
|
if (verb.includes('post') || verb.includes('put')) {
|
||||||
let requestBody;
|
let requestBody;
|
||||||
|
|
||||||
if (actionType === 'create' || actionType === 'update') {
|
if (actionType === 'create' || actionType === 'update') {
|
||||||
@ -633,7 +691,22 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
_.set(acc, [key, 'paths', endPoint, verb, 'requestBody'], requestBody);
|
|
||||||
|
if (Array.isArray(verb)) {
|
||||||
|
verb.forEach((method) => {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, method, 'requestBody'],
|
||||||
|
requestBody,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_.set(
|
||||||
|
acc,
|
||||||
|
[key, 'paths', endPoint, verb, 'requestBody'],
|
||||||
|
requestBody,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -932,18 +1005,23 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
generateResponseDescription: function(verb, tag, endPoint) {
|
generateResponseDescription: function(verb, tag, endPoint) {
|
||||||
const isModelRelated = strapi.models[tag] !== undefined && tag === endPoint;
|
const isModelRelated = strapi.models[tag] !== undefined && tag === endPoint;
|
||||||
|
|
||||||
|
if (Array.isArray(verb)) {
|
||||||
|
verb = verb.map((method) => method.toLocaleLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
switch (verb.toLocaleLowerCase()) {
|
if (
|
||||||
case 'get':
|
verb.includes('get') ||
|
||||||
case 'post':
|
verb.includes('put') ||
|
||||||
case 'put':
|
verb.includes('post')
|
||||||
return isModelRelated ? `Retrieve ${tag} document(s)` : 'response';
|
) {
|
||||||
case 'delete':
|
return isModelRelated ? `Retrieve ${tag} document(s)` : 'response';
|
||||||
return isModelRelated
|
} else if (verb.includes('delete')) {
|
||||||
? `deletes a single ${tag} based on the ID supplied`
|
return isModelRelated
|
||||||
: 'deletes a single record based on the ID supplied';
|
? `deletes a single ${tag} based on the ID supplied`
|
||||||
default:
|
: 'deletes a single record based on the ID supplied';
|
||||||
return 'response';
|
} else {
|
||||||
|
return 'response';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1080,6 +1158,26 @@ module.exports = {
|
|||||||
if (description) {
|
if (description) {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(verb)) {
|
||||||
|
const [, controllerMethod] = handler.split('.');
|
||||||
|
|
||||||
|
if (
|
||||||
|
(verb.includes('get') && verb.includes('post')) ||
|
||||||
|
controllerMethod === 'findOrCreate'
|
||||||
|
) {
|
||||||
|
return `Find or create ${tag} record`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
(verb.includes('put') && verb.includes('post')) ||
|
||||||
|
controllerMethod === 'createOrUpdate'
|
||||||
|
) {
|
||||||
|
return `Create or update ${tag} record`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
switch (verb) {
|
switch (verb) {
|
||||||
case 'get': {
|
case 'get': {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user