• 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;
23    authority : string;
24    ssp : string;
25    userInfo : string;
26    host : string;
27    port : string;
28    path : string;
29    query : string;
30    fragment : string;
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            let err = new BusinessError(`Parameter error.The type of ${input} must be string`);
56            throw err;
57        }
58        this.uricalss = new uri.Uri(input);
59        let errStr : string = this.uricalss.isFailed;
60        if (errStr.length !== 0) {
61            let err = new BusinessError('Syntax Error. Invalid Uri string');
62            err.code = SyntaxErrorCodeId;
63            throw err;
64        }
65    }
66    toString() {
67        return toAscllString(this.uricalss.toString());
68    }
69
70    equals(other : URI) {
71        return this.uricalss.equals(other.uricalss);
72    }
73
74    equalsTo(other : URI) {
75        if (other instanceof URI) {
76            return this.uricalss.equals(other.uricalss);
77        }
78        let err = new BusinessError(`Parameter error.The type of ${other} must be URI`);
79        throw err;
80    }
81
82    checkIsAbsolute() {
83        return this.uricalss.checkIsAbsolute();
84    }
85
86    normalize() {
87        let uriStr : string = this.uricalss.normalize();
88        return createNewUri(uriStr);
89    }
90
91    get scheme() {
92        return this.uricalss.scheme;
93    }
94
95    get authority() {
96        let thisAuthority : string = this.uricalss.authority;
97        if (thisAuthority.indexOf('[') !== -1) {
98            let arr : string[] = thisAuthority.split('[');
99            let brr : string[] = arr[1].split(']');
100            arr[1] = '[' + brr[0] + ']';
101            arr[2] = brr[1];
102            arr[0] = decodeURIComponent(arr[0]);
103            arr[2] = decodeURIComponent(arr[2]);
104            return arr.join('');
105        } else {
106            return decodeURIComponent(thisAuthority);
107        }
108    }
109
110    get ssp() {
111        let thisSsp : string = this.uricalss.ssp;
112        if (thisSsp.indexOf('[') !== -1) {
113            let arr : string[] = thisSsp.split('[');
114            let brr : string[] = arr[1].split(']');
115            arr[1] = '[' + brr[0] + ']';
116            arr[2] = brr[1];
117            arr[0] = decodeURIComponent(arr[0]);
118            arr[2] = decodeURIComponent(arr[2]);
119            return arr.join('');
120        } else {
121            return decodeURIComponent(thisSsp);
122        }
123    }
124
125    get userInfo() {
126        return decodeURIComponent(this.uricalss.userInfo);
127    }
128
129    get host() {
130        return this.uricalss.host;
131    }
132
133    get port() {
134        return this.uricalss.port;
135    }
136
137    get path() {
138        return decodeURIComponent(this.uricalss.path);
139    }
140
141    get query() {
142        return decodeURIComponent(this.uricalss.query);
143    }
144
145    get fragment() {
146        return decodeURIComponent(this.uricalss.fragment);
147    }
148}
149
150function toAscllString(uriStr : string) {
151    if (uriStr.indexOf('[') !== -1) {
152        let arr : string[] = uriStr.split('[');
153        let brr : string[] = arr[1].split(']');
154        arr[1] = '[' + brr[0] + ']';
155        arr[2] = brr[1];
156        arr[0] = encodeURI(arr[0]);
157        arr[2] = encodeURI(arr[2]);
158        return arr.join('');
159    } else {
160        return encodeURI(uriStr);
161    }
162}
163
164function createNewUri(uriStr : string) {
165    return new URI(uriStr);
166}
167
168export default  {
169    URI : URI
170}