-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathurls.ts
More file actions
76 lines (61 loc) · 2.02 KB
/
urls.ts
File metadata and controls
76 lines (61 loc) · 2.02 KB
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
import { loadConfig } from "./config.ts";
type PathFunction<T> = (params: T) => string;
type Path<T = unknown> = string | PathFunction<T>;
type TokenParams = {
token: string;
};
type IdParams = {
id: string;
};
const webPaths: Record<string, Path<TokenParams | never>> = {
cli_session_create: "/cli/session",
cli_session_get: ({ token }: TokenParams): string =>
`/cli/session?token=${token}`,
};
const apiPaths: Record<string, Path<IdParams | never>> = {
index: "/",
me: "/v0/me",
ping: "/v0/ping",
orders_list: "/v0/orders",
orders_get: ({ id }: IdParams): string => `/v0/orders/${id}`,
orders_cancel: ({ id }: IdParams): string => `/v0/orders/${id}`,
quote_get: "/v0/quote",
instances_list: "/v0/instances",
instances_get: ({ id }: IdParams): string => `/v0/instances/${id}`,
credentials_create: "/v0/credentials",
credentials_list: "/v0/credentials",
contracts_list: "/v0/contracts",
contracts_get: ({ id }: IdParams): string => `/v0/contracts/${id}`,
balance_get: "/v0/balance",
tokens_create: "/v0/tokens",
tokens_list: "/v0/tokens",
tokens_delete_by_id: ({ id }: IdParams): string => `/v0/tokens/${id}`,
vms_instances_list: "/v0/vms/instances",
vms_logs_list: "/v0/vms/logs",
vms_replace: "/v0/vms/replace",
vms_script_post: "/v0/vms/script",
vms_script_get: "/v0/vms/script",
vms_ssh_get: "/v0/vms/ssh",
};
export async function getWebAppUrl<T extends TokenParams | never>(
key: keyof typeof webPaths,
params?: T,
): Promise<string> {
const config = await loadConfig();
const path = webPaths[key];
if (typeof path === "function") {
return `${config.webapp_url}${params ? path(params) : ""}`;
}
return `${config.webapp_url}${path}`;
}
export async function getApiUrl<T extends IdParams | never>(
key: keyof typeof apiPaths,
params?: T,
): Promise<string> {
const config = await loadConfig();
const path = apiPaths[key];
if (typeof path === "function") {
return `${config.api_url}${params ? path(params) : ""}`;
}
return `${config.api_url}${path}`;
}