Skip to content

Commit 87aa7b0

Browse files
author
Robert Kiel
authoredFeb 28, 2020
fix: update typings and add type test (#112)
* ♻︎ make defaults implicit and implicit custom options explicit * fix types and add type tests
1 parent 225873c commit 87aa7b0

File tree

5 files changed

+105
-72
lines changed

5 files changed

+105
-72
lines changed
 

‎src/index.d.ts

+47-35
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
/// <reference types="node" />
2-
3-
declare interface NetOptions {
4-
family: 'ipv4' | 'ipv6';
1+
declare type NetOptions = {
2+
family: "ipv4" | "ipv6";
53
host: string;
6-
transport: string;
4+
transport: "tcp" | "udp";
75
port: number;
8-
}
6+
};
97

10-
declare interface Protocol {
8+
declare type Protocol = {
119
code: number;
1210
size: number;
1311
name: string;
14-
resolvable: boolean;
15-
path: boolean;
16-
}
12+
resolvable: boolean | undefined;
13+
path: boolean | undefined;
14+
};
1715

1816
declare interface Protocols {
19-
table: [number, number, string, boolean?, boolean?][];
20-
names: Record<string, Protocol>;
21-
codes: Record<number, Protocol>;
22-
object(code: number, size: number, name: string, resolvable?: any, path?: any): Protocol;
17+
table: {
18+
[index: number]: Protocol;
19+
};
20+
names: {
21+
[index: string]: Protocol;
22+
};
23+
codes: {
24+
[index: number]: Protocol;
25+
};
26+
object(
27+
code: number,
28+
size: number,
29+
name: string,
30+
resolvable?: any,
31+
path?: any
32+
): Protocol;
2333
}
2434

25-
declare interface NodeAddress {
26-
family: 4 | 6;
35+
declare type NodeAddress = {
36+
family: "IPv4" | "IPv6";
2737
address: string;
28-
port: number;
29-
}
38+
port: string;
39+
};
3040

31-
declare type MultiaddrInput = string | Buffer | Multiaddr;
41+
declare type MultiaddrInput = string | Buffer | Multiaddr | null;
3242

3343
declare class Multiaddr {
3444
/**
@@ -40,6 +50,8 @@ declare class Multiaddr {
4050
*/
4151
constructor(addr?: MultiaddrInput);
4252

53+
buffer: Buffer;
54+
4355
/**
4456
* Returns Multiaddr as a String
4557
*/
@@ -142,20 +154,11 @@ declare class Multiaddr {
142154
isThinWaistAddress(addr?: Multiaddr): boolean;
143155
}
144156

145-
declare interface MultiaddrClass {
146-
/**
147-
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
148-
* a Buffer, String or another Multiaddr instance
149-
* public key.
150-
* @param addr - If String or Buffer, needs to adhere
151-
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
152-
*/
153-
(addr?: MultiaddrInput): Multiaddr;
154-
157+
declare namespace Multiaddr {
155158
/**
156159
* Creates a Multiaddr from a node-friendly address object
157160
*/
158-
fromNodeAddress(addr: NodeAddress, transport: string): Multiaddr;
161+
function fromNodeAddress(addr: NodeAddress, transport: string): Multiaddr;
159162

160163
/**
161164
* Object containing table, names and codes of all supported protocols.
@@ -164,22 +167,31 @@ declare interface MultiaddrClass {
164167
* [`.protoCodes()`](#multiaddrprotocodes) or
165168
* [`.protoNames()`](#multiaddrprotonames)
166169
*/
167-
protocols: Protocols;
170+
const protocols: Protocols;
168171

169172
/**
170173
* Returns if something is a Multiaddr
171174
*/
172-
isMultiaddr(addr: unknown): addr is Multiaddr;
175+
function isMultiaddr(addr: unknown): addr is Multiaddr;
173176

174177
/**
175178
* Returns if something is a Multiaddr that is a name
176179
*/
177-
isName(addr: Multiaddr): boolean;
180+
function isName(addr: Multiaddr): boolean;
178181

179182
/**
180183
* Returns an array of multiaddrs, by resolving the multiaddr that is a name
181184
*/
182-
resolve(addr: Multiaddr): Promise<Multiaddr[]>
185+
function resolve(addr: Multiaddr): Promise<Multiaddr[]>;
183186
}
184187

185-
export = MultiaddrClass;
188+
/**
189+
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
190+
* a Buffer, String or another Multiaddr instance
191+
* public key.
192+
* @param addr - If String or Buffer, needs to adhere
193+
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
194+
*/
195+
declare function Multiaddr(input?: MultiaddrInput): Multiaddr;
196+
197+
export = Multiaddr;

‎src/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,17 @@ Multiaddr.prototype.nodeAddress = function nodeAddress () {
413413
Multiaddr.fromNodeAddress = function fromNodeAddress (addr, transport) {
414414
if (!addr) throw new Error('requires node address object')
415415
if (!transport) throw new Error('requires transport protocol')
416-
const ip = (addr.family === 'IPv6') ? 'ip6' : 'ip4'
416+
let ip
417+
switch (addr.family) {
418+
case 'IPv4':
419+
ip = 'ip4'
420+
break
421+
case 'IPv6':
422+
ip = 'ip6'
423+
break
424+
default:
425+
throw Error(`Invalid addr family. Got '${addr.family}' instead of 'IPv4' or 'IPv6'`)
426+
}
417427
return Multiaddr('/' + [ip, addr.address, transport, addr.port].join('/'))
418428
}
419429

‎test/index.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,23 @@ describe('construction', () => {
5353

5454
it('throws on truthy non string or buffer', () => {
5555
const errRegex = /addr must be a string/
56+
// @ts-ignore
5657
expect(() => multiaddr({})).to.throw(errRegex)
58+
// @ts-ignore
5759
expect(() => multiaddr([])).to.throw(errRegex)
60+
// @ts-ignore
5861
expect(() => multiaddr(138)).to.throw(errRegex)
62+
// @ts-ignore
5963
expect(() => multiaddr(true)).to.throw(errRegex)
6064
})
6165

6266
it('throws on falsy non string or buffer', () => {
6367
const errRegex = /addr must be a string/
68+
// @ts-ignore
6469
expect(() => multiaddr(NaN)).to.throw(errRegex)
70+
// @ts-ignore
6571
expect(() => multiaddr(false)).to.throw(errRegex)
72+
// @ts-ignore
6673
expect(() => multiaddr(0)).to.throw(errRegex)
6774
})
6875
})
@@ -694,6 +701,7 @@ describe('helpers', () => {
694701
describe('.fromNodeAddress', () => {
695702
it('throws on missing address object', () => {
696703
expect(
704+
// @ts-ignore
697705
() => multiaddr.fromNodeAddress()
698706
).to.throw(
699707
/requires node address/
@@ -702,6 +710,7 @@ describe('helpers', () => {
702710

703711
it('throws on missing transport', () => {
704712
expect(
713+
// @ts-ignore
705714
() => multiaddr.fromNodeAddress({ address: '0.0.0.0' })
706715
).to.throw(
707716
/requires transport protocol/

‎test/types.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Multiaddr from "../src";
2+
3+
const testStr: string = "/ip4/127.0.0.1";
4+
5+
const maFromFunctionConstructor: Multiaddr = Multiaddr(testStr);
6+
7+
const maFromClassConstructor: Multiaddr = new Multiaddr(testStr);
8+
9+
const maFromMa: Multiaddr = Multiaddr(new Multiaddr(testStr));
10+
11+
const maFromConstructorFunction: Multiaddr = Multiaddr.fromNodeAddress(
12+
{
13+
family: "IPv4",
14+
address: "127.0.0.1",
15+
port: "12345"
16+
},
17+
"udp"
18+
);
19+
20+
function doSthWithMa(ma: Multiaddr): void {
21+
ma.toOptions();
22+
}

‎tsconfig.json

+16-36
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
1-
21
{
3-
"compilerOptions": {
4-
"module": "commonjs",
5-
"lib": [
6-
"es6"
7-
],
8-
"target": "ES5",
9-
"noImplicitAny": false,
10-
"noImplicitThis": true,
11-
"strictFunctionTypes": true,
12-
"strictNullChecks": true,
13-
"esModuleInterop": true,
14-
"resolveJsonModule": true,
15-
"allowJs": true,
16-
"checkJs": true,
17-
"baseUrl": ".",
18-
"paths": {
19-
"multiaddr": [
20-
"./src",
21-
"../src",
22-
]
23-
},
24-
"types": [
25-
"node",
26-
"mocha",
27-
"chai"
28-
],
29-
"noEmit": true,
30-
"forceConsistentCasingInFileNames": true
31-
},
32-
"files": [
33-
"./src/index.d.ts",
34-
],
35-
"include": [
36-
"./test/**/*.spec.js"
37-
]
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es6"],
5+
"target": "ES5",
6+
"noImplicitAny": false,
7+
"noImplicitThis": true,
8+
"strictFunctionTypes": true,
9+
"strictNullChecks": true,
10+
"esModuleInterop": true,
11+
"resolveJsonModule": true,
12+
"allowJs": true,
13+
"checkJs": true,
14+
"noEmit": true,
15+
"forceConsistentCasingInFileNames": true
16+
},
17+
"include": ["src/index.d.ts", "test/**/*.spec.js", "test/**/*.spec.ts"],
3818
}

0 commit comments

Comments
 (0)
Please sign in to comment.