+ | スキーマ | +生成される型 | +
---|---|---|
+ ❌ 悪い + | ++ +```yaml +type: object +``` + + | +
+
+```ts
+Record |
+
+ ❌ 不十分 + | ++ +```yaml +type: object +additionalProperties: true +``` + + | +
+
+```ts
+Record |
+
+ ✅ 良い + | ++ +```yaml +type: object +additionalProperties: + type: string +``` + + | +
+
+```ts
+Record |
+
+ | スキーマ | +生成される型 | +
---|---|---|
+ ❌ 悪い + | ++ +```yaml +type: array +``` + + | ++ +```ts +unknown[] +``` + + | +
+ ❌ 不十分 + | ++ +```yaml +type: array +items: + type: number +``` + + | ++ +```ts +number[] +``` + + | +
+ ✅ 良い + | ++ +```yaml +type: array +items: + type: number +maxItems: 2 +minItems: 2 +``` + +— または — + +```yaml +type: array +items: + type: number +prefixItems: + - number + - number +``` + + | ++ +```ts +[number, number]; +``` + + | +
New post body
", + publish_date: new Date("2023-03-01T12:00:00Z").getTime(), + }, +}); +``` + +::: + +1. HTTPメソッドを `createClient()` から直接取得します。 +2. `GET()` , `PUT()` などに希望する `path` を渡します。 +3. TypeScriptが欠落しているものや無効なものがあれば有用なエラーを返します。 + +### Pathname + +`GET()` , `PUT()` , `POST()` などのpathnameは、**スキーマと厳密に一致している必要があります**。例では、URLは `/blogposts/{post_id}` です。このライブラリは、すべての `path` パラメータをすぐに置き換え、それらが型チェックされるようにします。 + +::: tip + +openapi-fetch は URL から型を推論します。動的な実行時の値よりも静的な文字列値を優先してください。例: + +- ✅ `"/blogposts/{post_id}"` +- ❌ `[...pathParts].join("/") + "{post_id}"` + +::: + +このライブラリはまた、**label** および **matrix** のシリアライゼーションスタイルもサポートしています([ドキュメント](https://swagger.io/docs/specification/serialization/#path))。 + +### Request + +`GET()` リクエストには、[タイプごとにパラメータをグループ化する](https://spec.openapis.org/oas/latest.html#parameter-object) `params` オブジェクトが必要です( `path`または `query` )。必須のパラメータが欠けている場合や、型が間違っている場合には、型エラーが発生します。 + +`POST()` リクエストでは、必要な [requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) データを提供する `body` オブジェクトが必要でした。 + +### Response + +すべてのメソッドは**data**、**error**および **response**を持つオブジェクトを返します。 + +```ts +const { data, error, response } = await client.GET("/url"); +``` + +| Object | Response | +| :--------- | :------------------------------------------------------------------------------------------------------------------------------ | +| `data` | OKの場合 `2xx` レスポンス、そうでない場合 `undefined` | +| `error` | OKでない場合 `5xx` 、`4xx` 、または `default` レスポンス、それ以外は `undefined` | +| `response` | [オリジナルのレスポンス](https://developer.mozilla.org/en-US/docs/Web/API/Response)であり、`status`, `headers` などを含みます。 | + +### Path-property スタイル + +パスをプロパティとして選択する方が好ましい場合は、パスベースのクライアントを作成できます: + +```ts +import { createPathBasedClient } from "openapi-fetch"; +import type { paths } from "./my-openapi-3-schema"; // openapi-typescriptで生成された型 + +const client = createPathBasedClient