Skip to content

Commit 8b58945

Browse files
authored
Merge pull request #3530 from hey-api/refactor/plugin-typescript
refactor: typescript plugin
2 parents 2e13802 + 476546c commit 8b58945

39 files changed

Lines changed: 1033 additions & 849 deletions

File tree

dev/typescript/presets.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ export const presets = {
1010
zod({ metadata: true }),
1111
tanstackReactQuery({ queryKeys: { tags: true } }),
1212
],
13-
minimal: () => [
14-
/** Just types, nothing else */
15-
typescript(),
16-
],
1713
sdk: () => [
1814
/** SDK with types */
1915
typescript(),
@@ -31,6 +27,10 @@ export const presets = {
3127
sdk(),
3228
tanstackReactQuery({ queryKeys: { tags: true } }),
3329
],
30+
types: () => [
31+
/** Just types, nothing else */
32+
typescript(),
33+
],
3434
validated: () => [
3535
/** SDK + Zod validation */
3636
typescript(),

packages/openapi-python/src/plugins/pydantic/shared/meta.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export function defaultMeta(schema: IR.SchemaObject): PydanticMeta {
1717
/**
1818
* Composes metadata from child results.
1919
*
20-
* Automatically propagates hasForwardReference, nullable, readonly from children.
21-
*
2220
* @param children - Results from walking child schemas
2321
* @param overrides - Explicit overrides (e.g., from parent schema)
2422
*/
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import type {
2-
IR,
3-
NamingConfig,
4-
SchemaProcessorContext,
5-
SchemaProcessorResult,
6-
} from '@hey-api/shared';
1+
import type { IR, NamingConfig, SchemaProcessorContext } from '@hey-api/shared';
72

83
import type { PydanticPlugin } from '../types';
4+
import type { PydanticFinal } from './types';
95

106
export type ProcessorContext = SchemaProcessorContext & {
7+
/** Whether to export the result (default: true) */
8+
export?: boolean;
119
naming: NamingConfig;
1210
/** The plugin instance. */
1311
plugin: PydanticPlugin['Instance'];
1412
schema: IR.SchemaObject;
1513
};
1614

17-
export type ProcessorResult = SchemaProcessorResult<ProcessorContext>;
15+
export type ProcessorResult = {
16+
process: (ctx: ProcessorContext) => PydanticFinal | void;
17+
};

packages/openapi-python/src/plugins/pydantic/v2/processor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ export function createProcessor(plugin: PydanticPlugin['Instance']): ProcessorRe
4242
return ctx.schema;
4343
}
4444

45-
function process(ctx: ProcessorContext): void {
45+
function process(ctx: ProcessorContext): PydanticFinal | void {
4646
if (!processor.markEmitted(ctx.path)) return;
4747

48-
processor.withContext({ anchor: ctx.namingAnchor, tags: ctx.tags }, () => {
48+
const shouldExport = ctx.export !== false;
49+
50+
return processor.withContext({ anchor: ctx.namingAnchor, tags: ctx.tags }, () => {
4951
const visitor = createVisitor({ schemaExtractor: extractor });
5052
const walk = createSchemaWalker(visitor);
5153

@@ -59,7 +61,12 @@ export function createProcessor(plugin: PydanticPlugin['Instance']): ProcessorRe
5961
plugin,
6062
}) as PydanticFinal;
6163

62-
exportAst({ ...ctx, final, plugin });
64+
if (shouldExport) {
65+
exportAst({ ...ctx, final, plugin });
66+
return;
67+
}
68+
69+
return final;
6370
});
6471
}
6572

packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { SymbolMeta } from '@hey-api/codegen-core';
2-
import { refs } from '@hey-api/codegen-core';
32
import type { IR } from '@hey-api/shared';
43
import { statusCodeToGroup } from '@hey-api/shared';
54

@@ -108,15 +107,9 @@ export function operationParameters({
108107
isParametersRequired = true;
109108
}
110109
flatParams.prop(parameter.name, (p) =>
111-
p.required(parameter.isRequired).type(
112-
pluginTypeScript.api.schemaToType({
113-
plugin: pluginTypeScript,
114-
schema: parameter.schema,
115-
state: refs({
116-
path: [],
117-
}),
118-
}),
119-
),
110+
p
111+
.required(parameter.isRequired)
112+
.type(pluginTypeScript.api.schemaToType(pluginTypeScript, parameter.schema)),
120113
);
121114
}
122115

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1-
import type { MaybeTsDsl, TypeTsDsl } from '../../../ts-dsl';
2-
import { irSchemaToAstV1 } from './v1/api';
1+
import type { IR } from '@hey-api/shared';
2+
3+
import { $ } from '../../../ts-dsl';
4+
import type { TypeScriptResult } from './shared/types';
5+
import type { HeyApiTypeScriptPlugin } from './types';
6+
import { createProcessor } from './v1/processor';
37

48
export type IApi = {
5-
schemaToType: (args: Parameters<typeof irSchemaToAstV1>[0]) => MaybeTsDsl<TypeTsDsl>;
9+
schemaToType: (
10+
plugin: HeyApiTypeScriptPlugin['Instance'],
11+
schema: IR.SchemaObject,
12+
) => TypeScriptResult['type'];
613
};
714

815
export class Api implements IApi {
9-
schemaToType(args: Parameters<typeof irSchemaToAstV1>[0]): MaybeTsDsl<TypeTsDsl> {
10-
return irSchemaToAstV1(args);
16+
schemaToType(
17+
plugin: HeyApiTypeScriptPlugin['Instance'],
18+
schema: IR.SchemaObject,
19+
): TypeScriptResult['type'] {
20+
const processor = createProcessor(plugin);
21+
const result = processor.process({
22+
export: false,
23+
meta: {
24+
resource: 'definition',
25+
resourceId: '',
26+
},
27+
naming: plugin.config.definitions,
28+
path: [],
29+
plugin,
30+
schema,
31+
});
32+
33+
if (!result) {
34+
return $.type(plugin.config.topType);
35+
}
36+
return result.type;
1137
}
1238
}

0 commit comments

Comments
 (0)