@eclipse-ditto/ditto-javascript-client-node
Version:
Node.js(r) implementation of Eclipse Ditto JavaScript API.
92 lines • 3.5 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImmutableURL = exports.authenticateWithUrlAndHeaders = exports.authenticateWithHeaders = exports.authenticateWithUrl = void 0;
/**
* 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}`;
}
}
exports.ImmutableURL = ImmutableURL;
/**
* 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;
};
exports.authenticateWithUrl = authenticateWithUrl;
/**
* 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;
};
exports.authenticateWithHeaders = authenticateWithHeaders;
/**
* 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];
};
exports.authenticateWithUrlAndHeaders = authenticateWithUrlAndHeaders;
//# sourceMappingURL=auth-provider.js.map