Skip to content

Commit 9e087d6

Browse files
Gozalarvagg
authored andcommitted
feat!: Make link.toJSON return a DAG-JSON link
1 parent a246054 commit 9e087d6

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

src/cid.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ export const format = (link, base) => {
3535
}
3636
}
3737

38+
/**
39+
* @template {API.UnknownLink} Link
40+
* @param {Link} link
41+
* @returns {API.LinkJSON<Link>}
42+
*/
43+
export const toJSON = (link) => ({
44+
'/': format(link)
45+
})
46+
47+
/**
48+
* @template {API.UnknownLink} Link
49+
* @param {API.LinkJSON<Link>} json
50+
*/
51+
export const fromJSON = (json) =>
52+
CID.parse(json['/'])
53+
3854
/** @type {WeakMap<API.UnknownLink, Map<string, string>>} */
3955
const cache = new WeakMap()
4056

@@ -200,11 +216,7 @@ export class CID {
200216
}
201217

202218
toJSON () {
203-
return {
204-
code: this.code,
205-
version: this.version,
206-
hash: this.multihash.bytes
207-
}
219+
return { '/': format(this) }
208220
}
209221

210222
link () {

src/link.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Linter can see that API is used in types.
22
// eslint-disable-next-line
33
import * as API from "./link/interface.js"
4-
import { CID, format } from './cid.js'
4+
import { CID, format, toJSON, fromJSON } from './cid.js'
55
// This way TS will also expose all the types from module
66
export * from './link/interface.js'
77

@@ -73,7 +73,7 @@ export const isLink = value => {
7373
*/
7474
export const parse = (source, base) => CID.parse(source, base)
7575

76-
export { format }
76+
export { format, toJSON, fromJSON }
7777

7878
/**
7979
* Decoded a CID from its binary representation. The byte array must contain

src/link/interface.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ export interface Link<
3535
equals: (other: unknown) => other is Link<Data, Format, Alg, Version>
3636

3737
toString: <Prefix extends string>(base?: MultibaseEncoder<Prefix>) => ToString<Link<Data, Format, Alg, Version>, Prefix>
38-
toJSON: () => { version: V, code: Format, hash: Uint8Array }
3938
link: () => Link<Data, Format, Alg, V>
4039

4140
toV1: () => Link<Data, Format, Alg, 1>
4241
}
4342

43+
export interface LinkJSON<T extends UnknownLink = UnknownLink> {
44+
'/': ToString<T>
45+
}
46+
4447
export interface LegacyLink<T extends unknown = unknown> extends Link<T, DAG_PB, SHA_256, 0> {
4548
}
4649

test/test-cid.spec.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,10 @@ describe('CID', () => {
482482
it('toJSON()', async () => {
483483
const hash = await sha256.digest(textEncoder.encode('abc'))
484484
const cid = CID.create(1, 112, hash)
485-
const json = cid.toJSON()
486485

487-
assert.deepStrictEqual(
488-
{ ...json, hash: null },
489-
{ code: 112, version: 1, hash: null }
490-
)
491-
assert.ok(equals(json.hash, hash.bytes))
486+
assert.deepStrictEqual(cid.toJSON(), {
487+
'/': cid.toString()
488+
})
492489
})
493490

494491
it('asCID', async () => {

test/test-link.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,42 @@ describe('Link', () => {
8989
assert.ok(t2)
9090
})
9191
})
92+
93+
describe('toJSON', () => {
94+
assert.deepStrictEqual(Link.toJSON(Link.parse(h1)), {
95+
'/': h1
96+
})
97+
98+
assert.deepStrictEqual(Link.toJSON(Link.parse(h4)), {
99+
'/': h4
100+
})
101+
})
102+
103+
describe('fromJSON', () => {
104+
assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
105+
'/': h1
106+
}))
107+
108+
assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
109+
'/': h1,
110+
// @ts-expect-error
111+
foo: 1
112+
}))
113+
114+
assert.deepStrictEqual(Link.parse(h4), Link.fromJSON({
115+
'/': h4
116+
}))
117+
})
118+
119+
describe('JSON.stringify', () => {
120+
assert.equal(JSON.stringify(Link.parse(h1)), JSON.stringify({
121+
'/': h1
122+
}))
123+
124+
assert.equal(JSON.stringify(Link.parse(h4)), JSON.stringify({
125+
'/': h4
126+
}))
127+
})
92128
})
93129

94130
describe('decode', () => {

0 commit comments

Comments
 (0)