Comments (3)
@TianyuKang Hi!
If you are satisfied with having several clients, I can share my solution. We have several schemes and we perform generation for each of them.
- We create
builder.ts
:
import * as fs from "fs";
import * as path from "path";
import * as process from "process";
import { load } from "js-yaml";
import { generateApi } from "swagger-typescript-api";
const SOURCE_TYPES: Readonly<string[]> = [".json", ".yaml"];
const SOURCE_DIR = "./lib/swagger/source"; // our swaggers
const OUTPUT_DIR = "./lib/swagger/dist"; // result of parse
const source = fs
.readdirSync(path.resolve(process.cwd(), SOURCE_DIR))
.filter((file) => SOURCE_TYPES.includes(path.extname(file)));
const getFilePath = (filename: string): string =>
path.resolve(process.cwd(), SOURCE_DIR, filename);
const getName = async (filename: string) => {
// some our logic to parse output name
};
const run = async (filename: string) => {
const parsedName = await getName(filename);
generateApi({
name: "index.ts",
input: getFilePath(filename),
output: path.resolve(process.cwd(), OUTPUT_DIR, parsedName),
generateRouteTypes: true,
extractEnums: true,
extractRequestBody: true,
extractResponseBody: true,
extractRequestParams: true,
extractNestedObjects: true, // custom option in our fork
cleanOutput: true,
httpClientType: "axios",
});
};
source.forEach(run);
- We add script inside package.json
"swagger:gen": "node --loader ts-node/esm ./lib/swagger/builder.ts"
- Now we have one script to generate all clients
from swagger-typescript-api.
It could potentially be improved so that the library accepts several sources at the same time, but there are some nuances there. You can solve the problem yourself by writing an adapter that will combine multiple schema files. Essentially, needs to be combined paths
and components
in source files, but for me this is a dangerous idea
from swagger-typescript-api.
It would be nice if we can just call npx swagger-typescript-api -p ./input/*.yml -o ./output
.
from swagger-typescript-api.
Related Issues (20)
- Response data and error is null when using --extract-response-body HOT 1
- Mapping schema iso Dates to js Date
- Addressing Naming Conflicts in Swagger-Generated TypeScript API
- Is there a plan to generate Zod validations as well? HOT 2
- RangeError: Maximum call stack size exceeded
- should generate 'http-client' only once
- bug: SyntaxError: Declaration or statement expected. HOT 4
- Strict version of typescript 5.1.6 in dependencies HOT 1
- Invalid doc block on --responses --extract-response-error
- Respect writeOnly
- Swagger type definition was skipped by generator
- `--disableStrictSSL` flag in `npx` command line is not working
- Content-Type: "multipart/form-data" is ignored HOT 3
- Syntax error if end of comment is present in example string HOT 2
- Using fetch client returned null data, while using axios client worked as expected HOT 2
- Can we change how list query parameters are handled?
- Separate interfaces for query and path params
- Non generic syntax for generating types with discriminators
- Help: Template for @apollo/datasource-rest or apollo-datasource-http
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swagger-typescript-api.