Open2

openapi-fetch

yoshinoyoshino

https://openapi-ts.dev/ja/openapi-fetch/

以下のようにcreateClientしつつpathsを渡して使う

import createClient from "openapi-fetch";
import type { paths } from "./my-openapi-3-schema"; // openapi-typescriptで生成された型

const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });

const {
  data, // 2XXレスポンスの場合のみ存在
  error, // 4XXまたは5XXレスポンスの場合のみ存在
} = await client.GET("/blogposts/{post_id}", {
  params: {
    path: { post_id: "123" },
  },
});

yoshinoyoshino

packageの構成について

  • packages/openapi-fetch/src/index.js がメインの実装
  • test
    • noStrictNullChecks.test
    • packages/openapi-fetch/test/index.bench.js で benchとってる
    • index.e2e.ts でplaywrightを使ったe2e
    • index.test.ts で基本的な動作のテストとexpectTypeOfを使用した型のテスト

e2eでどんなことを書いているか

https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/test/index.e2e.ts

index.testでどんなことを書いているか

https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/test/index.test.ts

generates all proper functions

TypeScript checks

options

requests

responses

GET()

POST()

DELETE()

OPTIONS()

HEAD()

PATCH()

TRACE()

path based client