UNPKG

@eclipse-ditto/ditto-javascript-client-dom

Version:

DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.

86 lines 3.18 kB
/*! * 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 */ export { authenticateWithUrl, authenticateWithHeaders, authenticateWithUrlAndHeaders, ImmutableURL }; /** * Immutable implementation of DittoURL. */ class ImmutableURL { constructor(protocol, domain, path, queryParams) { this.protocol = protocol; this.domain = domain; this.path = path; this.queryParams = queryParams; } static newInstance(protocol, domain, path, params = []) { return new ImmutableURL(protocol, domain, path, params); } withProtocol(protocol) { return new ImmutableURL(protocol, this.domain, this.path, this.queryParams); } withDomain(domain) { return new ImmutableURL(this.protocol, domain, this.path, this.queryParams); } withPath(path) { return new ImmutableURL(this.protocol, this.domain, path, this.queryParams); } withParams(params) { return new ImmutableURL(this.protocol, this.domain, this.path, params); } toString() { const theParams = this.queryParams.length > 0 ? `?${this.queryParams.join('&')}` : ''; return `${this.protocol}://${this.domain}${this.path}${theParams}`; } } /** * Enhance the url with all auth providers. * @param originalUrl - the url to enhance. * @param authProviders - the auth providers to apply. * @return the enhanced url. */ const authenticateWithUrl = (originalUrl, authProviders) => { let enhancedUrl = originalUrl; for (const authProvider of authProviders) { enhancedUrl = authProvider.authenticateWithUrl(enhancedUrl); } return enhancedUrl; }; /** * Enhance the headers with all auth providers. * @param originalHeaders - the headers to enhance. * @param authProviders - the auth providers to apply. * @return the enhanced headers. */ const authenticateWithHeaders = (originalHeaders, authProviders) => { let enhancedHeaders = originalHeaders; for (const authProvider of authProviders) { enhancedHeaders = authProvider.authenticateWithHeaders(enhancedHeaders); } return enhancedHeaders; }; /** * Enhance the url and headers with all auth providers. * @param originalUrl - the url to enhance. * @param originalHeaders - the headers to enhance. * @param authProviders - the auth providers to apply. * @return the enhanced url and headers. */ const authenticateWithUrlAndHeaders = (originalUrl, originalHeaders, authProviders) => { let enhancedUrl = originalUrl; let enhancedHeaders = originalHeaders; for (const authProvider of authProviders) { enhancedUrl = authProvider.authenticateWithUrl(enhancedUrl); enhancedHeaders = authProvider.authenticateWithHeaders(enhancedHeaders); } return [enhancedUrl, enhancedHeaders]; }; //# sourceMappingURL=auth-provider.js.map