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

Commit d79fca7

Browse files
committed
docs: describe all the features of the fetchUrls plugin
1 parent 21b6fc0 commit d79fca7

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
@@ -151,7 +151,45 @@ const { filesystem } = await load('./openapi.yaml', {
151151
const result = await dereference(filesystem)
152152
```
153153

154-
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.
154+
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.
155+
156+
#### Directly load URLs
157+
158+
Once the `fetchUrls` plugin is loaded, you can also just pass an URL:
159+
160+
```ts
161+
import { dereference, load } from '@scalar/openapi-parser'
162+
import { fetchUrls } from '@scalar/openapi-parser/plugins/fetch-urls'
163+
164+
// Load a file and all referenced files
165+
const { filesystem } = await load(
166+
'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml',
167+
{
168+
plugins: [fetchUrls()],
169+
},
170+
)
171+
```
172+
173+
#### Intercept HTTP requests
174+
175+
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:
176+
177+
```ts
178+
import { dereference, load } from '@scalar/openapi-parser'
179+
import { fetchUrls } from '@scalar/openapi-parser/plugins/fetch-urls'
180+
181+
// Load a file and all referenced files
182+
const { filesystem } = await load(
183+
'https://cdn.jsdelivr.net/npm/@scalar/galaxy/dist/latest.yaml',
184+
{
185+
plugins: [
186+
fetchUrls({
187+
fetch: (url) => fetch(url.replace('BANANA.net', 'jsdelivr.net')),
188+
}).get('https://cdn.BANANA.net/npm/@scalar/galaxy/dist/latest.yaml'),
189+
],
190+
},
191+
)
192+
```
155193

156194
## Community
157195

0 commit comments

Comments
 (0)