1 | import * as uriTemplate from 'uri-template';
|
2 | import { resolve } from './utils/url';
|
3 |
|
4 | type LinkInit = {
|
5 | context: string,
|
6 | href: string,
|
7 | name?: string,
|
8 | rel: string,
|
9 | templated?: boolean,
|
10 | title?: string,
|
11 | type?: string,
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export default class Link {
|
18 |
|
19 | |
20 |
|
21 |
|
22 | context: string;
|
23 |
|
24 | |
25 |
|
26 |
|
27 | href: string;
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | name?: string;
|
36 |
|
37 | |
38 |
|
39 |
|
40 | rel: string;
|
41 |
|
42 | |
43 |
|
44 |
|
45 | templated: boolean;
|
46 |
|
47 | |
48 |
|
49 |
|
50 | title: string | null;
|
51 |
|
52 | |
53 |
|
54 |
|
55 | type: string | null;
|
56 |
|
57 | constructor(properties: LinkInit) {
|
58 |
|
59 | this.templated = false;
|
60 | this.title = null;
|
61 | this.type = null;
|
62 | for (const key of ['context', 'href', 'name', 'rel', 'templated', 'title', 'type']) {
|
63 | if ((<any> properties)[key]) {
|
64 | (<any> this)[key] = (<any> properties)[key];
|
65 | }
|
66 | }
|
67 |
|
68 | }
|
69 |
|
70 |
|
71 | |
72 |
|
73 |
|
74 | resolve(): string {
|
75 |
|
76 | return resolve(this.context, this.href);
|
77 |
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | expand(variables: object): string {
|
87 |
|
88 | if (!this.templated) {
|
89 | return resolve(this.context, this.href);
|
90 | } else {
|
91 | const templ = uriTemplate.parse(this.href);
|
92 | const expanded = templ.expand(variables);
|
93 | return resolve(this.context, expanded);
|
94 | }
|
95 |
|
96 | }
|
97 |
|
98 | }
|