openreplay/frontend/app/api_client.js
Delirium e9e3e21a10
feat(ui/tracker): feature flags (#1097)
* fix(player): fix initial visual offset jump check

* change(ui): add empty feature flags page

* change(ui): add empty feature flags page

* fix(ui): some more fixes

* change(ui): add subrouting for sessions tab

* change(ui): more fixes for routing

* change(ui): add flag creation page, flags list table, flag store/type

* change(tracker): flags in tracker

* change(tracker): return all flags

* feat(ui): add API and types connector

* feat(ui): split components to prevent rerendering

* feat(ui): add icon, fix redirect.path crashlooping

* feat(ui): add conditions and stuff, add flags class to tracker to handle stuff

* feat(ui): add condition state and filters

* feat(ui): fix flag creation with api change

* feat(ui): fix flag editing (api changes); simplify new/edit flag component

* feat(ui): add filters, make table pretty :insert_magic_emoji:

* feat(ui): remove rollout percentage from list, remove console logs

* feat(ui): multivar toggler

* feat(tracker): add more methods to tracker

* feat(tracker): more type coverage

* feat(tracker): add tests

* fix(ui): some fixes for multivar

* feat(ui): multivar api support

* fix(ui):start adding tests for fflags

* fix(ui): rm not working file..

* fix(ui): rm unused packages

* fix(ui): remove name field, fix some api and type names

* fix(ui): fix crash

* fix(tracker/ui): keep flags in sessionStorage, support API errors in feature flags storage

* fix(tracker/ui): clear unused things, fix url handling, fix icons rendering etc
2023-06-21 12:35:40 +02:00

127 lines
3 KiB
JavaScript

import store from 'App/store';
import { queried } from './routes';
const siteIdRequiredPaths = [
'/dashboard',
'/sessions',
'/events',
'/filters',
'/alerts',
'/targets',
'/metadata',
'/integrations/sentry/events',
'/integrations/slack/notify',
'/integrations/msteams/notify',
'/assignments',
'/integration/sources',
'/issue_types',
'/sample_rate',
'/saved_search',
'/rehydrations',
'/sourcemaps',
'/errors',
'/funnels',
'/assist',
'/heatmaps',
'/custom_metrics',
'/dashboards',
'/cards',
'/unprocessed',
'/notes',
'/feature-flags',
// '/custom_metrics/sessions',
];
const noStoringFetchPathStarts = [
'/account/password',
'/password',
'/login'
];
// null?
export const clean = (obj, forbidenValues = [ undefined, '' ]) => {
const keys = Array.isArray(obj)
? new Array(obj.length).fill().map((_, i) => i)
: Object.keys(obj);
const retObj = Array.isArray(obj) ? [] : {};
keys.map(key => {
const value = obj[key];
if (typeof value === 'object' && value !== null) {
retObj[key] = clean(value);
} else if (!forbidenValues.includes(value)) {
retObj[key] = value;
}
});
return retObj;
}
export default class APIClient {
constructor() {
const jwt = store.getState().getIn(['user', 'jwt']);
const siteId = store.getState().getIn([ 'site', 'siteId' ]);
this.init = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
};
if (jwt !== null) {
this.init.headers.Authorization = `Bearer ${ jwt }`;
}
this.siteId = siteId;
}
fetch(path, params, options = { clean: true }) {
if (params !== undefined) {
const cleanedParams = options.clean ? clean(params) : params;
this.init.body = JSON.stringify(cleanedParams);
}
if (this.init.method === 'GET') {
delete this.init.body;
}
let fetch = window.fetch;
let edp = window.env.API_EDP || window.location.origin + '/api';
if (
path !== '/targets_temp' &&
!path.includes('/metadata/session_search') &&
!path.includes('/assist/credentials') &&
!!this.siteId &&
siteIdRequiredPaths.some(sidPath => path.startsWith(sidPath))
) {
edp = `${ edp }/${ this.siteId }`
}
return fetch(edp + path, this.init)
.then((response) => {
if (response.ok) {
return response
} else {
return Promise.reject({ message: `! ${this.init.method} error on ${path}; ${response.status}`, response });
}
})
}
get(path, params, options) {
this.init.method = 'GET';
return this.fetch(queried(path, params, options));
}
post(path, params, options) {
this.init.method = 'POST';
return this.fetch(path, params);
}
put(path, params, options) {
this.init.method = 'PUT';
return this.fetch(path, params);
}
delete(path, params, options) {
this.init.method = 'DELETE';
return this.fetch(path, params);
}
}