From e9adda3cd543a4a9e551df49dd8aafa0dfdb8290 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 11 Nov 2024 20:51:38 +0100 Subject: [PATCH] feat: set project endpoint based on the region --- src/lib/stores/sdk.ts | 20 +++++++++++++++++-- .../(console)/project-[project]/+layout.ts | 5 ++++- src/routes/+layout.ts | 4 +++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index c4b3189dd..b02674bdb 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -25,11 +25,27 @@ import { Billing } from '../sdk/billing'; import { Backups } from '../sdk/backups'; import { Sources } from '$lib/sdk/sources'; -export function getApiEndpoint(): string { +export function getApiEndpoint(region?: string): string { if (VARS.APPWRITE_ENDPOINT) return VARS.APPWRITE_ENDPOINT; - return globalThis?.location?.origin + '/v1'; + let protocol = globalThis?.location?.protocol; + let hostname = globalThis?.location?.hostname; + let subdomain = getSubdomain(region); + return `${protocol}://${subdomain}${hostname}/v1`; } +const getSubdomain = (region?: string) => { + switch (region) { + case 'fra': + return 'fra.'; + case 'syd': + return 'syd.'; + case 'nyc': + return 'nyc.'; + default: + return ''; + } +}; + const endpoint = getApiEndpoint(); const clientConsole = new Client(); diff --git a/src/routes/(console)/project-[project]/+layout.ts b/src/routes/(console)/project-[project]/+layout.ts index 57df9d528..11bbdbddc 100644 --- a/src/routes/(console)/project-[project]/+layout.ts +++ b/src/routes/(console)/project-[project]/+layout.ts @@ -1,5 +1,5 @@ import { Dependencies } from '$lib/constants'; -import { sdk } from '$lib/stores/sdk'; +import { getApiEndpoint, sdk } from '$lib/stores/sdk'; import { error } from '@sveltejs/kit'; import type { LayoutLoad } from './$types'; import { preferences } from '$lib/stores/preferences'; @@ -35,6 +35,9 @@ export const load: LayoutLoad = async ({ params, depends }) => { } } + console.log(getApiEndpoint(project.region)); + sdk.forProject.client.setEndpoint(getApiEndpoint(project.region)); + return { project, organization, diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index 77fd3e8b1..13d266834 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -1,7 +1,7 @@ import '@appwrite.io/pink'; import '@appwrite.io/pink-icons'; import 'tippy.js/dist/tippy.css'; -import { sdk } from '$lib/stores/sdk'; +import { getApiEndpoint, sdk } from '$lib/stores/sdk'; import { redirect } from '@sveltejs/kit'; import { Dependencies } from '$lib/constants'; import type { LayoutLoad } from './$types'; @@ -14,6 +14,8 @@ export const ssr = false; export const load: LayoutLoad = async ({ depends, url, route }) => { depends(Dependencies.ACCOUNT); + console.log(getApiEndpoint()); + sdk.forProject.client.setEndpoint(getApiEndpoint()); const [account, error] = (await sdk.forConsole.account .get()