fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2281 from j-ibarra/main

custom transformers

authored by

Lubos and committed by
GitHub
53363b3e 9cf1a08f

+139 -85
+5
.changeset/giant-carrots-change.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix(transformers): add `transformers` option allowing passing custom transform functions
+2
packages/openapi-ts/src/index.ts
··· 105 105 export const defineConfig = async (config: Configs): Promise<UserConfig> => 106 106 typeof config === 'function' ? await config() : config; 107 107 108 + export { compiler } from './compiler'; 108 109 export { defaultPaginationKeywords } from './config/parser'; 109 110 export { defaultPlugins } from './config/plugins'; 110 111 export type { IR } from './ir/types'; ··· 123 124 } from './plugins/@hey-api/client-core/config'; 124 125 export { clientPluginHandler } from './plugins/@hey-api/client-core/plugin'; 125 126 export type { Client } from './plugins/@hey-api/client-core/types'; 127 + export type { expressionTransformer } from './plugins/@hey-api/transformers'; 126 128 export { definePluginConfig } from './plugins/shared/utils/config'; 127 129 export type { DefinePlugin, Plugin } from './plugins/types'; 128 130 export type { UserConfig } from './types/config';
+96
packages/openapi-ts/src/plugins/@hey-api/transformers/expressions-transformers.ts
··· 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 + };
+2
packages/openapi-ts/src/plugins/@hey-api/transformers/index.ts
··· 1 + export { compiler } from '../../../compiler'; 1 2 export { defaultConfig, defineConfig } from './config'; 3 + export { type expressionTransformer } from './expressions-transformers'; 2 4 export type { HeyApiTransformersPlugin } from './types';
+27 -85
packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts
··· 10 10 import { stringCase } from '../../../utils/stringCase'; 11 11 import { operationIrRef } from '../../shared/utils/ref'; 12 12 import { typesId } from '../typescript/ref'; 13 + import { bigIntExpressions, dateExpressions } from './expressions-transformers'; 13 14 import type { HeyApiTransformersPlugin } from './types'; 14 15 15 16 interface OperationIRRef { ··· 18 19 */ 19 20 id: string; 20 21 } 21 - 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 22 91 23 export const operationTransformerIrRef = ({ 92 24 id, ··· 370 302 return nodes; 371 303 } 372 304 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 - 389 305 if (schema.items) { 390 306 if (schema.items.length === 1) { 391 307 return processSchemaType({ ··· 449 365 } 450 366 } 451 367 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 + 452 380 return []; 453 381 }; 454 382 ··· 458 386 id: transformersId, 459 387 path: plugin.output, 460 388 }); 389 + 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 + } 461 403 462 404 plugin.forEach('operation', ({ operation }) => { 463 405 const { response } = operationResponsesMap(operation);
+7
packages/openapi-ts/src/plugins/@hey-api/transformers/types.d.ts
··· 26 26 * @default 'transformers' 27 27 */ 28 28 output?: string; 29 + 30 + /** 31 + * transformers to apply to the generated code 32 + * @default [] 33 + */ 34 + 35 + transformers?: expressionTransformer[]; 29 36 }; 30 37 31 38 export type HeyApiTransformersPlugin = DefinePlugin<Config>;