Skip to content

Commit

Permalink
Call Backend API based of optional NEXT_PUBLIC_GAUZY_API_SERVER_URL v…
Browse files Browse the repository at this point in the history
…ariable
  • Loading branch information
badalkhatri0924 committed Nov 21, 2023
1 parent ddbc586 commit f4fb142
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/web/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false

NEXT_PUBLIC_GAUZY_API_SERVER_URL=https://api.gauzy.co
# NEXT_PUBLIC_GAUZY_API_SERVER_URL=https://api.gauzy.co
GAUZY_API_SERVER_URL=https://api.gauzy.co/api
NEXT_PUBLIC_GA_MEASUREMENT_ID=

Expand Down
24 changes: 21 additions & 3 deletions apps/web/app/services/client/api/task-sizes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CreateResponse, DeleteResponse, ITaskSizesCreate } from '@app/interfaces';
import api from '../axios';
import api, { apiDirect } from '../axios';
import { getAccessTokenCookie } from '@app/helpers';

export function createTaskSizesAPI(data: ITaskSizesCreate, tenantId?: string) {
return api.post<CreateResponse<ITaskSizesCreate>>('/task-sizes', data, {
Expand All @@ -21,6 +22,23 @@ export function deleteTaskSizesAPI(id: string) {
return api.delete<DeleteResponse>(`/task-sizes/${id}`);
}

export function getTaskSizesList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(`/task-sizes?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`);
export async function getTaskSizesList(tenantId: string, organizationId: string, activeTeamId: string | null) {
const endpoint = `/task-sizes?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${activeTeamId}`;

// API call direct to Backend Server
if (process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL) {
const token = getAccessTokenCookie();
const data = await apiDirect.get(endpoint, {
headers: {
authorization: `Bearer ${token}`
}
});
return {
data: data,
response: {}
};
}

// API call via Proxy Nextjs /api routes
return api.get(endpoint);
}
37 changes: 34 additions & 3 deletions apps/web/app/services/client/axios.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { API_BASE_URL, DEFAULT_APP_PATH } from '@app/constants';
import { getActiveTeamIdCookie } from '@app/helpers/cookies';
import { getAccessTokenCookie, getActiveTeamIdCookie } from '@app/helpers/cookies';
import axios, { AxiosResponse } from 'axios';

const api = axios.create({
baseURL: API_BASE_URL,
withCredentials: true,
timeout: 60 * 1000
});

api.interceptors.request.use(
async (config: any) => {
const cookie = getActiveTeamIdCookie();
Expand All @@ -22,7 +21,6 @@ api.interceptors.request.use(
Promise.reject(error);
}
);

api.interceptors.response.use(
(response: AxiosResponse) => response,
async (error: { response: AxiosResponse }) => {
Expand All @@ -36,4 +34,37 @@ api.interceptors.response.use(
}
);

const apiDirect = axios.create({
baseURL: `${process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL}/api`,
timeout: 60 * 1000
});
apiDirect.interceptors.request.use(
async (config: any) => {
const cookie = getAccessTokenCookie();

if (cookie) {
config.headers['Authorization'] = `Bearer ${cookie}`;
}

return config;
},
(error: any) => {
Promise.reject(error);
}
);
apiDirect.interceptors.response.use(
(response: AxiosResponse) => response,
async (error: { response: AxiosResponse }) => {
const statusCode = error.response?.status;

if (statusCode === 401) {
window.location.assign(DEFAULT_APP_PATH);
}

return Promise.reject(error);
}
);

export default api;

export { apiDirect };
6 changes: 3 additions & 3 deletions apps/web/app/services/server/requests/task-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export function getTaskSizesListRequest(
{
organizationId,
tenantId,
activeTeamId
}: { tenantId: string; organizationId: string; activeTeamId: string | null },
organizationTeamId
}: { tenantId: string; organizationId: string; organizationTeamId: string | null },
bearer_token: string
) {
return serverFetch({
path: `/task-sizes?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${activeTeamId}`,
path: `/task-sizes?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${organizationTeamId}`,
method: 'GET',
bearer_token
});
Expand Down
4 changes: 2 additions & 2 deletions apps/web/pages/api/task-sizes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

if (!user) return $res();

const { activeTeamId } = req.query;
const { organizationTeamId } = req.query;

const par = {
tenantId,
organizationId,
activeTeamId: (activeTeamId as string) || null
organizationTeamId: (organizationTeamId as string) || null
};

switch (req.method) {
Expand Down

0 comments on commit f4fb142

Please sign in to comment.