blob: 266555ca6d3509b5c0a9a5970c7a3756170edc26 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* 'License'); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { SupersetClient } from '@superset-ui/core';
import { getClientErrorObject } from './getClientErrorObject';
export type UrlParamType = 'string' | 'number' | 'boolean';
export function getUrlParam(paramName: string, type: 'string'): string;
export function getUrlParam(paramName: string, type: 'number'): number;
export function getUrlParam(paramName: string, type: 'boolean'): boolean;
export function getUrlParam(paramName: string, type: UrlParamType): unknown {
const urlParam = new URLSearchParams(window.location.search).get(paramName);
switch (type) {
case 'number':
if (!urlParam) {
return null;
}
if (urlParam === 'true') {
return 1;
}
if (urlParam === 'false') {
return 0;
}
if (!Number.isNaN(Number(urlParam))) {
return Number(urlParam);
}
return null;
// TODO: process other types when needed
default:
return urlParam;
}
}
export function getShortUrl(longUrl: string) {
return SupersetClient.post({
endpoint: '/r/shortner/',
postPayload: { data: `/${longUrl}` }, // note: url should contain 2x '/' to redirect properly
parseMethod: 'text',
stringify: false, // the url saves with an extra set of string quotes without this
})
.then(({ text }) => text)
.catch(response =>
// @ts-ignore
getClientErrorObject(response).then(({ error, statusText }) =>
Promise.reject(error || statusText),
),
);
}