-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathassetName.ts
46 lines (37 loc) · 1.12 KB
/
assetName.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { CAIP } from "./spec";
import { IdentifierSpec } from "./types";
import { isValidId, joinParams, getParams } from "./utils";
export interface AssetNameParams {
namespace: string;
reference: string;
}
export class AssetName {
public static spec: IdentifierSpec = CAIP["19"].assetName;
public static parse(id: string): AssetNameParams {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new AssetName(getParams<AssetNameParams>(id, this.spec)).toJSON();
}
public static format(params: AssetNameParams): string {
return joinParams(params as any, this.spec);
}
public namespace: string;
public reference: string;
constructor(params: AssetNameParams | string) {
if (typeof params === "string") {
params = AssetName.parse(params);
}
this.namespace = params.namespace;
this.reference = params.reference;
}
public toString(): string {
return AssetName.format(this.toJSON());
}
public toJSON(): AssetNameParams {
return {
namespace: this.namespace,
reference: this.reference,
};
}
}