Skip to content

Commit 396a9d2

Browse files
Gozalaachingbrain
andauthored
fix: typedef resolution & add examples that use types (#3359)
1. addresses #3356 in a different way based no findings in #3358. 2. Adds ts project example that uses ipfs and tests that it type checks. 3. Adds js project example that uses ipfs and runs type checker to ensure types are picked up and inferred. 4. Changes all the `ReturnType<import(...)>`'s to `ReturnType<typeof import(...)>` as former seems to raise errors in stricter TS setup. Co-authored-by: achingbrain <[email protected]>
1 parent eb961a5 commit 396a9d2

File tree

22 files changed

+23
-22
lines changed

22 files changed

+23
-22
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"typesVersions": {
2424
"*": {
2525
"*": [
26-
"dist/*"
26+
"dist/*",
27+
"dist/*/index"
2728
]
2829
}
2930
},

src/add-all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const AbortController = require('native-abort-controller')
1010

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

src/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = (options) => {
1111
const all = addAll(options)
1212
return configure(() => {
1313
/**
14-
* @type {import('.').Implements<import('ipfs-core/src/components/add')>}
14+
* @type {import('.').Implements<typeof import('ipfs-core/src/components/add')>}
1515
*/
1616
async function add (input, options = {}) {
1717
// @ts-ignore - last may return undefind if source is empty

src/bitswap/unwant.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('..').Implements<import('ipfs-core/src/components/bitswap/unwant')>}
9+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/unwant')>}
1010
*/
1111
async function unwant (cid, options = {}) {
1212
const res = await api.post('bitswap/unwant', {

src/block/get.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('..').Implements<import('ipfs-core/src/components/block/get')>}
10+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/block/get')>}
1111
*/
1212
async function get (cid, options = {}) {
1313
// @ts-ignore - CID|string seems to confuse typedef

src/block/put.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const AbortController = require('native-abort-controller')
1111

1212
module.exports = configure(api => {
1313
/**
14-
* @type {import('..').Implements<import('ipfs-core/src/components/block/put')>}
14+
* @type {import('..').Implements<typeof 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: 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('..').Implements<import('ipfs-core/src/components/block/rm')>}
9+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/block/rm')>}
1010
*/
1111
async function * rm (cid, options = {}) {
1212
if (!Array.isArray(cid)) {

src/block/stat.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('..').Implements<import('ipfs-core/src/components/block/stat')>}
9+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/block/stat')>}
1010
*/
1111
async function stat (cid, options = {}) {
1212
const res = await api.post('block/stat', {

src/bootstrap/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Multiaddr = require('multiaddr')
66

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

src/bootstrap/clear.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Multiaddr = require('multiaddr')
66

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

src/bootstrap/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Multiaddr = require('multiaddr')
66

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

src/bootstrap/reset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Multiaddr = require('multiaddr')
66

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

src/bootstrap/rm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Multiaddr = require('multiaddr')
66

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

src/cat.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('.').Implements<import('ipfs-core/src/components/cat')>}
9+
* @type {import('.').Implements<typeof import('ipfs-core/src/components/cat')>}
1010
*/
1111
async function * cat (path, options = {}) {
1212
const res = await api.post('cat', {

src/dag/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = configure((api, opts) => {
1010
const load = loadFormat(opts.ipld)
1111

1212
/**
13-
* @type {import('..').Implements<import('ipfs-core/src/components/dag/get')>}
13+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/dag/get')>}
1414
*/
1515
const get = async (cid, options = {}) => {
1616
const resolved = await dagResolve(cid, options)

src/dag/put.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = configure((api, opts) => {
1414
const load = loadFormat(opts.ipld)
1515

1616
/**
17-
* @type {import('..').Implements<import('ipfs-core/src/components/dag/put')>}
17+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/dag/put')>}
1818
*/
1919
const put = async (dagNode, options = {}) => {
2020
if (options.cid && (options.format || options.hashAlg)) {

src/dag/resolve.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('..').Implements<import('ipfs-core/src/components/dag/resolve')>}
9+
* @type {import('..').Implements<typeof import('ipfs-core/src/components/dag/resolve')>}
1010
*/
1111
const resolve = async (ipfsPath, options = {}) => {
1212
const res = await api.post('dag/resolve', {

src/dns.js

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

66
module.exports = configure(api => {
77
/**
8-
* @type {import('.').Implements<import('ipfs-core/src/components/dns')>}
8+
* @type {import('.').Implements<typeof import('ipfs-core/src/components/dns')>}
99
*/
1010
const dns = async (domain, options = {}) => {
1111
const res = await api.post('dns', {

src/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const map = require('it-map')
88

99
module.exports = configure(api => {
1010
/**
11-
* @type {import('.').Implements<import('ipfs-core/src/components/get')>}
11+
* @type {import('.').Implements<typeof import('ipfs-core/src/components/get')>}
1212
*/
1313
async function * get (path, options = {}) {
1414
const res = await api.post('get', {

src/id.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('.').Implements<import('ipfs-core/src/components/id')>}
10+
* @type {import('.').Implements<typeof import('ipfs-core/src/components/id')>}
1111
*/
1212
async function id (options = {}) {
1313
const res = await api.post('id', {

src/resolve.js

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

66
module.exports = configure(api => {
77
/**
8-
* @type {import('.').Implements<import('ipfs-core/src/components/resolve')>}
8+
* @type {import('.').Implements<typeof import('ipfs-core/src/components/resolve')>}
99
*/
1010
async function resolve (path, options = {}) {
1111
const res = await api.post('resolve', {

src/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pkg = require('../package.json')
77

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

0 commit comments

Comments
 (0)