Skip to content

Commit

Permalink
fix: API URL should be dynamic.
Browse files Browse the repository at this point in the history
  • Loading branch information
towfiqi committed Nov 30, 2022
1 parent 92ea548 commit d687109
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion components/common/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: '⚠️' });
Expand Down
2 changes: 1 addition & 1 deletion components/keywords/KeywordDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion pages/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions services/domains.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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!!');
Expand All @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down
12 changes: 6 additions & 6 deletions services/keywords.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand Down
4 changes: 2 additions & 2 deletions services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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');
}
Expand Down

0 comments on commit d687109

Please sign in to comment.