Skip to content

docs: added openapi-axios fetcher (#1122) #1911

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
merged 1 commit into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,76 @@ 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"; // generated by openapi-typescript
import Axios from "axios";

const axios = Axios.create({
baseURL: "https://myapi.dev/v1",
adapter: "fetch", // strongly recommended (available since [email protected])
});

// Example 1. Usage with "axios" (default) status handling strategy (validStatus: 'axios')

const api = new OpenApiAxios<paths, "axios">(axios, { validStatus: "axios" }); // throws like axios (e.g. status 400+, network errors, interceptor errors)
// const api = new OpenApiAxios<paths>(axios) // same result

try {
const { status, data, response } = await api.get("/users");
} catch (err) {
if (api.isAxiosError(err)) {
if (typeof err.status === "number") {
// status >= 400
}
// request failed (e.g. network error)
}
throw err; // axios.interceptors error
}

// Example 2. Usage with "fetch" status handling strategy (validStatus: 'fetch')

const fetchApi = new OpenApiAxios<paths, "fetch">(axios, { validStatus: "fetch" }); // throws like browser's fetch() (e.g. network errors, interceptor errors)

try {
const { status, data, error, response } = await api.get("/users");

if (error) {
// status >= 400
}
} catch (err) {
if (api.isAxiosError(err)) {
// request failed (e.g. network error)
}
throw err; // axios.interceptors error
}

// Example 3. Usage with "safe" status handling strategy (validStatus: 'all')
// (No try/catch required)

const safeApi = new OpenApiAxios<paths, "all">(axios, { validStatus: "all" }); // never throws errors

const { status, data, error, response } = await api.get("/users");

if (error) {
if (typeof status === "number") {
// status >= 400
} else if (api.isAxiosError(error)) {
// request failed (e.g. network error)
}
throw error; // axios.interceptors error
}
```

:::

</details>


::: tip

Expand Down