Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions apps/ocp-plugin/src/utils/apiCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getErrorMsgFromApiResponse,
} from '@flightctl/ui-components/src/utils/apiCalls';
import { ORGANIZATION_STORAGE_KEY } from '@flightctl/ui-components/src/utils/organizationStorage';
import { API_VERSION } from '@flightctl/ui-components/src/constants';

declare global {
interface Window {
Expand All @@ -15,12 +16,15 @@ declare global {
}
}

const addRequiredHeaders = (options: RequestInit): RequestInit => {
const addRequiredHeaders = (options: RequestInit, apiVersion?: string): RequestInit => {
const token = getCSRFToken();
const orgId = localStorage.getItem(ORGANIZATION_STORAGE_KEY);

const headers = new Headers(options.headers || {});
headers.set('X-CSRFToken', token);
if (apiVersion) {
headers.set('Flightctl-API-Version', apiVersion);
}
if (orgId) {
headers.set('X-FlightCtl-Organization-ID', orgId);
}
Expand Down Expand Up @@ -104,9 +108,9 @@ const putOrPostData = async <TRequest, TResponse = TRequest>(
body: JSON.stringify(data),
};

const options = addRequiredHeaders(baseOptions);
const { api, url } = getFullApiUrl(kind);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
try {
const { url } = getFullApiUrl(kind);
const response = await fetch(url, options);
return handleApiJSONResponse(response);
} catch (error) {
Expand All @@ -127,9 +131,9 @@ export const deleteData = async <R>(kind: string, abortSignal?: AbortSignal): Pr
signal: abortSignal,
};

const options = addRequiredHeaders(baseOptions);
const { api, url } = getFullApiUrl(kind);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
try {
const { url } = getFullApiUrl(kind);
const response = await fetch(url, options);
return handleApiJSONResponse(response);
} catch (error) {
Expand All @@ -148,9 +152,9 @@ export const patchData = async <R>(kind: string, data: PatchRequest, abortSignal
signal: abortSignal,
};

const options = addRequiredHeaders(baseOptions);
const { api, url } = getFullApiUrl(kind);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);
try {
const { url } = getFullApiUrl(kind);
const response = await fetch(url, options);
return handleApiJSONResponse(response);
} catch (error) {
Expand All @@ -167,7 +171,7 @@ export const fetchData = async <R>(path: string, abortSignal?: AbortSignal): Pro
signal: abortSignal,
};

const options = addRequiredHeaders(baseOptions);
const options = addRequiredHeaders(baseOptions, api === 'flightctl' ? API_VERSION : undefined);

const response = await fetch(url, options);
if (api === 'alerts') {
Expand Down
8 changes: 8 additions & 0 deletions apps/standalone/src/app/utils/apiCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getErrorMsgFromApiResponse,
} from '@flightctl/ui-components/src/utils/apiCalls';
import { ORGANIZATION_STORAGE_KEY } from '@flightctl/ui-components/src/utils/organizationStorage';
import { API_VERSION } from '@flightctl/ui-components/src/constants';

import { lastRefresh } from '../context/AuthContext';

Expand Down Expand Up @@ -108,6 +109,13 @@ const fetchWithRetry = async <R>(path: string, init?: RequestInit): Promise<R> =
// Add organization header if available
const options = addOrganizationHeader({ ...init });

// Add version header only for FlightCtl API
if (api === 'flightctl') {
const headers = new Headers(options.headers);
headers.set('Flightctl-API-Version', API_VERSION);
options.headers = headers;
}

const prevRefresh = lastRefresh;
let response = await fetch(url, options);
//if token refresh occured, lets try again
Expand Down
2 changes: 1 addition & 1 deletion proxy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func corsHandler(router *mux.Router) http.Handler {
return gorillaHandlers.CORS(
gorillaHandlers.AllowedOrigins([]string{"http://localhost:9000"}),
gorillaHandlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "PATCH"}),
gorillaHandlers.AllowedHeaders([]string{"Content-Type", "Authorization", "X-FlightCtl-Organization-ID"}),
gorillaHandlers.AllowedHeaders([]string{"Content-Type", "Authorization", "X-FlightCtl-Organization-ID", "Flightctl-API-Version"}),
gorillaHandlers.AllowCredentials(),
)(router)
}
Expand Down