Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit 4eb0c60

Browse files
carsonfarmervmx
authored andcommitted
feat: more correct type defs + docs
Using the default export only works in ES6+ modules, the export style is technically more correct. Also adds docs and removes extraneous namespace in favour of explicit types.
1 parent c919adb commit 4eb0c60

File tree

1 file changed

+102
-19
lines changed

1 file changed

+102
-19
lines changed

src/index.d.ts

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,112 @@
1-
export type Version = 0 | 1
2-
export type Codec = string
3-
export type Multihash = Buffer
4-
export type BaseEncodedString = string
5-
export type MultibaseName = string
6-
7-
export class CID {
8-
constructor(version: Version, codec: Codec, multhash: Multihash, multibaseName?: MultibaseName)
9-
constructor(cidStr: BaseEncodedString)
1+
/**
2+
* Class representing a CID `<mbase><version><mcodec><mhash>`
3+
* , as defined in [ipld/cid](https://github.com/multiformats/cid).
4+
*/
5+
declare class CID {
6+
/**
7+
* Create a new CID.
8+
*
9+
* The algorithm for argument input is roughly:
10+
* ```
11+
* if (cid)
12+
* -> create a copy
13+
* else if (str)
14+
* if (1st char is on multibase table) -> CID String
15+
* else -> bs58 encoded multihash
16+
* else if (Buffer)
17+
* if (1st byte is 0 or 1) -> CID
18+
* else -> multihash
19+
* else if (Number)
20+
* -> construct CID by parts
21+
* ```
22+
*
23+
* @example
24+
* new CID(<version>, <codec>, <multihash>, <multibaseName>)
25+
* new CID(<cidStr>)
26+
* new CID(<cid.buffer>)
27+
* new CID(<multihash>)
28+
* new CID(<bs58 encoded multihash>)
29+
* new CID(<cid>)
30+
*/
31+
constructor(version: 0 | 1, codec: string, multhash: Buffer, multibaseName?: string)
32+
constructor(cidStr: string)
1033
constructor(cidBuf: Buffer)
11-
constructor(cidMultihash: Multihash)
34+
constructor(cidMultihash: Buffer)
1235
constructor(cid: CID)
13-
codec: Codec
14-
multihash: Multihash
15-
buffer: Buffer
16-
prefix: Buffer
36+
37+
/**
38+
* The codec of the CID.
39+
*/
40+
codec: string
41+
42+
/**
43+
* The multihash of the CID.
44+
*/
45+
multihash: Buffer
46+
47+
/**
48+
* Multibase name as string.
49+
*/
50+
multibaseName: string
51+
52+
/**
53+
* The CID as a `Buffer`
54+
*/
55+
readonly buffer: Buffer
56+
57+
/**
58+
* The prefix of the CID.
59+
*/
60+
readonly prefix: Buffer
61+
62+
/**
63+
* The version of the CID.
64+
*/
1765
version: number
66+
67+
/**
68+
* Convert to a CID of version `0`.
69+
*/
1870
toV0(): CID
71+
72+
/**
73+
* Convert to a CID of version `1`.
74+
*/
1975
toV1(): CID
20-
toBaseEncodedString(base?: string): BaseEncodedString
21-
toString(): BaseEncodedString
22-
toJSON(): { codec: Codec, version: Version, hash: Multihash }
76+
/**
77+
* Encode the CID into a string.
78+
*
79+
* @param base Base encoding to use.
80+
*/
81+
toBaseEncodedString(base?: string): string
82+
83+
/**
84+
* Encode the CID into a string.
85+
*/
86+
toString(): string
87+
88+
/**
89+
* Serialize to a plain object.
90+
*/
91+
toJSON(): { codec: string, version: 0 | 1, hash: Buffer }
92+
93+
/**
94+
* Compare equality with another CID.
95+
*
96+
* @param other The other CID.
97+
*/
2398
equals(other: any): boolean
24-
static codecs: Record<Codec, Buffer>
99+
100+
static codecs: Record<string, Buffer>
25101
static isCID(mixed: any): boolean
102+
103+
/**
104+
* Test if the given input is a valid CID object.
105+
* Throws if it is not.
106+
*
107+
* @param other The other CID.
108+
*/
26109
static validateCID(other: any): void
27110
}
28111

29-
export default CID
112+
export = CID

0 commit comments

Comments
 (0)