Swagger Client Generator is a tool that enables developers to easily parse Swagger API specification files and generate code bindings that are type safe using the popular validation library Zod and the flexible HTTP client Wretch. Swagger Client Generator
npm install @balise/swagger-client-generatorSwagger Client Generator is a tool that enables developers to easily parse Swagger API specification files and generate code bindings that are type safe using the popular validation library Zod and the flexible HTTP client Wretch. Swagger Client Generator makes it easy to generate high-quality code that is both scalable and maintainable.
npm install @balise/swagger-client-generator
The following code explains how to generate code bindings from a swagger.json file.
import SwaggerGenerateClient from "@balise/swagger-client-generator";
let Query = new SwaggerGenerateClient('inputfile.json', 'outputfile.ts');
Todo
import ApiCallerInstance from "./outputfile";
const Call = new ApiCallerInstance({
rootUrl: 'https://localhost:3000',
headers: {
'Bearer: 'SomeToken'
}
}).ApiCall;
let u = {
first_name: "James",
last_name: "Bond"
}
Call.Projets.Users.post(u).then((res) => {
console.log(res)
}).catch((err) => {
console.error(err)
});
the u object will be validated trough the Zod library, and wrech will make a post request at the https://localhost:3000/Projets/Users endpoint using the bearer token from the setup
#Types
The Library also generates and exports Types and Validators from schemas from the Swagger file.
For the Project schema, ProjectValidator is a Zod validator and ProjectType is a type infered from this validator