I would recommend that you use a library that handles everything for you such as tsoa which can easily generate Swagger/OpenAPI documents from your TypeScript types. It also does the runtime validation for you so that you know the request actually is the type that TypeScript says it should be. The readme contains all of the setup information that you would need to start using it. It's compatible with express, hapi, koa, and more:
https://github.com/lukeautry/tsoa
(Full Transparency: I am one of the maintainers of tsoa. But I was first a consumer of tsoa and I find it to be a great product... that's why I asked to help maintain it! :) )
Answer from GreeneCreations on Stack Overflow
» npm install swagger-typescript-api
Videos
I'm using swagger-typescript-api to generate interfaces from swagger schema
npx swagger-typescript-api generate -p PATH_TO_YOUR_SCHEMA -o ./
Not sure if that's a sane way to do this, it's the first time I'm playing around with Swagger.
I bumped into the following link and pasted the schema from the project I integrate with. From the top 'Generate Client' menu I chose one of the TypeScript presets and it generated a minimal project where I could extract the bits I needed, interface and classes, etc.
http://editor.swagger.io/#/
I tried running your schema. Here's a small excerpt of the generated code:
export interface Bar {
"a"?: string;
"b": number;
"c": Date;
"baz"?: Baz;
}
export interface Baz {
"d": number;
"color": Color;
}
/**
*
*/
export type Color = "0" | "1" | "2";
Maybe with a bit more tweaking it can make exactly what you're looking for.
Further reading may be about tools like https://github.com/swagger-api/swagger-codegen but the online web editor is a quick and dirty way to do this.
At work we have a pretty simple set up: Next.js frontend hosted on Vercel and a Java/Dropwizrd backend hosted on AWS. Our backend uses Swagger/Open API, and we generate our types based on the swagger.json.
Frankly, this sucks to manage across deployments and branches. Our backend is in a separate repository, so we frequently block preview deployments because our hosted staging or dev instance is either ahead or behind our frontend.
Our types are generated either via script locally or right before our build on vercel. We do not commit them, and we’d prefer to never commit them
We are considering a monorepo, which for other reasons as well, might be the most sane solution. I have considered hosting our types, creating packages in a private NPM repository, trying some kind of gRPC solution, but none of them really seem that straight forward, nor do they really “fix” our problem.
How have you or how have you seen synchronizing backend and frontend types? Any solution or architecture ideas for this would be very helpful!