10fbfc30aSopenharmony_ci/** 20fbfc30aSopenharmony_ci * @file Describe the file 30fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 40fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 50fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License. 60fbfc30aSopenharmony_ci * You may obtain a copy of the License at 70fbfc30aSopenharmony_ci * 80fbfc30aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 90fbfc30aSopenharmony_ci * 100fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 110fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 120fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and 140fbfc30aSopenharmony_ci * limitations under the License. 150fbfc30aSopenharmony_ci */ 160fbfc30aSopenharmony_ci 170fbfc30aSopenharmony_ciimport { isEmptyStr } from './TextUtils' 180fbfc30aSopenharmony_ci 190fbfc30aSopenharmony_ciexport const QUERY_START = '?'; 200fbfc30aSopenharmony_ci 210fbfc30aSopenharmony_ciexport const QUERY_SPLIT = '&'; 220fbfc30aSopenharmony_ci 230fbfc30aSopenharmony_ciexport const QUERY_CONNECTOR = '='; 240fbfc30aSopenharmony_ci 250fbfc30aSopenharmony_ciexport const BUNDLE_NAME = 'bundleName'; 260fbfc30aSopenharmony_ci 270fbfc30aSopenharmony_ciexport const TOKEN_ID = 'tokenId'; 280fbfc30aSopenharmony_ci 290fbfc30aSopenharmony_ci/** 300fbfc30aSopenharmony_ci * parse query params from Uri, like: 310fbfc30aSopenharmony_ci * xxx?params1=value1¶ms2=value2¶ms3=value3 320fbfc30aSopenharmony_ci * 330fbfc30aSopenharmony_ci * @param url the url to be parsed. 340fbfc30aSopenharmony_ci * @return map Describes the key-value pair of the query parameter. 350fbfc30aSopenharmony_ci */ 360fbfc30aSopenharmony_ciexport function getQueries(url: string): Map<string, string> | undefined { 370fbfc30aSopenharmony_ci if (url === null || url === undefined) { 380fbfc30aSopenharmony_ci return undefined; 390fbfc30aSopenharmony_ci } 400fbfc30aSopenharmony_ci let startIndex = url.indexOf(QUERY_START) + 1; 410fbfc30aSopenharmony_ci const totalLength = url.length; 420fbfc30aSopenharmony_ci if (startIndex < 1 || startIndex >= totalLength) { 430fbfc30aSopenharmony_ci return undefined; 440fbfc30aSopenharmony_ci } 450fbfc30aSopenharmony_ci const query = url.substring(startIndex); 460fbfc30aSopenharmony_ci if (query === null || query === undefined || query.length < 1) { 470fbfc30aSopenharmony_ci return undefined; 480fbfc30aSopenharmony_ci } 490fbfc30aSopenharmony_ci const array = query.split(QUERY_SPLIT); 500fbfc30aSopenharmony_ci if (array === null || array === undefined || array.length < 1) { 510fbfc30aSopenharmony_ci return undefined; 520fbfc30aSopenharmony_ci } 530fbfc30aSopenharmony_ci const map = new Map<string, string>(); 540fbfc30aSopenharmony_ci for (let i = 0; i < array.length; i++) { 550fbfc30aSopenharmony_ci const item = array[i]; 560fbfc30aSopenharmony_ci if (isEmptyStr(item)) { 570fbfc30aSopenharmony_ci continue; 580fbfc30aSopenharmony_ci } 590fbfc30aSopenharmony_ci 600fbfc30aSopenharmony_ci const connectorIndex = item.indexOf(QUERY_CONNECTOR); 610fbfc30aSopenharmony_ci if (connectorIndex <= 0 || connectorIndex + 1 >= item.length) { 620fbfc30aSopenharmony_ci // QUERY_CONNECTOR at the first or last index is invalid 630fbfc30aSopenharmony_ci continue; 640fbfc30aSopenharmony_ci } 650fbfc30aSopenharmony_ci const key = item.substring(0, connectorIndex); 660fbfc30aSopenharmony_ci const value = item.substring(connectorIndex + 1); 670fbfc30aSopenharmony_ci map.set(key, value); 680fbfc30aSopenharmony_ci } 690fbfc30aSopenharmony_ci return map; 700fbfc30aSopenharmony_ci} 710fbfc30aSopenharmony_ci 720fbfc30aSopenharmony_ci/** 730fbfc30aSopenharmony_ci * transfer the user input uri to TokenID 740fbfc30aSopenharmony_ci * 750fbfc30aSopenharmony_ci * @param uri the user input uri 760fbfc30aSopenharmony_ci * @return the tokenID coming from user's input.if TokenID is undefined, this function will return "" 770fbfc30aSopenharmony_ci */ 780fbfc30aSopenharmony_ciexport function getTokenIDByUri(uri: string): string { 790fbfc30aSopenharmony_ci let queries = getQueries(uri); 800fbfc30aSopenharmony_ci if (queries && queries.has(TOKEN_ID)) { 810fbfc30aSopenharmony_ci return queries.get(TOKEN_ID) as string; 820fbfc30aSopenharmony_ci } 830fbfc30aSopenharmony_ci return ""; 840fbfc30aSopenharmony_ci} 850fbfc30aSopenharmony_ci 860fbfc30aSopenharmony_ci/** 870fbfc30aSopenharmony_ci * transfer the user input uri to TokenID 880fbfc30aSopenharmony_ci * 890fbfc30aSopenharmony_ci * @param uri the user input uri 900fbfc30aSopenharmony_ci * @return the bundleName coming from user's input.if TokenID is undefined, this function will return "" 910fbfc30aSopenharmony_ci */ 920fbfc30aSopenharmony_ciexport function getBundleNameByUri(uri: string): string { 930fbfc30aSopenharmony_ci let queries = getQueries(uri); 940fbfc30aSopenharmony_ci if (queries && queries.has(BUNDLE_NAME)) { 950fbfc30aSopenharmony_ci return queries.get(BUNDLE_NAME) as string; 960fbfc30aSopenharmony_ci } 970fbfc30aSopenharmony_ci return ""; 980fbfc30aSopenharmony_ci} 990fbfc30aSopenharmony_ci 1000fbfc30aSopenharmony_ci/** 1010fbfc30aSopenharmony_ci * transfer the user input uri to BundleNameAndTokenIdFromUri 1020fbfc30aSopenharmony_ci * 1030fbfc30aSopenharmony_ci * @param uri the user input uri 1040fbfc30aSopenharmony_ci * @return the object containing bundleName and tokenID come from user's input. 1050fbfc30aSopenharmony_ci * if bundleName or TokenID is undefined, this function will return null object. 1060fbfc30aSopenharmony_ci */ 1070fbfc30aSopenharmony_ciexport function getBundleNameAndTokenIDByUri(uri: string): BundleNameAndTokenIdFromUri { 1080fbfc30aSopenharmony_ci let queries = getQueries(uri); 1090fbfc30aSopenharmony_ci let bundleNameAndTokenIdFromUri: BundleNameAndTokenIdFromUri = new BundleNameAndTokenIdFromUri(); 1100fbfc30aSopenharmony_ci if (queries && queries.has(TOKEN_ID) && queries.has(BUNDLE_NAME)) { 1110fbfc30aSopenharmony_ci bundleNameAndTokenIdFromUri = { 1120fbfc30aSopenharmony_ci bundleName: queries.get(BUNDLE_NAME) as string, 1130fbfc30aSopenharmony_ci tokenId: queries.get(TOKEN_ID) as string 1140fbfc30aSopenharmony_ci }; 1150fbfc30aSopenharmony_ci return bundleNameAndTokenIdFromUri; 1160fbfc30aSopenharmony_ci } 1170fbfc30aSopenharmony_ci return bundleNameAndTokenIdFromUri; 1180fbfc30aSopenharmony_ci} 1190fbfc30aSopenharmony_ci 1200fbfc30aSopenharmony_ci/** 1210fbfc30aSopenharmony_ci * the structure of BundleName and tokenId which are got from user's uri. 1220fbfc30aSopenharmony_ci * 1230fbfc30aSopenharmony_ci * @since 2022-10-29 1240fbfc30aSopenharmony_ci */ 1250fbfc30aSopenharmony_ciexport class BundleNameAndTokenIdFromUri { 1260fbfc30aSopenharmony_ci bundleName: string = ''; 1270fbfc30aSopenharmony_ci tokenId: string = ''; 1280fbfc30aSopenharmony_ci} 129