メインコンテンツまでスキップ

クイックスタート

このガイドでは、実行可能なリクエスト例の生成方法と、利用できるジェネレーターの調べ方を説明します。

1. インストール​

npm install @powerduck/openapi-codegen

2. ドキュメントから生成する​

解析済みの OpenAPI ドキュメントと、対象の path、method、language、client を渡します。generate() はソースコードを文字列で返します。

import { generate } from "@powerduck/openapi-codegen";
import document from "./openapi.json" with { type: "json" };

const code = generate({
document,
path: "/pets/{id}",
method: "get",
language: "javascript",
client: "fetch",
});

console.log(code);

CommonJS での同等のコード:

const { generate } = require("@powerduck/openapi-codegen");
const fs = require("node:fs");

const document = JSON.parse(fs.readFileSync("./openapi.json", "utf8"));

const code = generate({
document,
path: "/pets",
method: "post",
language: "python",
client: "requests",
});

console.log(code);

3. 利用できるジェネレーターを一覧表示する​

list() は登録済みのすべてのジェネレーターの記述子を返します。

import { list } from "@powerduck/openapi-codegen";

for (const { language, client } of list()) {
console.log(`${language}/${client}`);
}

各記述子の形式は { language: string; client: string } です。識別子は大文字と小文字を区別します。API リファレンスのジェネレーター一覧に記載されている値をそのまま使ってください。 API リファレンス.

コードでジェネレーターを選択することもできます。

import { generate, list } from "@powerduck/openapi-codegen";

const picked = list().find(
(g) => g.language === "javascript" && g.client === "fetch",
);

if (!picked) throw new Error("Generator not available");

const code = generate({
document,
path: "/pets",
method: "get",
language: picked.language,
client: picked.client,
});

4. 認証情報を指定する​

securityValues で名前付きのセキュリティスキームに値を設定します。キーはドキュメント内のスキーム名と一致させてください。

const code = generate({
document,
path: "/pets/{id}",
method: "get",
language: "javascript",
client: "fetch",
securityValues: {
bearerAuth: "YOUR_ACCESS_TOKEN",
apiKey: "YOUR_API_KEY",
},
});

5. サーバー URL を上書きする​

const code = generate({
document,
path: "/pets",
method: "get",
language: "shell",
client: "curl",
serverUrl: "https://staging.example.com/v1",
});

エラー処理​

入力が不足している場合や、ジェネレーターがサポートされていない場合、generate() は例外を投げます。信頼できないドキュメントを扱う際は、例外を捕捉してください。

try {
const code = generate({
document,
path: "/pets/{id}",
method: "get",
language: "javascript",
client: "fetch",
});
console.log(code);
} catch (error) {
console.error("Generation failed:", error instanceof Error ? error.message : error);
}

次のステップ​

  • 設定 — GenerateOptions, RequestIR, Generator, Plugin.
  • 使用例 — JavaScript fetch、Python requests、cURL、カスタムジェネレーター、プラグイン、RequestIR の直接利用。
  • API リファレンス — すべてのエクスポート。