• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15interface NativeUri {
16  new(input: string): NativeUri;
17  normalize(): string;
18  equals(other: NativeUri): boolean;
19  equalsTo(other: NativeUri): boolean;
20  checkIsAbsolute(): boolean;
21  toString(): string;
22  scheme: string | null;
23  authority: string | null;
24  ssp: string;
25  userInfo: string | null;
26  host: string | null;
27  port: string;
28  path: string | null;
29  query: string | null;
30  fragment: string | null;
31  isFailed: string;
32}
33interface UriInterface {
34  Uri: NativeUri;
35}
36declare function requireInternal(s: string): UriInterface;
37const uri = requireInternal('uri');
38
39const TypeErrorCodeId = 401;
40const SyntaxErrorCodeId = 10200002;
41
42class BusinessError extends Error {
43  code: number;
44  constructor(msg: string) {
45    super(msg)
46    this.name = 'BusinessError';
47    this.code = TypeErrorCodeId;
48  }
49}
50
51class URI {
52  uricalss: NativeUri;
53  constructor(input: string) {
54    if (typeof input !== 'string' || input.length === 0) {
55      throw new BusinessError(`Parameter error.The type of ${input} must be string`);
56    }
57    console.debug('URI:: uri src is: ' + input);
58    this.uricalss = new uri.Uri(input);
59    let errStr: string = this.uricalss.isFailed;
60    if (errStr.length !== 0) {
61      let err : BusinessError = new BusinessError('Syntax Error. Invalid Uri string');
62      err.code = SyntaxErrorCodeId;
63      throw err;
64    }
65  }
66  toString(): string {
67    return toAscllString(this.uricalss.toString());
68  }
69
70  equals(other: URI): boolean {
71    return this.uricalss.equals(other.uricalss);
72  }
73
74  equalsTo(other: URI): boolean {
75    if (other instanceof URI) {
76      return this.uricalss.equals(other.uricalss);
77    }
78    throw new BusinessError(`Parameter error.The type of ${other} must be URI`);
79  }
80
81  checkIsAbsolute(): boolean {
82    return this.uricalss.checkIsAbsolute();
83  }
84
85  normalize(): URI {
86    let uriStr: string = this.uricalss.normalize();
87    return createNewUri(uriStr);
88  }
89
90  get scheme(): string | null {
91    return this.uricalss.scheme;
92  }
93
94  get authority(): string | null {
95    if (this.uricalss.authority === null) {
96      return this.uricalss.authority;
97    }
98    let thisAuthority: string = this.uricalss.authority;
99    if (thisAuthority.indexOf('[') !== -1) {
100      let arr: string[] = thisAuthority.split('[');
101      let brr: string[] = arr[1].split(']');
102      arr[1] = '[' + brr[0] + ']';
103      arr[2] = brr[1];
104      arr[0] = decodeURIComponent(arr[0]);
105      arr[2] = decodeURIComponent(arr[2]);
106      return arr.join('');
107    } else {
108      return decodeURIComponent(thisAuthority);
109    }
110  }
111
112  get ssp(): string {
113    let thisSsp: string = this.uricalss.ssp;
114    if (thisSsp.indexOf('[') !== -1) {
115      let arr: string[] = thisSsp.split('[');
116      let brr: string[] = arr[1].split(']');
117      arr[1] = '[' + brr[0] + ']';
118      arr[2] = brr[1];
119      arr[0] = decodeURIComponent(arr[0]);
120      arr[2] = decodeURIComponent(arr[2]);
121      return arr.join('');
122    } else {
123      return decodeURIComponent(thisSsp);
124    }
125  }
126
127  get userInfo(): string | null {
128    return this.uricalss.userInfo === null ? this.uricalss.userInfo : decodeURIComponent(this.uricalss.userInfo);
129  }
130
131  get host(): string | null {
132    return this.uricalss.host;
133  }
134
135  get port(): string {
136    return this.uricalss.port;
137  }
138
139  get path(): string | null {
140    return this.uricalss.path === null ? this.uricalss.path : decodeURIComponent(this.uricalss.path);
141  }
142
143  get query(): string | null {
144    return this.uricalss.query === null ? this.uricalss.query : decodeURIComponent(this.uricalss.query);
145  }
146
147  get fragment(): string | null {
148    return this.uricalss.fragment === null ? this.uricalss.fragment : decodeURIComponent(this.uricalss.fragment);
149  }
150}
151
152function toAscllString(uriStr: string): string {
153  return encodeURI(uriStr).replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%25/g, '%');
154}
155
156function createNewUri(uriStr: string): URI {
157  return new URI(uriStr);
158}
159
160export default {
161  URI: URI
162};