You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`data` and `error` are typechecked and expose their shapes to Intellisence in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
47
+
`data` and `error` are typechecked and expose their shapes to Intellisense in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
48
48
49
49
`GET()`, `PUT()`, `POST()`, etc. are thin wrappers around the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (which you can [swap for any call](/openapi-fetch/api#create-client)).
`data` and `error` are typechecked and expose their shapes to Intellisence in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
39
+
`data` and `error` are typechecked and expose their shapes to Intellisense in VS Code (and any other IDE with TypeScript support). Likewise, the request `body` will also typecheck its fields, erring if any required params are missing, or if there’s a type mismatch.
40
40
41
-
`GET()`, `PUT()`, `POST()`, etc. are thin wrappers around the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (which you can [swap for any call](/openapi-fetch/api#create-client)).
41
+
`GET()`, `PUT()`, `POST()`, etc. are thin wrappers around the native [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (which you can [swap for any call](https://openapi-ts.dev/openapi-fetch/api/#create-client)).
42
42
43
43
Notice there are no generics, and no manual typing. Your endpoint’s request and response were inferred automatically. This is a huge improvement in the type safety of your endpoints because **every manual assertion could lead to a bug**! This eliminates all of the following:
44
44
@@ -51,14 +51,13 @@ Notice there are no generics, and no manual typing. Your endpoint’s request an
51
51
52
52
## Setup
53
53
54
-
Install this library along with [openapi-typescript](/introduction):
54
+
Install this library along with [openapi-typescript](../openapi-typescript):
55
55
56
56
```bash
57
57
npm i openapi-fetch
58
58
npm i -D openapi-typescript typescript
59
59
```
60
60
61
-
> **TIP:**
62
61
> **Highly recommended**
63
62
>
64
63
> Enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](/advanced#enable-nouncheckedindexaccess-in-your-tsconfigjson))
Lastly, be sure to **run typechecking** in your project. This can be done by adding `tsc --noEmit` to your [npm scripts](https://docs.npmjs.com/cli/v9/using-npm/scripts) like so:
73
72
74
-
```json [package.json]
73
+
```json
75
74
{
76
75
"scripts": {
77
76
"test:ts": "tsc --noEmit"
@@ -91,7 +90,7 @@ The best part about using openapi-fetch over oldschool codegen is no documentati
2. You pass in your desired `path` to `GET()`, `PUT()`, etc.
118
117
3. TypeScript takes over the rest and returns helpful errors for anything missing or invalid
119
118
120
-
### Pathname
121
-
122
-
The pathname of `GET()`, `PUT()`, `POST()`, etc. **must match your schema literally.** Note in the example, the URL is `/blogposts/{post_id}`. This library will quickly replace all `path` params for you (so they can be typechecked).
123
-
124
-
125
-
> **TIP:**
126
-
>
127
-
> openapi-fetch infers types from the URL. Prefer static string values over dynamic runtime values, e.g.:
128
-
> - ✅ `"/blogposts/{post_id}"`
129
-
> - ❌ `[...pathParts].join("/") + "{post_id}"`
130
-
131
-
This library also supports the **label** and **matrix** serialization styles as well ([docs](https://swagger.io/docs/specification/serialization/#path)) automatically.
132
-
133
-
### Request
134
-
135
-
The `GET()` request shown needed the `params` object that groups [parameters by type](https://spec.openapis.org/oas/latest.html#parameter-object) (`path` or `query`). If a required param is missing, or the wrong type, a type error will be thrown.
136
-
137
-
The `POST()` request required a `body` object that provided all necessary [requestBody](https://spec.openapis.org/oas/latest.html#request-body-object) data.
138
-
139
-
### Response
140
-
141
-
All methods return an object with **data**, **error**, and **response**.
|**Browsers**|[See fetch API support](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#browser_compatibility) (widely-available in all major browsers) |
Copy file name to clipboardExpand all lines: packages/openapi-typescript/README.md
+10-7
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
openapi-typescript turns [OpenAPI 3.0 & 3.1](https://spec.openapis.org/oas/latest.html) schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.
4
4
5
-
The code is [MIT-licensed](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE") and free for use.
5
+
The code is [MIT-licensed](./LICENSE) and free for use.
6
6
7
7
> **Tip:**
8
8
> New to OpenAPI? Speakeasy’s [Intro to OpenAPI](https://www.speakeasyapi.dev/openapi) is an accessible guide to newcomers that explains the “why” and “how” of OpenAPI.
@@ -18,7 +18,7 @@ _Note: OpenAPI 2.x is supported with versions `5.x` and previous_
0 commit comments