OpenAPI Typescript Codegen コード ジェネレーターの使用


従来、フロントエンドはリクエストごとに個別にコーディングする必要がありました。少なくともリクエストパスを記述する必要があります。 OpenAPI Typescript Codegen
を使用することで 、バックエンド インターフェイスを呼び出すためのすべてのコードを 1 つのコマンドだけで生成できるようになりました。

OpenAPI Typescript Codegen の使用

インストール:npm install openapi-typescript-codegen --save-dev

npm install openapi-typescript-codegen --save-dev

使用法:openapi --input ./spec.json --output ./generated --client xhr

$ openapi --help

  Usage: openapi [options]

  Options:
    -V, --version             output the version number
    -i, --input <value>       OpenAPI specification, can be a path, url or string content (required)
    -o, --output <value>      Output directory (required)
    -c, --client <value>      HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
    --name <value>            Custom client class name
    --useOptions              Use options instead of arguments
    --useUnionTypes           Use union types instead of enums
    --exportCore <value>      Write core files to disk (default: true)
    --exportServices <value>  Write services to disk (default: true)
    --exportModels <value>    Write models to disk (default: true)
    --exportSchemas <value>   Write schemas to disk (default: false)
    --indent <value>          Indentation options [4, 2, tab] (default: "4")
    --postfixServices         Service name postfix (default: "Service")
    --postfixModels           Model name postfix
    --request <value>         Path to custom request file
    -h, --help                display help for command

  Examples
    $ openapi --input ./spec.json --output ./generated
    $ openapi --input ./spec.json --output ./generated --client xhr

❎ エラーを解決します: zsh: コマンドが見つかりません: openapi

理由: openapi-typescript-codegen がインストール後にシステムの PATH に追加されていないか、npx コマンドを使用して実行する必要があります。
解決策 1: npx を使用して openapi コマンドを実行します。npx openapi --input http://localhost:3000/swagger.json --output ./generated --client axios

npx は、プロジェクトの依存関係にある実行可能ファイルを実行するためのツールで、OpenAPI が確実に検出され、正しく実行されるようにします。

npx openapi --input http://localhost:3000/swagger.json --output ./generated --client axios
解決策 2: openapi-typescript-codegen をグローバル環境に追加します。npm install -g openapi-typescript-codegen

-g フラグを使用してツールをグローバル環境にインストールすると、コマンド ラインから直接 openapi を実行できるようになります。

npm install -g openapi-typescript-codegen
解決策 3:node_modules/.bin ディレクトリが PATH にあることを確認します。

node_modules/.bin ディレクトリを PATH 環境変数に追加すると、プロジェクトの依存関係にある実行可能ファイルをコマンド ラインから直接実行できます。これは通常、シェル構成ファイル (.zshrc や .bashrc など) を変更することで実現できます。
コマンドの例を次に示します。

export PATH="./node_modules/.bin:$PATH"

オペレーティング システムとシェルの種類に基づいて適切に設定します。

おすすめ

転載: blog.csdn.net/trinityleo5/article/details/133282852