Skip to content

Commit 074e8ab

Browse files
committed
TypeTransformer
1 parent 33665a5 commit 074e8ab

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

packages/openapi-ts/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export {
137137
export { clientPluginHandler } from './plugins/@hey-api/client-core/plugin';
138138
export type { Client } from './plugins/@hey-api/client-core/types';
139139
export type { ExpressionTransformer } from './plugins/@hey-api/transformers/expressions';
140+
export type { TypeTransformer } from './plugins/@hey-api/transformers/types';
140141
export { definePluginConfig } from './plugins/shared/utils/config';
141142
export type { DefinePlugin, Plugin } from './plugins/types';
142143
export { compiler, tsc } from './tsc';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { defaultConfig, defineConfig } from './config';
2-
export type { HeyApiTransformersPlugin } from './types';
2+
export type { HeyApiTransformersPlugin, TypeTransformer } from './types';

packages/openapi-ts/src/plugins/@hey-api/transformers/types.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
import type ts from 'typescript';
2+
3+
import type { GeneratedFile } from '../../../generate/file';
4+
import type { IR } from '../../../ir/types';
15
import type { DefinePlugin, Plugin } from '../../types';
26
import type { ExpressionTransformer } from './expressions';
37

8+
/**
9+
* Returns the TypeScript type node for a schema with a specific format.
10+
* If undefined is returned, the default type will be used.
11+
*/
12+
export type TypeTransformer = ({
13+
file,
14+
schema,
15+
}: {
16+
file: GeneratedFile;
17+
schema: IR.SchemaObject;
18+
}) => ts.TypeNode | undefined;
19+
420
export type UserConfig = Plugin.Name<'@hey-api/transformers'> & {
521
/**
622
* Convert long integers into BigInt values?
@@ -31,6 +47,11 @@ export type UserConfig = Plugin.Name<'@hey-api/transformers'> & {
3147
* Custom transforms to apply to the generated code.
3248
*/
3349
transformers?: ReadonlyArray<ExpressionTransformer>;
50+
51+
/**
52+
* Custom type transformers that modify the TypeScript types generated.
53+
*/
54+
typeTransformers?: ReadonlyArray<TypeTransformer>;
3455
};
3556

3657
export type HeyApiTransformersPlugin = DefinePlugin<UserConfig>;

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ const schemaTypeToIdentifier = ({
401401
schema: IR.SchemaObject;
402402
state: PluginState;
403403
}): ts.TypeNode => {
404+
const transformersPlugin = plugin.getPlugin('@hey-api/transformers');
405+
if (transformersPlugin?.config.typeTransformers) {
406+
for (const typeTransformer of transformersPlugin.config.typeTransformers) {
407+
const file = plugin.context.file({ id: typesId })!;
408+
const typeNode = typeTransformer({ file, schema });
409+
if (typeNode) {
410+
return typeNode;
411+
}
412+
}
413+
}
414+
404415
switch (schema.type as Required<IR.SchemaObject>['type']) {
405416
case 'array':
406417
return arrayTypeToIdentifier({

0 commit comments

Comments
 (0)