oazapfts
Version:
OpenApi TypeScript client generator
1 lines • 7.4 kB
Source Map (JSON)
{"version":3,"file":"plugin.cjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["import ts from \"typescript\";\nimport {\n AsyncSeriesBailHook,\n AsyncSeriesWaterfallHook,\n AsyncSeriesHook,\n SyncWaterfallHook,\n} from \"tapable\";\nimport type { OazapftsContext } from \"./context\";\nimport type * as OpenApi from \"./helpers/openApi3-x\";\nimport { HttpMethod } from \"./helpers\";\n\nexport enum UNSTABLE_OAZAPFTS_PLUGIN_PRECEDENCE {\n EAGER = \"eager\",\n DEFAULT = \"default\",\n LAZY = \"lazy\",\n}\n\nexport type UNSTABLE_OazapftsPluginOptions = {\n name?: string;\n version?: string;\n precedence?: UNSTABLE_OAZAPFTS_PLUGIN_PRECEDENCE;\n};\n/**\n * A plugin initiator function that receives hooks and can tap into them.\n */\nexport type UNSTABLE_OazapftsPluginFn = (\n hooks: UNSTABLE_OazapftsPluginHooks,\n) => void | Promise<void>;\nexport type UNSTABLE_OazapftsPlugin = UNSTABLE_OazapftsPluginFn &\n UNSTABLE_OazapftsPluginOptions;\n\nexport type UNSTABLE_QuerySerializerHookArgs = [\n ts.Expression[],\n {\n method: HttpMethod;\n path: string;\n operation: OpenApi.OperationObject;\n pathItem: OpenApi.PathItemObject;\n formatter: string;\n parameters: OpenApi.ParameterObject[];\n query: OpenApi.ParameterObject[];\n },\n OazapftsContext,\n];\n\nexport type UNSTABLE_EndpointHookArgs = [\n {\n method: HttpMethod;\n path: string;\n operation: OpenApi.OperationObject;\n pathItem: OpenApi.PathItemObject;\n },\n OazapftsContext,\n];\n\nexport type UNSTABLE_ComposeSourceHookArgs = [OazapftsContext, ts.Statement[]];\n\nexport type UNSTABLE_OazapftsPluginHooks = {\n /**\n * Called after context is created with all template parts initialized.\n * Use this to modify the spec, context, or template parts.\n * This is the only hook where ctx.spec is mutable.\n */\n prepare: AsyncSeriesHook<[OazapftsContext]>;\n /**\n * Decide whether a given endpoint should be generated.\n * Receives the current decision (default true) as first argument.\n * Return false to skip endpoint generation.\n */\n filterEndpoint: SyncWaterfallHook<\n [\n boolean,\n {\n method: HttpMethod;\n path: string;\n operation: OpenApi.OperationObject;\n pathItem: OpenApi.PathItemObject;\n },\n OazapftsContext,\n ]\n >;\n /**\n * Generate client methods for an endpoint.\n * This is a bail hook: the first plugin that returns a value wins.\n * Return `undefined` to delegate to later plugins.\n */\n generateMethod: AsyncSeriesBailHook<\n UNSTABLE_EndpointHookArgs,\n ts.Statement[] | undefined\n >;\n /**\n * Refine client methods for an endpoint.\n * Receives generated methods and can return a modified array.\n * Runs after generateMethod for each endpoint.\n */\n refineMethod: AsyncSeriesWaterfallHook<\n [ts.Statement[], ...UNSTABLE_EndpointHookArgs]\n >;\n /**\n * Compose top-level source statements from context and generated methods.\n * This is a bail hook: the first plugin that returns a value wins.\n * Return `undefined` to delegate to later plugins.\n */\n composeSource: AsyncSeriesBailHook<\n UNSTABLE_ComposeSourceHookArgs,\n ts.Statement[] | undefined\n >;\n /**\n * Refine top-level source statements before SourceFile construction.\n * Receives composed statements and can return a modified array.\n * Runs after composeSource.\n */\n refineSource: AsyncSeriesWaterfallHook<\n [ts.Statement[], ...UNSTABLE_ComposeSourceHookArgs]\n >;\n /**\n * Customize query serializer call arguments for each formatter call.\n * Default behavior is identity (returns the original args unchanged).\n */\n querySerializerArgs: SyncWaterfallHook<UNSTABLE_QuerySerializerHookArgs>;\n /**\n * Called after the full AST has been generated, before printing to string.\n * Use this to add/modify/remove statements from the final source file.\n */\n astGenerated: AsyncSeriesWaterfallHook<[ts.SourceFile, OazapftsContext]>;\n};\n\n/**\n * Create a oazapfts plugin\n */\nexport function UNSTABLE_createPlugin(\n fn: UNSTABLE_OazapftsPluginFn,\n options: UNSTABLE_OazapftsPluginOptions = {},\n) {\n return Object.assign(fn, options);\n}\n\n/**\n * Create a fresh Hooks instance for a generation run.\n */\nexport function UNSTABLE_createHooks() {\n return {\n prepare: new AsyncSeriesHook([\"ctx\"], \"prepare\"),\n filterEndpoint: new SyncWaterfallHook(\n [\"generate\", \"endpoint\", \"ctx\"],\n \"filterEndpoint\",\n ),\n generateMethod: new AsyncSeriesBailHook(\n [\"endpoint\", \"ctx\"],\n \"generateMethod\",\n ),\n refineMethod: new AsyncSeriesWaterfallHook(\n [\"methods\", \"endpoint\", \"ctx\"],\n \"refineMethod\",\n ),\n composeSource: new AsyncSeriesBailHook([\"ctx\", \"methods\"], \"composeSource\"),\n refineSource: new AsyncSeriesWaterfallHook(\n [\"statements\", \"ctx\", \"methods\"],\n \"refineSource\",\n ),\n querySerializerArgs: new SyncWaterfallHook(\n [\"args\", \"queryContext\", \"ctx\"],\n \"querySerializerArgs\",\n ),\n astGenerated: new AsyncSeriesWaterfallHook([\"ast\", \"ctx\"], \"astGenerated\"),\n } satisfies UNSTABLE_OazapftsPluginHooks;\n}\n\n/**\n * Apply plugins to a hooks instance.\n * Plugins are applied in order, and each can tap into hooks.\n */\nexport async function UNSTABLE_applyPlugins(\n hooks: UNSTABLE_OazapftsPluginHooks,\n plugins: UNSTABLE_OazapftsPlugin[],\n): Promise<void> {\n for (const plugin of UNSTABLE_sortPlugins(plugins)) {\n await plugin(hooks);\n }\n}\n\nexport function UNSTABLE_sortPlugins<\n Plugin extends Pick<UNSTABLE_OazapftsPlugin, \"precedence\">,\n>(plugins: Plugin[]) {\n const eagerPlugins: Plugin[] = [];\n const defaultPlugins: Plugin[] = [];\n const lazyPlugins: Plugin[] = [];\n for (const plugin of plugins) {\n switch (plugin.precedence) {\n case UNSTABLE_OAZAPFTS_PLUGIN_PRECEDENCE.EAGER:\n eagerPlugins.push(plugin);\n break;\n case UNSTABLE_OAZAPFTS_PLUGIN_PRECEDENCE.LAZY:\n lazyPlugins.push(plugin);\n break;\n case UNSTABLE_OAZAPFTS_PLUGIN_PRECEDENCE.DEFAULT:\n default:\n defaultPlugins.push(plugin);\n break;\n }\n }\n\n return [...eagerPlugins, ...defaultPlugins, ...lazyPlugins];\n}\n"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