Skip to content

Commit

Permalink
add status docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DjordyKoert committed Nov 28, 2024
1 parent 8fcfc57 commit 21c7f26
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/openapi-fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
const {
data, // only present if 2XX response
error, // only present if 4XX or 5XX response
status, // HTTP Status code
} = await client.GET("/blogposts/{post_id}", {
params: {
path: { post_id: "123" },
Expand All @@ -47,6 +48,8 @@ await client.PUT("/blogposts", {

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

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.

`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)).

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

### Response

All methods return an object with **data**, **error**, and **response**.
All methods return an object with **data**, **error**, **status** and **response**.

```ts
const { data, error, response } = await client.GET("/url");
const { data, error, status, response } = await client.GET("/url");
```

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

### Path-property style
Expand Down

0 comments on commit 21c7f26

Please sign in to comment.