File tree Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Original file line number Diff line number Diff 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+
1013const fetch = createFetch <paths >({ baseUrl: " https://petstore3.swagger.io" });
1114
1215const 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
Original file line number Diff line number Diff line change 1- import createFetch , { FetchMethods } from "../../src/openapi-typescript" ;
1+ import createFetch from "../../src/openapi-typescript" ;
22import { paths , components } from "./test-data/petstore-openapi3" ;
33import { IsEqual } from "../test-tools" ;
44
You can’t perform that action at this time.
0 commit comments