-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: v5 * Update bin/build-markdown.js * Update docs/authentication.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/faq.md Co-authored-by: Kanad Gupta <[email protected]> * Update packages/api/README.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/upgrading-from-v4.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/making-requests.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/upgrading-from-v4.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/making-requests.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/making-requests.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/making-requests.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/making-requests.md Co-authored-by: Kanad Gupta <[email protected]> * Update docs/making-requests.md Co-authored-by: Kanad Gupta <[email protected]> Co-authored-by: Kanad Gupta <[email protected]>
- Loading branch information
1 parent
5b6a86a
commit 5faaa11
Showing
14 changed files
with
161 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
title: Making Requests | ||
category: 62cc6ce22b8b6601da6cb12d | ||
--- | ||
|
||
If the API you're using doesn't have any documented [operation IDs](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#user-content-operationid), `api` will generate some for you to use. | ||
|
||
If you're using code generation, these will be immediately available to use in your generated library. However, due to the nature of the [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) architecture in the dynamic version of the library, this isn't the case for dynamically generated libraries. For these cases, you can check out the documentation for the API you're using. | ||
|
||
> ⚠️ | ||
> | ||
> We recommend using code generation as it'll give you the additional benefit of TypeScript type assistance and autocompletion (even if you aren't using TypeScript in your codebase). | ||
With an instance of your SDK, you make an HTTP request against an operation on the API like so: | ||
|
||
```js | ||
const petstore = require('@api/petstore'); | ||
|
||
petstore.listPets().then(({ data, status, headers, res }) => { | ||
console.log(`My pets name is ${data[0].name}!`); | ||
}); | ||
``` | ||
|
||
Here, the data returned in the promise from `listPets` is an object containing the following data: | ||
|
||
- `data`: The data that came back in the API request. Under the hood this is the result of `api` running `res.json()` or `res.text()` on the [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object from the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). | ||
- `status`: This is the HTTP status code of the response. | ||
- `headers`: The headers that came back in the response. This is an instance of the [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object. | ||
- `res`: This is the raw [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object we received from the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). | ||
|
||
## Error Handling | ||
|
||
For every response that is returned with an HTTP status code between 400 and 599, `api` will throw a custom error: `FetchError`. Identical to how responses are returned from successful requests, instances of `FetchError` can be destructured to obtain the same data. For example: | ||
|
||
```js | ||
await petstore.deletePet({ id: petId }).catch(({ status, data }) => { | ||
// status = 404 | ||
// data = 'Not Found' | ||
}); | ||
``` | ||
|
||
Alternatively if you'd like to check for an error other than `FetchError` (i.e., an error with the SDK itself), you can do that too: | ||
|
||
```js | ||
await petstore.deletePet({ id: petId }).catch(err => { | ||
if (!(err instanceof FetchError)) { | ||
throw err; | ||
} | ||
|
||
// err.status = 404 | ||
// err.data = 'Not Found' | ||
}); | ||
``` | ||
|
||
## Request Timeouts | ||
|
||
By default, the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) generally has a default timeout of 30 seconds. If you wish to configure this, you can with the `.config()` method: | ||
|
||
```js | ||
petstore.config({ timeout: 100 }); // 100 milliseconds | ||
|
||
await petstore | ||
.deletePet({ id: petId }) | ||
.then(() => { | ||
// goodbye, friend | ||
}) | ||
.catch(err => { | ||
// err.message = The operation was aborted. | ||
}); | ||
``` | ||
|
||
By supplying a timeout to `.config({ timeout })`, in milliseconds, `api` will automatically initialize an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) to abort your request if it takes longer than your `timeout` value. Please note that if you set a custom timeout, the error that is thrown will be an instance of `AbortError`, **not** `FetchError`. | ||
|
||
> 📘 | ||
> | ||
> The `.config()` method is not chainable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: Upgrading from v4 | ||
category: 5d4c940cae4e610060475769 | ||
--- | ||
|
||
If you're upgrading from v4 of `api`, welcome 👋 a lot's changed! With the introduction of v5, `api` now offers a complete [code generation offering](https://api.readme.dev/docs/usage#code-generation), complete with: | ||
|
||
- Full TypeScript types (generated off your OpenAPI definition) 📖 | ||
- Ability to export to TypeScript, CJS, and ESM compiled files 📦 | ||
- Ability to run your SDK within a browser 🌍 | ||
|
||
It's neat, and you should check it out. 🥺 | ||
|
||
If you'd like to continue using the [dynamic syntax](https://api.readme.dev/docs/usage#dynamically) you will need to update how you're handling promises that are returned from API requests, but it shouldn't be too bad. | ||
|
||
Here's what your `api` implementation may have looked like before under v4: | ||
|
||
```js | ||
const petstore = require('api')('https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore.json'); | ||
|
||
petstore.listPets().then(res => { | ||
console.log(`My pets name is ${res[0].name}!`); | ||
}); | ||
``` | ||
|
||
And here's what it should look like now: | ||
|
||
```js | ||
const petstore = require('api')('https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore.json'); | ||
|
||
petstore.listPets().then(({ data }) => { | ||
console.log(`My pets name is ${data[0].name}!`); | ||
}); | ||
``` | ||
|
||
If you were using the `.config({ parseResponse: false })` option, that option has been removed in favor of this new resolved data shape where we return `data`, `status`, `headers`, and `res` to you. You can see documentation on those [here](https://api.readme.dev/docs/making-requests). | ||
|
||
And if you have any trouble or need help we're more than happy to give you an assist. https://github.com/readmeio/api/issues |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters