-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathsdk.ts
114 lines (105 loc) · 3.12 KB
/
sdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { getProjectId } from '$lib/helpers/project';
import { VARS } from '$lib/system';
import {
Account,
Assistant,
Avatars,
Client,
Console,
Databases,
Functions,
Health,
Locale,
Messaging,
Migrations,
Project,
Project as ProjectApi,
Projects,
Proxy,
Storage,
Teams,
Users,
Vcs
} from '@appwrite.io/console';
import { Billing } from '../sdk/billing';
import { Backups } from '../sdk/backups';
import { Sources } from '$lib/sdk/sources';
import {
REGION_FRA,
REGION_NYC,
REGION_SYD,
SUBDOMAIN_FRA,
SUBDOMAIN_NYC,
SUBDOMAIN_SYD
} from '$lib/constants';
export function getApiEndpoint(region?: string): string {
if (VARS.APPWRITE_ENDPOINT) return VARS.APPWRITE_ENDPOINT;
const protocol = globalThis?.location?.protocol;
const hostname = globalThis?.location?.hostname;
const subdomain = getSubdomain(region);
return `${protocol}//${subdomain}${hostname}/v1`;
}
const getSubdomain = (region?: string) => {
switch (region) {
case REGION_FRA:
return SUBDOMAIN_FRA;
case REGION_SYD:
return SUBDOMAIN_SYD;
case REGION_NYC:
return SUBDOMAIN_NYC;
default:
return '';
}
};
const endpoint = getApiEndpoint();
const clientConsole = new Client();
clientConsole.setEndpoint(endpoint).setProject('console');
const clientProject = new Client();
clientProject.setEndpoint(endpoint).setMode('admin');
const sdkForProject = {
client: clientProject,
account: new Account(clientProject),
avatars: new Avatars(clientProject),
backups: new Backups(clientProject),
databases: new Databases(clientProject),
functions: new Functions(clientProject),
health: new Health(clientProject),
locale: new Locale(clientProject),
messaging: new Messaging(clientProject),
project: new Project(clientProject),
projectApi: new ProjectApi(clientProject),
storage: new Storage(clientProject),
teams: new Teams(clientProject),
users: new Users(clientProject),
vcs: new Vcs(clientProject),
proxy: new Proxy(clientProject),
migrations: new Migrations(clientProject)
};
export const getSdkForProject = (projectId: string) => {
if (projectId && projectId !== clientProject.config.project) {
clientProject.setProject(projectId);
}
return sdkForProject;
};
export const sdk = {
forConsole: {
client: clientConsole,
account: new Account(clientConsole),
avatars: new Avatars(clientConsole),
functions: new Functions(clientConsole),
health: new Health(clientConsole),
locale: new Locale(clientConsole),
projects: new Projects(clientConsole),
teams: new Teams(clientConsole),
users: new Users(clientConsole),
migrations: new Migrations(clientConsole),
console: new Console(clientConsole),
assistant: new Assistant(clientConsole),
billing: new Billing(clientConsole),
sources: new Sources(clientConsole)
},
get forProject() {
const projectId = getProjectId();
return getSdkForProject(projectId);
}
};