Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Generate Typescript Typings via JsDoc #95

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions .jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"tags": {
"allowUnknownTags": true
},
"source": {
"include": ["src/"],
"exclude": ["node_modules"]
},
"opts": {
"destination": "src/",
"template": "./node_modules/tsd-jsdoc/dist"
},
"plugins": ["./node_modules/jsdoc-export-default-interop/dist/index"]

}
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Node.js IPFS Implementation of the railing process of a Node through a bootstrap peer list",
"leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>",
"main": "src/index.js",
"typings": "src/types.d.ts",
"scripts": {
"lint": "aegir lint",
"test": "aegir test",
Expand All @@ -14,7 +15,8 @@
"release-minor": "aegir release --type minor -t node",
"release-major": "aegir release --type major -t node",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage publish"
"coverage-publish": "aegir coverage publish",
"generate-typings": "./node_modules/.bin/jsdoc -c ./.jsdoc.json"
},
"keywords": [
"IPFS"
Expand All @@ -29,7 +31,11 @@
},
"devDependencies": {
"aegir": "^20.0.0",
"chai": "^4.2.0"
"chai": "^4.2.0",
"jsdoc": "^3.6.3",
"jsdoc-export-default-interop": "^0.3.1",
"tsd-jsdoc": "^2.3.1",
"typescript-definition-tester": "0.0.6"
},
"dependencies": {
"debug": "^4.1.1",
Expand Down Expand Up @@ -59,6 +65,7 @@
"Zane Starr <zcstarr@gmail.com>",
"dirkmc <dirkmdev@gmail.com>",
"victorbjelkholm <victor@protocol.ai>",
"ころ <koropicot@gmail.com>"
"ころ <koropicot@gmail.com>",
"omidiora samuel <samparsky@gmail.com>"
]
}
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @module libp2p-bootstrap
*/
'use strict'

const PeerId = require('peer-id')
Expand All @@ -11,15 +14,17 @@ const log = debug('libp2p:bootstrap')
log.error = debug('libp2p:bootstrap:error')

/**
* Emits 'peer' events on a regular interval for each peer in the provided list.
* @class
* @memberof module:libp2p-bootstrap
*/
class Bootstrap extends EventEmitter {
/**
* Constructs a new Bootstrap.
* Emits 'peer' events on a regular interval for each peer in the provided list
*
* @constructs
* @param {Object} options
* @param {Array<string>} options.list - the list of peer addresses in multi-address format
* @param {number} options.interval - the interval between emitting addresses (in milli-seconds)
* @param {number} [options.interval] - the interval between emitting addresses (in milli-seconds)
*
*/
constructor (options) {
Expand All @@ -44,6 +49,7 @@ class Bootstrap extends EventEmitter {

/**
* Emit each address in the list as a PeerInfo.
* @ignore
*/
_discoverBootstrapPeers () {
this._list.forEach(async (candidate) => {
Expand Down Expand Up @@ -75,6 +81,8 @@ class Bootstrap extends EventEmitter {
}
}
}

exports = module.exports = Bootstrap
/** Tag
* @type string
*/
exports.tag = 'bootstrap'
46 changes: 46 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @module libp2p-bootstrap
*/
declare module "libp2p-bootstrap" {
/**
* Emits 'peer' events on a regular interval for each peer in the provided list.
*
* @constructs
* @param {Object} options
* @param {Array<string>} options.list - the list of peer addresses in multi-address format
* @param {number} [options.interval] - the interval between emitting addresses (in milli-seconds)
*
*/
class Bootstrap {
constructor(options: {
list: string[];
interval?: number;
});
/**
* Start emitting events.
*/
start(): void;
/**
* Stop emitting events.
*/
stop(): void;
}
/** Tag
* @type string
*/
var tag: string;

/**
* NB: Always include the snippet below because its not
* generated by tsd-jsdoc else the test would keep failing
*
* Due to some wierd behaviour in Node.JS module resolution
* it's not possible to access 'Bootstrap' via dot notation
* even though its exported as
* ```
* exports = module.exports = Bootstrap
* ```
*/
export default Bootstrap;
}

29 changes: 29 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-env mocha */
'use strict'
/// <reference path="../../src/types.d.ts" />

const tt = require('typescript-definition-tester')
// typings test should run only in the node
// environment due to reading of files
describe('typings declaration tests', () => {
it('should compile typings examples successfully against types.d.ts', (done) => {
tt.compileDirectory(
`${__dirname}/typings`,
(fileName) => fileName.indexOf('.pass.ts') > -1,
(error) => done(error)
)
})

it('should fail to compile typings examples successfully against types.d.ts', (done) => {
tt.compileDirectory(
`${__dirname}/typings`,
(fileName) => fileName.indexOf('.fail.ts') > -1,
(error) => {
if (error) {
return done(null)
}
done(new Error('Should throw compilation error as Bootstrap is default export'))
}
)
})
})
4 changes: 4 additions & 0 deletions test/typings/test.fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// <reference path="../../src/types.d.ts" />
import { Bootstrap } from "libp2p-bootstrap";
const bootstrap1 = new Bootstrap({list: ["item"], interval: 1})
bootstrap1.start()
6 changes: 6 additions & 0 deletions test/typings/test2.pass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path="../../src/types.d.ts" />

import Bootstrap from "libp2p-bootstrap";
const bootstrap1 = new Bootstrap({list: ["item"], interval: 1})
bootstrap1.start()
bootstrap1.stop()