Skip to content
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

[FeatureRequest] transformer for a custom format #1262

Open
mrclrchtr opened this issue Nov 9, 2024 · 2 comments
Open

[FeatureRequest] transformer for a custom format #1262

mrclrchtr opened this issue Nov 9, 2024 · 2 comments
Labels
feature 🚀 New feature or request RSVP 👍👎 Explicit request for reactions to gauge interest in resolving this issue

Comments

@mrclrchtr
Copy link

I would like to define a transformer for a custom format and this should be applied when a string has this format:

e.g.

export class LocalTime {
  hour: number;
  minute: number;

  constructor(hour: number, minute: number) {
    this.hour = hour;
    this.minute = minute;
  }
}
      time:
          type: string
          format: time

then I would like to be able to parse this string into LocalTime.

Maybe, I could create my own plugin for this?

If this doesn't fit here, I'll open a separate topic or issue.

Originally posted by @mrclrchtr in #1039 (comment)

@mrlubos mrlubos added feature 🚀 New feature or request RSVP 👍👎 Explicit request for reactions to gauge interest in resolving this issue labels Nov 9, 2024
@mrlubos
Copy link
Member

mrlubos commented Nov 9, 2024

You'd also need to be able to transform the generated types, right? What would the appropriate type be in your example?

@mrclrchtr
Copy link
Author

mrclrchtr commented Nov 9, 2024

Unfortunately, I don't quite understand the question.

I'll try to explain my idea again:

  • In the OpenAPI spec, I say that field has type: string and e.g. format: localTime (it's a custom format).
  • In my WebApp I have defined the class LocalTime myself:
export class LocalTime {
  hour: number;
  minute: number;

  constructor(hour: string, minute: string) {
    this.hour = Number.parseInt(hour, 10);
    this.minute = Number.parseInt(minute, 10);
  }
}
  • I tell HeyAPI that if a string has the format localTime, that it should use a special transformer to set the field in the generated types:

proposal:

{
	name: '@hey-api/transformers',
	custom: {
		type: string,
		format: localTime,
		parser: {
			regex: "/^([01]?\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/", # For demo only
		},
		target: {
			pathToCass: "/calendar/LocalTime", # to be used for the import
			constructor: ["$1", "$2"]
		}
	}
},

then the parser would know how to parse the string and the transformer would know which elemento to set where in the constructor.

I just thought of that quickly... there is certainly a better API for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request RSVP 👍👎 Explicit request for reactions to gauge interest in resolving this issue
Projects
None yet
Development

No branches or pull requests

2 participants