UNPKG

@lonu/stc

Version:

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

60 lines (59 loc) 2.12 kB
import { parserDefinition } from "../definition.js"; import { parserActions } from "../action.js"; import { createFile } from "../../utils.js"; import template from "./template/index.js"; import shared from "./shared/index.js"; /** * Swift plugin * Depends on [Alamofire](https://github.com/Alamofire/Alamofire) */ export const SwiftPlugin = { name: "stc:SwiftPlugin", lang: "swift", setup() { const pluginSetup = { unknownType: "Any", typeMap(func, type) { const _newType = type && func(type, undefined, undefined, pluginSetup) || pluginSetup.unknownType; return { string: "String", integer: "Int", boolean: "Bool", file: "Data", array: `[${_newType}]`, object: `[String: ${_newType}]`, null: "nil", }; }, template: { enum: template.enum, definitionHeader: template.definitionHeader, definitionBody: template.definitionBody, definitionFooter: template.definitionFooter, actionImport: template.actionImport, actionMethod: template.actionMethod, }, }; return pluginSetup; }, onTransform(def, action, options) { const typeFileName = "Models"; const defContent = parserDefinition(def, options); const actionData = parserActions(action, typeFileName, options); return { definition: { filename: `${typeFileName}.${this.lang}`, content: defContent, }, action: actionData, }; }, onEnd(options) { if (!options.shared) return; // Copy template files to output directory createFile(`${options.outDir}/shared/APIClientBase.${this.lang}`, shared.APIClientBase); createFile(`${options.outDir}/shared/alamofire/APIClient.${this.lang}`, shared.APIClient); }, };