0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

chore(core): add dev feature tag for openapi.json (#6025)

chore(core): add dev feature tag for openapi.json to indicate operation should not show up in swagger.json
This commit is contained in:
Darcy Ye 2024-06-21 16:09:21 +08:00 committed by GitHub
parent 7f9c0f295c
commit 651a027b49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 9 deletions

View file

@ -25,7 +25,7 @@ import {
buildTag,
findSupplementFiles,
normalizePath,
removeCloudOnlyOperations,
removeUnnecessaryOperations,
validateSupplement,
validateSwaggerDocument,
} from './utils/general.js';
@ -199,7 +199,7 @@ export default function swaggerRoutes<T extends AnonymousRouter, R extends Route
const supplementPaths = await findSupplementFiles(routesDirectory);
const supplementDocuments = await Promise.all(
supplementPaths.map(async (path) =>
removeCloudOnlyOperations(
removeUnnecessaryOperations(
// eslint-disable-next-line no-restricted-syntax -- trust the type here as we'll validate it later
JSON.parse(await fs.readFile(path, 'utf8')) as DeepPartial<OpenAPIV3.Document>
)

View file

@ -14,6 +14,8 @@ const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slic
/** The tag name used in the supplement document to indicate that the operation is cloud only. */
const cloudOnlyTag = 'Cloud only';
/** The tag name is used in the supplement document to indicate that the corresponding API operation is a dev feature. */
const devFeatureTag = 'Dev feature';
/**
* Get the root component name from the given absolute path.
@ -114,10 +116,12 @@ const validateSupplementPaths = (
if (
isKeyInObject(operation, 'tags') &&
Array.isArray(operation.tags) &&
(operation.tags.length > 1 || operation.tags[0] !== cloudOnlyTag)
!operation.tags.every(
(tag) => typeof tag === 'string' && [cloudOnlyTag, devFeatureTag].includes(tag)
)
) {
throw new TypeError(
`Cannot use \`tags\` in supplement document on path \`${path}\` and operation \`${method}\` except for tag \`${cloudOnlyTag}\`. Define tags in the document root instead.`
`Cannot use \`tags\` in supplement document on path \`${path}\` and operation \`${method}\` except for tag \`${cloudOnlyTag}\` and \`${devFeatureTag}\`. Define tags in the document root instead.`
);
}
}
@ -218,19 +222,28 @@ export const validateSwaggerDocument = (document: OpenAPIV3.Document) => {
* **CAUTION**: This function mutates the input document.
*
* Remove operations (path + method) that are tagged with `Cloud only` if the application is not
* running in the cloud. This will prevent the swagger validation from failing in the OSS
* environment.
* running in the cloud and remove operations with `Dev feature` tag if Logto's `isDevFeatureEnabled` flag
* is set to be false.
*
* This will prevent the swagger validation from failing in the OSS environment.
*
*/
export const removeCloudOnlyOperations = (
// eslint-disable-next-line complexity
export const removeUnnecessaryOperations = (
document: DeepPartial<OpenAPIV3.Document>
): DeepPartial<OpenAPIV3.Document> => {
if (EnvSet.values.isCloud || !document.paths) {
const { isCloud, isDevFeaturesEnabled } = EnvSet.values;
if ((isCloud && isDevFeaturesEnabled) || !document.paths) {
return document;
}
for (const [path, pathItem] of Object.entries(document.paths)) {
for (const method of Object.values(OpenAPIV3.HttpMethods)) {
if (pathItem?.[method]?.tags?.includes(cloudOnlyTag)) {
if (
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(!isCloud && pathItem?.[method]?.tags?.includes(cloudOnlyTag)) ||
(!isDevFeaturesEnabled && pathItem?.[method]?.tags?.includes(devFeatureTag))
) {
// eslint-disable-next-line @silverhand/fp/no-delete, @typescript-eslint/no-dynamic-delete -- intended
delete pathItem[method];
}