Skip to content

Commit f4b634a

Browse files
jgbernalpPeterYurkovich
authored andcommitted
remove getCSRFToken usages in favor of consoleFetch
Signed-off-by: Gabriel Bernal <gbernal@redhat.com>
1 parent 5ae23cb commit f4b634a

6 files changed

Lines changed: 20 additions & 105 deletions

File tree

web/src/components/dashboards/perses/perses-client.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1+
import { consoleFetchJSON } from '@openshift-console/dynamic-plugin-sdk';
2+
import { DashboardResource, ProjectResource } from '@perses-dev/core';
13
import { useQuery } from '@tanstack/react-query';
2-
import { DashboardResource, ProjectResource, fetchJson } from '@perses-dev/core';
34
import { NumberParam, useQueryParam } from 'use-query-params';
45
import { QueryParams } from '../../query-params';
5-
import { getCSRFToken } from '@openshift-console/dynamic-plugin-sdk/lib/utils/fetch/console-fetch-utils';
66

77
export const PERSES_PROXY_BASE_PATH = '/api/proxy/plugin/monitoring-console-plugin/perses';
88

99
export const fetchPersesDashboardsMetadata = (): Promise<DashboardResource[]> => {
1010
const listDashboardsMetadata = '/api/v1/dashboards';
1111
const persesURL = `${PERSES_PROXY_BASE_PATH}${listDashboardsMetadata}`;
1212

13-
return ocpPersesFetchJson<DashboardResource[]>(persesURL);
13+
return consoleFetchJSON(persesURL);
1414
};
1515

1616
export const fetchPersesDashboardsByProject = (project: string): Promise<DashboardResource[]> => {
1717
const dashboardsEndpoint = `${PERSES_PROXY_BASE_PATH}/api/v1/dashboards`;
1818
const persesURL = `${dashboardsEndpoint}?project=${encodeURIComponent(project)}`;
1919

20-
return ocpPersesFetchJson<DashboardResource[]>(persesURL);
20+
return consoleFetchJSON(persesURL);
2121
};
2222

2323
export const fetchPersesProjects = (): Promise<ProjectResource[]> => {
2424
const listProjectURL = '/api/v1/projects';
2525
const persesURL = `${PERSES_PROXY_BASE_PATH}${listProjectURL}`;
2626

27-
return ocpPersesFetchJson<ProjectResource[]>(persesURL);
27+
return consoleFetchJSON(persesURL);
2828
};
2929

3030
export interface PersesPermission {
@@ -40,26 +40,17 @@ export const fetchPersesUserPermissions = (username: string): Promise<PersesUser
4040
const userPermissionsURL = `/api/v1/users/${encodeURIComponent(username)}/permissions`;
4141
const persesURL = `${PERSES_PROXY_BASE_PATH}${userPermissionsURL}`;
4242

43-
return ocpPersesFetchJson<PersesUserPermissions>(persesURL);
43+
return consoleFetchJSON(persesURL);
4444
};
4545

46-
export async function ocpPersesFetchJson<T>(url: string): Promise<T> {
47-
// Use perses fetch as base fetch call as it handles refresh tokens
48-
return fetchJson(url, {
49-
headers: {
50-
'X-CSRFToken': getCSRFToken(),
51-
},
52-
});
53-
}
54-
5546
export const fetchPersesDashboard = async (
5647
project: string,
5748
dashboardName: string,
5849
): Promise<DashboardResource> => {
5950
const getDashboardURL = `/api/v1/projects/${project}/dashboards/${dashboardName}`;
6051
const persesURL = `${PERSES_PROXY_BASE_PATH}${getDashboardURL}`;
6152

62-
return await ocpPersesFetchJson<DashboardResource>(persesURL);
53+
return await consoleFetchJSON(persesURL);
6354
};
6455

6556
export const useFetchPersesDashboard = (project: string, dashboardName: string) => {

web/src/components/dashboards/perses/perses/datasource-cache-api.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
import { getCSRFToken } from '@openshift-console/dynamic-plugin-sdk/lib/utils/fetch/console-fetch-utils';
1514
import {
1615
DatasourceResource,
1716
DatasourceSelector,
@@ -127,7 +126,7 @@ export class CachedDatasourceAPI implements DatasourceApi {
127126
): Promise<DatasourceResource | undefined> {
128127
const { resource, keyExist } = this.cache.getDatasource(project, selector);
129128
if (resource) {
130-
return Promise.resolve(addCsrfToken(resource));
129+
return Promise.resolve(resource);
131130
}
132131
if (keyExist) {
133132
// in case the keyExist, then it means we already did the query,
@@ -146,14 +145,14 @@ export class CachedDatasourceAPI implements DatasourceApi {
146145
} else {
147146
this.cache.setDatasource(result);
148147
}
149-
return addCsrfToken(result);
148+
return result;
150149
});
151150
}
152151

153152
getGlobalDatasource(selector: DatasourceSelector): Promise<GlobalDatasourceResource | undefined> {
154153
const { resource, keyExist } = this.cache.getGlobalDatasource(selector);
155154
if (resource) {
156-
return Promise.resolve(addCsrfToken(resource));
155+
return Promise.resolve(resource);
157156
}
158157
if (keyExist) {
159158
return Promise.resolve(undefined);
@@ -164,7 +163,7 @@ export class CachedDatasourceAPI implements DatasourceApi {
164163
} else {
165164
this.cache.setGlobalDatasource(result);
166165
}
167-
return addCsrfToken(result);
166+
return result;
168167
});
169168
}
170169

@@ -184,18 +183,3 @@ export class CachedDatasourceAPI implements DatasourceApi {
184183
});
185184
}
186185
}
187-
188-
const addCsrfToken = (datasource) => {
189-
datasource.spec.plugin.spec = {
190-
...datasource.spec.plugin.spec,
191-
proxy: {
192-
spec: {
193-
headers: {
194-
'X-CSRFToken': getCSRFToken(),
195-
'Sec-Fetch-Site': 'same-origin',
196-
},
197-
},
198-
},
199-
};
200-
return datasource;
201-
};

web/src/components/dashboards/perses/perses/datasource-client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import { DatasourceResource } from '@perses-dev/core';
1515
import buildURL from './url-builder';
16-
import { ocpPersesFetchJson } from '../perses-client';
16+
import { consoleFetchJSON } from '@openshift-console/dynamic-plugin-sdk';
1717

1818
export const resource = 'datasources';
1919

@@ -40,11 +40,11 @@ export function fetchDatasourceList(
4040
kind?: string,
4141
defaultDatasource?: boolean,
4242
name?: string,
43-
) {
43+
): Promise<DatasourceResource[]> {
4444
const url = buildURL({
4545
resource: resource,
4646
project: project,
4747
queryParams: buildDatasourceQueryParameters(kind, defaultDatasource, name),
4848
});
49-
return ocpPersesFetchJson<DatasourceResource[]>(url);
49+
return consoleFetchJSON(url);
5050
}

web/src/components/dashboards/perses/perses/global-datasource-client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
import { GlobalDatasourceResource } from '@perses-dev/core';
1515
import buildURL from './url-builder';
1616
import { buildDatasourceQueryParameters } from './datasource-client';
17-
import { ocpPersesFetchJson } from '../perses-client';
17+
import { consoleFetchJSON } from '@openshift-console/dynamic-plugin-sdk';
1818

1919
const globalDatasourceResource = 'globaldatasources';
2020

2121
export function fetchGlobalDatasourceList(
2222
kind?: string,
2323
defaultDatasource?: boolean,
2424
name?: string,
25-
) {
25+
): Promise<GlobalDatasourceResource[]> {
2626
const url = buildURL({
2727
resource: globalDatasourceResource,
2828
queryParams: buildDatasourceQueryParameters(kind, defaultDatasource, name),
2929
});
30-
return ocpPersesFetchJson<GlobalDatasourceResource[]>(url);
30+
return consoleFetchJSON(url);
3131
}

web/src/components/hooks/useFeatures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useState, useRef, useCallback, useEffect } from 'react';
2-
import { proxiedFetch } from '../proxied-fetch';
2+
import { consoleFetchJSON } from '@openshift-console/dynamic-plugin-sdk';
33

44
type features = {
55
'acm-alerting': boolean;
66
'perses-dashboards': boolean;
77
incidents: boolean;
88
};
99

10-
type featuresResponse = {
10+
type FeaturesResponse = {
1111
'acm-alerting'?: boolean;
1212
'perses-dashboards'?: boolean;
1313
incidents?: boolean;
@@ -32,7 +32,7 @@ export const useFeatures = () => {
3232
dashboardsAbort.current();
3333
}
3434

35-
const response = await proxiedFetch<featuresResponse>(featuresEndpoint);
35+
const response: FeaturesResponse = await consoleFetchJSON(featuresEndpoint);
3636
setFeatures({ ...noFeatures, ...response });
3737
} catch {
3838
setFeatures(noFeatures);

web/src/components/proxied-fetch.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)