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
node.js - How to generate swagger API doc from TypeScript based Express app? - Stack Overflow
How to generate basic TypeScript interfaces from Swagger schema? - Stack Overflow
Any examples of how to use the output from swagger-typescript-api?
API Testing Schema Validation?
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.
Lots of examples of how to run the tool to generate a client based on an OpenAPI spec.... but damn'd if I can't find an example of how to use it.
Here's where I'm at (this is in a ReactJS app):
import { Api } from "../../../swaggergen/Api";
import { loginRequest } from "../authConfig";
import { msalInstance } from "../index";
export async function getApiClient() {
var client = new Api({
baseUrl: "",
securityWorker: () => {
const account = msalInstance.getActiveAccount();
if (!account) {
throw Error("No active account! Verify a user has been signed in and setActiveAccount has been called.");
}
const response = await msalInstance.acquireTokenSilent({
...loginRequest,
account: account
});
return {
headers: {
Authorization: `Bearer ${response.accessToken}`
},
};
},
});
// Halp... how to use client?
//? var d = await client.v1.MyFunkyList();
//? var r = await d.json();
}How do I get typed results out of the API? MyFunkyList() on success should returns and instance of MyFunkyData. But danged if I see how?
Also, how do I handle error conditions? For example some POST calls will return 400 bad request with standard JSON message that contains the error details. How do I detect 400; how do I get the JSON data?
The tool's site: https://www.npmjs.com/package/swagger-typescript-api
Thanks a ton!