0.6.27 • Published 1 year ago

oas-gen-ts v0.6.27

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

oas-gen-ts

code-review dependency-review Codacy Badge Codacy Badge npm release license

OpenAPI Specification ➡️ TypeScript

Convert OpenAPI Specification declaration files into type declarations and executable functions. Compared with other similar tools, it has the following characteristics

  • Each API is a function for easy tree shaking at build time
  • Easily integrate with local request clients, such as Axios instances created in local projects
  • Easy to use, easy to learn, type safe

Install

npm i -D oas-gen-ts

or

yarn add --dev oas-gen-ts

Usage

CLI

Create oas.config.js or oas.json in the root directory of the project. The search order for configuration files is oas.config.cjs, oas.config.js, oas.json.

// oas.config.cjs
const { defineConfig } = require('oas-gen-ts');

module.exports = defineConfig({
  list: [
    {
      name: 'swagger/pet',
      url: 'https://petstore3.swagger.io/api/v3/openapi.json',
    },
  ],
});
# Generate typescript files based on configuration files
npx oas-gen-ts

# The `src/apis/swagger/pet.ts` file will be generated

The generated file will be exported as one function and one operation, like this:

// src/apis/swagger/pet.ts

// ...

export interface Pet {
  /**
   * @format int64
   * @example 10
   */
  id?: number;
  /** @example "doggie" */
  name: string;
  category?: Category;
  photoUrls: string[];
  tags?: Tag[];
  /** pet status in the store */
  status?: 'available' | 'pending' | 'sold';
}

// ...

/**
 * @summary Finds Pets by status
 * @description Multiple status values can be provided with comma separated strings
 * @tags pet
 * @request GET:/pet/findByStatus
 * @secure
 */
export async function findPetsByStatus(
  query?: {
    /**
     * Status values that need to be considered for filter
     * @default "available"
     */
    status?: 'available' | 'pending' | 'sold';
  },
  axiosRequestConfig?: AxiosRequestConfig
): AxiosReturn<Pet[]> {
  return axios.request({
    url: `${BASE_URL}/pet/findByStatus`,
    method: MethodType.GET,
    params: query,
    headers: formatHeaders(ContentKind.OTHER),
    responseType: ResponseType.Json,
    ...axiosRequestConfig,
  });
}

// ...

Then you can directly import a function and use it. Calling an interface is as simple as calling a local function, is it similar to RPC (remote procedure call).

import { findPetsByStatus } from '@/apis/swagger/pet';

// There are type hints when calling functions and writing parameters, thanks to TypeScript.
const pets = await findPetsByStatus({
  status: ['avaliable'],
});

API

import { generate } from 'oas-gen-ts';

generate({
  // ...config
});

Config

NameTypeRequiredDescriptionDefault
cwdstringfalsecurrent working directoryprocess.cwd()
deststringfalseDestination directory for generated filessrc/apis
axiosImportstringfalseaxios import stringImport from the official and create an instance
unwrapResponseDatabooleanfalseunwrap the data item from the responsefalse
listOasItem[]falseList of OpenAPI Specification declarations[]

OasItem:

NameTypeRequiredDescriptionDefault
namestringtruefilename, which can be a pathprocess.cwd()
axiosImportstringfalseaxios import string, highest priorityImport from the official and create an instance
urlstringfalseThe remote address of the OpenAPI Specificationundefined
specSpecfalseThe local Objects of the OpenAPI Specificationundefined

At least one of url and spec exists

0.6.27

1 year ago

0.6.26

1 year ago

0.6.25

1 year ago

0.6.24

1 year ago

0.6.23

1 year ago

0.6.22

1 year ago

0.6.21

1 year ago

0.6.20

1 year ago

0.6.18

1 year ago

0.6.15

1 year ago

0.6.11

1 year ago

0.6.10

1 year ago

0.6.9

1 year ago

0.6.8

1 year ago

0.6.3

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago