Skip to content

Commit 70c5706

Browse files
committed
custom transformers
1 parent 9cf1a08 commit 70c5706

File tree

5 files changed

+134
-85
lines changed

5 files changed

+134
-85
lines changed

packages/openapi-ts/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export const createClient = async (
105105
export const defineConfig = async (config: Configs): Promise<UserConfig> =>
106106
typeof config === 'function' ? await config() : config;
107107

108+
export { compiler } from './compiler';
108109
export { defaultPaginationKeywords } from './config/parser';
109110
export { defaultPlugins } from './config/plugins';
110111
export type { IR } from './ir/types';
@@ -123,6 +124,7 @@ export {
123124
} from './plugins/@hey-api/client-core/config';
124125
export { clientPluginHandler } from './plugins/@hey-api/client-core/plugin';
125126
export type { Client } from './plugins/@hey-api/client-core/types';
127+
export type { expressionTransformer } from './plugins/@hey-api/transformers';
126128
export { definePluginConfig } from './plugins/shared/utils/config';
127129
export type { DefinePlugin, Plugin } from './plugins/types';
128130
export type { UserConfig } from './types/config';
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import type ts from 'typescript';
2+
3+
import { compiler } from '../../../compiler';
4+
import type { TypeScriptFile } from '../../../generate/files';
5+
import type { IR } from '../../../ir/types';
6+
import type { Config } from './types';
7+
8+
export type expressionTransformer = ({
9+
config,
10+
dataExpression,
11+
file,
12+
schema,
13+
}: {
14+
config: Config;
15+
dataExpression?: ts.Expression | string;
16+
file: TypeScriptFile;
17+
schema: IR.SchemaObject;
18+
}) => Array<ts.Expression> | undefined;
19+
20+
export const bigIntExpressions: expressionTransformer = ({
21+
dataExpression,
22+
schema,
23+
}) => {
24+
if (schema.type !== 'integer' || schema.format !== 'int64') {
25+
return undefined;
26+
}
27+
28+
const bigIntCallExpression =
29+
dataExpression !== undefined
30+
? compiler.callExpression({
31+
functionName: 'BigInt',
32+
parameters: [
33+
compiler.callExpression({
34+
functionName: compiler.propertyAccessExpression({
35+
expression: dataExpression,
36+
name: 'toString',
37+
}),
38+
}),
39+
],
40+
})
41+
: undefined;
42+
43+
if (bigIntCallExpression) {
44+
if (typeof dataExpression === 'string') {
45+
return [bigIntCallExpression];
46+
}
47+
48+
if (dataExpression) {
49+
return [
50+
compiler.assignment({
51+
left: dataExpression,
52+
right: bigIntCallExpression,
53+
}),
54+
];
55+
}
56+
}
57+
58+
return [];
59+
};
60+
61+
export const dateExpressions: expressionTransformer = ({
62+
dataExpression,
63+
schema,
64+
}) => {
65+
if (
66+
schema.type !== 'string' ||
67+
!(schema.format === 'date' || schema.format === 'date-time')
68+
) {
69+
return undefined;
70+
}
71+
72+
const identifierDate = compiler.identifier({ text: 'Date' });
73+
74+
if (typeof dataExpression === 'string') {
75+
return [
76+
compiler.newExpression({
77+
argumentsArray: [compiler.identifier({ text: dataExpression })],
78+
expression: identifierDate,
79+
}),
80+
];
81+
}
82+
83+
if (dataExpression) {
84+
return [
85+
compiler.assignment({
86+
left: dataExpression,
87+
right: compiler.newExpression({
88+
argumentsArray: [dataExpression],
89+
expression: identifierDate,
90+
}),
91+
}),
92+
];
93+
}
94+
95+
return [];
96+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
export { compiler } from '../../../compiler';
12
export { defaultConfig, defineConfig } from './config';
3+
export { type expressionTransformer } from './expressions-transformers';
24
export type { HeyApiTransformersPlugin } from './types';

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

Lines changed: 27 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { irRef } from '../../../utils/ref';
1010
import { stringCase } from '../../../utils/stringCase';
1111
import { operationIrRef } from '../../shared/utils/ref';
1212
import { typesId } from '../typescript/ref';
13+
import { bigIntExpressions, dateExpressions } from './expressions-transformers';
1314
import type { HeyApiTransformersPlugin } from './types';
1415

1516
interface OperationIRRef {
@@ -19,75 +20,6 @@ interface OperationIRRef {
1920
id: string;
2021
}
2122

22-
const bigIntExpressions = ({
23-
dataExpression,
24-
}: {
25-
dataExpression?: ts.Expression | string;
26-
}): Array<ts.Expression> => {
27-
const bigIntCallExpression =
28-
dataExpression !== undefined
29-
? compiler.callExpression({
30-
functionName: 'BigInt',
31-
parameters: [
32-
compiler.callExpression({
33-
functionName: compiler.propertyAccessExpression({
34-
expression: dataExpression,
35-
name: 'toString',
36-
}),
37-
}),
38-
],
39-
})
40-
: undefined;
41-
42-
if (bigIntCallExpression) {
43-
if (typeof dataExpression === 'string') {
44-
return [bigIntCallExpression];
45-
}
46-
47-
if (dataExpression) {
48-
return [
49-
compiler.assignment({
50-
left: dataExpression,
51-
right: bigIntCallExpression,
52-
}),
53-
];
54-
}
55-
}
56-
57-
return [];
58-
};
59-
60-
const dateExpressions = ({
61-
dataExpression,
62-
}: {
63-
dataExpression?: ts.Expression | string;
64-
}): Array<ts.Expression> => {
65-
const identifierDate = compiler.identifier({ text: 'Date' });
66-
67-
if (typeof dataExpression === 'string') {
68-
return [
69-
compiler.newExpression({
70-
argumentsArray: [compiler.identifier({ text: dataExpression })],
71-
expression: identifierDate,
72-
}),
73-
];
74-
}
75-
76-
if (dataExpression) {
77-
return [
78-
compiler.assignment({
79-
left: dataExpression,
80-
right: compiler.newExpression({
81-
argumentsArray: [dataExpression],
82-
expression: identifierDate,
83-
}),
84-
}),
85-
];
86-
}
87-
88-
return [];
89-
};
90-
9123
export const operationTransformerIrRef = ({
9224
id,
9325
type,
@@ -370,22 +302,6 @@ const processSchemaType = ({
370302
return nodes;
371303
}
372304

373-
if (
374-
plugin.config.dates &&
375-
schema.type === 'string' &&
376-
(schema.format === 'date' || schema.format === 'date-time')
377-
) {
378-
return dateExpressions({ dataExpression });
379-
}
380-
381-
if (
382-
plugin.config.bigInt &&
383-
schema.type === 'integer' &&
384-
schema.format === 'int64'
385-
) {
386-
return bigIntExpressions({ dataExpression });
387-
}
388-
389305
if (schema.items) {
390306
if (schema.items.length === 1) {
391307
return processSchemaType({
@@ -449,6 +365,18 @@ const processSchemaType = ({
449365
}
450366
}
451367

368+
for (const transformer of plugin.config.transformers ?? []) {
369+
const t = transformer({
370+
config: plugin.config,
371+
dataExpression,
372+
file,
373+
schema,
374+
});
375+
if (t) {
376+
return t;
377+
}
378+
}
379+
452380
return [];
453381
};
454382

@@ -459,6 +387,20 @@ export const handler: HeyApiTransformersPlugin['Handler'] = ({ plugin }) => {
459387
path: plugin.output,
460388
});
461389

390+
if (plugin.config.dates) {
391+
plugin.config.transformers = [
392+
...(plugin.config.transformers ?? []),
393+
dateExpressions,
394+
];
395+
}
396+
397+
if (plugin.config.bigInt) {
398+
plugin.config.transformers = [
399+
...(plugin.config.transformers ?? []),
400+
bigIntExpressions,
401+
];
402+
}
403+
462404
plugin.forEach('operation', ({ operation }) => {
463405
const { response } = operationResponsesMap(operation);
464406

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export type Config = Plugin.Name<'@hey-api/transformers'> & {
2626
* @default 'transformers'
2727
*/
2828
output?: string;
29+
30+
/**
31+
* transformers to apply to the generated code
32+
* @default []
33+
*/
34+
35+
transformers?: expressionTransformer[];
2936
};
3037

3138
export type HeyApiTransformersPlugin = DefinePlugin<Config>;

0 commit comments

Comments
 (0)