diff --git a/examples/types-use-ipfs-from-ts/package.json b/examples/types-use-ipfs-from-ts/package.json index 06d6482147..f36edbd714 100644 --- a/examples/types-use-ipfs-from-ts/package.json +++ b/examples/types-use-ipfs-from-ts/package.json @@ -1,5 +1,5 @@ { - "name": "types-use-ipfs-from-ts", + "name": "example-types-use-ipfs-from-ts", "private": true, "dependencies": { "ipfs": "^0.52.1" diff --git a/examples/types-use-ipfs-from-ts/src/main.ts b/examples/types-use-ipfs-from-ts/src/main.ts index c066d324df..a6763cade9 100644 --- a/examples/types-use-ipfs-from-ts/src/main.ts +++ b/examples/types-use-ipfs-from-ts/src/main.ts @@ -1,7 +1,8 @@ -import IPFS from 'ipfs' +import { IPFS, create } from 'ipfs' +import CID from 'cids' -export default async function main () { - const node = await IPFS.create() +export default async function main() { + const node = await create() const version = await node.version() console.log('Version:', version.version) @@ -13,16 +14,23 @@ export default async function main () { console.log('Added file:', file.path, file.cid.toString()) try { + // @ts-expect-error CID has no toUpperCase method file.cid.toUpperCase() - } catch(error) { + } catch (error) { } + const content = await readFile(node, file.cid) + + console.log('Added file contents:', content) +} + +const readFile = async (ipfs: IPFS, cid: CID): Promise => { const decoder = new TextDecoder() let content = '' - for await (const chunk of node.cat(file.cid)) { - content += decoder.decode(chunk) + for await (const chunk of ipfs.cat(cid)) { + content += decoder.decode(chunk) } - console.log('Added file contents:', content) + return content } diff --git a/examples/types-use-ipfs-from-typed-js/package.json b/examples/types-use-ipfs-from-typed-js/package.json index 87eb9000f5..c5912b4e55 100644 --- a/examples/types-use-ipfs-from-typed-js/package.json +++ b/examples/types-use-ipfs-from-typed-js/package.json @@ -1,5 +1,5 @@ { - "name": "types-use-ipfs-from-typed-js", + "name": "example-types-use-ipfs-from-typed-js", "private": true, "dependencies": { "ipfs": "^0.52.1" diff --git a/examples/types-use-ipfs-from-typed-js/src/main.js b/examples/types-use-ipfs-from-typed-js/src/main.js index 79af577bcb..0ff2166786 100644 --- a/examples/types-use-ipfs-from-typed-js/src/main.js +++ b/examples/types-use-ipfs-from-typed-js/src/main.js @@ -1,7 +1,11 @@ -const IPFS = require('ipfs') +const { create } = require('ipfs') +/** + * @typedef {import('ipfs').IPFS} IPFS + * @typedef {import('cids')} CID + */ async function main () { - const node = await IPFS.create() + const node = await create() const version = await node.version() console.log('Version:', version.version) @@ -13,18 +17,29 @@ async function main () { console.log('Added file:', file.path, file.cid.toString()) try { + // @ts-expect-error CID has no toUpperCase method file.cid.toUpperCase() } catch(error) { } - const decoder = new TextDecoder() - let content = '' - for await (const chunk of node.cat(file.cid)) { - content += decoder.decode(chunk) - } + const content = await readFile(node, file.cid) console.log('Added file contents:', content) } +/** + * @param {IPFS} ipfs + * @param {CID} cid + * @returns {Promise} + */ +const readFile = async (ipfs, cid) => { + const decoder = new TextDecoder() + let content = '' + for await (const chunk of ipfs.cat(cid)) { + content += decoder.decode(chunk) + } + return content +} + main() diff --git a/packages/ipfs-core/src/index.js b/packages/ipfs-core/src/index.js index bef8d923e3..f52ec102c6 100644 --- a/packages/ipfs-core/src/index.js +++ b/packages/ipfs-core/src/index.js @@ -11,10 +11,14 @@ const multicodec = require('multicodec') const multihashing = require('multihashing-async') const multihash = multihashing.multihash const CID = require('cids') -const IPFS = require('./components') +const { create } = require('./components') + +/** + * @typedef {import('./components')} IPFS + */ module.exports = { - create: IPFS.create, + create, crypto, isIPFS, CID, diff --git a/packages/ipfs/src/index.js b/packages/ipfs/src/index.js index 0ca85a967e..378329035b 100644 --- a/packages/ipfs/src/index.js +++ b/packages/ipfs/src/index.js @@ -1,5 +1,7 @@ 'use strict' -const IPFS = require('ipfs-core') +/** + * @typedef {import('ipfs-core/src/components')} IPFS + */ -module.exports = IPFS +module.exports = { ...require('ipfs-core') }