Skip to content

Commit c7c3056

Browse files
committed
docs: clarify usage in first example
1 parent 84a286c commit c7c3056

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ It is primarily focused on using the TypeScript definitions generated by [openap
77
## Example
88

99
```ts
10+
import createFetch from "typed-api-fetch";
11+
import { paths } from "./petstore-schema.d.ts"; // generated by openapi-typescript
12+
1013
const fetch = createFetch<paths>({ baseUrl: "https://petstore3.swagger.io" });
1114

1215
const response = await fetch(
@@ -19,10 +22,16 @@ const response = await fetch(
1922
},
2023
);
2124

22-
const data = await response.json();
25+
if (response.ok) {
26+
const dataOk = await response.json(); // Infered type of HTTP 2XX status codes
2327

24-
console.log(data.name);
25-
console.log(data.age); // ❌ property 'age' does not exist
28+
console.log(dataOk.name);
29+
console.log(dataOk.age); // ❌ property 'age' does not exist
30+
}
31+
32+
if (response.status === 404) {
33+
const data404 = await response.json(); // Infered type on HTTP 404 status responses
34+
}
2635
```
2736

2837
## Install

test/openapi-typescript/fetch-factory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createFetch, { FetchMethods } from "../../src/openapi-typescript";
1+
import createFetch from "../../src/openapi-typescript";
22
import { paths, components } from "./test-data/petstore-openapi3";
33
import { IsEqual } from "../test-tools";
44

0 commit comments

Comments
 (0)