Skip to content

Commit 21c7f26

Browse files
committed
add status docs
1 parent 8fcfc57 commit 21c7f26

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Diff for: docs/openapi-fetch/index.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
3030
const {
3131
data, // only present if 2XX response
3232
error, // only present if 4XX or 5XX response
33+
status, // HTTP Status code
3334
} = await client.GET("/blogposts/{post_id}", {
3435
params: {
3536
path: { post_id: "123" },
@@ -47,6 +48,8 @@ await client.PUT("/blogposts", {
4748

4849
`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.
4950

51+
The `status` property is also available and contains the HTTP status code of the response (`response.status`). This property is useful for handling different status codes in your application and narrowing down the `data` and `error` properties.
52+
5053
`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)).
5154

5255
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:
@@ -158,16 +161,17 @@ The `POST()` request required a `body` object that provided all necessary [reque
158161

159162
### Response
160163

161-
All methods return an object with **data**, **error**, and **response**.
164+
All methods return an object with **data**, **error**, **status** and **response**.
162165

163166
```ts
164-
const { data, error, response } = await client.GET("/url");
167+
const { data, error, status, response } = await client.GET("/url");
165168
```
166169

167170
| Object | Response |
168171
| :--------- | :-------------------------------------------------------------------------------------------------------------------------- |
169172
| `data` | `2xx` response if OK; otherwise `undefined` |
170173
| `error` | `5xx`, `4xx`, or `default` response if not OK; otherwise `undefined` |
174+
| `status` | HTTP status code of the response (`response.status`) |
171175
| `response` | [The original Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) which contains `status`, `headers`, etc. |
172176

173177
### Path-property style

0 commit comments

Comments
 (0)