|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const ndjson = require('iterable-ndjson')
|
4 |
| -const { objectToQuery } = require('../lib/querystring') |
5 | 4 | const configure = require('../lib/configure')
|
6 |
| -const { ok, toIterable } = require('../lib/fetch') |
| 5 | +const toIterable = require('../lib/stream-to-iterable') |
7 | 6 | const { toFormData } = require('./form-data')
|
8 | 7 | const toCamel = require('../lib/object-to-camel')
|
9 | 8 |
|
10 |
| -module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => { |
| 9 | +module.exports = configure(({ ky }) => { |
11 | 10 | return (input, options) => (async function * () {
|
12 | 11 | options = options || {}
|
13 | 12 |
|
14 |
| - const qs = objectToQuery({ |
15 |
| - 'stream-channels': true, |
16 |
| - chunker: options.chunker, |
17 |
| - 'cid-version': options.cidVersion, |
18 |
| - 'cid-base': options.cidBase, |
19 |
| - 'enable-sharding-experiment': options.enableShardingExperiment, |
20 |
| - hash: options.hashAlg, |
21 |
| - 'only-hash': options.onlyHash, |
22 |
| - pin: options.pin, |
23 |
| - progress: options.progress ? true : null, |
24 |
| - quiet: options.quiet, |
25 |
| - quieter: options.quieter, |
26 |
| - 'raw-leaves': options.rawLeaves, |
27 |
| - 'shard-split-threshold': options.shardSplitThreshold, |
28 |
| - silent: options.silent, |
29 |
| - trickle: options.trickle, |
30 |
| - 'wrap-with-directory': options.wrapWithDirectory, |
31 |
| - ...(options.qs || {}) |
32 |
| - }) |
| 13 | + const searchParams = new URLSearchParams(options.searchParams) |
| 14 | + |
| 15 | + searchParams.set('stream-channels', true) |
| 16 | + if (options.chunker) searchParams.set('chunker', options.chunker) |
| 17 | + if (options.cidVersion) searchParams.set('cid-version', options.cidVersion) |
| 18 | + if (options.cidBase) searchParams.set('cid-base', options.cidBase) |
| 19 | + if (options.enableShardingExperiment != null) searchParams.set('enable-sharding-experiment', options.enableShardingExperiment) |
| 20 | + if (options.hashAlg) searchParams.set('hash', options.hashAlg) |
| 21 | + if (options.onlyHash != null) searchParams.set('only-hash', options.onlyHash) |
| 22 | + if (options.pin != null) searchParams.set('pin', options.pin) |
| 23 | + if (options.progress) searchParams.set('progress', true) |
| 24 | + if (options.quiet != null) searchParams.set('quiet', options.quiet) |
| 25 | + if (options.quieter != null) searchParams.set('quieter', options.quieter) |
| 26 | + if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves) |
| 27 | + if (options.shardSplitThreshold) searchParams.set('shard-split-threshold', options.shardSplitThreshold) |
| 28 | + if (options.silent) searchParams.set('silent', options.silent) |
| 29 | + if (options.trickle != null) searchParams.set('trickle', options.trickle) |
| 30 | + if (options.wrapWithDirectory != null) searchParams.set('wrap-with-directory', options.wrapWithDirectory) |
33 | 31 |
|
34 |
| - const url = `${apiAddr}${apiPath}/add${qs}` |
35 |
| - const res = await ok(fetch(url, { |
36 |
| - method: 'POST', |
| 32 | + const res = await ky.post('add', { |
| 33 | + timeout: options.timeout, |
37 | 34 | signal: options.signal,
|
38 |
| - headers: options.headers || headers, |
| 35 | + headers: options.headers, |
| 36 | + searchParams, |
39 | 37 | body: await toFormData(input)
|
40 |
| - })) |
| 38 | + }) |
41 | 39 |
|
42 | 40 | for await (let file of ndjson(toIterable(res.body))) {
|
43 | 41 | file = toCamel(file)
|
|
0 commit comments