♻️(frontend) add api versionning per request

We were using the version of the api from the
.env file, but we could have different versions
of the api in the same app. So we now use the
version from the request.
This commit is contained in:
Anthony LC
2024-06-06 09:54:44 +02:00
committed by Anthony LC
parent 197b16c5d0
commit da3ad91283
9 changed files with 27 additions and 21 deletions

View File

@@ -1,3 +1,2 @@
NEXT_PUBLIC_API_ORIGIN=http://localhost:8071 NEXT_PUBLIC_API_ORIGIN=http://localhost:8071
NEXT_PUBLIC_API_URL=/api/v1.0/
NEXT_PUBLIC_SIGNALING_URL=ws://localhost:4444 NEXT_PUBLIC_SIGNALING_URL=ws://localhost:4444

View File

@@ -1 +0,0 @@
NEXT_PUBLIC_API_URL=/api/v1.0/

View File

@@ -1,2 +1 @@
NEXT_PUBLIC_API_ORIGIN=http://test.jest NEXT_PUBLIC_API_ORIGIN=http://test.jest
NEXT_PUBLIC_API_URL=/api/

View File

@@ -9,15 +9,15 @@ describe('fetchAPI', () => {
}); });
it('adds correctly the basename', () => { it('adds correctly the basename', () => {
fetchMock.mock('http://test.jest/api/some/url', 200); fetchMock.mock('http://test.jest/api/v1.0/some/url', 200);
void fetchAPI('some/url'); void fetchAPI('some/url');
expect(fetchMock.lastUrl()).toEqual('http://test.jest/api/some/url'); expect(fetchMock.lastUrl()).toEqual('http://test.jest/api/v1.0/some/url');
}); });
it('adds the credentials automatically', () => { it('adds the credentials automatically', () => {
fetchMock.mock('http://test.jest/api/some/url', 200); fetchMock.mock('http://test.jest/api/v1.0/some/url', 200);
void fetchAPI('some/url', { body: 'some body' }); void fetchAPI('some/url', { body: 'some body' });
@@ -36,10 +36,18 @@ describe('fetchAPI', () => {
.spyOn(useAuthStore.getState(), 'logout') .spyOn(useAuthStore.getState(), 'logout')
.mockImplementation(logoutMock); .mockImplementation(logoutMock);
fetchMock.mock('http://test.jest/api/some/url', 401); fetchMock.mock('http://test.jest/api/v1.0/some/url', 401);
await fetchAPI('some/url'); await fetchAPI('some/url');
expect(logoutMock).toHaveBeenCalled(); expect(logoutMock).toHaveBeenCalled();
}); });
it('check the versionning', () => {
fetchMock.mock('http://test.jest/api/v2.0/some/url', 200);
void fetchAPI('some/url', {}, '2.0');
expect(fetchMock.lastUrl()).toEqual('http://test.jest/api/v2.0/some/url');
});
}); });

View File

@@ -13,9 +13,12 @@ function getCSRFToken() {
.pop(); .pop();
} }
export const fetchAPI = async (input: string, init?: RequestInit) => { export const fetchAPI = async (
const apiUrl = `${baseApiUrl()}${input}`; input: string,
init?: RequestInit,
apiVersion = '1.0',
) => {
const apiUrl = `${baseApiUrl(apiVersion)}${input}`;
const csrfToken = getCSRFToken(); const csrfToken = getCSRFToken();
const response = await fetch(apiUrl, { const response = await fetch(apiUrl, {

View File

@@ -1,9 +1,9 @@
export const baseApiUrl = () => { export const baseApiUrl = (apiVersion: string = '1.0') => {
const origin = const origin =
process.env.NEXT_PUBLIC_API_ORIGIN || process.env.NEXT_PUBLIC_API_ORIGIN ||
(typeof window !== 'undefined' ? window.location.origin : ''); (typeof window !== 'undefined' ? window.location.origin : '');
return `${origin}${process.env.NEXT_PUBLIC_API_URL}`; return `${origin}/api/v${apiVersion}/`;
}; };
export const signalingUrl = (padId: string) => { export const signalingUrl = (padId: string) => {

View File

@@ -20,7 +20,6 @@ declare module '*.svg?url' {
namespace NodeJS { namespace NodeJS {
interface ProcessEnv { interface ProcessEnv {
NEXT_PUBLIC_API_ORIGIN?: string; NEXT_PUBLIC_API_ORIGIN?: string;
NEXT_PUBLIC_API_URL?: string;
NEXT_PUBLIC_SIGNALING_URL?: string; NEXT_PUBLIC_SIGNALING_URL?: string;
} }
} }

View File

@@ -23,7 +23,7 @@ describe('PanelPads', () => {
}); });
it('renders with no pad to display', async () => { it('renders with no pad to display', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
count: 0, count: 0,
results: [], results: [],
}); });
@@ -40,7 +40,7 @@ describe('PanelPads', () => {
}); });
it('renders an empty pad', async () => { it('renders an empty pad', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
count: 1, count: 1,
results: [ results: [
{ {
@@ -59,7 +59,7 @@ describe('PanelPads', () => {
}); });
it('renders a pad with only 1 member', async () => { it('renders a pad with only 1 member', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
count: 1, count: 1,
results: [ results: [
{ {
@@ -83,7 +83,7 @@ describe('PanelPads', () => {
}); });
it('renders a non-empty pad', async () => { it('renders a non-empty pad', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
count: 1, count: 1,
results: [ results: [
{ {
@@ -111,7 +111,7 @@ describe('PanelPads', () => {
}); });
it('renders the error', async () => { it('renders the error', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
status: 500, status: 500,
}); });
@@ -127,7 +127,7 @@ describe('PanelPads', () => {
}); });
it('renders with doc panel open', async () => { it('renders with doc panel open', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
count: 1, count: 1,
results: [], results: [],
}); });
@@ -142,7 +142,7 @@ describe('PanelPads', () => {
}); });
it('closes and opens the doc panel', async () => { it('closes and opens the doc panel', async () => {
fetchMock.mock(`end:/api/documents/?page=1&ordering=-created_at`, { fetchMock.mock(`end:/documents/?page=1&ordering=-created_at`, {
count: 1, count: 1,
results: [], results: [],
}); });

View File

@@ -72,7 +72,6 @@ frontend:
envVars: envVars:
PORT: 8080 PORT: 8080
NEXT_PUBLIC_API_ORIGIN: https://impress.127.0.0.1.nip.io NEXT_PUBLIC_API_ORIGIN: https://impress.127.0.0.1.nip.io
NEXT_PUBLIC_API_URL: /api/v1.0/
NEXT_PUBLIC_SIGNALING_URL: wss://impress.127.0.0.1.nip.io/ws NEXT_PUBLIC_SIGNALING_URL: wss://impress.127.0.0.1.nip.io/ws
replicas: 1 replicas: 1