From 6df35db1bdbe083b0a1b4bd643738a4cd1dab725 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Wed, 17 Jan 2024 21:49:22 -0500 Subject: [PATCH 1/5] Create JS configs for each environment and replace process.env with config imports --- src/addons/progress/AddonStatus.svelte | 3 +- src/api/auth.js | 11 +- src/api/base.js | 4 +- src/api/document.js | 20 ++-- src/api/languages.js | 12 ++- src/api/session.js | 4 +- src/api/viewer.js | 17 +-- src/common/FilePicker.svelte | 13 +-- src/common/Progress.svelte | 10 +- src/common/ProgressiveImage.svelte | 10 +- src/common/SpecialMessage.svelte | 11 +- src/common/Toast.svelte | 11 +- src/common/dialog/DataDialog.svelte | 16 +-- src/common/dialog/DiagnosticDialog.svelte | 13 +-- src/common/dialog/EditSectionsDialog.svelte | 24 +++-- src/common/dialog/NoteEmbedDialog.svelte | 10 +- src/common/dialog/PageEmbedDialog.svelte | 7 +- src/common/dialog/ProjectDialog.svelte | 13 ++- src/common/dialog/metaDialogs.js | 22 ++-- src/common/fileHandler.js | 7 +- src/config/config.js | 108 ++++++++++++++++++++ src/config/production.js | 6 ++ src/config/staging.js | 6 ++ src/embed/documentLoader.js | 4 +- src/embed/enhance.js | 3 +- src/embed/noteLoader.js | 3 +- src/embed/projectLoader.js | 8 +- src/manager/documents.js | 7 +- src/pages/FlatPage.svelte | 4 +- src/pages/app/Document.svelte | 7 +- src/pages/app/upload/UploadDialog.svelte | 25 ++--- src/pages/embed/note/Note.svelte | 6 +- src/pages/embed/page/Annotation.svelte | 2 +- src/pages/embed/page/Page.svelte | 13 ++- src/pages/entities/Entities.svelte | 4 +- src/pages/legacy/Redirect.svelte | 11 +- src/pages/viewer/Annotation.svelte | 14 +-- src/search/search.js | 3 +- src/search/searchFields.js | 5 +- src/structure/document.js | 12 ++- src/structure/documentFactory.js | 3 +- src/structure/project.js | 2 +- src/ticker/ticker.js | 3 +- src/util/paginate.js | 2 +- 44 files changed, 314 insertions(+), 185 deletions(-) create mode 100644 src/config/config.js create mode 100644 src/config/production.js create mode 100644 src/config/staging.js diff --git a/src/addons/progress/AddonStatus.svelte b/src/addons/progress/AddonStatus.svelte index 0c1160316..9ad7b647f 100644 --- a/src/addons/progress/AddonStatus.svelte +++ b/src/addons/progress/AddonStatus.svelte @@ -4,8 +4,7 @@ import AddonRun, { runs } from "./AddonRun.svelte"; import { baseApiUrl } from "../../api/base.js"; - - const POLL_INTERVAL = parseInt(process.env.POLL_INTERVAL, 10); + import { POLL_INTERVAL } from "../../config/config.js"; const endpoint = new URL( "/api/addon_runs/?expand=addon&dismissed=false&per_page=20", diff --git a/src/api/auth.js b/src/api/auth.js index c76bb191f..459d5d4cb 100644 --- a/src/api/auth.js +++ b/src/api/auth.js @@ -1,14 +1,15 @@ import { Svue } from "svue"; +import * as config from "../config/config.js"; const DOCUMENTCLOUD_TOKEN_STORAGE_KEY = "documentcloud_token"; -export const SQUARELET_URL = process.env.SQUARELET_BASE; -export const SIGN_IN_URL = process.env.DC_BASE + process.env.DC_LOGIN; +export const SQUARELET_URL = config.SQUARELET_BASE; +export const SIGN_IN_URL = config.DC_BASE + config.DC_LOGIN; export const SIGN_UP_URL = - process.env.SQUARELET_BASE + - process.env.SQUARELET_SIGNUP + + config.SQUARELET_BASE + + config.SQUARELET_SIGNUP + encodeURIComponent(window.location.href); -export const SIGN_OUT_URL = process.env.DC_BASE + process.env.DC_LOGOUT; +export const SIGN_OUT_URL = config.DC_BASE + config.DC_LOGOUT; export const auth = new Svue({ data() { diff --git a/src/api/base.js b/src/api/base.js index 794350c3c..e4c01dd96 100644 --- a/src/api/base.js +++ b/src/api/base.js @@ -1,4 +1,6 @@ -export const baseApiUrl = process.env.DC_BASE + process.env.API; +import { DC_BASE, API } from "../config/config.js"; + +export const baseApiUrl = DC_BASE + API; export function apiUrl(url) { return `${baseApiUrl}${url}`; diff --git a/src/api/document.js b/src/api/document.js index 66bc9907c..493c4fec0 100644 --- a/src/api/document.js +++ b/src/api/document.js @@ -15,15 +15,15 @@ import axios from "axios"; import { Document, transformHighlights } from "@/structure/document.js"; -const POLL_TIMEOUT = process.env.POLL_TIMEOUT; - -const GET_BATCH = parseInt(process.env.GET_BATCH); -const GET_BATCH_DELAY = parseInt(process.env.GET_BATCH_DELAY); -const UPLOAD_BATCH = parseInt(process.env.UPLOAD_BATCH); -const UPLOAD_BATCH_DELAY = parseInt(process.env.UPLOAD_BATCH_DELAY); - -const HIGHLIGHT_START = process.env.HIGHLIGHT_START; -const HIGHLIGHT_END = process.env.HIGHLIGHT_END; +import { + POLL_TIMEOUT, + GET_BATCH, + GET_BATCH_DELAY, + UPLOAD_BATCH, + UPLOAD_BATCH_DELAY, + HIGHLIGHT_START, + HIGHLIGHT_END, +} from "../config/config.js"; // Statuses export const PENDING = "pending"; @@ -169,7 +169,7 @@ export async function changeRevisionControl(id, revision_control) { expand: [DEFAULT_EXPAND, "revisions"].join(","), }), ), - {revision_control} + { revision_control }, ); return data; } diff --git a/src/api/languages.js b/src/api/languages.js index d9ea8b438..9fc6e4747 100644 --- a/src/api/languages.js +++ b/src/api/languages.js @@ -1,6 +1,10 @@ -const languageCodes = process.env.LANGUAGE_CODES.split("|"); -const languageNames = process.env.LANGUAGE_NAMES.split("|"); -export const defaultLanguage = process.env.DEFAULT_LANGUAGE; +import { + LANGUAGE_CODES, + LANGUAGE_NAMES, + DEFAULT_LANGUAGE, +} from "../config/config.js"; + +export const defaultLanguage = DEFAULT_LANGUAGE; function makeLanguagePairs(codes, names) { const results = []; @@ -11,7 +15,7 @@ function makeLanguagePairs(codes, names) { return results; } -export const languages = makeLanguagePairs(languageCodes, languageNames); +export const languages = makeLanguagePairs(LANGUAGE_CODES, LANGUAGE_NAMES); const textractLanguageCodes = ["eng", "spa", "ita", "por", "fra", "deu"]; export const textractLanguages = languages.filter( diff --git a/src/api/session.js b/src/api/session.js index 2b4b53e47..fc6267ebd 100644 --- a/src/api/session.js +++ b/src/api/session.js @@ -1,6 +1,8 @@ import axios from "axios"; import axiosRetry from "axios-retry"; +import { DC_BASE } from "../config/config.js"; + // Hook in failed request interceptor axiosRetry(axios, { retries: 3, @@ -97,7 +99,7 @@ session.getStatic = async function getStatic(url) { // On the second request, we do not sent the session cookie // If we are fetching a public asset, the first request is directly to S3. In // that case we must not send the session cookie on the first request. - if (url.startsWith(process.env.DC_BASE)) { + if (url.startsWith(DC_BASE)) { result = await session.get(url).then((r) => r.data); let redirect = result.location; diff --git a/src/api/viewer.js b/src/api/viewer.js index f16c43ed0..d83c2c1be 100644 --- a/src/api/viewer.js +++ b/src/api/viewer.js @@ -1,13 +1,4 @@ -function processImageWidths(widthSpec) { - let result = widthSpec.split(",").map((width) => { - const parts = width.split(":"); - return [parseFloat(parts[1]), parts[0]]; - }); - result = result.sort((a, b) => a[0] - b[0]); - return result; -} - -export const imageWidths = processImageWidths(process.env.IMAGE_WIDTHS); +import { IMAGE_WIDTHS } from "../config/config.js"; export function documentDimensionUrl(document) { return `${document.assetUrl}documents/${document.id}/${document.slug}.pagesize`; @@ -19,11 +10,11 @@ export function documentDimensionUrl(document) { * @param {number} desiredWidth The desired width of the page image. */ function getDesiredSize(desiredWidth) { - for (let i = 0; i < imageWidths.length; i++) { - const [width, name] = imageWidths[i]; + for (let i = 0; i < IMAGE_WIDTHS.length; i++) { + const [width, name] = IMAGE_WIDTHS[i]; if (desiredWidth <= width) return name; } - return imageWidths[imageWidths.length - 1][1]; + return IMAGE_WIDTHS[IMAGE_WIDTHS.length - 1][1]; } export function pageImageUrl(document, pageNumber, desiredWidth) { diff --git a/src/common/FilePicker.svelte b/src/common/FilePicker.svelte index 11231b2ae..62c8b701e 100644 --- a/src/common/FilePicker.svelte +++ b/src/common/FilePicker.svelte @@ -1,4 +1,5 @@ - + -