1 | async function fetch (url, options) {
|
2 | let protocol = null
|
3 |
|
4 | if (/^[a-z]+:/.test(url)) {
|
5 | protocol = new URL(url).protocol.slice(0, -1)
|
6 | }
|
7 |
|
8 | if (protocol in this.protocols) {
|
9 | return this.protocols[protocol](url, options)
|
10 | }
|
11 |
|
12 | throw new Error(`unknown protocol: ${protocol}`)
|
13 | }
|
14 |
|
15 | function factory (protocols) {
|
16 | const instance = (url, options) => {
|
17 | return fetch.call(instance, url, options)
|
18 | }
|
19 |
|
20 | instance.protocols = protocols
|
21 |
|
22 | return instance
|
23 | }
|
24 |
|
25 | export default factory
|