blob: 58aac99e642a55b23c457bbe1a8b168b4c513a97 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {HttpErrorResponse} from "@angular/common/http";
import {EHttpStatusCodes} from "./EHttpStatusCodes";
export function isHttpErrorWithStatus(error: any, ...status: EHttpStatusCodes[]): boolean {
return error instanceof HttpErrorResponse
&& (status == null || status.find((s) => error.status === s) != null);
}
export function urlJoin(...args: string[]): string {
let result = args.length === 0 ? "" : args.reduce((previous, current) => previous + "/" + current);
const indexOfProtocol = result.indexOf("://");
const protocol = indexOfProtocol === -1 ? "" : result.slice(0, indexOfProtocol);
result = indexOfProtocol === -1 ? result : result.slice(indexOfProtocol + 2);
while (result.indexOf("//") > -1) {
result = result.replace("//", "/");
}
return indexOfProtocol === -1 ? result : protocol + ":/" + result;
}
export function objectToHttpParams<T extends object>(
object: { [key: string]: string | number | Array<string | number> }
): { [key: string]: string | string[] } {
if (object == null) {
return {};
}
const result: { [key: string]: string | string[] } = {};
Object.entries(object)
.filter(([key, value]) => value != null)
.forEach(([key, value]) => {
result[key] = Array.isArray(value) ? value.map((_) => "" + _) : "" + value;
});
return result;
}