Skip to content

Commit

Permalink
Merge pull request #374 from huwshimi/custom-axios-instance
Browse files Browse the repository at this point in the history
WD-14004 - feat: add custom axios instance
  • Loading branch information
huwshimi authored Aug 8, 2024
2 parents f78cf85 + 722a331 commit 03ef78e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 35 deletions.
4 changes: 2 additions & 2 deletions ui/src/api/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import axios from "axios";
import MockAdapter from "axios-mock-adapter";

import { fetchMe, authURLs } from "./auth";
import { axiosInstance } from "./axios";

const mock = new MockAdapter(axios);
const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
Expand Down
5 changes: 2 additions & 3 deletions ui/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import axios from "axios";

import type { UserPrincipal } from "types/auth";
import { ErrorResponse } from "types/api";
import { AxiosError } from "axios";
import { axiosInstance } from "./axios";

const BASE = "auth";

Expand All @@ -13,7 +12,7 @@ export const authURLs = {

export const fetchMe = (): Promise<UserPrincipal | null> => {
return new Promise((resolve, reject) => {
axios
axiosInstance
.get<UserPrincipal>(authURLs.me)
.then(({ data }) => resolve(data))
.catch(({ response }: AxiosError<ErrorResponse>) => {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import axios from "axios";
import { apiBasePath } from "util/basePaths";

export const axiosInstance = axios.create({ baseURL: apiBasePath });
16 changes: 10 additions & 6 deletions ui/src/api/client.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import { Client } from "types/client";
import { PaginatedResponse, ApiResponse } from "types/api";
import { handleRequest, PAGE_SIZE } from "util/api";
import axios from "axios";
import { axiosInstance } from "./axios";

export const fetchClients = (
pageToken: string,
): Promise<PaginatedResponse<Client[]>> =>
handleRequest(() =>
axios.get<PaginatedResponse<Client[]>>(
axiosInstance.get<PaginatedResponse<Client[]>>(
`/clients?page_token=${pageToken}&size=${PAGE_SIZE}`,
),
);

export const fetchClient = (clientId: string): Promise<ApiResponse<Client>> =>
handleRequest(() => axios.get<ApiResponse<Client>>(`/clients/${clientId}`));
handleRequest(() =>
axiosInstance.get<ApiResponse<Client>>(`/clients/${clientId}`),
);

export const createClient = (values: string): Promise<ApiResponse<Client>> =>
handleRequest(() => axios.post<ApiResponse<Client>>("/clients", values));
handleRequest(() =>
axiosInstance.post<ApiResponse<Client>>("/clients", values),
);

export const updateClient = (
clientId: string,
values: string,
): Promise<ApiResponse<Client>> =>
handleRequest(() =>
axios.post<ApiResponse<Client>>(`/clients/${clientId}`, values),
axiosInstance.post<ApiResponse<Client>>(`/clients/${clientId}`, values),
);

export const deleteClient = (client: string) =>
handleRequest(() =>
axios.get<ApiResponse<string>>(`/clients/${client}`, {
axiosInstance.get<ApiResponse<string>>(`/clients/${client}`, {
method: "DELETE",
}),
);
15 changes: 9 additions & 6 deletions ui/src/api/identities.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ApiResponse, PaginatedResponse } from "types/api";
import { handleRequest, PAGE_SIZE } from "util/api";
import { Identity } from "types/identity";
import axios from "axios";
import { axiosInstance } from "./axios";

export const fetchIdentities = (
pageToken: string,
): Promise<PaginatedResponse<Identity[]>> =>
handleRequest(() =>
axios.get<PaginatedResponse<Identity[]>>(
axiosInstance.get<PaginatedResponse<Identity[]>>(
`/identities?page_token=${pageToken}&size=${PAGE_SIZE}`,
),
);
Expand All @@ -16,19 +16,22 @@ export const fetchIdentity = (
identityId: string,
): Promise<ApiResponse<Identity>> =>
handleRequest(() =>
axios.get<ApiResponse<Identity>>(`/identities/${identityId}`),
axiosInstance.get<ApiResponse<Identity>>(`/identities/${identityId}`),
);

export const createIdentity = (body: string): Promise<unknown> =>
handleRequest(() => axios.post("/identities", body));
handleRequest(() => axiosInstance.post("/identities", body));

export const updateIdentity = (
identityId: string,
values: string,
): Promise<ApiResponse<Identity>> =>
handleRequest(() =>
axios.patch<ApiResponse<Identity>>(`/identities/${identityId}`, values),
axiosInstance.patch<ApiResponse<Identity>>(
`/identities/${identityId}`,
values,
),
);

export const deleteIdentity = (identityId: string): Promise<unknown> =>
handleRequest(() => axios.delete(`/identities/${identityId}`));
handleRequest(() => axiosInstance.delete(`/identities/${identityId}`));
15 changes: 9 additions & 6 deletions ui/src/api/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import { handleRequest } from "util/api";
import { IdentityProvider } from "types/provider";
import axios from "axios";
import { PaginatedResponse, ApiResponse } from "types/api";
import { axiosInstance } from "./axios";

export const fetchProviders = (): Promise<
PaginatedResponse<IdentityProvider[]>
> =>
handleRequest(() =>
axios.get<PaginatedResponse<IdentityProvider[]>>(`/idps`),
axiosInstance.get<PaginatedResponse<IdentityProvider[]>>(`/idps`),
);

export const fetchProvider = (
providerId: string,
): Promise<IdentityProvider> => {
return new Promise((resolve, reject) => {
handleRequest<IdentityProvider[], ApiResponse<IdentityProvider[]>>(() =>
axios.get<ApiResponse<IdentityProvider[]>>(`/idps/${providerId}`),
axiosInstance.get<ApiResponse<IdentityProvider[]>>(`/idps/${providerId}`),
)
.then(({ data }) => resolve(data[0]))
.catch((error) => reject(error));
});
};

export const createProvider = (body: string): Promise<unknown> =>
handleRequest(() => axios.post("/idps", body));
handleRequest(() => axiosInstance.post("/idps", body));

export const updateProvider = (
providerId: string,
values: string,
): Promise<ApiResponse<IdentityProvider>> =>
handleRequest(() =>
axios.patch<ApiResponse<IdentityProvider>>(`/idps/${providerId}`, values),
axiosInstance.patch<ApiResponse<IdentityProvider>>(
`/idps/${providerId}`,
values,
),
);

export const deleteProvider = (providerId: string): Promise<unknown> =>
handleRequest(() => axios.delete(`/idps/${providerId}`));
handleRequest(() => axiosInstance.delete(`/idps/${providerId}`));
12 changes: 6 additions & 6 deletions ui/src/api/schema.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { PaginatedResponse, ApiResponse } from "types/api";
import { handleRequest, PAGE_SIZE } from "util/api";
import { Schema } from "types/schema";
import axios from "axios";
import { axiosInstance } from "./axios";

export const fetchSchemas = (
pageToken: string,
): Promise<PaginatedResponse<Schema[]>> =>
handleRequest(() =>
axios.get<PaginatedResponse<Schema[]>>(
axiosInstance.get<PaginatedResponse<Schema[]>>(
`/schemas?page_token=${pageToken}&page_size=${PAGE_SIZE}`,
),
);

export const fetchSchema = (schemaId: string): Promise<Schema> => {
return new Promise((resolve, reject) => {
handleRequest<Schema[], ApiResponse<Schema[]>>(() =>
axios.get<ApiResponse<Schema[]>>(`/schemas/${schemaId}`),
axiosInstance.get<ApiResponse<Schema[]>>(`/schemas/${schemaId}`),
)
.then(({ data }) => resolve(data[0]))
.catch((error) => reject(error));
});
};

export const createSchema = (body: string): Promise<unknown> =>
handleRequest(() => axios.post("/schemas", body));
handleRequest(() => axiosInstance.post("/schemas", body));

export const updateSchema = (
schemaId: string,
values: string,
): Promise<ApiResponse<Schema>> =>
handleRequest(() =>
axios.patch<ApiResponse<Schema>>(`/schemas/${schemaId}`, values),
axiosInstance.patch<ApiResponse<Schema>>(`/schemas/${schemaId}`, values),
);

export const deleteSchema = (schemaId: string): Promise<unknown> =>
handleRequest(() => axios.delete(`/schemas/${schemaId}`));
handleRequest(() => axiosInstance.delete(`/schemas/${schemaId}`));
4 changes: 2 additions & 2 deletions ui/src/components/Layout/Layout.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { screen } from "@testing-library/dom";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";

import { authURLs } from "api/auth";
import { LoginLabel } from "components/Login";
import { renderComponent } from "test/utils";

import Layout from "./Layout";
import { axiosInstance } from "api/axios";

const mock = new MockAdapter(axios);
const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
Expand Down
5 changes: 1 addition & 4 deletions ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import App from "./App";
import "./sass/styles.scss";
import { NotificationProvider } from "@canonical/react-components";
import { apiBasePath, basePath } from "util/basePaths";
import axios from "axios";
import { basePath } from "util/basePaths";

const queryClient = new QueryClient({
defaultOptions: {
Expand All @@ -18,8 +17,6 @@ const queryClient = new QueryClient({
},
});

axios.defaults.baseURL = apiBasePath;

const rootElement = document.getElementById("app");
if (!rootElement) throw new Error("Failed to find the root element");
const root = createRoot(rootElement);
Expand Down

0 comments on commit 03ef78e

Please sign in to comment.