From 2e9286f641d6ab8ac694eb304712c918494d15dd Mon Sep 17 00:00:00 2001 From: Nick Lucas Date: Thu, 13 Jun 2024 12:27:19 +0100 Subject: [PATCH] Rewrite array transform to utilise existing transform --- packages/openapi-ts/src/compiler/transform.ts | 63 ++++++++++++------- packages/openapi-ts/src/utils/write/types.ts | 29 ++++----- 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/packages/openapi-ts/src/compiler/transform.ts b/packages/openapi-ts/src/compiler/transform.ts index c1e27b06b..53a17bf1d 100644 --- a/packages/openapi-ts/src/compiler/transform.ts +++ b/packages/openapi-ts/src/compiler/transform.ts @@ -50,10 +50,12 @@ export const createDateTransformMutation = ({ export const createArrayTransformMutation = ({ path, - statements, + transformer, + // statements, }: { path: string[]; - statements: ts.Statement[]; + transformer: string; + // statements: ts.Statement[]; }): ts.Statement => { const safeAccessExpression = path .slice(1) @@ -82,32 +84,47 @@ export const createArrayTransformMutation = ({ ts.factory.createCallChain( ts.factory.createPropertyAccessChain( safeAccessExpression, - undefined, + ts.factory.createToken(ts.SyntaxKind.QuestionDotToken), ts.factory.createIdentifier('forEach'), ), + ts.factory.createToken(ts.SyntaxKind.QuestionDotToken), undefined, - undefined, - [ - ts.factory.createArrowFunction( - undefined, - undefined, - [ - ts.factory.createParameterDeclaration( - undefined, - undefined, - ts.factory.createIdentifier('item'), - undefined, - undefined, - undefined, - ), - ], - undefined, - ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - ts.factory.createBlock(statements, true), - ), - ], + [ts.factory.createIdentifier(transformer)], ), ), + + // TODO: might need this but clean up otherwise + // ts.factory.createExpressionStatement( + // ts.factory.createCallChain( + // ts.factory.createPropertyAccessChain( + // safeAccessExpression, + // undefined, + // ts.factory.createIdentifier('forEach'), + // ), + // undefined, + // [ts.factory.createIdentifier(transformer)] + // // undefined, + // // [ + // // ts.factory.createArrowFunction( + // // undefined, + // // undefined, + // // [ + // // ts.factory.createParameterDeclaration( + // // undefined, + // // undefined, + // // ts.factory.createIdentifier('item'), + // // undefined, + // // undefined, + // // undefined, + // // ), + // // ], + // // undefined, + // // ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + // // ts.factory.createBlock(statements, true), + // // ), + // // ], + // ), + // ), ], true, ), diff --git a/packages/openapi-ts/src/utils/write/types.ts b/packages/openapi-ts/src/utils/write/types.ts index f29b2589d..3e91ca95f 100644 --- a/packages/openapi-ts/src/utils/write/types.ts +++ b/packages/openapi-ts/src/utils/write/types.ts @@ -193,12 +193,17 @@ const processType = (client: Client, model: Model, onNode: OnNode) => { const generateTransform = (client: Client, model: Model, onNode: OnNode) => { const config = getConfig(); if (config.types.dates === 'types+transform') { - const mapTypeToTransformStatements = ( - localPath: string[], + if (model.meta?.hasTransformer) { + // Transform already created (maybe the model is used in other models) so we just bail here + return; + } + + const generateForProperty = ( + rootPath: string[], property: Model, isArrayElement?: boolean, ) => { - const path = [...localPath, property.name]; + const path = [...rootPath, property.name]; if ( property.type === 'string' && @@ -214,7 +219,7 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => { return []; } else { // otherwise we recurse in case it's an object/array, and if it's not that will just bail with [] - return generateTransformStatements(path, property); + return generateForModel(path, property); } }; @@ -235,16 +240,12 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => { ); } - const statements = mapTypeToTransformStatements(['item'], nextModel); - - if (statements.length === 0) { - return []; - } + generateTransform(client, nextModel, onNode); return [ compiler.transform.arrayTransformMutation({ path: localPath, - statements, + transformer: nextModel.meta!.name, }), ]; } @@ -252,14 +253,14 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => { throw new Error('Unsupported array type'); } - function generateTransformStatements( + function generateForModel( localPath: string[], localModel: Model, ): ts.Statement[] { switch (localModel.export) { case 'interface': return localModel.properties.flatMap((property) => - mapTypeToTransformStatements(localPath, property), + generateForProperty(localPath, property), ); case 'array': return generateForArray(localPath, localModel); @@ -270,11 +271,11 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => { } } - const transforms = generateTransformStatements(['data'], model); + const transforms = generateForModel(['data'], model); if (transforms.length > 0) { const transformFunction = compiler.transform.transformMutationFunction({ modelName: model.name, - statements: generateTransformStatements(['data'], model), + statements: generateForModel(['data'], model), }); client.types[model.meta!.name].hasTransformer = true;