Skip to content

Commit d4217c8

Browse files
GozalaXmaderachingbrain
authored
feat: type check & generate defs from jsdoc (#3281)
1. Add missing type info (via jsdoc) and fix existing one so that `tsc --noEmit` can succeed without an error. 2. Fix all the issues that `tsc` pointed out once it got enabled. 3. Add a npm script that generates typedefs & typedef maps in `dist` directory & corresponding settings in `package.json` so that when package installed from npm no config will be required to get all the typings. Co-authored-by: Xmader <[email protected]> Co-authored-by: Alex Potsides <[email protected]> Co-authored-by: achingbrain <[email protected]>
1 parent 93a0637 commit d4217c8

Some content is hidden

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

61 files changed

+467
-152
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const server = createServer({
1616
let echoServer = new EchoServer()
1717

1818
module.exports = {
19-
bundlesize: { maxSize: '90kB' },
19+
bundlesize: { maxSize: '81kB' },
2020
karma: {
2121
files: [{
2222
pattern: 'node_modules/interface-ipfs-core/test/fixtures/**/*',

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ All core API methods take _additional_ `options` specific to the HTTP API:
9696

9797
* `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.
9898
* `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.
99-
* `ipld.formats` - An array of additional [IPLD formats](https://github.com/ipld/interface-ipld-format) to support
100-
* `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
10199

102100
### Instance Utils
103101

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
"go-ipfs": false,
2121
"ipfs-core-utils/src/files/normalise-input": "ipfs-core-utils/src/files/normalise-input/index.browser.js"
2222
},
23+
"typesVersions": {
24+
"*": {
25+
"*": [
26+
"dist/*"
27+
]
28+
}
29+
},
2330
"repository": {
2431
"type": "git",
2532
"url": "git+https://github.com/ipfs/js-ipfs.git"
@@ -34,7 +41,9 @@
3441
"test:chrome": "cross-env ECHO_SERVER_PORT=37496 aegir test -t browser -t webworker -- --browsers ChromeHeadless",
3542
"test:firefox": "cross-env ECHO_SERVER_PORT=37497 aegir test -t browser -t webworker -- --browsers FirefoxHeadless",
3643
"lint": "aegir lint",
37-
"build": "aegir build",
44+
"build": "npm run build:js && npm run build:types",
45+
"build:js": "aegir build",
46+
"build:types": "tsc --build",
3847
"coverage": "npx nyc -r html npm run test:node -- --bail",
3948
"clean": "rm -rf ./dist",
4049
"dep-check": "aegir dep-check"
@@ -70,15 +79,17 @@
7079
"uint8arrays": "^1.1.0"
7180
},
7281
"devDependencies": {
73-
"aegir": "^27.0.0",
82+
"aegir": "^28.0.0",
7483
"cross-env": "^7.0.0",
7584
"go-ipfs": "^0.7.0",
7685
"interface-ipfs-core": "^0.140.0",
7786
"ipfsd-ctl": "^7.0.2",
7887
"it-all": "^1.0.4",
7988
"it-concat": "^1.0.1",
8089
"it-pipe": "^1.1.0",
81-
"nock": "^13.0.2"
90+
"nock": "^13.0.2",
91+
"ipfs-core": "0.0.1",
92+
"typescript": "^4.0.3"
8293
},
8394
"engines": {
8495
"node": ">=10.3.0",

src/add-all.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const toCamel = require('./lib/object-to-camel')
55
const configure = require('./lib/configure')
66
const multipartRequest = require('./lib/multipart-request')
77
const toUrlSearchParams = require('./lib/to-url-search-params')
8-
const anySignal = require('any-signal')
8+
const { anySignal } = require('any-signal')
99
const AbortController = require('native-abort-controller')
1010

1111
module.exports = configure((api) => {
1212
/**
13-
* @type {import('../../ipfs/src/core/components/add-all').AddAll<import('.').HttpOptions>}
13+
* @type {import('.').Implements<import('ipfs-core/src/components/add-all/index')>}
1414
*/
15-
async function * addAll (input, options = {}) {
15+
async function * addAll (source, options = {}) {
1616
const progressFn = options.progress
1717

1818
// allow aborting requests on body errors
@@ -28,7 +28,7 @@ module.exports = configure((api) => {
2828
timeout: options.timeout,
2929
signal,
3030
...(
31-
await multipartRequest(input, controller, options.headers)
31+
await multipartRequest(source, controller, options.headers)
3232
)
3333
})
3434

@@ -46,11 +46,7 @@ module.exports = configure((api) => {
4646
})
4747

4848
/**
49-
* @typedef {import('../../ipfs/src/core/components/add-all').UnixFSEntry} UnixFSEntry
50-
*/
51-
52-
/**
53-
* @param {*} input
49+
* @param {any} input
5450
* @returns {UnixFSEntry}
5551
*/
5652
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
@@ -71,6 +67,9 @@ function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
7167
}
7268
}
7369

74-
// @ts-ignore
7570
return output
7671
}
72+
73+
/**
74+
* @typedef {import('ipfs-core/src/components/add-all/index').UnixFSEntry} UnixFSEntry
75+
*/

src/add.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@ const last = require('it-last')
55
const configure = require('./lib/configure')
66

77
/**
8-
* @typedef {import("./lib/core").ClientOptions} ClientOptions
9-
*/
10-
11-
/**
12-
* @param {ClientOptions} options
8+
* @param {import("./lib/core").ClientOptions} options
139
*/
1410
module.exports = (options) => {
1511
const all = addAll(options)
16-
1712
return configure(() => {
1813
/**
19-
* @type {import('../../ipfs/src/core/components/add').Add<import('.').HttpOptions>}
14+
* @type {import('.').Implements<import('ipfs-core/src/components/add')>}
2015
*/
21-
async function add (input, options = {}) { // eslint-disable-line require-await
22-
// @ts-ignore
23-
return last(all(input, options))
16+
async function add (input, options = {}) {
17+
// @ts-ignore - last may return undefind if source is empty
18+
return await last(all(input, options))
2419
}
2520
return add
2621
})(options)

src/bitswap/stat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
77

88
module.exports = configure(api => {
99
/**
10-
* @type {import('../../../ipfs/src/core/components/bitswap/stat').Stat<import('..').HttpOptions>}
10+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/stat')>}
1111
*/
1212
async function stat (options = {}) {
1313
const res = await api.post('bitswap/stat', {

src/bitswap/unwant.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
88
/**
9-
* @type {import('../../../ipfs/src/core/components/bitswap/unwant').Unwant<import('..').HttpOptions>}
9+
* @type {import('..').Implements<import('ipfs-core/src/components/bitswap/unwant')>}
1010
*/
1111
async function unwant (cid, options = {}) {
1212
const res = await api.post('bitswap/unwant', {
1313
timeout: options.timeout,
1414
signal: options.signal,
1515
searchParams: toUrlSearchParams({
16+
// @ts-ignore - CID|string seems to confuse typedef
1617
arg: typeof cid === 'string' ? cid : new CID(cid).toString(),
1718
...options
1819
}),

src/bitswap/wantlist-for-peer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
88
/**
9-
* @type {import('../../../ipfs/src/core/components/bitswap/wantlist-for-peer').WantlistForPeer<import('..').HttpOptions>}
9+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/wantlist-for-peer')>}
1010
*/
1111
async function wantlistForPeer (peerId, options = {}) {
12+
// @ts-ignore - CID|string seems to confuse typedef
1213
peerId = typeof peerId === 'string' ? peerId : new CID(peerId).toString()
1314

1415
const res = await (await api.post('bitswap/wantlist', {

src/bitswap/wantlist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
88
/**
9-
* @type {import('../../../ipfs/src/core/components/bitswap/wantlist').WantlistFn<import('..').HttpOptions>}
9+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/wantlist')>}
1010
*/
1111
async function wantlist (options = {}) {
1212
const res = await (await api.post('bitswap/wantlist', {

src/block/get.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
77

88
module.exports = configure(api => {
99
/**
10-
* @type {import('../../../ipfs/src/core/components/block/get').BlockGet<import('..').HttpOptions>}
10+
* @type {import('..').Implements<import('ipfs-core/src/components/block/get')>}
1111
*/
1212
async function get (cid, options = {}) {
13+
// @ts-ignore - CID|string seems to confuse typedef
1314
cid = new CID(cid)
1415

1516
const res = await api.post('block/get', {

src/block/put.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const multihash = require('multihashes')
66
const multipartRequest = require('../lib/multipart-request')
77
const configure = require('../lib/configure')
88
const toUrlSearchParams = require('../lib/to-url-search-params')
9-
const anySignal = require('any-signal')
9+
const { anySignal } = require('any-signal')
1010
const AbortController = require('native-abort-controller')
1111

1212
module.exports = configure(api => {
1313
/**
14-
* @type {import('../../../ipfs/src/core/components/block/put').BlockPut<import('..').HttpOptions>}
14+
* @type {import('..').Implements<import('ipfs-core/src/components/block/put')>}
1515
*/
1616
async function put (data, options = {}) {
1717
if (Block.isBlock(data)) {

src/block/rm.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

77
module.exports = configure(api => {
8-
return async function * rm (cid, options = {}) {
8+
/**
9+
* @type {import('..').Implements<import('ipfs-core/src/components/block/rm')>}
10+
*/
11+
async function * rm (cid, options = {}) {
912
if (!Array.isArray(cid)) {
1013
cid = [cid]
1114
}
@@ -25,6 +28,8 @@ module.exports = configure(api => {
2528
yield toCoreInterface(removed)
2629
}
2730
}
31+
32+
return rm
2833
})
2934

3035
function toCoreInterface (removed) {

src/block/stat.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ 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+
/**
9+
* @type {import('..').Implements<import('ipfs-core/src/components/block/stat')>}
10+
*/
11+
async function stat (cid, options = {}) {
912
const res = await api.post('block/stat', {
1013
timeout: options.timeout,
1114
signal: options.signal,
@@ -19,4 +22,6 @@ module.exports = configure(api => {
1922

2023
return { cid: new CID(data.Key), size: data.Size }
2124
}
25+
26+
return stat
2227
})

src/bootstrap/add.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use strict'
22

3-
const Multiaddr = require('multiaddr')
43
const configure = require('../lib/configure')
54
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
const Multiaddr = require('multiaddr')
66

77
module.exports = configure(api => {
8-
return async (addr, options = {}) => {
9-
if (addr && typeof addr === 'object' && !Multiaddr.isMultiaddr(addr)) {
10-
options = addr
11-
addr = null
12-
}
13-
8+
/**
9+
* @type {import('..').Implements<import('ipfs-core/src/components/bootstrap/add')>}
10+
*/
11+
async function add (addr, options = {}) {
1412
const res = await api.post('bootstrap/add', {
1513
timeout: options.timeout,
1614
signal: options.signal,
@@ -21,6 +19,10 @@ module.exports = configure(api => {
2119
headers: options.headers
2220
})
2321

24-
return res.json()
22+
const { Peers } = await res.json()
23+
24+
return { Peers: Peers.map(ma => new Multiaddr(ma)) }
2525
}
26+
27+
return add
2628
})

src/bootstrap/clear.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
const configure = require('../lib/configure')
44
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
const Multiaddr = require('multiaddr')
56

67
module.exports = configure(api => {
7-
return async (options = {}) => {
8+
/**
9+
* @type {import('..').Implements<import('ipfs-core/src/components/bootstrap/clear')>}
10+
*/
11+
async function clear (options = {}) {
812
const res = await api.post('bootstrap/rm', {
913
timeout: options.timeout,
1014
signal: options.signal,
@@ -15,6 +19,10 @@ module.exports = configure(api => {
1519
headers: options.headers
1620
})
1721

18-
return res.json()
22+
const { Peers } = await res.json()
23+
24+
return { Peers: Peers.map(ma => new Multiaddr(ma)) }
1925
}
26+
27+
return clear
2028
})

src/bootstrap/list.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
const configure = require('../lib/configure')
44
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
const Multiaddr = require('multiaddr')
56

67
module.exports = configure(api => {
7-
return async (options = {}) => {
8+
/**
9+
* @type {import('..').Implements<import('ipfs-core/src/components/bootstrap/list')>}
10+
*/
11+
async function list (options = {}) {
812
const res = await api.post('bootstrap/list', {
913
timeout: options.timeout,
1014
signal: options.signal,
1115
searchParams: toUrlSearchParams(options),
1216
headers: options.headers
1317
})
1418

15-
return res.json()
19+
const { Peers } = await res.json()
20+
21+
return { Peers: Peers.map(ma => new Multiaddr(ma)) }
1622
}
23+
24+
return list
1725
})

src/bootstrap/reset.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
const configure = require('../lib/configure')
44
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
const Multiaddr = require('multiaddr')
56

67
module.exports = configure(api => {
7-
return async (options = {}) => {
8+
/**
9+
* @type {import('..').Implements<import('ipfs-core/src/components/bootstrap/reset')>}
10+
*/
11+
async function reset (options = {}) {
812
const res = await api.post('bootstrap/add', {
913
timeout: options.timeout,
1014
signal: options.signal,
@@ -15,6 +19,10 @@ module.exports = configure(api => {
1519
headers: options.headers
1620
})
1721

18-
return res.json()
22+
const { Peers } = await res.json()
23+
24+
return { Peers: Peers.map(ma => new Multiaddr(ma)) }
1925
}
26+
27+
return reset
2028
})

0 commit comments

Comments
 (0)