fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 58 lines 1.7 kB view raw
1import { refs } from '@hey-api/codegen-core'; 2import type { IR } from '@hey-api/shared'; 3import { createSchemaProcessor, pathToJsonPointer } from '@hey-api/shared'; 4 5import { exportAst } from '../shared/export'; 6import type { ProcessorContext, ProcessorResult } from '../shared/processor'; 7import type { PluginState } from '../shared/types'; 8import type { PydanticPlugin } from '../types'; 9import { irSchemaToAst } from './plugin'; 10 11export function createProcessor(plugin: PydanticPlugin['Instance']): ProcessorResult { 12 const processor = createSchemaProcessor(); 13 14 const hooks = [plugin.config['~hooks']?.schemas, plugin.context.config.parser.hooks.schemas]; 15 16 function extractor(ctx: ProcessorContext): IR.SchemaObject { 17 if (processor.hasEmitted(ctx.path)) { 18 return ctx.schema; 19 } 20 21 for (const hook of hooks) { 22 const result = hook?.shouldExtract?.(ctx); 23 if (result) { 24 process({ 25 namingAnchor: processor.context.anchor, 26 tags: processor.context.tags, 27 ...ctx, 28 }); 29 return { $ref: pathToJsonPointer(ctx.path) }; 30 } 31 } 32 33 return ctx.schema; 34 } 35 36 function process(ctx: ProcessorContext): void { 37 if (!processor.markEmitted(ctx.path)) return; 38 39 processor.withContext({ anchor: ctx.namingAnchor, tags: ctx.tags }, () => { 40 const state = refs<PluginState>({ 41 hasLazyExpression: false, 42 path: ctx.path, 43 tags: ctx.tags, 44 }); 45 46 const ast = irSchemaToAst({ 47 plugin, 48 schema: ctx.schema, 49 schemaExtractor: extractor, 50 state, 51 }); 52 53 exportAst({ ...ctx, ast, plugin, state }); 54 }); 55 } 56 57 return { process }; 58}