UNPKG

@lonu/stc

Version:

A tool for converting OpenApi/Swagger/Apifox into code.

71 lines (70 loc) 2.81 kB
import { createFile } from "../../utils.js"; import { parserDefinition } from "../definition.js"; import { parserActions } from "../action.js"; import { renderEtaString } from "../common.js"; import shared from "./shared/index.js"; import template from "./template/index.js"; // Deno 当前版本(1.45.2)未支持 // import webClientBaseFile from "./shared/apiClientBase.ts" with { type: "text" }; export const TypeScriptPlugin = { name: "stc:TypeScriptPlugin", lang: "ts", setup() { const pluginSetup = { unknownType: "unknown", typeMap(func, type) { return { string: "string", integer: "number", boolean: "boolean", array: `${type && func(type, undefined, pluginSetup) || pluginSetup.unknownType}[]`, object: `Record<string, ${pluginSetup.unknownType}>`, file: "File", null: "null", bool: "boolean", }; }, template: { enum: template.enum, definitionHeader: template.definitionHeader, definitionBody: template.definitionBody, definitionFooter: template.definitionFooter, actionImport: template.actionImport, actionMethod: template.actionMethod, }, langDirectoryName: "typescript", }; return pluginSetup; }, /** * Transforms the given definition and action into an object containing * the definition content and action data. * * @param {type} def - the definition parameter * @param {type} action - the action parameter * @return {type} an object containing the definition content and action data */ onTransform(def, action, options) { const typeFileName = "_types"; const defContent = parserDefinition(def, options); const actionData = parserActions(action, typeFileName, options); return { definition: { filename: `${typeFileName}.ts`, content: defContent, }, action: actionData, }; }, onEnd(options) { if (!options.shared) return; // 创建运行时需要的文件 const _fetchRuntimeFileContent = renderEtaString(shared.fetchRuntime, options); const _httpClientContent = shared[options.client]; createFile(`${options.outDir}/shared/${options.client}/index.ts`, _httpClientContent); createFile(`${options.outDir}/shared/apiClientBase.ts`, shared.apiClientBase); createFile(`${options.outDir}/shared/fetchRuntime.ts`, _fetchRuntimeFileContent); }, };