fix: API URL should be dynamic.

This commit is contained in:
Towfiq
2022-11-30 11:40:53 +06:00
parent 92ea548cfa
commit d6871097a2
7 changed files with 16 additions and 16 deletions

View File

@@ -15,7 +15,7 @@ const TopBar = ({ showSettings, showAddModal }:TopbarProps) => {
const logoutUser = async () => {
try {
const fetchOpts = { method: 'POST', headers: new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' }) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/logout`, fetchOpts).then((result) => result.json());
const res = await fetch(`${window.location.origin}/api/logout`, fetchOpts).then((result) => result.json());
console.log(res);
if (!res.success) {
toast(res.error, { icon: '⚠️' });

View File

@@ -29,7 +29,7 @@ const KeywordDetails = ({ keyword, closeDetails }:KeywordDetailsProps) => {
useEffect(() => {
const fetchFullKeyword = async () => {
try {
const fetchURL = `${process.env.NEXT_PUBLIC_APP_URL}/api/keyword?id=${keyword.ID}`;
const fetchURL = `${window.location.origin}/api/keyword?id=${keyword.ID}`;
const res = await fetch(fetchURL, { method: 'GET' }).then((result) => result.json());
if (res.keyword) {
console.log(res.keyword, new Date().getTime());

View File

@@ -15,7 +15,7 @@ const Home: NextPage = () => {
const router = useRouter();
useEffect(() => {
setLoading(true);
fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/domains`)
fetch(`${window.location.origin}/api/domains`)
.then((result) => {
if (result.status === 401) {
router.push('/login');

View File

@@ -33,7 +33,7 @@ const Login: NextPage = () => {
try {
const header = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'POST', headers: header, body: JSON.stringify({ username, password }) };
const fetchRoute = `${process.env.NEXT_PUBLIC_APP_URL}/api/login`;
const fetchRoute = `${window.location.origin}/api/login`;
const res = await fetch(fetchRoute, fetchOpts).then((result) => result.json());
// console.log(res);
if (!res.success) {

View File

@@ -8,7 +8,7 @@ type UpdatePayload = {
}
export async function fetchDomains(router: NextRouter) {
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/domains`, { method: 'GET' });
const res = await fetch(`${window.location.origin}/api/domains`, { method: 'GET' });
if (res.status >= 400 && res.status < 600) {
if (res.status === 401) {
console.log('Unauthorized!!');
@@ -29,7 +29,7 @@ export function useAddDomain(onSuccess:Function) {
return useMutation(async (domainName:string) => {
const headers = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'POST', headers, body: JSON.stringify({ domain: domainName }) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/domains`, fetchOpts);
const res = await fetch(`${window.location.origin}/api/domains`, fetchOpts);
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
@@ -57,7 +57,7 @@ export function useUpdateDomain(onSuccess:Function) {
return useMutation(async ({ domainSettings, domain }: UpdatePayload) => {
const headers = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'PUT', headers, body: JSON.stringify(domainSettings) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/domains?domain=${domain.domain}`, fetchOpts);
const res = await fetch(`${window.location.origin}/api/domains?domain=${domain.domain}`, fetchOpts);
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
@@ -79,7 +79,7 @@ export function useUpdateDomain(onSuccess:Function) {
export function useDeleteDomain(onSuccess:Function) {
const queryClient = useQueryClient();
return useMutation(async (domain:Domain) => {
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/domains?domain=${domain.domain}`, { method: 'DELETE' });
const res = await fetch(`${window.location.origin}/api/domains?domain=${domain.domain}`, { method: 'DELETE' });
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}

View File

@@ -12,7 +12,7 @@ type KeywordsInput = {
export const fetchKeywords = async (router: NextRouter) => {
if (!router.query.slug) { return []; }
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/keywords?domain=${router.query.slug}`, { method: 'GET' });
const res = await fetch(`${window.location.origin}/api/keywords?domain=${router.query.slug}`, { method: 'GET' });
return res.json();
};
@@ -47,7 +47,7 @@ export function useAddKeywords(onSuccess:Function) {
return useMutation(async (newKeywords:KeywordsInput) => {
const headers = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'POST', headers, body: JSON.stringify(newKeywords) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/keywords`, fetchOpts);
const res = await fetch(`${window.location.origin}/api/keywords`, fetchOpts);
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
@@ -70,7 +70,7 @@ export function useDeleteKeywords(onSuccess:Function) {
const queryClient = useQueryClient();
return useMutation(async (keywordIDs:number[]) => {
const keywordIds = keywordIDs.join(',');
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/keywords?id=${keywordIds}`, { method: 'DELETE' });
const res = await fetch(`${window.location.origin}/api/keywords?id=${keywordIds}`, { method: 'DELETE' });
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
@@ -94,7 +94,7 @@ export function useFavKeywords(onSuccess:Function) {
return useMutation(async ({ keywordID, sticky }:{keywordID:number, sticky:boolean}) => {
const headers = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'PUT', headers, body: JSON.stringify({ sticky }) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/keywords?id=${keywordID}`, fetchOpts);
const res = await fetch(`${window.location.origin}/api/keywords?id=${keywordID}`, fetchOpts);
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
@@ -119,7 +119,7 @@ export function useUpdateKeywordTags(onSuccess:Function) {
const keywordIds = Object.keys(tags).join(',');
const headers = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'PUT', headers, body: JSON.stringify({ tags }) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/keywords?id=${keywordIds}`, fetchOpts);
const res = await fetch(`${window.location.origin}/api/keywords?id=${keywordIds}`, fetchOpts);
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}
@@ -143,7 +143,7 @@ export function useRefreshKeywords(onSuccess:Function) {
const keywordIds = ids.join(',');
console.log(keywordIds);
const query = ids.length === 0 && domain ? `?id=all&domain=${domain}` : `?id=${keywordIds}`;
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/refresh${query}`, { method: 'POST' });
const res = await fetch(`${window.location.origin}/api/refresh${query}`, { method: 'POST' });
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}

View File

@@ -2,7 +2,7 @@ import toast from 'react-hot-toast';
import { useMutation, useQuery, useQueryClient } from 'react-query';
export async function fetchSettings() {
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/settings`, { method: 'GET' });
const res = await fetch(`${window.location.origin}/api/settings`, { method: 'GET' });
return res.json();
}
@@ -18,7 +18,7 @@ const useUpdateSettings = (onSuccess:Function|undefined) => {
const headers = new Headers({ 'Content-Type': 'application/json', Accept: 'application/json' });
const fetchOpts = { method: 'PUT', headers, body: JSON.stringify({ settings }) };
const res = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/settings`, fetchOpts);
const res = await fetch(`${window.location.origin}/api/settings`, fetchOpts);
if (res.status >= 400 && res.status < 600) {
throw new Error('Bad response from server');
}