@ogs-gmbh/ngx-http
Version:
A lightweight Angular library that enhances HTTP functionalities by providing types, static values, and utility functions.
145 lines (131 loc) • 5.51 kB
JavaScript
import { HttpHeaders as HttpHeaders$1, HttpParams } from '@angular/common/http';
import _ from 'lodash';
import { InjectionToken } from '@angular/core';
function mergeHttpHeaders(...headers) {
let httpHeaderResult = new HttpHeaders$1();
headers.forEach((currentHeader) => {
if (currentHeader instanceof HttpHeaders$1) {
currentHeader.keys().forEach((currentHeaderKey) => {
const currentHeaderValue = currentHeader.get(currentHeaderKey);
httpHeaderResult = httpHeaderResult.append(currentHeaderKey, currentHeaderValue);
});
}
else {
Object.keys(currentHeader).forEach((currentHeaderKey) => {
const currentHeaderValue = currentHeader[currentHeaderKey];
if (Array.isArray(currentHeaderValue)) {
currentHeaderValue.forEach((currentHeaderValueItem) => {
httpHeaderResult = httpHeaderResult.append(currentHeaderKey, currentHeaderValueItem);
});
return;
}
httpHeaderResult = httpHeaderResult.append(currentHeaderKey, currentHeaderValue);
});
}
});
return httpHeaderResult;
}
function mergeHttpParams(...params) {
let httpParamsResult = new HttpParams();
params.forEach((currentParams) => {
if (currentParams instanceof HttpParams) {
currentParams.keys().forEach((currentParamsKey) => {
const currentParamsValue = currentParams.get(currentParamsKey);
httpParamsResult = httpParamsResult.append(currentParamsKey, currentParamsValue);
});
}
else {
Object.keys(currentParams).forEach((currentParamsKey) => {
const currentParamsValue = currentParams[currentParamsKey];
if (Array.isArray(currentParamsValue)) {
currentParamsValue.forEach((currentParamsValueItem) => {
httpParamsResult = httpParamsResult.append(currentParamsKey, currentParamsValueItem);
});
}
else
httpParamsResult = httpParamsResult.append(currentParamsKey, currentParamsValue);
});
}
});
return httpParamsResult;
}
function mergeHttpJsonBody(...body) {
let httpBodyResult = {};
body.forEach((currentBody) => {
httpBodyResult = _.merge(httpBodyResult, currentBody);
});
return httpBodyResult;
}
function mergeHttpFormDataBody(...body) {
const httpBodyResult = new FormData();
body.forEach((currentBody) => {
currentBody.forEach((value, key) => {
httpBodyResult.append(key, value);
});
});
return httpBodyResult;
}
function mergeHttpUrlSearchParamsBody(...body) {
const httpBodyResult = new URLSearchParams();
body.forEach((currentBody) => {
currentBody.forEach((value, key) => {
httpBodyResult.append(key, value);
});
});
return httpBodyResult;
}
const buildHttpConnectionString = (httpConfig) => `${httpConfig.protocol ?? "http"}://${httpConfig.host ?? "localhost"}${httpConfig.port ? `:${httpConfig.port}` : ""}${httpConfig.path ?? ""}`;
const mergeHttpConfig = (baseHttpConfig, httpConfig) => ({
...baseHttpConfig,
...httpConfig
});
var HttpHeaders;
(function (HttpHeaders) {
HttpHeaders.CONTENT_TYPE = "Content-Type";
})(HttpHeaders || (HttpHeaders = {}));
var HttpPorts;
(function (HttpPorts) {
HttpPorts.HTTP_PORT = 80;
HttpPorts.HTTPS_PORT = 443;
HttpPorts.HTTP_PORT_ALTERNATE = 7104;
HttpPorts.HTTPS_PORT_ALTERNATE = 7102;
})(HttpPorts || (HttpPorts = {}));
var HttpContentTypes;
(function (HttpContentTypes) {
let Application;
(function (Application) {
Application.X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
})(Application = HttpContentTypes.Application || (HttpContentTypes.Application = {}));
})(HttpContentTypes || (HttpContentTypes = {}));
var HTTP_PROTOCOLS;
(function (HTTP_PROTOCOLS) {
HTTP_PROTOCOLS.HTTPS = "https";
HTTP_PROTOCOLS.HTTP = "http";
})(HTTP_PROTOCOLS || (HTTP_PROTOCOLS = {}));
var HttpResponseTypes;
(function (HttpResponseTypes) {
HttpResponseTypes.JSON = "json";
HttpResponseTypes.ARRAYBUFFER = "arraybuffer";
HttpResponseTypes.BLOB = "blob";
HttpResponseTypes.TEXT = "text";
})(HttpResponseTypes || (HttpResponseTypes = {}));
var HttpObserve;
(function (HttpObserve) {
HttpObserve.BODY = "body";
HttpObserve.EVENTS = "events";
HttpObserve.RESPONSE = "response";
})(HttpObserve || (HttpObserve = {}));
const HTTP_ENVIRONMENT_TOKEN = new InjectionToken("http-environment-token");
var HttpRequestStatus;
(function (HttpRequestStatus) {
HttpRequestStatus["PENDING"] = "PENDING";
HttpRequestStatus["SUCCESS"] = "SUCCESS";
HttpRequestStatus["ERROR"] = "ERROR";
})(HttpRequestStatus || (HttpRequestStatus = {}));
const provideHttpConfig = (httpConfig) => ({
provide: HTTP_ENVIRONMENT_TOKEN,
useFactory: () => buildHttpConnectionString(httpConfig),
multi: false
});
export { HTTP_ENVIRONMENT_TOKEN, HTTP_PROTOCOLS, HttpContentTypes, HttpHeaders, HttpObserve, HttpPorts, HttpRequestStatus, HttpResponseTypes, buildHttpConnectionString, mergeHttpConfig, mergeHttpFormDataBody, mergeHttpHeaders, mergeHttpJsonBody, mergeHttpParams, mergeHttpUrlSearchParamsBody, provideHttpConfig };
//# sourceMappingURL=ogs-gmbh-ngx-http.mjs.map