Skip to content

Trim Link / CID interface and introduce something like LinkView interface #214

Closed
@Gozala

Description

@Gozala

Given #213 #212 #208 and #211 I would like to propose trimming down Link interface to bare minimum where it acts like Model and introduce another interface e.g. LinkView (can call whatever) that extends Link with bunch of methods:

export interface Link<
  Data extends unknown = unknown,
  Format extends number = number,
  Alg extends number = number,
  V extends Version = 1
  > extends Phantom<Data> {
  readonly version: V
  readonly code: Format
  readonly multihash: MultihashDigest<Alg>
  readonly bytes: ByteView<Link<Data, Format, Alg, V>>
  readonly ['/']: this['bytes']
}

export interface LinkView<
  Data extends unknown = unknown,
  Format extends number = number,
  Alg extends number = number,
  V extends Version = 1
  > extends Link<Data, Format, Alg> {


  equals: (other: unknown) => other is Link<Data, Format, Alg, Version>
  toString: <Prefix extends string>(base?: MultibaseEncoder<Prefix>) => ToString<Link<Data, Format, Alg, Version>, Prefix>
  toJSON: () => { version: V, code: Format, hash: Uint8Array }
  link: () => Link<Data, Format, Alg, V>

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

Note I have removed following entirely

readonly byteOffset: number
readonly byteLength: number

Idea here would be that CID class will implement both interfaces, but all the stuff on LinkView will be implemented using CID.prototype which should ensure that value equality will hold.

This also would imply that isLink would become something like this:

const isLink = (value:unknown): value is Link => value && value['/'] === value.bytes

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions