From 1c65df282ed6c2db09bdadbda932aba699180e12 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 11 Jul 2022 20:32:27 +0000 Subject: [PATCH] feat: Gitpod ready code(almost) --- apps/ui/src/app.d.ts | 3 +++ apps/ui/src/lib/api.ts | 8 ++++++-- apps/ui/vite.config.js | 13 +++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/ui/src/app.d.ts b/apps/ui/src/app.d.ts index c743bfaf6..ed5f4e197 100644 --- a/apps/ui/src/app.d.ts +++ b/apps/ui/src/app.d.ts @@ -19,3 +19,6 @@ declare namespace App { privatePort: string; } } + +declare const GITPOD_WORKSPACE_URL: string + \ No newline at end of file diff --git a/apps/ui/src/lib/api.ts b/apps/ui/src/lib/api.ts index 2244d79d3..10a034f67 100644 --- a/apps/ui/src/lib/api.ts +++ b/apps/ui/src/lib/api.ts @@ -1,8 +1,12 @@ import { browser, dev } from '$app/env'; import Cookies from 'js-cookie'; -import { toast } from '@zerodevx/svelte-toast'; export function getAPIUrl() { + if (GITPOD_WORKSPACE_URL) { + const {href} = new URL(GITPOD_WORKSPACE_URL) + const newURL = href.replace('https://','https://3001-').replace(/\/$/,'') + return newURL + } return dev ? 'http://localhost:3001' : 'http://localhost:3000'; } async function send({ @@ -52,7 +56,7 @@ async function send({ } if (dev && !path.startsWith('https://')) { - path = `http://localhost:3001${path}`; + path = `${getAPIUrl()}${path}`; } const response = await fetch(`${path}`, opts); diff --git a/apps/ui/vite.config.js b/apps/ui/vite.config.js index c4e3951d7..f60afcdf9 100644 --- a/apps/ui/vite.config.js +++ b/apps/ui/vite.config.js @@ -3,7 +3,20 @@ import { sveltekit } from '@sveltejs/kit/vite'; /** @type {import('vite').UserConfig} */ export default { plugins: [sveltekit()], + define: { + 'GITPOD_WORKSPACE_URL': JSON.stringify(process.env.GITPOD_WORKSPACE_URL) + }, server: { + hmr: process.env.GITPOD_WORKSPACE_URL + ? { + // Due to port fowarding, we have to replace + // 'https' with the forwarded port, as this + // is the URI created by Gitpod. + host: process.env.GITPOD_WORKSPACE_URL.replace("https://", "3000-"), + protocol: "wss", + clientPort: 443 + } + : true, fs: { allow: ['./src/lib/locales/'] }