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 #690 from Nick-Lucas/145-dates-conversion-fixes

authored by

Lubos and committed by
GitHub
032bd93a 4a2fb1f8

+147 -15
+5
.changeset/old-waves-taste.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + Fix an issue where transforms for endpoints with array returns were not generated correctly
+1
packages/openapi-ts/src/compiler/index.ts
··· 141 141 dateTransformMutation: transform.createDateTransformMutation, 142 142 mapArray: transform.createArrayMapTransform, 143 143 newDate: transform.createDateTransformerExpression, 144 + responseArrayTransform: transform.createResponseArrayTransform, 144 145 transformItem: transform.createFunctionTransformMutation, 145 146 transformMutationFunction: transform.createTransformMutationFunction, 146 147 },
+74
packages/openapi-ts/src/compiler/transform.ts
··· 258 258 ts.NodeFlags.Const, 259 259 ), 260 260 ); 261 + 262 + export const createResponseArrayTransform = ({ 263 + transform, 264 + name, 265 + }: { 266 + transform: string; 267 + name: string; 268 + }) => { 269 + const transformFunction = ts.factory.createArrowFunction( 270 + undefined, 271 + undefined, 272 + [ 273 + ts.factory.createParameterDeclaration( 274 + undefined, 275 + undefined, 276 + ts.factory.createIdentifier('data'), 277 + undefined, 278 + ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), 279 + undefined, 280 + ), 281 + ], 282 + undefined, 283 + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), 284 + ts.factory.createBlock( 285 + [ 286 + ts.factory.createIfStatement( 287 + ts.factory.createCallExpression( 288 + ts.factory.createPropertyAccessExpression( 289 + ts.factory.createIdentifier('Array'), 290 + ts.factory.createIdentifier('isArray'), 291 + ), 292 + undefined, 293 + [ts.factory.createIdentifier('data')], 294 + ), 295 + ts.factory.createBlock( 296 + [ 297 + ts.factory.createExpressionStatement( 298 + ts.factory.createCallExpression( 299 + ts.factory.createPropertyAccessExpression( 300 + ts.factory.createIdentifier('data'), 301 + ts.factory.createIdentifier('forEach'), 302 + ), 303 + undefined, 304 + [ts.factory.createIdentifier(transform)], 305 + ), 306 + ), 307 + ], 308 + true, 309 + ), 310 + undefined, 311 + ), 312 + ts.factory.createReturnStatement(ts.factory.createIdentifier('data')), 313 + ], 314 + true, 315 + ), 316 + ); 317 + 318 + const declaration = ts.factory.createVariableStatement( 319 + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], 320 + ts.factory.createVariableDeclarationList( 321 + [ 322 + ts.factory.createVariableDeclaration( 323 + ts.factory.createIdentifier(name), 324 + undefined, 325 + undefined, 326 + transformFunction, 327 + ), 328 + ], 329 + ts.NodeFlags.Const, 330 + ), 331 + ); 332 + 333 + return declaration; 334 + };
+19 -7
packages/openapi-ts/src/utils/write/types.ts
··· 366 366 367 367 if (config.types.dates === 'types+transform') { 368 368 if (responses.length === 1) { 369 - if (client.types[responses[0].type]?.hasTransformer) { 369 + const response = responses[0]; 370 + 371 + if (client.types[response.type]?.hasTransformer) { 370 372 const name = operationResponseTypeName(operation.name); 371 - const transformAlias = compiler.transform.alias({ 372 - existingName: responses[0].type, 373 - name, 374 - }); 375 373 376 - client.types[name].hasTransformer = true; 374 + if (response.export === 'array') { 375 + const arrayTransformer = 376 + compiler.transform.responseArrayTransform({ 377 + name, 378 + transform: response.type, 379 + }); 380 + onNode(arrayTransformer); 381 + } else { 382 + const transformAlias = compiler.transform.alias({ 383 + existingName: response.type, 384 + name, 385 + }); 377 386 378 - onNode(transformAlias); 387 + onNode(transformAlias); 388 + } 389 + 390 + client.types[name].hasTransformer = true; 379 391 } 380 392 } else if (responses.length > 1) { 381 393 console.log(
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular_transform/types.gen.ts.snap
··· 66 66 67 67 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 68 68 69 - export const ModelWithDatesArrayResponse = ModelWithDates; 69 + export const ModelWithDatesArrayResponse = (data: any) => { 70 + if (Array.isArray(data)) { 71 + data.forEach(ModelWithDates); 72 + } 73 + return data; 74 + }; 70 75 71 76 export type ArrayOfDatesResponse = Array<(Date)>; 72 77
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_axios_transform/types.gen.ts.snap
··· 66 66 67 67 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 68 68 69 - export const ModelWithDatesArrayResponse = ModelWithDates; 69 + export const ModelWithDatesArrayResponse = (data: any) => { 70 + if (Array.isArray(data)) { 71 + data.forEach(ModelWithDates); 72 + } 73 + return data; 74 + }; 70 75 71 76 export type ArrayOfDatesResponse = Array<(Date)>; 72 77
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_client_transform/types.gen.ts.snap
··· 66 66 67 67 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 68 68 69 - export const ModelWithDatesArrayResponse = ModelWithDates; 69 + export const ModelWithDatesArrayResponse = (data: any) => { 70 + if (Array.isArray(data)) { 71 + data.forEach(ModelWithDates); 72 + } 73 + return data; 74 + }; 70 75 71 76 export type ArrayOfDatesResponse = Array<(Date)>; 72 77
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-axios_transform/types.gen.ts.snap
··· 68 68 69 69 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 70 70 71 - export const ModelWithDatesArrayResponse = ModelWithDates; 71 + export const ModelWithDatesArrayResponse = (data: any) => { 72 + if (Array.isArray(data)) { 73 + data.forEach(ModelWithDates); 74 + } 75 + return data; 76 + }; 72 77 73 78 export type ModelWithDatesArrayError = unknown; 74 79
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_hey-api_client-fetch_transform/types.gen.ts.snap
··· 68 68 69 69 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 70 70 71 - export const ModelWithDatesArrayResponse = ModelWithDates; 71 + export const ModelWithDatesArrayResponse = (data: any) => { 72 + if (Array.isArray(data)) { 73 + data.forEach(ModelWithDates); 74 + } 75 + return data; 76 + }; 72 77 73 78 export type ModelWithDatesArrayError = unknown; 74 79
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_node_transform/types.gen.ts.snap
··· 66 66 67 67 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 68 68 69 - export const ModelWithDatesArrayResponse = ModelWithDates; 69 + export const ModelWithDatesArrayResponse = (data: any) => { 70 + if (Array.isArray(data)) { 71 + data.forEach(ModelWithDates); 72 + } 73 + return data; 74 + }; 70 75 71 76 export type ArrayOfDatesResponse = Array<(Date)>; 72 77
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_transform/types.gen.ts.snap
··· 66 66 67 67 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 68 68 69 - export const ModelWithDatesArrayResponse = ModelWithDates; 69 + export const ModelWithDatesArrayResponse = (data: any) => { 70 + if (Array.isArray(data)) { 71 + data.forEach(ModelWithDates); 72 + } 73 + return data; 74 + }; 70 75 71 76 export type ArrayOfDatesResponse = Array<(Date)>; 72 77
+6 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_xhr_transform/types.gen.ts.snap
··· 66 66 67 67 export type ModelWithDatesArrayResponse = Array<ModelWithDates>; 68 68 69 - export const ModelWithDatesArrayResponse = ModelWithDates; 69 + export const ModelWithDatesArrayResponse = (data: any) => { 70 + if (Array.isArray(data)) { 71 + data.forEach(ModelWithDates); 72 + } 73 + return data; 74 + }; 70 75 71 76 export type ArrayOfDatesResponse = Array<(Date)>; 72 77