@lonu/stc
Version:
A tool for converting OpenApi/Swagger/Apifox into code.
100 lines (99 loc) • 3.44 kB
JavaScript
import { Eta } from "../../deps/jsr.io/@eta-dev/eta/3.5.0/src/index.js";
import Logs from "../console.js";
import { getT } from "../i18n/index.js";
let etaInstance = null;
/**
* Sets up the template engine with the provided options.
*
* @param {IPluginOptions} options - The plugin options to use for setting up the template.
* @param {object} [template] - The template options.
* @return {Eta} The set up template instance.
*/
export const setupTemplate = (options, template) => {
etaInstance = new Eta({
views: template?.path ??
`./src/plugins/${template?.langDirectoryName ?? options.lang}/template`,
});
return etaInstance;
};
/**
* Renders a template based on the provided name and data.
*
* @param {string} name - The name of the template to render.
* @param {Record<string, unknown>} data - The data to be used in the template.
* @return {string} The rendered template as a string.
*/
export const renderTemplate = (name, data) => {
if (!etaInstance) {
throw new Error("Please call `setupTemplate` first.");
}
return etaInstance.render(name, data);
};
/**
* Render a template with Eta.
*
* @param content - A string of template.
* @param data - A object contains data.
* @returns A rendered string.
*/
export const renderEtaString = (content, data) => {
if (!etaInstance) {
throw new Error("Please call `setupTemplate` first.");
}
return etaInstance?.renderString(content, data);
};
/**
* Converts a given type to a string representation, taking into account the provided reference and plugin setup.
*
* @param {string|string[]} type - The type to be converted.
* @param {string} [ref] - The reference type.
* @param {pluginSetup | IPluginOptions} [pluginSetup] - The plugin setup.
* @return {string} The converted type as a string.
*/
export const convertType = (type, ref, pluginSetup) => {
// 当只有 ref 或者 type 为 object 时,直接返回 ref
if ((!type || type === "object") && ref)
return ref;
// 若 type 与 ref 相等,则表示为自定义类型
if (type === ref)
return type || (pluginSetup?.unknownType ?? "");
const _action = pluginSetup?.typeMap?.(convertType, ref) ?? {};
const _newType = Array.isArray(type) ? type : [type];
const _type = _newType
.map((item) => _action[item] || item)
.filter((item) => item)
.join(" | ");
return _type;
};
export const validTemplate = (template) => {
if (!template?.actionImport) {
const _msg = getT("$t(plugin.template.actionImportRequired)");
Logs.error(_msg);
throw _msg;
}
if (!template.actionMethod) {
const _msg = getT("$t(plugin.template.actionMethodRequired)");
Logs.error(_msg);
throw _msg;
}
if (!template.definitionHeader) {
const _msg = getT("$t(plugin.template.definitionHeaderRequired)");
Logs.error(_msg);
throw _msg;
}
if (!template.definitionBody) {
const _msg = getT("$t(plugin.template.definitionBodyRequired)");
Logs.error(_msg);
throw _msg;
}
if (!template.definitionFooter) {
const _msg = getT("$t(plugin.template.definitionFooterRequired)");
Logs.error(_msg);
throw _msg;
}
if (!template.enum) {
const _msg = getT("$t(plugin.template.enumRequired)");
Logs.error(_msg);
throw _msg;
}
};