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