swagger-typescript-api

repository·main·Indexed 26 days ago

https://github.com/acacode/swagger-typescript-api

A tool to generate TypeScript API clients compatible with Fetch or Axios from OpenAPI specifications (versions 2.0 and 3.0), JSON, or YAML files. It provides a CLI with 'generate' and 'generate-templates' commands, as well as a programmatic Library API via the 'generateApi' function. Users can customize output through CodeGenConfig, including enum styles (enum, union, const, const-enum), file naming conventions, and a comprehensive set of hooks to intercept and modify the generation process.

Tokens
1.9K
Snippets
4
Records
16
Agent score
88%

What's inside swagger-typescript-api

  1. Generate API client using the CLI

    main

    You can generate an API client from an OpenAPI specification (OpenAPI 3.0, 2.0, JSON, or YAML) using the command line interface. You can run it directly via npx or install it as a dev dependency in your project.

    # Run without installing
    npx swagger-typescript-api generate --path ./swagger.json
    
    # Or install locally first
    npm install --save-dev swagger-typescript-api
    npx swagger-typescript-api generate --path ./swagger.json
  2. Generate API client using the Library API

    main

    For programmatic control, import generateApi from swagger-typescript-api. This allows you to integrate the generation process directly into your build scripts or Node.js applications.

    import * as path from "node:path";
    import * as process from "node:process";
    import { generateApi } from "swagger-typescript-api";
    
    await generateApi({ input: path.resolve(process.cwd(), "./swagger.json") });
  3. Use the CLI to generate TypeScript API clients

    main

    The swagger-typescript-api CLI provides two main subcommands: generate for creating TypeScript API clients from a Swagger/OpenAPI schema, and generate-templates for generating the .ejs templates used by the generator.

    generate command

    Use this command to transform a Swagger schema into a TypeScript API client. You can customize the output via various flags including HTTP client type, enum styles, and modularity settings.

    generate-templates command

    Use this command to generate the .ejs templates required for the generate command. This is useful if you want to customize how the code is generated.

    To use the CLI, call the package name followed by the desired subcommand and its arguments.

  4. Customize generation using Hooks

    main

    The Hooks interface allows you to intercept and modify various stages of the code generation process. This is useful for custom naming, route manipulation, or schema parsing.

    Key hooks include:

    • onInit: The starting point after fetching the schema. Can modify the configuration.
    • onPreParseSchema / onParseSchema: Intercept schema parsing.
    • onFormatTypeName: Customize how model type names are generated.
    • onFormatRouteName: Customize how route names (operationIds) are generated.
    • onCreateRoute: Customize or ignore specific routes.
    • onPreBuildRoutePath / onBuildRoutePath: Manipulate route path strings.
    • onPrepareConfig: Customize the configuration object before it is sent to templates.
  5. Generate an API client programmatically with generateApi()

    main

    Use the generateApi function to programmatically trigger the API client generation process. It accepts a partial configuration object of type GenerateApiConfiguration["config"].

    Key configuration options:

    • debug: If set to true, enables maximum logging verbosity.
    • silent: If set to true, suppresses all console output.
  6. Use Hooks to Extend Code Generation

    main

    The hooks object in CodeGenConfig allows you to intercept and modify various stages of the generation process.

    Available hooks include:

    • onPreBuildRoutePath: Before building a route path.
    • onBuildRoutePath: During route path construction.
    • onInsertPathParam: When inserting a path parameter.
    • onCreateComponent: To transform a SchemaComponent.
    • onPreParseSchema: Before parsing a schema.
    • onParseSchema: To modify the parsed schema.
    • onCreateRoute: To modify route data.
    • onInit: Called during initialization.
    • onPrepareConfig: Called before configuration is finalized.
    • onCreateRequestParams: To modify request parameters.
    • onCreateRouteName: To modify the route name.
    • onFormatTypeName: To customize type name formatting.
    • onFormatRouteName: To customize route name formatting.
  7. Configure API generation with GenerateApiParams

    main

    When calling generateApi, you can provide several input methods via GenerateApiParams:

    • input: A string representing the local path to the swagger schema file.
    • url: A string representing the URL of the swagger schema.
    • spec: A literal Swagger/OpenAPI specification object.

    All these interfaces extend GenerateApiConfiguration["config"], allowing you to pass any valid configuration option (like modular, enumStyle, httpClientType, etc.) alongside the input source.

  8. Generate API via generateApi()

    main
    The primary way to use the library is by calling generateApi. This function takes parameters to locate a Swagger/OpenAPI schema and returns an object containing the generated files and configuration. You can provide the schema via a file path, a URL, or a literal JSON object.