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

Commit be26dd7

Browse files
XmaderGozalaachingbrain
authored
feat: add typeScript support (#3236)
TypeScript support for `ipfs` and `ipfs-http-client` Refs: #2945 Refs: #1166 Co-authored-by: Irakli Gozalishvili <contact@gozala.io> Co-authored-by: Alex Potsides <alex@achingbrain.net>
1 parent ce0d093 commit be26dd7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+596
-99
lines changed

docs/core-api/FILES.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ One of `path` or `content` _must_ be passed.
143143
`FileContent` is one of the following types:
144144

145145
```js
146-
Uint8Array | Blob | String | Iterable<Uint8Array|Number> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>
146+
Uint8Array | Blob | String | Iterable<Uint8Array> | Iterable<number> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>
147147
```
148148

149149
`UnixTime` is one of the following types:
@@ -162,7 +162,7 @@ An optional object which may have the following keys:
162162

163163
| Name | Type | Default | Description |
164164
| ---- | ---- | ------- | ----------- |
165-
| chunker | `String` | `'size-262144` | chunking algorithm used to build ipfs DAGs |
165+
| chunker | `String` | `'size-262144'` | chunking algorithm used to build ipfs DAGs |
166166
| cidVersion | `Number` | `0` | the CID version to use when storing the data |
167167
| hashAlg | `String` | `'sha2-256'` | multihash hashing algorithm to use |
168168
| onlyHash | `boolean` | `false` | If true, will not add blocks to the blockstore |
@@ -178,7 +178,7 @@ An optional object which may have the following keys:
178178

179179
| Type | Description |
180180
| -------- | -------- |
181-
| `UnixFSEntry` | A object describing the added data |
181+
| `Promise<UnixFSEntry>` | A object describing the added data |
182182

183183
Each yielded object is of the form:
184184

@@ -226,7 +226,7 @@ Now [ipfs.io/ipfs/Qm..pg/myfile.txt](https://ipfs.io/ipfs/QmWXdjNC362aPDtwHPUE9o
226226

227227
| Name | Type | Description |
228228
| ---- | ---- | ----------- |
229-
| source | [FileStream<FileContent|FileObject>](#filestream) | Data to import (see below) |
229+
| source | [FileStream<FileContent\|FileObject>](#filestream) | Data to import (see below) |
230230

231231
##### FileStream
232232

@@ -242,7 +242,7 @@ An optional object which may have the following keys:
242242

243243
| Name | Type | Default | Description |
244244
| ---- | ---- | ------- | ----------- |
245-
| chunker | `String` | `'size-262144` | chunking algorithm used to build ipfs DAGs |
245+
| chunker | `String` | `'size-262144'` | chunking algorithm used to build ipfs DAGs |
246246
| cidVersion | `Number` | `0` | the CID version to use when storing the data |
247247
| enableShardingExperiment | `boolean` | `false` | allows to create directories with an unlimited number of entries currently size of unixfs directories is limited by the maximum block size. Note that this is an experimental feature |
248248
| hashAlg | `String` | `'sha2-256'` | multihash hashing algorithm to use |

docs/core-api/REPO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ An optional object which may have the following keys:
6868

6969
| Name | Type | Default | Description |
7070
| ---- | ---- | ------- | ----------- |
71-
| options | `boolean` | `false` | Return storage numbers in `MiB` |
71+
| human | `boolean` | `false` | Return storage numbers in `MiB` |
7272
| timeout | `Number` | `undefined` | A timeout in ms |
7373
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
7474

packages/ipfs-http-client/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
"shunkin <hiyoko.san.ipod@gmail.com>",
186186
"victorbjelkholm <victorbjelkholm@gmail.com>",
187187
"Łukasz Magiera <magik6k@users.noreply.github.com>",
188-
"Łukasz Magiera <magik6k@gmail.com>"
188+
"Łukasz Magiera <magik6k@gmail.com>",
189+
"Xmader <xmader@outlook.com>"
189190
]
190191
}

packages/ipfs-http-client/src/add-all.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ const configure = require('./lib/configure')
66
const multipartRequest = require('./lib/multipart-request')
77
const toUrlSearchParams = require('./lib/to-url-search-params')
88
const anySignal = require('any-signal')
9-
const AbortController = require('abort-controller')
9+
const { AbortController } = require('abort-controller')
1010

1111
module.exports = configure((api) => {
12-
return async function * addAll (input, options = {}) {
12+
// eslint-disable-next-line valid-jsdoc
13+
/**
14+
* @type {import('../../ipfs/src/core/components/add-all').AddAll<import('.').HttpOptions>}
15+
*/
16+
async function * addAll (input, options = {}) {
1317
const progressFn = options.progress
1418

1519
// allow aborting requests on body errors
@@ -39,8 +43,16 @@ module.exports = configure((api) => {
3943
}
4044
}
4145
}
46+
return addAll
4247
})
4348

49+
/**
50+
* @typedef {import('../../ipfs/src/core/components/add-all').UnixFSEntry} UnixFSEntry
51+
*/
52+
53+
/**
54+
* @returns {UnixFSEntry}
55+
*/
4456
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
4557
const output = {
4658
path: name,
@@ -59,5 +71,6 @@ function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
5971
}
6072
}
6173

74+
// @ts-ignore
6275
return output
6376
}

packages/ipfs-http-client/src/add.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@ const addAll = require('./add-all')
44
const last = require('it-last')
55
const configure = require('./lib/configure')
66

7+
/**
8+
* @typedef {import("./lib/core").ClientOptions} ClientOptions
9+
*/
10+
11+
// eslint-disable-next-line valid-jsdoc
12+
/**
13+
* @param {ClientOptions} options
14+
*/
715
module.exports = (options) => {
816
const all = addAll(options)
917

1018
return configure(() => {
11-
return async function add (input, options = {}) { // eslint-disable-line require-await
19+
// eslint-disable-next-line valid-jsdoc
20+
/**
21+
* @type {import('../../ipfs/src/core/components/add').Add<import('.').HttpOptions>}
22+
*/
23+
async function add (input, options = {}) { // eslint-disable-line require-await
24+
// @ts-ignore
1225
return last(all(input, options))
1326
}
27+
return add
1428
})(options)
1529
}

packages/ipfs-http-client/src/bitswap/stat.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const configure = require('../lib/configure')
66
const toUrlSearchParams = require('../lib/to-url-search-params')
77

88
module.exports = configure(api => {
9-
return async (options = {}) => {
9+
// eslint-disable-next-line valid-jsdoc
10+
/**
11+
* @type {import('../../../ipfs/src/core/components/bitswap/stat').Stat<import('..').HttpOptions>}
12+
*/
13+
async function stat (options = {}) {
1014
const res = await api.post('bitswap/stat', {
1115
searchParams: toUrlSearchParams(options),
1216
timeout: options.timeout,
@@ -16,6 +20,7 @@ module.exports = configure(api => {
1620

1721
return toCoreInterface(await res.json())
1822
}
23+
return stat
1924
})
2025

2126
function toCoreInterface (res) {

packages/ipfs-http-client/src/bitswap/unwant.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async (cid, options = {}) => {
8+
// eslint-disable-next-line valid-jsdoc
9+
/**
10+
* @type {import('../../../ipfs/src/core/components/bitswap/unwant').Unwant<import('..').HttpOptions>}
11+
*/
12+
async function unwant (cid, options = {}) {
913
const res = await api.post('bitswap/unwant', {
1014
timeout: options.timeout,
1115
signal: options.signal,
@@ -18,4 +22,5 @@ module.exports = configure(api => {
1822

1923
return res.json()
2024
}
25+
return unwant
2126
})

packages/ipfs-http-client/src/bitswap/wantlist-for-peer.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async (peerId, options = {}) => {
8+
// eslint-disable-next-line valid-jsdoc
9+
/**
10+
* @type {import('../../../ipfs/src/core/components/bitswap/wantlist-for-peer').WantlistForPeer<import('..').HttpOptions>}
11+
*/
12+
async function wantlistForPeer (peerId, options = {}) {
913
peerId = typeof peerId === 'string' ? peerId : new CID(peerId).toString()
1014

1115
const res = await (await api.post('bitswap/wantlist', {
@@ -20,4 +24,5 @@ module.exports = configure(api => {
2024

2125
return (res.Keys || []).map(k => new CID(k['/']))
2226
}
27+
return wantlistForPeer
2328
})

packages/ipfs-http-client/src/bitswap/wantlist.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async (options = {}) => {
8+
// eslint-disable-next-line valid-jsdoc
9+
/**
10+
* @type {import('../../../ipfs/src/core/components/bitswap/wantlist').WantlistFn<import('..').HttpOptions>}
11+
*/
12+
async function wantlist (options = {}) {
913
const res = await (await api.post('bitswap/wantlist', {
1014
timeout: options.timeout,
1115
signal: options.signal,
@@ -15,4 +19,5 @@ module.exports = configure(api => {
1519

1620
return (res.Keys || []).map(k => new CID(k['/']))
1721
}
22+
return wantlist
1823
})

packages/ipfs-http-client/src/block/get.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const configure = require('../lib/configure')
66
const toUrlSearchParams = require('../lib/to-url-search-params')
77

88
module.exports = configure(api => {
9-
return async (cid, options = {}) => {
9+
// eslint-disable-next-line valid-jsdoc
10+
/**
11+
* @type {import('../../../ipfs/src/core/components/block/get').BlockGet<import('..').HttpOptions>}
12+
*/
13+
async function get (cid, options = {}) {
1014
cid = new CID(cid)
1115

1216
const res = await api.post('block/get', {
@@ -21,4 +25,5 @@ module.exports = configure(api => {
2125

2226
return new Block(new Uint8Array(await res.arrayBuffer()), cid)
2327
}
28+
return get
2429
})

packages/ipfs-http-client/src/block/put.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ const multipartRequest = require('../lib/multipart-request')
77
const configure = require('../lib/configure')
88
const toUrlSearchParams = require('../lib/to-url-search-params')
99
const anySignal = require('any-signal')
10-
const AbortController = require('abort-controller')
10+
const { AbortController } = require('abort-controller')
1111

1212
module.exports = configure(api => {
13+
// eslint-disable-next-line valid-jsdoc
14+
/**
15+
* @type {import('../../../ipfs/src/core/components/block/put').BlockPut<import('..').HttpOptions>}
16+
*/
1317
async function put (data, options = {}) {
1418
if (Block.isBlock(data)) {
1519
const { name, length } = multihash.decode(data.cid.multihash)

packages/ipfs-http-client/src/config/replace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const multipartRequest = require('../lib/multipart-request')
55
const configure = require('../lib/configure')
66
const toUrlSearchParams = require('../lib/to-url-search-params')
77
const anySignal = require('any-signal')
8-
const AbortController = require('abort-controller')
8+
const { AbortController } = require('abort-controller')
99

1010
module.exports = configure(api => {
1111
return async (config, options = {}) => {

packages/ipfs-http-client/src/dag/put.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const configure = require('../lib/configure')
99
const multipartRequest = require('../lib/multipart-request')
1010
const toUrlSearchParams = require('../lib/to-url-search-params')
1111
const anySignal = require('any-signal')
12-
const AbortController = require('abort-controller')
12+
const { AbortController } = require('abort-controller')
1313
const multicodec = require('multicodec')
1414

1515
module.exports = configure((api, opts) => {

packages/ipfs-http-client/src/files/write.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const configure = require('../lib/configure')
66
const multipartRequest = require('../lib/multipart-request')
77
const toUrlSearchParams = require('../lib/to-url-search-params')
88
const anySignal = require('any-signal')
9-
const AbortController = require('abort-controller')
9+
const { AbortController } = require('abort-controller')
1010

1111
module.exports = configure(api => {
1212
return async (path, input, options = {}) => {

packages/ipfs-http-client/src/id.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const configure = require('./lib/configure')
66
const toUrlSearchParams = require('./lib/to-url-search-params')
77

88
module.exports = configure(api => {
9-
return async (options = {}) => {
9+
// eslint-disable-next-line valid-jsdoc
10+
/**
11+
* @type {import('../../ipfs/src/core/components/id').Id<import('.').HttpOptions>}
12+
*/
13+
async function id (options = {}) {
1014
const res = await api.post('id', {
1115
timeout: options.timeout,
1216
signal: options.signal,
@@ -23,4 +27,5 @@ module.exports = configure(api => {
2327

2428
return output
2529
}
30+
return id
2631
})

packages/ipfs-http-client/src/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ const urlSource = require('ipfs-utils/src/files/url-source')
1414
*/
1515

1616
/**
17-
*
18-
* @param {ClientOptions } options
19-
* @return {Object}
17+
* @typedef {object} HttpOptions
18+
* @property {Headers | Record<string, string>} [headers] - An object or [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) instance that can be used to set custom HTTP headers. Note that this option can also be [configured globally](#custom-headers) via the constructor options.
19+
* @property {URLSearchParams | Record<string, string>} [searchParams] - An object or [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) instance that can be used to add additional query parameters to the query string sent with each request.
20+
* @property {object} [ipld]
21+
* @property {any[]} [ipld.formats] - An array of additional [IPLD formats](https://github.com/ipld/interface-ipld-format) to support
22+
* @property {(format: string) => Promise<any>} [ipld.loadFormat] - an async function that takes the name of an [IPLD format](https://github.com/ipld/interface-ipld-format) as a string and should return the implementation of that codec
23+
*/
24+
25+
// eslint-disable-next-line valid-jsdoc
26+
/**
27+
* @param {ClientOptions} options
2028
*/
2129
function ipfsClient (options = {}) {
2230
return {

packages/ipfs-http-client/src/lib/configure.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ const Client = require('./core')
99
*/
1010

1111
/**
12-
* @param {function(Client, ClientOptions): void} fn
13-
* @returns {function(ClientOptions): void}
12+
* @template T
13+
* @typedef {(client: Client, clientOptions: ClientOptions) => T} Fn
14+
*/
15+
16+
/**
17+
* @template T
18+
* @typedef {(clientOptions: ClientOptions) => T} Factory
19+
*/
20+
21+
/**
22+
* @template T
23+
* @param {Fn<T>} fn
24+
* @returns {Factory<T>}
1425
*/
1526
const configure = (fn) => {
1627
return (options) => {

packages/ipfs-http-client/src/lib/to-url-search-params.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
const modeToString = require('./mode-to-string')
44
const mtimeToObject = require('./mtime-to-object')
55

6+
/**
7+
* @param {object} params
8+
* @returns {URLSearchParams}
9+
*/
610
module.exports = ({ arg, searchParams, hashAlg, mtime, mode, ...options } = {}) => {
711
if (searchParams) {
812
options = {

packages/ipfs-http-client/src/object/patch/append-data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const multipartRequest = require('../../lib/multipart-request')
55
const configure = require('../../lib/configure')
66
const toUrlSearchParams = require('../../lib/to-url-search-params')
77
const anySignal = require('any-signal')
8-
const AbortController = require('abort-controller')
8+
const { AbortController } = require('abort-controller')
99

1010
module.exports = configure(api => {
1111
return async (cid, data, options = {}) => {

packages/ipfs-http-client/src/object/patch/set-data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const multipartRequest = require('../../lib/multipart-request')
55
const configure = require('../../lib/configure')
66
const toUrlSearchParams = require('../../lib/to-url-search-params')
77
const anySignal = require('any-signal')
8-
const AbortController = require('abort-controller')
8+
const { AbortController } = require('abort-controller')
99

1010
module.exports = configure(api => {
1111
return async (cid, data, options = {}) => {

packages/ipfs-http-client/src/object/put.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const multipartRequest = require('../lib/multipart-request')
66
const configure = require('../lib/configure')
77
const toUrlSearchParams = require('../lib/to-url-search-params')
88
const anySignal = require('any-signal')
9-
const AbortController = require('abort-controller')
9+
const { AbortController } = require('abort-controller')
1010
const unit8ArrayToString = require('uint8arrays/to-string')
1111
const uint8ArrayFromString = require('uint8arrays/from-string')
1212

packages/ipfs-http-client/src/pubsub/publish.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const configure = require('../lib/configure')
44
const toUrlSearchParams = require('../lib/to-url-search-params')
55
const multipartRequest = require('../lib/multipart-request')
66
const anySignal = require('any-signal')
7-
const AbortController = require('abort-controller')
7+
const { AbortController } = require('abort-controller')
88

99
module.exports = configure(api => {
1010
return async (topic, data, options = {}) => {

packages/ipfs-http-client/src/pubsub/subscription-tracker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const AbortController = require('abort-controller')
3+
const { AbortController } = require('abort-controller')
44

55
class SubscriptionTracker {
66
constructor () {

0 commit comments

Comments
 (0)