Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 8d8aa80

Browse files
authored
docs: describe all the features of the fetchUrls plugin (#137)
1 parent d7d9b90 commit 8d8aa80

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,45 @@ const { filesystem } = await load('./openapi.yaml', {
171171
const result = await dereference(filesystem)
172172
```
173173

174-
As you see, `load()` supports plugin. You can write your own plugin, if you’d like to fetch API defintions from another data source, for example your database. Look at the source code of the `readFiles` to learn how this could look like.
174+
As you see, `load()` supports plugins. You can write your own plugin, if you’d like to fetch API defintions from another data source, for example your database. Look at the source code of the `readFiles` to learn how this could look like.
175+
176+
#### Directly load URLs
177+
178+
Once the `fetchUrls` plugin is loaded, you can also just pass an URL:
179+
180+
```ts
181+
import { dereference, load } from '@scalar/openapi-parser'
182+
import { fetchUrls } from '@scalar/openapi-parser/plugins/fetch-urls'
183+
184+
// Load a file and all referenced files
185+
const { filesystem } = await load(
186+
'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml',
187+
{
188+
plugins: [fetchUrls()],
189+
},
190+
)
191+
```
192+
193+
#### Intercept HTTP requests
194+
195+
If you’re using the package in a browser environment, you may run into CORS issues when fetching from URLs. You can intercept the requests, for example to use a proxy, though:
196+
197+
```ts
198+
import { dereference, load } from '@scalar/openapi-parser'
199+
import { fetchUrls } from '@scalar/openapi-parser/plugins/fetch-urls'
200+
201+
// Load a file and all referenced files
202+
const { filesystem } = await load(
203+
'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml',
204+
{
205+
plugins: [
206+
fetchUrls({
207+
fetch: (url) => fetch(url.replace('BANANA.net', 'jsdelivr.net')),
208+
}).get('https://cdn.BANANA.net/npm/@scalar/galaxy/dist/latest.yaml'),
209+
],
210+
},
211+
)
212+
```
175213

176214
## Community
177215

0 commit comments

Comments
 (0)