@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
89 lines • 3.56 kB
JavaScript
/*!
* Copyright (c) 2019 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 { RequestSender } from './request-sender';
import { authenticateWithUrl } from '../../auth/auth-provider';
import { Header } from '../constants/header';
import { ContentType } from '../constants/content-type';
/**
* Handle to send HTTP requests.
*/
export class HttpRequestSender extends RequestSender {
constructor(requester, baseUrl, authenticationProviders) {
super();
this.requester = requester;
this.baseUrl = baseUrl;
this.authenticationProviders = authenticationProviders;
}
fetchRequest(options) {
return this.requester.doRequest(options.verb, this.buildUrl(options.id, options.path, options.requestOptions), this.buildHeader(options.requestOptions), options.payload)
.then(response => {
if (response.status >= 200 && response.status < 300) {
return response;
}
return Promise.reject(response.body);
});
}
/**
* Builds a URL to make HTTP calls with.
*
* @param id - The id of the basic entity the request is for.
* @param path - The path to the entity the request is about from the basic entity.
* @param options - The options provided in the request.
* @returns The request URL
*/
buildUrl(id, path, options) {
let urlSuffix = id === undefined ? '' : `/${id}`;
urlSuffix = path === undefined ? urlSuffix : `${urlSuffix}/${path}`;
if (options !== undefined && options.getOptions().size > 0) {
urlSuffix = `${urlSuffix}?${this.mapToQueryParams(options.getOptions())}`;
}
const baseUrlWithSuffix = this.baseUrl.withPath(`${this.baseUrl.path}${urlSuffix}`);
const authenticatedBaseUrl = authenticateWithUrl(baseUrlWithSuffix, this.authenticationProviders);
return authenticatedBaseUrl.toString();
}
/**
* Builds headers to make HTTP calls with by combining authentication and options headers.
* Options headers will override authentication headers.
*
* @param options - The options to provided in the request.
* @returns The combined headers
*/
buildHeader(options) {
const headers = new Map();
if (options) {
options.getHeaders().forEach((v, k) => headers.set(k, v));
}
if (!headers.has(Header.CONTENT_TYPE)) {
headers.set(Header.CONTENT_TYPE, ContentType.JSON);
}
let authenticatedHeaders = headers;
for (const authenticationProvider of this.authenticationProviders) {
authenticatedHeaders = authenticationProvider.authenticateWithHeaders(authenticatedHeaders);
}
return authenticatedHeaders;
}
}
/**
* A Factory for a HttpRequestSender.
*/
export class HttpRequestSenderBuilder {
constructor(requester, url, authProviders) {
this.requester = requester;
this.url = url;
this.authProviders = authProviders;
}
buildInstance(group) {
return new HttpRequestSender(this.requester, this.url.withPath(`${this.url.path}/${group}`), this.authProviders);
}
}
//# sourceMappingURL=http-request-sender.js.map