feat: set project endpoint based on the region

This commit is contained in:
Christy Jacob
2024-11-11 20:51:38 +01:00
parent f351a62d59
commit e9adda3cd5
3 changed files with 25 additions and 4 deletions
+18 -2
View File
@@ -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();
@@ -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,
+3 -1
View File
@@ -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()