From c1ea31f8742188000443775abaabf6948f348640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 3 Nov 2022 15:04:57 +0000 Subject: [PATCH] Advent22 typescript plugin --- ui/src/components/CalendarDoor.vue | 2 +- ui/src/d.ts/shims-advent22.d.ts | 10 ++++++++++ ui/src/{ => d.ts}/shims-vue.d.ts | 0 ui/src/main.ts | 6 +++++- ui/src/plugins/advent22.ts | 30 ++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 ui/src/d.ts/shims-advent22.d.ts rename ui/src/{ => d.ts}/shims-vue.d.ts (100%) create mode 100644 ui/src/plugins/advent22.ts diff --git a/ui/src/components/CalendarDoor.vue b/ui/src/components/CalendarDoor.vue index 63a15c8..c8c157f 100644 --- a/ui/src/components/CalendarDoor.vue +++ b/ui/src/components/CalendarDoor.vue @@ -22,7 +22,7 @@ export default class CalendarDoor extends Vue { modal_visible = false; private get image_url(): string { - return "http://localhost:8000/api/days/picture/" + this.day; + return this.$advent22.api_url("days/picture/" + this.day); } } diff --git a/ui/src/d.ts/shims-advent22.d.ts b/ui/src/d.ts/shims-advent22.d.ts new file mode 100644 index 0000000..88800a1 --- /dev/null +++ b/ui/src/d.ts/shims-advent22.d.ts @@ -0,0 +1,10 @@ +import { Advent22 } from "@/plugins/advent22"; + +declare module "@vue/runtime-core" { + // bind to `this` keyword + interface ComponentCustomProperties { + $advent22: Advent22; + } +} + +export { }; diff --git a/ui/src/shims-vue.d.ts b/ui/src/d.ts/shims-vue.d.ts similarity index 100% rename from ui/src/shims-vue.d.ts rename to ui/src/d.ts/shims-vue.d.ts diff --git a/ui/src/main.ts b/ui/src/main.ts index 4d6f39e..7dd1696 100644 --- a/ui/src/main.ts +++ b/ui/src/main.ts @@ -1,6 +1,10 @@ +import { Advent22Plugin } from "@/plugins/advent22" import { createApp } from 'vue' import App from './App.vue' import "bulma/css/bulma.css" -createApp(App).mount('#app') +const app = createApp(App) +app.use(Advent22Plugin) + +app.mount('#app') diff --git a/ui/src/plugins/advent22.ts b/ui/src/plugins/advent22.ts new file mode 100644 index 0000000..e6e88cf --- /dev/null +++ b/ui/src/plugins/advent22.ts @@ -0,0 +1,30 @@ +import { App, Plugin } from 'vue'; + +export class Advent22 { + private get api_baseurl(): string { + // in production mode, return "//host/api" + if (process.env.NODE_ENV === "production") { + return `//${window.location.host}/api`; + + } else if (process.env.NODE_ENV !== "development") { + // not in prouction or development mode + console.warn("Unexpected NODE_ENV value"); + } + + // in development mode, return "//hostname:8000/api" + return `//${window.location.hostname}:8000/api`; + } + + public api_url(endpoint?: string): string { + if (endpoint === undefined) + return `${this.api_baseurl}`; + + return `${this.api_baseurl}/${endpoint}`; + } +} + +export const Advent22Plugin: Plugin = { + install(app: App) { + app.config.globalProperties.$advent22 = new Advent22(); + } +}