UNPKG

@finos/legend-shared

Version:
118 lines 6.02 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed 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 { StatusCodes } from 'http-status-codes'; import type { PlainObject } from '../CommonUtils.js'; export declare const URL_SEPARATOR = "/"; export declare const HttpStatus: typeof StatusCodes; export declare const CHARSET = "charset=utf-8"; export declare enum HttpHeader { CONTENT_TYPE = "Content-Type", ACCEPT = "Accept" } export declare enum ContentType { APPLICATION_JSON = "application/json", APPLICATION_XML = "application/xml", APPLICATION_ZLIB = "application/zlib", APPLICATION_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", TEXT_PLAIN = "text/plain", TEXT_HTML = "text/html", TEXT_CSV = "text/csv", ALL = "*/*", MESSAGE_RFC822 = "message/rfc822" } export declare enum HttpMethod { GET = "GET", PUT = "PUT", POST = "POST", DELETE = "DELETE" } export declare const getContentTypeFileExtension: (type: ContentType) => string; /** * NOTE: the latter headers value will override the those of the first */ export declare const mergeRequestHeaders: (headersOne: RequestHeaders | undefined, headersTwo: RequestHeaders | undefined) => RequestHeaders; type ParamterValue = string | number | boolean | undefined; /** * NOTE: we could not use the Headers class object since `fetch` * does not process it but treat it simply as an object, so we will in fact * lose header if we send the network request. As such we create this * type for request header purely for annotation purpose * See https://github.github.io/fetch/ */ export type RequestHeaders = Record<string, string>; export type Parameters = Record<string, ParamterValue | ParamterValue[]>; export type Payload = PlainObject | string; /** * This is a fairly basic way to attempt re-authentication. * We create an <iframe> to load a re-authentication url * which suppose to silently refresh the authentication cookie * and requires no action from users. * * NOTE: authentication is very specific to the deployment context * i.e. how the servers are being setup, so this way of re-authenticate * should be optional and configurable. */ export declare const autoReAuthenticate: (url: string) => Promise<void>; export declare class NetworkClientError extends Error { response: Response & { data?: object; }; payload?: Payload | undefined; constructor(response: Response, payload: Payload | undefined); } export declare const makeUrl: (baseUrl: string | undefined, url: string, parameters: Parameters) => string; export interface ResponseProcessConfig { skipProcessing?: boolean | undefined; preprocess?: ((response: Response) => void) | undefined; autoReAuthenticateUrl?: string | undefined; } export interface RequestProcessConfig { enableCompression?: boolean | undefined; } export declare const createRequestHeaders: (method: HttpMethod, headers?: RequestHeaders) => RequestHeaders; export interface NetworkClientConfig { options?: PlainObject | undefined; baseUrl?: string | undefined; } /** * Simple wrapper around native `fetch` API. For `options`, see documentation for "init" * See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API */ export declare class NetworkClient { private options; baseUrl?: string | undefined; constructor(config?: NetworkClientConfig); get<T>(url: string, options?: RequestInit, headers?: RequestHeaders, parameters?: Parameters, requestProcessConfig?: RequestProcessConfig, responseProcessConfig?: ResponseProcessConfig): Promise<T>; put<T>(url: string, data?: unknown, options?: RequestInit, headers?: RequestHeaders, parameters?: Parameters, requestProcessConfig?: RequestProcessConfig, responseProcessConfig?: ResponseProcessConfig): Promise<T>; post<T>(url: string, data?: unknown, options?: RequestInit, headers?: RequestHeaders, parameters?: Parameters, requestProcessConfig?: RequestProcessConfig, responseProcessConfig?: ResponseProcessConfig): Promise<T>; delete<T>(url: string, data?: unknown, options?: RequestInit, headers?: RequestHeaders, parameters?: Parameters, requestProcessConfig?: RequestProcessConfig, responseProcessConfig?: ResponseProcessConfig): Promise<T>; request<T>(method: HttpMethod, url: string, data: unknown, options: RequestInit, headers?: RequestHeaders | undefined, parameters?: Parameters | undefined, requestProcessConfig?: RequestProcessConfig | undefined, responseProcessConfig?: ResponseProcessConfig | undefined): Promise<T>; } /** * Create and download a file using data URI * See http://stackoverflow.com/questions/283956 */ export declare const downloadFileUsingDataURI: (fileName: string, content: string, contentType: ContentType) => void; export declare const createUrlStringFromData: (data: string, contentType: ContentType, base64: boolean) => string; export declare const getQueryParameters: <T>(url: string, isFullUrl?: boolean) => T; export declare const getQueryParameterValue: (key: string, data: Record<string, string | undefined>) => string | undefined; export declare const stringifyQueryParams: (params: PlainObject) => string; export declare const addQueryParametersToUrl: (url: string, val: string | undefined) => string; export declare const buildUrl: (parts: string[]) => string; export declare const sanitizeURL: (val: string) => string; export declare const isValidURL: (val: string) => boolean; export {}; //# sourceMappingURL=NetworkUtils.d.ts.map