@finos/legend-shared
Version:
Legend Studio shared utilities and helpers
117 lines • 5.96 kB
JavaScript
/**
* 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 { guaranteeNonNullable } from '../error/AssertionUtils.js';
import { HttpMethod, makeUrl, createRequestHeaders, NetworkClient, } from './NetworkUtils.js';
/**
* This is a template for server clients, it is a wrapper around `NetworkClient` with more features
* such as request payload compression, etc.
*/
export class AbstractServerClient {
networkClient;
_tracerService;
enableCompression;
enableDebuggingPayload;
payloadDebugger;
baseUrl;
baseHeaders;
autoReAuthenticateUrl;
constructor(config) {
this.networkClient = new NetworkClient({
baseUrl: config.baseUrl,
options: config.networkClientOptions,
});
this.baseUrl = config.baseUrl;
this.enableCompression = Boolean(config.enableCompression);
this.enableDebuggingPayload = Boolean(config.enableDebuggingPayload);
this.payloadDebugger = config.payloadDebugger;
this.baseHeaders = config.baseHeaders;
this.autoReAuthenticateUrl = config.autoReAuthenticateUrl;
}
setBaseUrl(val) {
this.baseUrl = val;
this.networkClient.baseUrl = val;
}
setCompression(val) {
this.enableCompression = val;
}
setDebugPayload(val) {
this.enableDebuggingPayload = val;
}
debugPayload(payload, identifier) {
if (this.enableDebuggingPayload && this.payloadDebugger) {
this.payloadDebugger(payload,
// convert the identifier to kebab case ot be more machine friendly
identifier.replace(/[\s_]+/g, '-').toLowerCase());
}
return payload;
}
setTracerService(val) {
this._tracerService = val;
}
get tracerService() {
return guaranteeNonNullable(this._tracerService, `Tracer service has not been set`);
}
async get(url, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
// NOTE: do not use Content-Type for GET to avoid unnecessary pre-flight when cross-origin
return this.request(HttpMethod.GET, url, undefined, options, headers, parameters, requestProcessConfig, responseProcessConfig);
}
async put(url, data = {}, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
return this.request(HttpMethod.PUT, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig);
}
async post(url, data = {}, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
return this.request(HttpMethod.POST, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig);
}
async delete(url, data = {}, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
return this.request(HttpMethod.DELETE, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig);
}
async getWithTracing(traceData, url, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
// NOTE: do not use Content-Type for GET to avoid unnecessary pre-flight when cross-origin
return this.request(HttpMethod.GET, url, undefined, options, headers, parameters, requestProcessConfig, responseProcessConfig, traceData);
}
async putWithTracing(traceData, url, data = {}, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
return this.request(HttpMethod.PUT, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig, traceData);
}
async postWithTracing(traceData, url, data = {}, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
return this.request(HttpMethod.POST, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig, traceData);
}
async deleteWithTracing(traceData, url, data = {}, options = {}, headers, parameters, requestProcessConfig, responseProcessConfig) {
return this.request(HttpMethod.DELETE, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig, traceData);
}
async request(method, url, data, options, headers, parameters, requestProcessConfig, responseProcessConfig, traceData) {
const requestUrl = makeUrl(this.networkClient.baseUrl, url, parameters ?? {});
headers = createRequestHeaders(method, headers);
// tracing
const trace = this.tracerService.createTrace(traceData, method.toString(), requestUrl, headers);
return this.networkClient
.request(method, url, data, options, this.baseHeaders ? { ...this.baseHeaders, ...headers } : headers, parameters, {
...(requestProcessConfig ?? {}),
enableCompression: requestProcessConfig?.enableCompression && this.enableCompression,
}, {
...(responseProcessConfig ?? {}),
preprocess: (response) => trace.bootstrap(response),
autoReAuthenticateUrl: this.autoReAuthenticateUrl,
})
.then((result) => {
trace.reportSuccess();
return Promise.resolve(result);
})
.catch((error) => {
trace.reportError(error);
return Promise.reject(error);
});
}
}
//# sourceMappingURL=AbstractServerClient.js.map