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

Commit

Permalink
feat: add typeScript support (#3236)
Browse files Browse the repository at this point in the history
TypeScript support for `ipfs` and `ipfs-http-client`

Refs: #2945
Refs: #1166 

Co-authored-by: Irakli Gozalishvili <[email protected]>
Co-authored-by: Alex Potsides <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2020
1 parent ce0d093 commit be26dd7
Show file tree
Hide file tree
Showing 48 changed files with 596 additions and 99 deletions.
10 changes: 5 additions & 5 deletions docs/core-api/FILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ One of `path` or `content` _must_ be passed.
`FileContent` is one of the following types:

```js
Uint8Array | Blob | String | Iterable<Uint8Array|Number> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>
Uint8Array | Blob | String | Iterable<Uint8Array> | Iterable<number> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>
```

`UnixTime` is one of the following types:
Expand All @@ -162,7 +162,7 @@ An optional object which may have the following keys:

| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| chunker | `String` | `'size-262144` | chunking algorithm used to build ipfs DAGs |
| chunker | `String` | `'size-262144'` | chunking algorithm used to build ipfs DAGs |
| cidVersion | `Number` | `0` | the CID version to use when storing the data |
| hashAlg | `String` | `'sha2-256'` | multihash hashing algorithm to use |
| onlyHash | `boolean` | `false` | If true, will not add blocks to the blockstore |
Expand All @@ -178,7 +178,7 @@ An optional object which may have the following keys:

| Type | Description |
| -------- | -------- |
| `UnixFSEntry` | A object describing the added data |
| `Promise<UnixFSEntry>` | A object describing the added data |

Each yielded object is of the form:

Expand Down Expand Up @@ -226,7 +226,7 @@ Now [ipfs.io/ipfs/Qm..pg/myfile.txt](https://ipfs.io/ipfs/QmWXdjNC362aPDtwHPUE9o

| Name | Type | Description |
| ---- | ---- | ----------- |
| source | [FileStream<FileContent|FileObject>](#filestream) | Data to import (see below) |
| source | [FileStream<FileContent\|FileObject>](#filestream) | Data to import (see below) |

##### FileStream

Expand All @@ -242,7 +242,7 @@ An optional object which may have the following keys:

| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| chunker | `String` | `'size-262144` | chunking algorithm used to build ipfs DAGs |
| chunker | `String` | `'size-262144'` | chunking algorithm used to build ipfs DAGs |
| cidVersion | `Number` | `0` | the CID version to use when storing the data |
| enableShardingExperiment | `boolean` | `false` | allows to create directories with an unlimited number of entries currently size of unixfs directories is limited by the maximum block size. Note that this is an experimental feature |
| hashAlg | `String` | `'sha2-256'` | multihash hashing algorithm to use |
Expand Down
2 changes: 1 addition & 1 deletion docs/core-api/REPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ An optional object which may have the following keys:

| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| options | `boolean` | `false` | Return storage numbers in `MiB` |
| human | `boolean` | `false` | Return storage numbers in `MiB` |
| timeout | `Number` | `undefined` | A timeout in ms |
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |

Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
"shunkin <[email protected]>",
"victorbjelkholm <[email protected]>",
"Łukasz Magiera <[email protected]>",
"Łukasz Magiera <[email protected]>"
"Łukasz Magiera <[email protected]>",
"Xmader <[email protected]>"
]
}
17 changes: 15 additions & 2 deletions packages/ipfs-http-client/src/add-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const configure = require('./lib/configure')
const multipartRequest = require('./lib/multipart-request')
const toUrlSearchParams = require('./lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure((api) => {
return async function * addAll (input, options = {}) {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../ipfs/src/core/components/add-all').AddAll<import('.').HttpOptions>}
*/
async function * addAll (input, options = {}) {
const progressFn = options.progress

// allow aborting requests on body errors
Expand Down Expand Up @@ -39,8 +43,16 @@ module.exports = configure((api) => {
}
}
}
return addAll
})

/**
* @typedef {import('../../ipfs/src/core/components/add-all').UnixFSEntry} UnixFSEntry
*/

/**
* @returns {UnixFSEntry}
*/
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
const output = {
path: name,
Expand All @@ -59,5 +71,6 @@ function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
}
}

// @ts-ignore
return output
}
16 changes: 15 additions & 1 deletion packages/ipfs-http-client/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ const addAll = require('./add-all')
const last = require('it-last')
const configure = require('./lib/configure')

/**
* @typedef {import("./lib/core").ClientOptions} ClientOptions
*/

// eslint-disable-next-line valid-jsdoc
/**
* @param {ClientOptions} options
*/
module.exports = (options) => {
const all = addAll(options)

return configure(() => {
return async function add (input, options = {}) { // eslint-disable-line require-await
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../ipfs/src/core/components/add').Add<import('.').HttpOptions>}
*/
async function add (input, options = {}) { // eslint-disable-line require-await
// @ts-ignore
return last(all(input, options))
}
return add
})(options)
}
7 changes: 6 additions & 1 deletion packages/ipfs-http-client/src/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../../ipfs/src/core/components/bitswap/stat').Stat<import('..').HttpOptions>}
*/
async function stat (options = {}) {
const res = await api.post('bitswap/stat', {
searchParams: toUrlSearchParams(options),
timeout: options.timeout,
Expand All @@ -16,6 +20,7 @@ module.exports = configure(api => {

return toCoreInterface(await res.json())
}
return stat
})

function toCoreInterface (res) {
Expand Down
7 changes: 6 additions & 1 deletion packages/ipfs-http-client/src/bitswap/unwant.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (cid, options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../../ipfs/src/core/components/bitswap/unwant').Unwant<import('..').HttpOptions>}
*/
async function unwant (cid, options = {}) {
const res = await api.post('bitswap/unwant', {
timeout: options.timeout,
signal: options.signal,
Expand All @@ -18,4 +22,5 @@ module.exports = configure(api => {

return res.json()
}
return unwant
})
7 changes: 6 additions & 1 deletion packages/ipfs-http-client/src/bitswap/wantlist-for-peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (peerId, options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../../ipfs/src/core/components/bitswap/wantlist-for-peer').WantlistForPeer<import('..').HttpOptions>}
*/
async function wantlistForPeer (peerId, options = {}) {
peerId = typeof peerId === 'string' ? peerId : new CID(peerId).toString()

const res = await (await api.post('bitswap/wantlist', {
Expand All @@ -20,4 +24,5 @@ module.exports = configure(api => {

return (res.Keys || []).map(k => new CID(k['/']))
}
return wantlistForPeer
})
7 changes: 6 additions & 1 deletion packages/ipfs-http-client/src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../../ipfs/src/core/components/bitswap/wantlist').WantlistFn<import('..').HttpOptions>}
*/
async function wantlist (options = {}) {
const res = await (await api.post('bitswap/wantlist', {
timeout: options.timeout,
signal: options.signal,
Expand All @@ -15,4 +19,5 @@ module.exports = configure(api => {

return (res.Keys || []).map(k => new CID(k['/']))
}
return wantlist
})
7 changes: 6 additions & 1 deletion packages/ipfs-http-client/src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (cid, options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../../ipfs/src/core/components/block/get').BlockGet<import('..').HttpOptions>}
*/
async function get (cid, options = {}) {
cid = new CID(cid)

const res = await api.post('block/get', {
Expand All @@ -21,4 +25,5 @@ module.exports = configure(api => {

return new Block(new Uint8Array(await res.arrayBuffer()), cid)
}
return get
})
6 changes: 5 additions & 1 deletion packages/ipfs-http-client/src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure(api => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../../ipfs/src/core/components/block/put').BlockPut<import('..').HttpOptions>}
*/
async function put (data, options = {}) {
if (Block.isBlock(data)) {
const { name, length } = multihash.decode(data.cid.multihash)
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/config/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure(api => {
return async (config, options = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const configure = require('../lib/configure')
const multipartRequest = require('../lib/multipart-request')
const toUrlSearchParams = require('../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')
const multicodec = require('multicodec')

module.exports = configure((api, opts) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/files/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const configure = require('../lib/configure')
const multipartRequest = require('../lib/multipart-request')
const toUrlSearchParams = require('../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure(api => {
return async (path, input, options = {}) => {
Expand Down
7 changes: 6 additions & 1 deletion packages/ipfs-http-client/src/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const configure = require('./lib/configure')
const toUrlSearchParams = require('./lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../ipfs/src/core/components/id').Id<import('.').HttpOptions>}
*/
async function id (options = {}) {
const res = await api.post('id', {
timeout: options.timeout,
signal: options.signal,
Expand All @@ -23,4 +27,5 @@ module.exports = configure(api => {

return output
}
return id
})
14 changes: 11 additions & 3 deletions packages/ipfs-http-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ const urlSource = require('ipfs-utils/src/files/url-source')
*/

/**
*
* @param {ClientOptions } options
* @return {Object}
* @typedef {object} HttpOptions
* @property {Headers | Record<string, string>} [headers] - An object or [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) instance that can be used to set custom HTTP headers. Note that this option can also be [configured globally](#custom-headers) via the constructor options.
* @property {URLSearchParams | Record<string, string>} [searchParams] - An object or [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) instance that can be used to add additional query parameters to the query string sent with each request.
* @property {object} [ipld]
* @property {any[]} [ipld.formats] - An array of additional [IPLD formats](https://github.com/ipld/interface-ipld-format) to support
* @property {(format: string) => Promise<any>} [ipld.loadFormat] - an async function that takes the name of an [IPLD format](https://github.com/ipld/interface-ipld-format) as a string and should return the implementation of that codec
*/

// eslint-disable-next-line valid-jsdoc
/**
* @param {ClientOptions} options
*/
function ipfsClient (options = {}) {
return {
Expand Down
15 changes: 13 additions & 2 deletions packages/ipfs-http-client/src/lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ const Client = require('./core')
*/

/**
* @param {function(Client, ClientOptions): void} fn
* @returns {function(ClientOptions): void}
* @template T
* @typedef {(client: Client, clientOptions: ClientOptions) => T} Fn
*/

/**
* @template T
* @typedef {(clientOptions: ClientOptions) => T} Factory
*/

/**
* @template T
* @param {Fn<T>} fn
* @returns {Factory<T>}
*/
const configure = (fn) => {
return (options) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/ipfs-http-client/src/lib/to-url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
const modeToString = require('./mode-to-string')
const mtimeToObject = require('./mtime-to-object')

/**
* @param {object} params
* @returns {URLSearchParams}
*/
module.exports = ({ arg, searchParams, hashAlg, mtime, mode, ...options } = {}) => {
if (searchParams) {
options = {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/object/patch/append-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const multipartRequest = require('../../lib/multipart-request')
const configure = require('../../lib/configure')
const toUrlSearchParams = require('../../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure(api => {
return async (cid, data, options = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/object/patch/set-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const multipartRequest = require('../../lib/multipart-request')
const configure = require('../../lib/configure')
const toUrlSearchParams = require('../../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure(api => {
return async (cid, data, options = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')
const unit8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')

Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/pubsub/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
const multipartRequest = require('../lib/multipart-request')
const anySignal = require('any-signal')
const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

module.exports = configure(api => {
return async (topic, data, options = {}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const AbortController = require('abort-controller')
const { AbortController } = require('abort-controller')

class SubscriptionTracker {
constructor () {
Expand Down
Loading

0 comments on commit be26dd7

Please sign in to comment.