-
-
Notifications
You must be signed in to change notification settings - Fork 539
docs(ja): update Japanese docs (08/17/2024 ~ 09/21/2024) #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
drwpow
merged 5 commits into
openapi-ts:main
from
yoshi2no:docs/update-ja-docs-240817-240921
Oct 25, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad854a0
docs(ja): add `--root-types` to cli flags, a9cd9aa56dd9dec043a4e4fd8…
yoshi2no 1ca5cd3
docs(ja): add `web-bee-ru/openapi-axios` to examples, 4fc9588cabe985…
yoshi2no ac4b522
docs(ja): add tip on openapi-fetch Content-Type, b893c44f4290917f24…
yoshi2no dd3a273
docs(ja): add `baseUrl` to fetch options, 2a4b067f43f7e0b75aecbf5c2f…
yoshi2no 60dc0b8
docs(ja): fix a few expressions
yoshi2no File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -142,6 +142,82 @@ try { | |||||
|
||||||
</details> | ||||||
|
||||||
<details> | ||||||
<summary><a href="https://www.npmjs.com/package/@web-bee-ru/openapi-axios" target="_blank" rel="noreferrer">openapi-axios</a> by <a href="https://github.com/web-bee-ru" target="_blank" rel="noreferrer">@web-bee-ru</a></summary> | ||||||
|
||||||
::: code-group | ||||||
|
||||||
```ts [test/my-project.ts] | ||||||
import { OpenApiAxios } from "@web-bee-ru/openapi-axios"; | ||||||
import type { paths } from "./my-openapi-3-schema"; // openapi-typescriptで生成された型 | ||||||
import Axios from "axios"; | ||||||
|
||||||
const axios = Axios.create({ | ||||||
baseURL: "https://myapi.dev/v1", | ||||||
adapter: "fetch", // 強く推奨 (axios@1.7.0 から利用可能) | ||||||
}); | ||||||
|
||||||
// 例1. "axios"(デフォルト)のステータス処理方法での使用 (validStatus: 'axios') | ||||||
|
||||||
// axiosのようにエラーを投げる(例:status >= 400、ネットワークエラー、インターセプターエラー) | ||||||
const api = new OpenApiAxios<paths, "axios">(axios, { validStatus: "axios" }); | ||||||
|
||||||
// const api = new OpenApiAxios<paths>(axios) // 同じ結果になる | ||||||
|
||||||
try { | ||||||
const { status, data, response } = await api.get("/users"); | ||||||
} catch (err) { | ||||||
if (api.isAxiosError(err)) { | ||||||
if (typeof err.status === "number") { | ||||||
// status >= 400 | ||||||
} | ||||||
// リクエスト失敗(例:ネットワークエラー) | ||||||
} | ||||||
throw err; // axios.interceptors のエラー | ||||||
} | ||||||
|
||||||
// 例2. "fetch" ステータス処理方法での使用 (validStatus: 'fetch') | ||||||
|
||||||
// ブラウザのfetch()のようにエラーを投げる(例:ネットワークエラー、インターセプターエラー) | ||||||
const fetchApi = new OpenApiAxios<paths, "fetch">(axios, { | ||||||
validStatus: "fetch", | ||||||
}); | ||||||
|
||||||
try { | ||||||
const { status, data, error, response } = await api.get("/users"); | ||||||
|
||||||
if (error) { | ||||||
// status >= 400 | ||||||
} | ||||||
} catch (err) { | ||||||
if (api.isAxiosError(err)) { | ||||||
// リクエスト失敗(例:ネットワークエラー) | ||||||
} | ||||||
throw err; // axios.interceptors のエラー | ||||||
} | ||||||
|
||||||
// 例3. "safe" ステータス処理方法での使用 (validStatus: 'all') | ||||||
// (try/catch は不要) | ||||||
|
||||||
// エラーは投げない | ||||||
const safeApi = new OpenApiAxios<paths, "all">(axios, { validStatus: "all" }); | ||||||
|
||||||
const { status, data, error, response } = await api.get("/users"); | ||||||
|
||||||
if (error) { | ||||||
if (typeof status === "number") { | ||||||
// status >= 400 | ||||||
} else if (api.isAxiosError(error)) { | ||||||
// リクエスト失敗(例:ネットワークエラー | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits
Suggested change
|
||||||
} | ||||||
throw error; // axios.interceptors のエラー | ||||||
} | ||||||
``` | ||||||
|
||||||
::: | ||||||
|
||||||
</details> | ||||||
|
||||||
::: tip | ||||||
|
||||||
良い fetch ラッパーは**ジェネリクスの使用は避ける**べきです。ジェネリクスは多くのタイプ指定が必要で、エラーを隠してしまう可能性があります! | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,6 +36,7 @@ client.GET("/my-url", options); | |||||
| `querySerializer` | QuerySerializer | (任意) [querySerializer](#queryserializer) を提供します | | ||||||
| `bodySerializer` | BodySerializer | (任意) [bodySerializer](#bodyserializer) を提供します | | ||||||
| `parseAs` | `"json"` \| `"text"` \| `"arrayBuffer"` \| `"blob"` \| `"stream"` | (任意) [組み込みのインスタンスメソッド](https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods) を使用してレスポンスを解析します (デフォルト: `"json"`). `"stream"` は解析をスキップし、未処理のストリームを返します。 | | ||||||
| `baseUrl` | `string` | fetch URL にこのオプションで指定されたプレフィックスを付与します (e.g. `"https://myapi.dev/v1/"`) | | ||||||
| `fetch` | `fetch` | リクエストに使用する Fetch インスタンス (デフォルト: `createClient` で指定された fetch ) | | ||||||
| `middleware` | `Middleware[]` | [ドキュメントを参照](/openapi-fetch/middleware-auth) | | ||||||
| (Fetch options) | | 有効なすべての fetch オプション(`headers`, `mode`, `cache`, `signal`など) ([ドキュメント](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)) | | ||||||
|
@@ -180,6 +181,15 @@ const { data, error } = await client.PUT("/submit", { | |||||
}); | ||||||
``` | ||||||
|
||||||
::: tip | ||||||
|
||||||
便宜上、 `openapi-fetch` は `body` パラメーターに値が指定されたリクエストに対して、`Content-Type` を自動的に `application/json` に設定します。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just an idea to avoid duplicated "に"
Suggested change
|
||||||
`bodySerializer` が `FormData` のインスタンスを返す場合、`Content-Type` ヘッダーは省略され、ブラウザがメッセージの複数のパートを正しく区切るboundaryを含む `Content-Type` を自動的に設定します。 | ||||||
|
||||||
また、fetch オプションやクライアントのインスタンス化時に、`headers` オブジェクトを通じて `Content-Type` を手動で設定することもできます。 | ||||||
|
||||||
::: | ||||||
|
||||||
## Pathのシリアライズ | ||||||
|
||||||
openapi-fetchは、[3.1仕様書に記載されている](https://swagger.io/docs/specification/serialization/#path)ように、pathのシリアライズをサポートしています。これは、OpenAPIスキーマ内の特定のフォーマットに基づいて自動的に行われます。 | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO "エラーを投げる" sounds slightly casual, "スローする" is used widely than the former.