Commit d2dcc2ce authored by Samir Sadyhov's avatar Samir Sadyhov 🤔

new ApiClient

parent bd122c8d
this.ApiClient = class {
constructor() {
this._baseURL = `${window.location.origin}/Synergy`;
this._authHeader = this._buildAuthHeader();
this._requestInterceptors = [];
this._responseInterceptors = [];
}
async request({ url, method = 'GET', body, headers = {} }) {
let finalHeaders = this._normalizeHeaders({
authorization: this._authHeader,
...headers,
});
let finalBody = body;
const contentType = this._getContentType(finalHeaders);
if (body != null) {
// FormData
if (body instanceof FormData) {
delete finalHeaders['content-type'];
}
// JSON
else if (contentType === 'application/json') {
finalBody = JSON.stringify(body);
}
// x-www-form-urlencoded
else if (contentType === 'application/x-www-form-urlencoded') {
finalBody = new URLSearchParams(body).toString();
}
// авто JSON
else if (!contentType && this._isJsonLike(body)) {
finalHeaders['content-type'] = 'application/json';
finalBody = JSON.stringify(body);
}
}
let config = {
url: `${this._baseURL}/${url}`,
method,
headers: finalHeaders,
body: ['GET', 'HEAD'].includes(method) ? undefined : finalBody,
};
// request interceptors
for (const interceptor of this._requestInterceptors) {
config = (await interceptor(config)) || config;
}
let response;
try {
response = await fetch(config.url, config);
} catch (err) {
console.error({config, msg: err});
throw this._handleNetworkError(err);
}
const parsedData = await this._parseResponse(response);
let result = {
ok: response.ok,
status: response.status,
headers: response.headers,
data: parsedData,
};
// response interceptors
for (const interceptor of this._responseInterceptors) {
result = (await interceptor(result)) || result;
}
if (!response.ok) {
console.error({config, msg: result});
throw this._handleHttpError(result);
}
return result.data;
}
addRequestInterceptor(fn) {
this._requestInterceptors.push(fn);
}
addResponseInterceptor(fn) {
this._responseInterceptors.push(fn);
}
_buildAuthHeader() {
const {login, password, token} = AS.OPTIONS;
if (token) {
return `Bearer ${token}`;
} else {
const base64 = btoa(
new TextEncoder().encode(`${login}:${password}`)
.reduce((str, byte) => str + String.fromCharCode(byte), '')
);
return `Basic ${base64}`;
}
}
_normalizeHeaders(headers = {}) {
const result = {};
for (const key in headers) {
result[key.toLowerCase()] = headers[key];
}
return result;
}
_getContentType(headers = {}) {
const ct = headers['content-type'];
if (!ct) return null;
return ct.split(';')[0].trim().toLowerCase();
}
_isJsonLike(body) {
return (
typeof body === 'object' &&
!(body instanceof FormData) &&
!(body instanceof Blob) &&
!(body instanceof ArrayBuffer)
);
}
async _parseResponse(response) {
const contentType = response.headers.get('content-type') || '';
try {
if (contentType.includes('application/json')) {
return await response.json();
}
if (contentType.includes('text/')) {
return await response.text();
}
return await response.blob();
} catch {
return null;
}
}
_handleNetworkError(err) {
return new Error(`Network error: ${err.message}`);
}
_handleHttpError(res) {
return new Error(`HTTP error: ${res.status} - ${JSON.stringify(res.data)}`);
}
}
\ No newline at end of file
......@@ -40,13 +40,13 @@ const getRegistryRoute = async registryID => {
return new Promise(resolve => {
rest.synergyGet(`api/registry/route?registryID=${registryID}&type=ACTIVATE&locale=${AS.OPTIONS.locale}`, res => {
if(res.hasOwnProperty('errorCode')) {
console.log(`ERROR [ getRegistryRoute ]: ${JSON.stringify(res)}`);
console.log(`ERROR [ getRegistryRoute ]`, res);
resolve(null);
} else {
resolve(res);
}
}, err => {
console.log(`ERROR [ getRegistryRoute ]: ${JSON.stringify(err)}`);
console.log(`ERROR [ getRegistryRoute ]`, err);
resolve(null);
});
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment