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

Commit

Permalink
fix: export IPFS type (#3447)
Browse files Browse the repository at this point in the history
`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: #3439

Co-authored-by: achingbrain <alex@achingbrain.net>
Co-authored-by: Xmader <xmader@outlook.com>
  • Loading branch information
3 people authored Dec 15, 2020
1 parent eceb0d4 commit cacbfc6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/types-use-ipfs-from-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "types-use-ipfs-from-ts",
"name": "example-types-use-ipfs-from-ts",
"private": true,
"dependencies": {
"ipfs": "^0.52.1"
Expand Down
22 changes: 15 additions & 7 deletions examples/types-use-ipfs-from-ts/src/main.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<string> => {
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
}
2 changes: 1 addition & 1 deletion examples/types-use-ipfs-from-typed-js/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
29 changes: 22 additions & 7 deletions examples/types-use-ipfs-from-typed-js/src/main.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<string>}
*/
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()
8 changes: 6 additions & 2 deletions packages/ipfs-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions packages/ipfs/src/index.js
Original file line number Diff line number Diff line change
@@ -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') }

0 comments on commit cacbfc6

Please sign in to comment.