Skip to content

Commit cacbfc6

Browse files
GozalaachingbrainXmader
authored
fix: export IPFS type (ipfs#3447)
`IPFS` type is exported with its name so that user in ts can write `import { IPFS, create } from "ipfs"` and equivalent in js as well. Closes: ipfs#3439 Co-authored-by: achingbrain <alex@achingbrain.net> Co-authored-by: Xmader <xmader@outlook.com>
1 parent eceb0d4 commit cacbfc6

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

Diff for: examples/types-use-ipfs-from-ts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "types-use-ipfs-from-ts",
2+
"name": "example-types-use-ipfs-from-ts",
33
"private": true,
44
"dependencies": {
55
"ipfs": "^0.52.1"

Diff for: examples/types-use-ipfs-from-ts/src/main.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import IPFS from 'ipfs'
1+
import { IPFS, create } from 'ipfs'
2+
import CID from 'cids'
23

3-
export default async function main () {
4-
const node = await IPFS.create()
4+
export default async function main() {
5+
const node = await create()
56
const version = await node.version()
67

78
console.log('Version:', version.version)
@@ -13,16 +14,23 @@ export default async function main () {
1314

1415
console.log('Added file:', file.path, file.cid.toString())
1516
try {
17+
// @ts-expect-error CID has no toUpperCase method
1618
file.cid.toUpperCase()
17-
} catch(error) {
19+
} catch (error) {
1820

1921
}
2022

23+
const content = await readFile(node, file.cid)
24+
25+
console.log('Added file contents:', content)
26+
}
27+
28+
const readFile = async (ipfs: IPFS, cid: CID): Promise<string> => {
2129
const decoder = new TextDecoder()
2230
let content = ''
23-
for await (const chunk of node.cat(file.cid)) {
24-
content += decoder.decode(chunk)
31+
for await (const chunk of ipfs.cat(cid)) {
32+
content += decoder.decode(chunk)
2533
}
2634

27-
console.log('Added file contents:', content)
35+
return content
2836
}

Diff for: examples/types-use-ipfs-from-typed-js/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "types-use-ipfs-from-typed-js",
2+
"name": "example-types-use-ipfs-from-typed-js",
33
"private": true,
44
"dependencies": {
55
"ipfs": "^0.52.1"

Diff for: examples/types-use-ipfs-from-typed-js/src/main.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const IPFS = require('ipfs')
1+
const { create } = require('ipfs')
2+
/**
3+
* @typedef {import('ipfs').IPFS} IPFS
4+
* @typedef {import('cids')} CID
5+
*/
26

37
async function main () {
4-
const node = await IPFS.create()
8+
const node = await create()
59
const version = await node.version()
610

711
console.log('Version:', version.version)
@@ -13,18 +17,29 @@ async function main () {
1317

1418
console.log('Added file:', file.path, file.cid.toString())
1519
try {
20+
// @ts-expect-error CID has no toUpperCase method
1621
file.cid.toUpperCase()
1722
} catch(error) {
1823

1924
}
2025

21-
const decoder = new TextDecoder()
22-
let content = ''
23-
for await (const chunk of node.cat(file.cid)) {
24-
content += decoder.decode(chunk)
25-
}
26+
const content = await readFile(node, file.cid)
2627

2728
console.log('Added file contents:', content)
2829
}
2930

31+
/**
32+
* @param {IPFS} ipfs
33+
* @param {CID} cid
34+
* @returns {Promise<string>}
35+
*/
36+
const readFile = async (ipfs, cid) => {
37+
const decoder = new TextDecoder()
38+
let content = ''
39+
for await (const chunk of ipfs.cat(cid)) {
40+
content += decoder.decode(chunk)
41+
}
42+
return content
43+
}
44+
3045
main()

Diff for: packages/ipfs-core/src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ const multicodec = require('multicodec')
1111
const multihashing = require('multihashing-async')
1212
const multihash = multihashing.multihash
1313
const CID = require('cids')
14-
const IPFS = require('./components')
14+
const { create } = require('./components')
15+
16+
/**
17+
* @typedef {import('./components')} IPFS
18+
*/
1519

1620
module.exports = {
17-
create: IPFS.create,
21+
create,
1822
crypto,
1923
isIPFS,
2024
CID,

Diff for: packages/ipfs/src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3-
const IPFS = require('ipfs-core')
3+
/**
4+
* @typedef {import('ipfs-core/src/components')} IPFS
5+
*/
46

5-
module.exports = IPFS
7+
module.exports = { ...require('ipfs-core') }

0 commit comments

Comments
 (0)