Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: swagger

RSS

OpenAPI client generation

This article documents the selection process and practical experience of automatically generating SDKs based on OpenAPI metadata. The author compared three libraries: `autorest`, `swagger-codegen-cli`, and `@hey-api/openapi-ts`, ultimately choosing the latter due to its superior performance in frontend environment compatibility, code conciseness, and type reuse. This choice effectively reduces the workload of manually writing request code and improves development efficiency. Using a NestJS backend and Vue frontend as examples, the article details the complete technical pipeline: configuring `nest-cli.json` to extract DTO and Entity types, utilizing a GitHub Actions pipeline to start the service in a CI environment to retrieve metadata, invoking `@hey-api/openapi-ts` to generate the SDK, and pushing it to a separate sub-repository. The frontend wraps the `@hey-api/client-fetch` client to implement unified authentication interception and error handling, directly importing the generated types and functions within Pinia Stores to achieve type safety and code reuse. This workflow not only reduces the collaboration cost between frontend and backend teams but also minimizes format errors through standardized interface definitions. Although the currently auto-generated parameter validation feature has limitations, the overall solution offers significant advantages in terms of type safety and development efficiency.

Views 386
2024 ci-cd github openapi study swagger vue work