Skip to content

custom transformers #2281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2025
Merged

custom transformers #2281

merged 2 commits into from
Jul 7, 2025

Conversation

j-ibarra
Copy link
Contributor

@j-ibarra j-ibarra commented Jul 5, 2025

config

{
	name: "@hey-api/transformers",
	dates: true,
	bigInt: false,
	transformers: [decimalExpressions],
}

custom transformer

import { compiler, type expressionTransformer } from "@hey-api/openapi-ts";

export const decimalExpressions: expressionTransformer = ({
	dataExpression,
	schema,
	file,
}) => {
	if (schema.type !== "string" || schema.format !== "decimal") {
		return undefined;
	}

	const decimalCallExpression =
		dataExpression !== undefined
			? compiler.newExpression({
					argumentsArray: [
						typeof dataExpression === "string"
							? compiler.identifier({ text: dataExpression })
							: dataExpression,
					],
					expression: compiler.identifier({ text: "Decimal" }),
				})
			: undefined;

	if (decimalCallExpression) {
		if (typeof dataExpression === "string") {
			return [decimalCallExpression];
		}

		file.import({
			module: "decimal.js",
			name: "Decimal",
		});

		if (dataExpression) {
			return [
				compiler.assignment({
					left: dataExpression,
					right: decimalCallExpression,
				}),
			];
		}
	}

	return [];
};

output

import { Decimal } from "decimal.js";
import type { myResponse } from "./types.gen";

const myResponseSchemaResponseTransformer = (data: any) => {
	if (data.amount) {
		data.amount = new Decimal(data.amount);
	}
	if (data.createdAt) {
		data.createdAt = new Date(data.createdAt);
	}
	return data;
};

export const myResponseTransformer = async (
	data: any,
): Promise<myResponse> => {
	data = myResponseSchemaResponseTransformer(data);
	return data;
};

Limitation

In types.gen.ts you need to change the string type to the correct type (in this example Decimal) as the Date type

export type myResponse = {
	/**
	 * The amount to withdraw
	 */
	amount?: string;
	/**
	 * The time the preview was created
	 */
	createdAt?: Date;
};

Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

changeset-bot bot commented Jul 5, 2025

🦋 Changeset detected

Latest commit: 565830b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hey-api/openapi-ts Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jul 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hey-api-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 7, 2025 1:17am

Copy link

pkg-pr-new bot commented Jul 5, 2025

Open in StackBlitz

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@2281
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@2281
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@2281

commit: 565830b

Copy link

codecov bot commented Jul 5, 2025

Codecov Report

Attention: Patch coverage is 6.31579% with 89 lines in your changes missing coverage. Please review.

Project coverage is 24.01%. Comparing base (9cf1a08) to head (565830b).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
.../@hey-api/transformers/expressions-transformers.ts 4.34% 66 Missing ⚠️
...api-ts/src/plugins/@hey-api/transformers/plugin.ts 4.16% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2281      +/-   ##
==========================================
- Coverage   24.02%   24.01%   -0.01%     
==========================================
  Files         313      314       +1     
  Lines       28927    28949      +22     
  Branches     1227     1227              
==========================================
+ Hits         6949     6953       +4     
- Misses      21969    21987      +18     
  Partials        9        9              
Flag Coverage Δ
unittests 24.01% <6.31%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mrlubos mrlubos merged commit 53363b3 into hey-api:main Jul 7, 2025
11 of 12 checks passed
@github-actions github-actions bot mentioned this pull request Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants