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
`api` supports API authentication through an `.auth()` method:
7
7
8
8
```js
9
-
sdk.auth('myApiToken');
10
-
sdk.listPets().then(res=> {
11
-
// response
9
+
constpetstore=require('@api/petstore');
10
+
11
+
petstore.auth('myApiToken');
12
+
petstore.listPets().then(({ data }) => {
13
+
// authenticated response
12
14
});
13
15
```
14
16
15
-
With the exception of OpenID, it supports all forms of authentication supported by the OpenAPI specification! Supply `.auth()` with your auth credentials and it'll magically figure out how to use it according to the API you're using. 🧙♀️
17
+
With the exception of OpenID and Mutual TLS it supports all forms of authentication supported by the OpenAPI specification! Supply `.auth()` with your auth credentials and it'll magically figure out how to use it according to the API you're using. 🧙♀️
Copy file name to clipboardExpand all lines: docs/faq.md
+15-13Lines changed: 15 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,14 @@ Yes! YAML definitions will be automatically converted to JSON when they're fetch
11
11
12
12
At the moment it does not. If you wish to use an API that has a Swagger 2.0 file, you'll need to first convert it to an OpenAPI 3 definition.
13
13
14
+
### Does this support OpenAPI 3.1 definitions?
15
+
16
+
Yes!
17
+
18
+
### Does this support OpenAPI 4 (Moonwalk) definitions?
19
+
20
+
No! Since v4 is still in the early planning stages we don't support it but once it's finalized we will.
21
+
14
22
### Does this support traditional OAuth 2 flows of creating tokens?
15
23
16
24
Not yet, unfortunately. For APIs that use OAuth 2, you'll need a fully-qualified token already for `api` to make requests.
@@ -19,19 +27,21 @@ Not yet, unfortunately. For APIs that use OAuth 2, you'll need a fully-qualified
19
27
20
28
Not yet! This is something we're thinking about how to handle, but it's difficult with the simplified nature of the `.auth()` method as it by design does not ask the user to inform the SDK of what kind of authentication scheme the token they're supplying it should match up against.
21
29
30
+
If you have ideas on how to handle this [we'd love to hear them](https://github.com/readmeio/api/discussions/547).
31
+
22
32
### Will this work in browsers?
23
33
24
-
If you generate an SDK with the CLI installation process, then yes! If you're having troubles getting autogenerated SDKs working in a browser, [please let us know](https://github.com/readmeio/api/issues)!
34
+
If you generate an SDK with the CLI installation process then yes! If you're having trouble getting autogenerated SDKs working in a browser, [please let us know](https://github.com/readmeio/api/issues)!
25
35
26
-
Unfortunately the dynamic version of `api` will **not** work in browsers as it requires access to some filesystem handling for managing its cache state.
36
+
Unfortunately the dynamic version of `api` will **not** work in browsers as it requires access to the filesystem for handling and managing its cache state.
27
37
28
38
### Will this validate my data before it reaches the API?
29
39
30
-
Not yet! This is something we've got planned down the road.
40
+
Not yet! This is something we're thinking about in the future.
31
41
32
-
### Does this support OpenAPI definitions that require authentication to download?
42
+
### Does this support fetching OpenAPI definitions that require authentication to download?
33
43
34
-
Not yet! The URL that you give the module must be publicy accessible. If it isn't, you can download it to your computer/server and then use the absolute path to that file instead.
44
+
Not yet! The URL that you give the module must be publicly accessible. If it isn't, you can download it to your computer or server and then use the absolute path to that file instead.
35
45
36
46
```sh
37
47
$ npx api install ./path/to/downloaded.json
@@ -41,14 +51,6 @@ $ npx api install ./path/to/downloaded.json
Copy file name to clipboardExpand all lines: docs/how-it-works.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,7 @@ The `hash` within this is an `md5` of the full OpenAPI definition that we retrie
93
93
94
94
If for some reason this file gets lost, or the accessor you're supplying to `api` changes for whatever reason `api` will re-retrieve the OpenAPI definition at run-time.
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.
7
+
8
+
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.
9
+
10
+
> ⚠️
11
+
>
12
+
> 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).
13
+
14
+
With an instance of your SDK, you make an HTTP request against an operation on the API like so:
15
+
16
+
```js
17
+
constpetstore=require('@api/petstore');
18
+
19
+
petstore.listPets().then(({ data, status, headers, res }) => {
20
+
console.log(`My pets name is ${data[0].name}!`);
21
+
});
22
+
```
23
+
24
+
Here, the data returned in the promise from `listPets` is an object containing the following data:
25
+
26
+
-`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).
27
+
-`status`: This is the HTTP status code of the response.
28
+
-`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.
29
+
-`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).
30
+
31
+
## Error Handling
32
+
33
+
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:
34
+
35
+
```js
36
+
awaitpetstore.deletePet({ id: petId }).catch(({ status, data }) => {
37
+
// status = 404
38
+
// data = 'Not Found'
39
+
});
40
+
```
41
+
42
+
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:
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:
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`.
Since we've supplied two objects here, the SDK automatically knows that you're supplying both a `body` and `metadata`, and can make a PUT request against `/pets/1234` for you.
When your request is executed, it will be made to `https://eu.api.example.com/v14/pets`. Alternatively if you don't want to deal with URL templates, you can opt to pass a full URL in instead:
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:
7
+
8
+
- Full TypeScript types (generated off your OpenAPI definition) 📖
9
+
- Ability to export to TypeScript, CJS, and ESM compiled files 📦
10
+
- Ability to run your SDK within a browser 🌍
11
+
12
+
It's neat, and you should check it out. 🥺
13
+
14
+
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.
15
+
16
+
Here's what your `api` implementation may have looked like before under v4:
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).
37
+
38
+
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
0 commit comments