Giter VIP home page Giter VIP logo

genapi's Introduction

API Generator

中文 | English

API generator, which is used to convert OpenApi (v2~v3) and other input sources into TS/JS APIs

  • swag-axios-ts
  • swag-axios-js
  • swag-fetch-ts
  • swag-fetch-js
  • swag-ky-ts
  • swag-ky-js
  • swag-got-js
  • swag-got-js
  • swag-ofetch-js
  • swag-ofetch-js

⚙️ Install

Install it locally in your project folder:

pnpm add @genapi/cli @genapi/swag-axios-ts -D
# Or Yarn
yarn add @genapi/cli @genapi/swag-axios-ts --dev

You can also install it globally but it's not recommended.

📖 Usage

Currently, the CLI option is not provided, and the output content is determined by the config file. Currently, the following config files are supported:

  • genapi.config.ts
  • genapi.config.js
  • genapi.config.cjs
  • genapi.config.json
import { defineConfig } from '@genapi/cli'

export default defineConfig({
  // your input source and output file (swagger api url or json)
  // if you have multiple sources, you can use 'server'
  input: 'http://...api-docs',
  output: {
    main: 'src/api/index.ts',
    type: 'src/api/index.type.ts',
  },

  // your API baseUrl
  baseURL: 'import.meta.env.VITE_APP_BASE_API',
  // customize the output response type. default 'T'
  responseType: 'T extends { data?: infer V } ? V : void',
})
genapi --pipe swag-axios-ts

cli-case

Input

Input supports three input sources url|json

export default defineConfig({
  // directly pass in url
  input: 'http://...api-docs',
  // or
  input: { /* url|json */ }
})

Server

Maybe you have multiple services. You can use 'server' to set multiple services. Usually, other config at the top level are used as additional config

export default defineConfig({
  // Your API baseUrl, this configuration will be passed to the axios request
  baseUrl: 'https://...',
  // all servers inherit the upper layer configuration
  server: [
    { import: '...', output: {/* ... */} },
    { import: '...', output: {/* ... */} },
    { import: '...', output: {/* ... */} },
  ]
})

swag-axios-js

Use the swag-axios-js pipeline to generate JavaScript files with both types.

export default defineConfig({
  pipeline: 'swag-axios-js',
  input: {
    uri: 'https://petstore.swagger.io/v2/swagger.json',
  },
})

Run genapi

swag-axios-js

Pipeline

When defining the configuration, genapi passes in the 'pipeline' parameter to support the npm package (prefix @genapi/ and genapi-) and local path.

export default defineConfig({
  pipeline: './custom-pipe',
})

pipeline is defined by the pipeline method provided by genapi.

// custom-pipe.ts

// create an API pipeline generator using the pipeline provided by genapi
import { pipeline } from '@genapi/core'

// each pipeline exposes corresponding methods, which can be reused and reorganized
import { dest, generate, original } from '@genapi/swag-axios-ts'

function myCustomPipe(config) {
  const process = pipeline(
    // read config, convert to internal config, and provide default values
    config => readConfig(config),
    // get data source
    configRead => original(configRead),
    // parse the data source as data graphs
    configRead => parser(configRead),
    // compile data and convert it into abstract syntax tree (AST)
    configRead => compiler(configRead),
    // generate code string
    configRead => generate(configRead),
    // use outputs to output files
    configRead => dest(configRead),
  )
  return process(config)
}

function readConfig(config) {
  // ...
}

function parser(configRead) {
  // ...
}

function compiler(configRead) {
  // ...
}

genapi's People

Contributors

hairyf avatar renovate[bot] avatar levi-ma avatar

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.