| 6 | }; |
| 7 | |
| 8 | export const fetchURLs = (customConfiguration: { |
| 9 | /** |
| 10 | * Root URL to resolve relative URLs. |
| 11 | */ |
| 12 | rootURL: string | null; |
| 13 | |
| 14 | /** |
| 15 | * Limit the number of requests. Set to `false` to disable the limit. |
| 16 | */ |
| 17 | limit?: number | false; |
| 18 | }): Plugin => { |
| 19 | // State |
| 20 | let numberOfRequests = 0; |
| 21 | |
| 22 | // Configuration |
| 23 | const configuration = { |
| 24 | ...fetchUrlsDefaultConfiguration, |
| 25 | ...customConfiguration, |
| 26 | }; |
| 27 | |
| 28 | return { |
| 29 | type: 'loader', |
| 30 | validate(value) { |
| 31 | // Not a string |
| 32 | if (typeof value !== 'string') { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | // Not http/https or relative path |
| 37 | if ( |
| 38 | !value.startsWith('http://') && |
| 39 | !value.startsWith('https://') && |
| 40 | !isRelativePath(value) |
| 41 | ) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | return true; |
| 46 | }, |
| 47 | async exec(value) { |
| 48 | // Limit the number of requests |
| 49 | if (configuration?.limit !== false && numberOfRequests >= configuration?.limit) { |
| 50 | return { ok: false }; |
| 51 | } |
| 52 | |
| 53 | try { |
| 54 | numberOfRequests++; |
| 55 | const url = getReferenceUrl({ value, rootURL: configuration.rootURL }); |
| 56 | const response = await fetch(url); |
| 57 | if (!response.ok) { |
| 58 | return { ok: false }; |
| 59 | } |
| 60 | const text = await response.text(); |
| 61 | // Try to normalize the text to be sure it's a valid JSON or YAML. |
| 62 | await normalize(text); |
| 63 | return { |
| 64 | ok: true, |
| 65 | data: text, |