Skip to content

Commit

Permalink
Merge pull request #690 from Nick-Lucas/145-dates-conversion-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos authored Jun 18, 2024
2 parents 4a2fb1f + 1017ace commit 032bd93
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-waves-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

Fix an issue where transforms for endpoints with array returns were not generated correctly
1 change: 1 addition & 0 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const compiler = {
dateTransformMutation: transform.createDateTransformMutation,
mapArray: transform.createArrayMapTransform,
newDate: transform.createDateTransformerExpression,
responseArrayTransform: transform.createResponseArrayTransform,
transformItem: transform.createFunctionTransformMutation,
transformMutationFunction: transform.createTransformMutationFunction,
},
Expand Down
74 changes: 74 additions & 0 deletions packages/openapi-ts/src/compiler/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,77 @@ export const createAlias = ({
ts.NodeFlags.Const,
),
);

export const createResponseArrayTransform = ({
transform,
name,
}: {
transform: string;
name: string;
}) => {
const transformFunction = ts.factory.createArrowFunction(
undefined,
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier('data'),
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
undefined,
),
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(
[
ts.factory.createIfStatement(
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier('Array'),
ts.factory.createIdentifier('isArray'),
),
undefined,
[ts.factory.createIdentifier('data')],
),
ts.factory.createBlock(
[
ts.factory.createExpressionStatement(
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier('data'),
ts.factory.createIdentifier('forEach'),
),
undefined,
[ts.factory.createIdentifier(transform)],
),
),
],
true,
),
undefined,
),
ts.factory.createReturnStatement(ts.factory.createIdentifier('data')),
],
true,
),
);

const declaration = ts.factory.createVariableStatement(
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier(name),
undefined,
undefined,
transformFunction,
),
],
ts.NodeFlags.Const,
),
);

return declaration;
};
26 changes: 19 additions & 7 deletions packages/openapi-ts/src/utils/write/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,28 @@ const processServiceTypes = (client: Client, onNode: OnNode) => {

if (config.types.dates === 'types+transform') {
if (responses.length === 1) {
if (client.types[responses[0].type]?.hasTransformer) {
const response = responses[0];

if (client.types[response.type]?.hasTransformer) {
const name = operationResponseTypeName(operation.name);
const transformAlias = compiler.transform.alias({
existingName: responses[0].type,
name,
});

client.types[name].hasTransformer = true;
if (response.export === 'array') {
const arrayTransformer =
compiler.transform.responseArrayTransform({
name,
transform: response.type,
});
onNode(arrayTransformer);
} else {
const transformAlias = compiler.transform.alias({
existingName: response.type,
name,
});

onNode(transformAlias);
}

onNode(transformAlias);
client.types[name].hasTransformer = true;
}
} else if (responses.length > 1) {
console.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const ModelWithDatesResponse = ModelWithDates;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ArrayOfDatesResponse = Array<(Date)>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const ModelWithDatesResponse = ModelWithDates;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ArrayOfDatesResponse = Array<(Date)>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const ModelWithDatesResponse = ModelWithDates;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ArrayOfDatesResponse = Array<(Date)>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export type ModelWithDatesError = unknown;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ModelWithDatesArrayError = unknown;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export type ModelWithDatesError = unknown;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ModelWithDatesArrayError = unknown;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const ModelWithDatesResponse = ModelWithDates;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ArrayOfDatesResponse = Array<(Date)>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const ModelWithDatesResponse = ModelWithDates;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ArrayOfDatesResponse = Array<(Date)>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const ModelWithDatesResponse = ModelWithDates;

export type ModelWithDatesArrayResponse = Array<ModelWithDates>;

export const ModelWithDatesArrayResponse = ModelWithDates;
export const ModelWithDatesArrayResponse = (data: any) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDates);
}
return data;
};

export type ArrayOfDatesResponse = Array<(Date)>;

Expand Down

0 comments on commit 032bd93

Please sign in to comment.