Skip to content

Commit

Permalink
Deduplicate user-agent constant
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Dec 3, 2024
1 parent cefd43c commit b0ec9c3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
3 changes: 0 additions & 3 deletions frontend/i18n/scripts/constants.mjs

This file was deleted.

5 changes: 3 additions & 2 deletions frontend/i18n/scripts/metadata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import { join } from "path"
import { copyFileSync, existsSync, readdirSync } from "fs"

import { userAgentHeader } from "../../shared/constants/user-agent.mjs"

import { readToJson, snakeToCamel } from "./utils.mjs"
import { i18nHeaders } from "./constants.mjs"
import { i18nDataDir, localesDir, testLocalesDir } from "./paths.mjs"

const base_url =
Expand Down Expand Up @@ -58,7 +59,7 @@ const PROPERTY_PATTERNS = createPropertyRePatterns()
*/
async function fetchLocalesPhpFile() {
try {
const res = await fetch(base_url, { headers: i18nHeaders })
const res = await fetch(base_url, { headers: userAgentHeader })
return await res.text()
} catch (error) {
console.error("Failed to fetch locales.php from GlotPress", error)
Expand Down
1 change: 0 additions & 1 deletion frontend/i18n/scripts/setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { watch } from "chokidar"

import { prettify, writeJson } from "./utils.mjs"
import { downloadTranslations, writeEnglish } from "./translations.mjs"

import {
enJson5 as enJson5File,
i18nDataDir,
Expand Down
5 changes: 3 additions & 2 deletions frontend/i18n/scripts/translations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { pipeline } from "stream/promises"
import AdmZip from "adm-zip"
import json5 from "json5"

import { i18nHeaders } from "./constants.mjs"
import { userAgentHeader } from "../../shared/constants/user-agent.mjs"

import { kebabToCamel, prettify, readToJson, writeJson } from "./utils.mjs"
import {
enJson as enJsonFile,
Expand All @@ -18,7 +19,7 @@ const NGX_URL =

const fetchBulkNgx = async () => {
const zipPath = join(localesDir, "openverse.zip")
const res = await fetch(NGX_URL, { headers: i18nHeaders })
const res = await fetch(NGX_URL, { headers: userAgentHeader })

if (!res.ok) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* A user agent string by which Openverse.org identifies itself to services
*/
const userAgent =
export const userAgent =
"Openverse/0.1 (https://openverse.org; openverse@wordpress.org)"

module.exports = {
userAgent,
}
export const userAgentHeader = { "User-Agent": userAgent }
13 changes: 5 additions & 8 deletions frontend/src/data/api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRuntimeConfig } from "#imports"
import axios, { AxiosRequestConfig, AxiosResponse } from "axios"

import { AUDIO, type SupportedMediaType } from "#shared/constants/media"
import { userAgentHeader } from "#shared/constants/user-agent.mjs"
import { mediaSlug } from "#shared/utils/query-utils"
import type { Events } from "#shared/types/analytics"
import type { Media } from "#shared/types/media"
Expand Down Expand Up @@ -45,8 +46,6 @@ export interface MediaResult<
results: T
}

const userAgent =
"Openverse/0.1 (https://openverse.org; openverse@wordpress.org)"
/**
* Decodes the text data to avoid encoding problems.
* Also, converts the results from an array of media
Expand Down Expand Up @@ -145,13 +144,11 @@ export const createApiClient = ({
const baseUrl =
useRuntimeConfig().public.apiUrl ?? "https://api.openverse.org/"

const headers: AxiosRequestConfig["headers"] = {}
if (import.meta.server) {
headers["User-Agent"] = userAgent
}
if (accessToken) {
headers["Authorization"] = `Bearer ${accessToken}`
const headers: AxiosRequestConfig["headers"] = {
...(import.meta.server ? userAgentHeader : {}),
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
}

const axiosParams: AxiosRequestConfig = {
baseURL: isVersioned ? `${baseUrl}v1/` : baseUrl,
timeout: DEFAULT_REQUEST_TIMEOUT,
Expand Down

0 comments on commit b0ec9c3

Please sign in to comment.