Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 625a6a2

Browse files
author
Alan Shaw
committed
refactor: use ky
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent f973a13 commit 625a6a2

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/add-from-url.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
'use strict'
22

3+
const kyDefault = require('ky-universal').default
34
const configure = require('./lib/configure')
4-
const { ok, toIterable } = require('./lib/fetch')
5+
const toIterable = require('./lib/stream-to-iterable')
56

6-
module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => {
7-
const add = require('./add')({ fetch, apiAddr, apiPath, headers })
7+
module.exports = configure(({ ky }) => {
8+
const add = require('./add')({ ky })
89

910
return (url, options) => (async function * () {
1011
options = options || {}
11-
const res = await ok(fetch(url, {
12-
signal: options.signal,
13-
headers: options.headers || headers
14-
}))
12+
13+
const { body } = await kyDefault.get(url)
1514

1615
const input = {
1716
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
18-
content: toIterable(res.body)
17+
content: toIterable(body)
1918
}
2019

2120
for await (const file of add(input, options)) {

src/add/index.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
11
'use strict'
22

33
const ndjson = require('iterable-ndjson')
4-
const { objectToQuery } = require('../lib/querystring')
54
const configure = require('../lib/configure')
6-
const { ok, toIterable } = require('../lib/fetch')
5+
const toIterable = require('../lib/stream-to-iterable')
76
const { toFormData } = require('./form-data')
87
const toCamel = require('../lib/object-to-camel')
98

10-
module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => {
9+
module.exports = configure(({ ky }) => {
1110
return (input, options) => (async function * () {
1211
options = options || {}
1312

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)
3331

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,
3734
signal: options.signal,
38-
headers: options.headers || headers,
35+
headers: options.headers,
36+
searchParams,
3937
body: await toFormData(input)
40-
}))
38+
})
4139

4240
for await (let file of ndjson(toIterable(res.body))) {
4341
file = toCamel(file)

0 commit comments

Comments
 (0)