camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
33 lines (32 loc) • 1.08 kB
JavaScript
import { addParametersToPath } from '../internal/utils';
export class DefaultClient {
domain;
constructor(domain = '') {
this.domain = domain;
}
get = (params) => {
return this.fetchWithTimeout(addParametersToPath(params.path, params.parameters), {
method: 'GET',
headers: params.headers,
}, params.timeout);
};
post = (params) => {
return this.fetchWithTimeout(addParametersToPath(params.path, params.parameters), {
method: 'POST',
body: params.data,
headers: params.headers,
}, params.timeout);
};
async fetchWithTimeout(path, options, timeout) {
const controller = new AbortController();
const timeoutId = timeout !== undefined ? setTimeout(() => controller.abort(), timeout) : null;
try {
return await fetch(`${this.domain}${path}`, { ...options, signal: controller.signal });
}
finally {
if (timeoutId) {
clearTimeout(timeoutId);
}
}
}
}