Skip to content

Commit

Permalink
feat(extension): automatically change API url depending on webpack mo…
Browse files Browse the repository at this point in the history
…de (dev or prod)
  • Loading branch information
kethan1 committed Jan 20, 2024
1 parent c09a6cb commit ca9d498
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 5 additions & 7 deletions extension/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import moment from "moment";
import { Dismiss } from "flowbite";


const SERVER_URL = "http://localhost:5000";

let GET_SEARCH_PARAMS;
let url;

Expand Down Expand Up @@ -68,8 +66,8 @@ chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
url: url,
});
Promise.all([
fetch(`${SERVER_URL}/api/v1/get-comments` + GET_SEARCH_PARAMS),
fetch(`${SERVER_URL}/api/v1/get-ratings` + GET_SEARCH_PARAMS),
fetch(`${API_URL}/api/v1/get-comments` + GET_SEARCH_PARAMS),
fetch(`${API_URL}/api/v1/get-ratings` + GET_SEARCH_PARAMS),
]).then((responses) => {
const [commentsResponse, ratingsResponse] = responses;
Promise.all([commentsResponse.json(), ratingsResponse.json()]).then(
Expand Down Expand Up @@ -129,7 +127,7 @@ function refreshComments() {
document.getElementById("comments").classList.add("hidden");
document.getElementById("commentsNone").classList.add("hidden");

fetch(`${SERVER_URL}/api/v1/get-comments` + GET_SEARCH_PARAMS).then(commentsResponse => {
fetch(`${API_URL}/api/v1/get-comments` + GET_SEARCH_PARAMS).then(commentsResponse => {
commentsResponse.json().then(comments => {
for (let comment of comments) {
createComment(comment.comment, comment.username, comment.timestamp);
Expand All @@ -142,7 +140,7 @@ function refreshComments() {
}

function postComment(username, comment) {
fetch(`${SERVER_URL}/api/v1/post-comment`, {
fetch(`${API_URL}/api/v1/post-comment`, {
method: "POST",
headers: {
Accept: "application/json, text/plain, */*",
Expand Down Expand Up @@ -282,7 +280,7 @@ function submitRating() {
if (rating === -1) {
toast("warning", "Please select a rating.");
} else {
fetch(`${SERVER_URL}/api/v1/add-rating`, {
fetch(`${API_URL}/api/v1/add-rating`, {
method: "POST",
headers: {
Accept: "application/json, text/plain, */*",
Expand Down
12 changes: 12 additions & 0 deletions extension/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const path = require("path");
const webpack = require("webpack");
const CopyPlugin = require("copy-webpack-plugin");


const API_URL = {
production: JSON.stringify("https://bias-buster-backend.vercel.app/"),
development: JSON.stringify("http://localhost:5000")
};
const ENVIRONMENT = process.env.NODE_ENV === "production" ? "production" : "development";


module.exports = {
entry: "./main.js",
devtool: "cheap-module-source-map",
Expand All @@ -25,5 +34,8 @@ module.exports = {
{ from: "icon.png" },
],
}),
new webpack.DefinePlugin({
"API_URL": API_URL[ENVIRONMENT]
}),
],
};

0 comments on commit ca9d498

Please sign in to comment.