Skip to content
Open
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
51 changes: 36 additions & 15 deletions src/api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import type { Endpoints } from '@/types/axios.type';
import { buildFullUrls } from '@/api/utils';

import type { Endpoints, SuccessOutput } from '@/types/axios.type';

const endpoints = {
login: {
method: 'POST',
url: '/auth/login',
manageToast: (res) => !!res.message,
},
getProfile: {
method: 'GET',
url: '/user/profile/me',
// Auth endpoints
auth: {
prefix: 'auth',
login: {
method: 'POST',
url: 'login',
manageToast: (res: SuccessOutput) => !!res.message,
},
},
logout: {
method: 'POST',
url: '/user/profile/logout',

// User endpoints
user: {
prefix: 'user',
profile: {
prefix: 'profile',
me: {
method: 'GET',
url: 'me',
manageToast: (res: SuccessOutput) => !!res.message,
},
logout: {
method: 'POST',
url: 'logout',
manageToast: (res: SuccessOutput) => !!res.message,
},
},
},
getAllUsers: {

// Users endpoints
users: {
method: 'GET',
url: '/users',
url: 'users',
manageToast: (res: SuccessOutput) => !!res.message,
},
} satisfies Endpoints;

export default endpoints;
const finalEndpoints = buildFullUrls(endpoints);

export default finalEndpoints;
11 changes: 6 additions & 5 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios from '@/api/axios';
import endpoints from '@/api/endpoints';
import finalEndpoints from '@/api/endpoints';

import type { AxiosOutput, AxiosPaginatedOutput } from '@/types/axios.type';

const nonPaginatedApis = {
login: async (data) => axios({ ...endpoints.login, ...data }),
login: async (data) => axios({ ...(finalEndpoints.auth as any).login, ...data }),

getProfile: async (data) => axios({ ...endpoints.getProfile, ...data }),
getProfile: async (data) => axios({ ...(finalEndpoints.user as any).profile.me, ...data }),

logout: async (data) => axios({ ...endpoints.logout, ...data }),
logout: async (data) => axios({ ...(finalEndpoints.user as any).profile.logout, ...data }),

getAllUsers: async (data) => axios({ ...endpoints.getAllUsers, ...data }),
getAllUsers: async (data) => axios({ ...finalEndpoints.users, ...data }),
} satisfies Record<string, AxiosOutput>;

const paginatedApis = {} satisfies Record<string, AxiosPaginatedOutput>;
Expand Down
35 changes: 34 additions & 1 deletion src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { toast } from 'react-toastify';

import { AppError } from '@/api/appError';

import type { AxiosErrConfig, ShowToast, SuccessOutput, ThrowAxiosError } from '@/types/axios.type';
import type {
AxiosErrConfig,
Endpoints,
ShowToast,
SuccessOutput,
ThrowAxiosError,
} from '@/types/axios.type';

export class AxiosErr extends AxiosError {
config?: AxiosErrConfig;
Expand Down Expand Up @@ -44,3 +50,30 @@ export const showToast: ShowToast = (res) => {
const call = isError ? 'error' : 'success';
toast[call](toastMessage);
};

export function buildFullUrls(obj: Endpoints, parentPrefix = ''): Endpoints {
const result: Endpoints = {};

for (const key in obj) {
if (Object.hasOwn(obj, key)) {
const value = obj[key];

if (typeof value === 'object' && !Array.isArray(value)) {
const currentPrefix =
parentPrefix + ('prefix' in value && value.prefix ? `/${value.prefix}` : '');

result[key] =
'method' in value && 'url' in value
? {
...value,
url: `${currentPrefix}/${value.url}`,
}
: buildFullUrls(value as Endpoints, currentPrefix);
} else {
result[key] = value;
}
}
}

return result;
}
7 changes: 6 additions & 1 deletion src/types/axios.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export type AxiosRequestConfigWithExtraProps = AxiosRequestConfig & AxiosExtraPr

type AxiosRequestInput = Pick<AxiosRequestConfigWithExtraProps, 'method' | 'url'> & ManageToast;

export type Endpoints = Record<string, AxiosRequestInput>;
interface NestedEndpoint {
prefix?: string;
[key: string]: NestedEndpoint | AxiosRequestInput | string | undefined;
}

export type Endpoints = Record<string, AxiosRequestInput | NestedEndpoint>;

export type InternalAxiosRequestConfigWithExtraProps = InternalAxiosRequestConfig & AxiosExtraProps;

Expand Down