Giter VIP home page Giter VIP logo

Comments (3)

frolovsky avatar frolovsky commented on June 9, 2024

@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.

  1. 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);
  1. We add script inside package.json "swagger:gen": "node --loader ts-node/esm ./lib/swagger/builder.ts"
  2. Now we have one script to generate all clients

from swagger-typescript-api.

frolovsky avatar frolovsky commented on June 9, 2024

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.

derBinder avatar derBinder commented on June 9, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.