Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
TS-ify plugins and middleware (#1228)
Olga Bulataf23271439f2
diff --git a/nuxt.config.ts b/nuxt.config.tsindex 2750ce170..0f8137841 100644--- a/nuxt.config.ts+++ b/nuxt.config.ts@@ -137,9 +137,9 @@ const config: NuxtConfig = {{ path: '~/components', extensions: ['vue'], pathPrefix: false },],plugins: [- { src: '~/plugins/url-change.js' },- { src: '~/plugins/migration-notice.js' },- { src: '~/plugins/ua-parse.js' },+ { src: '~/plugins/url-change' },+ { src: '~/plugins/migration-notice' },+ { src: '~/plugins/ua-parse' },{ src: '~/plugins/focus-visible', mode: 'client' },],css: [@@ -206,7 +206,7 @@ const config: NuxtConfig = {* - [Browser language detection info](https://i18n.nuxtjs.org/browser-language-detection)* */detectBrowserLanguage: false,- vueI18n: '~/plugins/vue-i18n.js',+ vueI18n: '~/plugins/vue-i18n',},/*** Map the old route for /photos/_id page to /image/_id permanently to keep links working.diff --git a/src/middleware/middleware.js b/src/middleware/middleware.tssimilarity index 81%rename from src/middleware/middleware.jsrename to src/middleware/middleware.tsindex 7a8beb7fd..4567dc283 100644--- a/src/middleware/middleware.js+++ b/src/middleware/middleware.ts@@ -1,7 +1,8 @@import { sendWindowMessage } from '~/utils/send-message'-import { useNavStore } from '~/stores/nav'+import type { Context, Middleware } from '@nuxt/types'+/*** In embedded mode, the app sends its url* to the outer window to improve the user experience.@@ -11,11 +12,11 @@ import { useNavStore } from '~/stores/nav'* add `?embedded=false` to the end of the URL.** Messages sent to the outer window have the following format:- * {type: <event type>, value: <event value>}.+ * `{type: <event type>, value: <event value>}`.* Currently, one event type is used:* - `urlChange` sends the relative path of the URL on every URL change.*/-export default function ({ query, route, $pinia }) {+const middleware: Middleware = ({ query, route, $pinia }: Context) => {const navStore = useNavStore($pinia)if ('embedded' in query) {@@ -32,3 +33,4 @@ export default function ({ query, route, $pinia }) {navStore.setIsReferredFromCc(false)}}+export default middlewarediff --git a/src/plugins/focus-visible.js b/src/plugins/focus-visible.tssimilarity index 100%rename from src/plugins/focus-visible.jsrename to src/plugins/focus-visible.tsdiff --git a/src/plugins/migration-notice.js b/src/plugins/migration-notice.tssimilarity index 73%rename from src/plugins/migration-notice.jsrename to src/plugins/migration-notice.tsindex d64eb49a9..f95e72b17 100644--- a/src/plugins/migration-notice.js+++ b/src/plugins/migration-notice.ts@@ -1,14 +1,18 @@import { useNavStore } from '~/stores/nav'+import type { Plugin } from '@nuxt/types'+/*** If the URL contains a referral parameter pointing containing* 'creativecommons.org' the migration notice needs to be displayed. This* plugin checks the presence of the param and updates the store.*/-export default function ({ query }) {+const migrationNoticePlugin: Plugin = ({ query }) => {if (query.referrer) {useNavStore().setIsReferredFromCc(query.referrer.includes('creativecommons.org'))}}++export default migrationNoticePlugindiff --git a/src/plugins/ua-parse.js b/src/plugins/ua-parse.tssimilarity index 68%rename from src/plugins/ua-parse.jsrename to src/plugins/ua-parse.tsindex 55518b870..91f1aab42 100644--- a/src/plugins/ua-parse.js+++ b/src/plugins/ua-parse.ts@@ -1,6 +1,8 @@import useragent from 'express-useragent'-export default function (context, inject) {+import type { Plugin } from '@nuxt/types'++const uaParsePlugin: Plugin = (context, inject) => {let userAgentif (typeof context.req !== 'undefined') {@@ -9,7 +11,7 @@ export default function (context, inject) {userAgent = navigator.userAgent}let ua- if (userAgent !== null || userAgent !== undefined) {+ if (typeof userAgent == 'string') {ua = useragent.parse(userAgent)} else {ua = null@@ -18,3 +20,5 @@ export default function (context, inject) {context.$ua = uainject('ua', ua)}++export default uaParsePlugindiff --git a/src/plugins/url-change.js b/src/plugins/url-change.tssimilarity index 74%rename from src/plugins/url-change.jsrename to src/plugins/url-change.tsindex 31366a425..6866e40f6 100644--- a/src/plugins/url-change.js+++ b/src/plugins/url-change.ts@@ -1,17 +1,18 @@import { sendWindowMessage } from '~/utils/send-message'-import { useNavStore } from '~/stores/nav'+import type { Plugin } from '@nuxt/types'+/*** In embedded mode, we need to notify the outer window of the current URL.* Normally, the `src/middleware/embed.js` middleware does this on every* route change. However, it does not run on the initial render.* So, for the initial render this plugin runs when router is ready.*/-export default function ({ app, $pinia }) {- app.router.onReady(() => {+const urlChangePlugin: Plugin = ({ app, $pinia }): void => {+ app.router?.onReady(() => {const isEmbedded = useNavStore($pinia).isEmbedded- if (process.client && isEmbedded) {+ if (process.client && isEmbedded && app.router) {sendWindowMessage({type: 'urlChange',value: {@@ -22,3 +23,5 @@ export default function ({ app, $pinia }) {}})}++export default urlChangePlugindiff --git a/src/plugins/vue-i18n.js b/src/plugins/vue-i18n.tssimilarity index 73%rename from src/plugins/vue-i18n.jsrename to src/plugins/vue-i18n.tsindex e31a8bb09..a2940e2d8 100644--- a/src/plugins/vue-i18n.js+++ b/src/plugins/vue-i18n.ts@@ -4,11 +4,11 @@ export default () => {silentFallbackWarn: true,pluralizationRules: {/**- * @param choice {number} a choice index given by the input to $tc: `$tc('path.to.rule', choiceIndex)`- * @param choicesLength {number} an overall amount of available choices+ * @param choice - a choice index given by the input to $tc: `$tc('path.to.rule', choiceIndex)`+ * @param choicesLength - an overall amount of available choices* @returns a final choice index to select plural word by*/- ru: function (choice, choicesLength) {+ ru: function (choice: number, choicesLength: number) {if (choice === 0) {return 0}diff --git a/tsconfig.json b/tsconfig.jsonindex b9d680b19..76d253e46 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -25,7 +25,7 @@/* Module Resolution Options */"moduleResolution": "Node","resolveJsonModule": true,- "types": ["@types/jest", "@nuxt/types", "@nuxtjs/i18n"],+ "types": ["@types/jest", "@nuxt/types", "@nuxtjs/i18n", "@pinia/nuxt"],"typeRoots": ["./typings", "./node_modules/@types"],"paths": {"~/*": ["./src/*"],diff --git a/typings/nuxt__types/index.d.ts b/typings/nuxt__types/index.d.tsindex 10a53e1e7..a3f1532fc 100644--- a/typings/nuxt__types/index.d.ts+++ b/typings/nuxt__types/index.d.ts@@ -2,6 +2,9 @@ import '@nuxt/types'import type { Details as UADetails } from 'express-useragent'declare module '@nuxt/types' {+ export interface Context {+ $ua: UADetails | null+ }export interface NuxtAppOptions {$ua: UADetails | null}