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

fix: export IPFS type #3447

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 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 @@ -14,15 +15,21 @@ export default async function main () {
console.log('Added file:', file.path, file.cid.toString())
try {
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
}
24 changes: 19 additions & 5 deletions examples/types-use-ipfs-from-typed-js/src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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()
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -18,13 +22,23 @@ async function main () {

}

const decoder = new TextDecoder()
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 node.cat(file.cid)) {
for await (const chunk of ipfs.cat(cid)) {
content += decoder.decode(chunk)
}

console.log('Added file contents:', content)
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') }