UNPKG

@alova/wormhole

Version:

More modern openAPI generating solution for alova.js

101 lines (100 loc) 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processApiTags = processApiTags; exports.tagModifier = tagModifier; /** * Validates if tag name follows naming conventions * @param tag The tag name to validate * @returns true if valid, false otherwise */ function isValidTagName(tag) { if (!tag || typeof tag !== 'string') { return false; } const trimmedTag = tag.trim(); if (!trimmedTag) { return false; } // Basic naming convention: allow letters, numbers, hyphens, underscores and Chinese characters const validPattern = /^[\u4E00-\u9FA5\w-]+$/; return validPattern.test(trimmedTag); } /** * Processes tags in the API descriptor * @param apiDescriptor The API descriptor * @param handler Tag modifier handler function * @returns Modified API descriptor */ function processApiTags(apiDescriptor, handler) { if (!apiDescriptor) return apiDescriptor; const newDescriptor = { ...apiDescriptor }; // Check if tags property exists and is an array if (!newDescriptor.tags || !Array.isArray(newDescriptor.tags)) { return newDescriptor; } // Process each tag and filter out null/undefined results newDescriptor.tags = newDescriptor.tags .map((tag) => { try { // Call user provided handler function const modifiedTag = handler(tag); // If handler returns null/undefined/void, remove this tag if (modifiedTag == null) { return null; } // Validate if modified tag follows naming conventions if (!isValidTagName(modifiedTag)) { return tag; // Keep original tag if invalid } return modifiedTag.trim(); // Return trimmed modified tag } catch { return tag; // Return original tag on error } }) .filter((tag) => tag != null); // Filter out null/undefined values return newDescriptor; } /** * Creates a tag modifier plugin * * @param handler Tag modifier handler function that receives a tag string and returns modified tag or null/undefined/void to remove the tag * @returns API plugin instance * * @example * ```ts * // Convert all tags to uppercase * const upperCasePlugin = tagModifier(tag => tag.toUpperCase()); * * // Add prefix to tags * const prefixPlugin = tagModifier(tag => `api-${tag}`); * * // Remove specific tags * const filterPlugin = tagModifier(tag => tag === 'internal' ? null : tag); * * // Use the plugin * export default { * generator: [{ * // ...other config * plugins: [upperCasePlugin] * }] * }; * ``` */ function tagModifier(handler) { if (!handler || typeof handler !== 'function') { throw new Error('tagModifier requires a valid handler function'); } return { name: 'tagModifier', extends: { handleApi: (apiDescriptor) => { if (!apiDescriptor) return null; return processApiTags(apiDescriptor, handler); }, }, }; } exports.default = tagModifier;