Replies: 3 comments 7 replies
-
Bump. Any thoughts on this one? I haven't dug into the code, but I'm hoping for a way to skip the component generation and import from the outside. |
Beta Was this translation helpful? Give feedback.
-
Sorry, I haven't been paying attention to discussions and missed this. Counterfact generates types for components but not classes. Using the petstore as an example, it generates this type for import type { Category } from "./Category.js";
import type { Tag } from "./Tag.js";
export type Pet = {
id?: number;
name: string;
category?: Category;
photoUrls: Array<string>;
tags?: Array<Tag>;
status?: "available" | "pending" | "sold";
}; I've been thinking it may be useful to also generate components. They might look something like this: import { Pet as IPet } from "../types/components/Pet.js";
export class Pet implements IPet {
id?: number;
name: string;
category?: Category;
photoUrls: Array<string>;
tags?: Array<Tag>;
status?: "available" | "pending" | "sold";
constructor(pet: IPet) {
this.id = pet.id;
this.name = pet.name
this.category = pet.category;
this.photoUrls = pet.photoUrls;
this.tags = pet.tags;
this.status = pet.status;
}
} Why two different files? Because the type is kept in sync with OpenAPI. The class file would be treated the same way as routes. Counterfact creates the file. Then you can augment it without worrying that your changes will be overwritten. What do you think? Would that be helpful? |
Beta Was this translation helpful? Give feedback.
-
If another tool generates classes, you should be able to use them as long as the classes fit the types generated by Counterfact. Some concepts from JSON Schema don't map directly to TypeScript so there's room for interpretation. What tool are you using to generate classes? |
Beta Was this translation helpful? Give feedback.
-
The docs suggest that CF does not have to generate the component types. One of our projects already generates the types from the openapi spec and we would like CF to import those types instead of generating the schema components. Of course, CF would then need to use those imports in all its types and paths.
Beta Was this translation helpful? Give feedback.
All reactions