(ts) convert used nuxt plugins

This commit is contained in:
Valentyne Stigloher 2024-03-17 23:02:44 +01:00
parent d10dd7b1e1
commit bf7acd987f
8 changed files with 51 additions and 32 deletions

View File

@ -167,12 +167,12 @@ const nuxtConfig: NuxtConfig = {
], ],
plugins: [ plugins: [
{ src: '~/plugins/polyfill.ts', mode: 'client' }, { src: '~/plugins/polyfill.ts', mode: 'client' },
{ src: '~/plugins/axios.js' }, { src: '~/plugins/axios.ts' },
{ src: '~/plugins/globals.ts' }, { src: '~/plugins/globals.ts' },
{ src: '~/plugins/auth.ts' }, { src: '~/plugins/auth.ts' },
{ src: '~/plugins/datepicker.js', mode: 'client' }, { src: '~/plugins/datepicker.ts', mode: 'client' },
{ src: '~/plugins/track.js', mode: 'client' }, { src: '~/plugins/track.ts', mode: 'client' },
{ src: '~/plugins/browserDetect.js' }, { src: '~/plugins/browserDetect.ts' },
], ],
components: true, components: true,
buildModules: [ buildModules: [

View File

@ -1,4 +1,6 @@
export default function ({ $axios, app }) { import type { Plugin } from '@nuxt/types';
const plugin: Plugin = ({ $axios, app }) => {
$axios.onRequest((config) => { $axios.onRequest((config) => {
const token = app.$csrfToken(); const token = app.$csrfToken();
@ -8,4 +10,6 @@ export default function ({ $axios, app }) {
return config; return config;
}); });
} };
export default plugin;

View File

@ -1,9 +1,11 @@
import type { Plugin } from '@nuxt/types';
const SAFARI_REGEX = /^((?!chrome|android).)*safari/i; const SAFARI_REGEX = /^((?!chrome|android).)*safari/i;
export default ({ req }, inject) => { const plugin: Plugin = ({ req }, inject) => {
inject('isSafari', () => { inject('isSafari', () => {
if (process.server && req) { if (process.server && req) {
return SAFARI_REGEX.test(req.headers['user-agent']); return SAFARI_REGEX.test(req.headers['user-agent']!);
} }
if (process.client) { if (process.client) {
@ -13,3 +15,5 @@ export default ({ req }, inject) => {
return false; return false;
}); });
}; };
export default plugin;

View File

@ -1,6 +1,9 @@
import Vue from 'vue'; import Vue from 'vue';
import VuejsDatePicker from 'vuejs-datepicker'; import VuejsDatePicker from 'vuejs-datepicker';
import type { Plugin } from '@nuxt/types';
export default () => { const plugin: Plugin = () => {
Vue.component('Datepicker', VuejsDatePicker); Vue.component('Datepicker', VuejsDatePicker);
}; };
export default plugin;

View File

@ -1,32 +1,44 @@
import * as Sentry from '@sentry/vue'; import * as Sentry from '@sentry/vue';
import type { Plugin } from '@nuxt/types';
import type Plausible from 'plausible-tracker';
function defaultHandler({ plausible, to }) { interface PlausibleContext {
plausible: ReturnType<typeof Plausible>;
to: URL;
from: URL;
}
const defaultHandler = ({ plausible, to }: PlausibleContext): void => {
console.debug('[analytics] Tracking default handler: %O', to); console.debug('[analytics] Tracking default handler: %O', to);
plausible.trackPageview({ plausible.trackPageview({
url: to.toString(), url: to.toString(),
}); });
} };
/**
* @param {(value: URL) => URL} redactor const redact = (
* @param {(ctx) => void} base redactor: (v: URL) => URL,
* @return {(ctx) => void} base: (ctx: PlausibleContext) => void = defaultHandler,
*/ ): (ctx: PlausibleContext) => void => {
function redact(redactor, base = defaultHandler) {
return (ctx) => base({ return (ctx) => base({
...ctx, ...ctx,
to: redactor(ctx.to), to: redactor(ctx.to),
}); });
};
interface TrackerOverride {
test(path: string): boolean;
handling(ctx: PlausibleContext): void;
} }
const USER_AT = /^\/@.+/; const USER_AT = /^\/@.+/;
const USER_SUBPAGE = /^\/(u|card)\/.*/; const USER_SUBPAGE = /^\/(u|card)\/.*/;
const TRACKER_OVERRIDES = [ const TRACKER_OVERRIDES: TrackerOverride[] = [
{ {
test(v) { test(v: string): boolean {
return USER_AT.test(v) || USER_SUBPAGE.test(v); return USER_AT.test(v) || USER_SUBPAGE.test(v);
}, },
handling: redact((v) => { handling: redact((v: URL): URL => {
let pathname = v.pathname; let pathname = v.pathname;
if (USER_AT.test(pathname)) { if (USER_AT.test(pathname)) {
pathname = pathname.replace(USER_AT, '/@--redacted--'); pathname = pathname.replace(USER_AT, '/@--redacted--');
@ -40,24 +52,16 @@ const TRACKER_OVERRIDES = [
}, },
]; ];
export const plugin = function ({ app }) { const plugin: Plugin = ({ app }) => {
const plausible = app.$plausible; const plausible = app.$plausible;
app.router.afterEach((to, from) => { app.router?.afterEach((to, from) => {
let handler = defaultHandler; let handler = defaultHandler;
for (const trackerOverride of TRACKER_OVERRIDES) { for (const trackerOverride of TRACKER_OVERRIDES) {
if (!trackerOverride.test(to.fullPath)) { if (!trackerOverride.test(to.fullPath)) {
continue; continue;
} }
if (trackerOverride.handling === false) {
// console.debug("[analytics] Page is blocked from tracking");
return;
} else if (typeof trackerOverride.handling === 'function') {
handler = trackerOverride.handling; handler = trackerOverride.handling;
} else {
throw new Error('Tracking override handling is invalid');
}
break; break;
} }

3
plugins/vuejs-datepicker.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
declare module 'vuejs-datepicker' {
export = unknown;
}

View File

@ -21,7 +21,8 @@
"@nuxt/typescript-build", "@nuxt/typescript-build",
"@nuxtjs/axios", "@nuxtjs/axios",
"@nuxtjs/sentry", "@nuxtjs/sentry",
"@types/node" "@types/node",
"vue-plausible"
] ]
}, },
"exclude": [ "exclude": [