Skip to content

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 <contact@gozala.io>
Co-authored-by: Alex Potsides <alex@achingbrain.net>
  • Loading branch information
3 people authored Sep 3, 2020
1 parent ba83a0a commit 1502822
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 26 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
"shunkin <hiyoko.san.ipod@gmail.com>",
"victorbjelkholm <victorbjelkholm@gmail.com>",
"Łukasz Magiera <magik6k@users.noreply.github.com>",
"Łukasz Magiera <magik6k@gmail.com>"
"Łukasz Magiera <magik6k@gmail.com>",
"Xmader <xmader@outlook.com>"
]
}
17 changes: 15 additions & 2 deletions 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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
2 changes: 1 addition & 1 deletion src/pubsub/subscription-tracker.js
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
7 changes: 6 additions & 1 deletion src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const configure = require('./lib/configure')
const toUrlSearchParams = require('./lib/to-url-search-params')

module.exports = configure(api => {
return async (path, options = {}) => {
// eslint-disable-next-line valid-jsdoc
/**
* @type {import('../../ipfs/src/core/components/resolve').Resolve<import('.').HttpOptions>}
*/
async function resolve (path, options = {}) {
const res = await api.post('resolve', {
timeout: options.timeout,
signal: options.signal,
Expand All @@ -17,4 +21,5 @@ module.exports = configure(api => {
const { Path } = await res.json()
return Path
}
return resolve
})
7 changes: 6 additions & 1 deletion src/version.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/version').Version<import('.').HttpOptions>}
*/
async function version (options = {}) {
const res = await api.post('version', {
timeout: options.timeout,
signal: options.signal,
Expand All @@ -16,4 +20,5 @@ module.exports = configure(api => {

return toCamel(data)
}
return version
})

0 comments on commit 1502822

Please sign in to comment.