-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
121 lines (115 loc) · 6.63 KB
/
Copy pathroutes.js
File metadata and controls
121 lines (115 loc) · 6.63 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
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
115
116
117
118
119
120
121
export const PUBLIC_BASE = '/api/v1';
export const ENTERPRISE_BASE = '/api/enterprise/v1.0';
export function apiPrefix(flavor = 'public') {
return flavor === 'enterprise' ? ENTERPRISE_BASE : PUBLIC_BASE;
}
export const routes = {
public: {
health: ['GET', '/health', false],
'user show': ['GET', '/user'],
'user tokens': ['GET', '/user/tokens'],
'user revoke-token': ['DELETE', tokenId => `/user/tokens/${tokenId}`, true],
'integrations cloudflare': ['GET', '/integrations/cloudflare'],
'blueprints list': ['GET', '/blueprints'],
'servers list': ['GET', '/servers'],
'servers show': ['GET', serverId => `/servers/${serverId}`],
'servers sites': ['GET', serverId => `/servers/${serverId}/sites`],
'servers databases': ['GET', serverId => `/servers/${serverId}/databases`],
'servers cron-jobs': ['GET', serverId => `/servers/${serverId}/cron-jobs`],
'servers monitoring': ['GET', serverId => `/servers/${serverId}/monitoring`],
'servers tasks': ['GET', serverId => `/servers/${serverId}/tasks`],
'servers php-versions': ['GET', serverId => `/servers/${serverId}/php-versions`],
'servers sudo-users': ['GET', serverId => `/servers/${serverId}/sudo-users`],
'servers create-sudo-user': ['POST', serverId => `/servers/${serverId}/sudo-users`, true],
'servers delete-sudo-user': ['DELETE', (serverId, sudoUserId) => `/servers/${serverId}/sudo-users/${sudoUserId}`, true],
'servers create-wordpress-site': ['POST', serverId => `/servers/${serverId}/sites/wordpress`, true],
'servers power-cycle': ['POST', serverId => `/servers/${serverId}/reboot`, true],
'sites list': ['GET', '/sites'],
'sites show': ['GET', siteId => `/sites/${siteId}`],
'sites backups': ['GET', siteId => `/sites/${siteId}/backups`],
'sites status': ['GET', siteId => `/sites/${siteId}/status`],
'sites ssl': ['GET', siteId => `/sites/${siteId}/ssl`],
'sites domain': ['GET', siteId => `/sites/${siteId}/domain`],
'sites monitoring': ['GET', siteId => `/sites/${siteId}/monitoring`],
'sites events': ['GET', siteId => `/sites/${siteId}/events`],
'sites deployment-logs': ['GET', siteId => `/sites/${siteId}/deployment-logs`],
'sites git': ['GET', siteId => `/sites/${siteId}/git`],
'sites ssh': ['GET', siteId => `/sites/${siteId}/ssh`],
'sites update-ssh': ['PUT', siteId => `/sites/${siteId}/ssh`, true],
'sites backup': ['POST', siteId => `/sites/${siteId}/backup`, true],
'sites purge-cache': ['POST', siteId => `/sites/${siteId}/cache/purge`, true]
},
enterprise: {
health: ['GET', '/health', false],
'enterprise servers list': ['GET', '/servers'],
'enterprise servers show': ['GET', serverId => `/server/${serverId}`],
'enterprise servers create': ['POST', '/server', true],
'enterprise servers update': ['PATCH', serverId => `/server/${serverId}`, true],
'enterprise servers delete': ['DELETE', serverId => `/server/${serverId}`, true],
'enterprise sites list': ['GET', '/sites'],
'enterprise sites show': ['GET', siteId => `/site/${siteId}`],
'enterprise sites create': ['POST', '/site', true],
'enterprise sites update': ['PUT', siteId => `/site/${siteId}`, true],
'enterprise sites delete': ['DELETE', siteId => `/site/${siteId}`, true],
'enterprise sites suspend': ['POST', siteId => `/site/${siteId}/suspend`, true],
'enterprise sites staging': ['POST', siteId => `/site/${siteId}/staging`, true],
'enterprise sites publish-staging': ['POST', siteId => `/site/${siteId}/staging/publish`, true],
'enterprise sites purge-cache': ['POST', siteId => `/site/${siteId}/cache/purge`, true],
'enterprise sites ssh-keys': ['GET', siteId => `/site/${siteId}/ssh-keys`],
'enterprise sites create-ssh-key': ['POST', siteId => `/site/${siteId}/ssh-keys`, true],
'enterprise users list': ['GET', '/users'],
'enterprise users show': ['GET', id => `/user/${id}`],
'enterprise users create': ['POST', '/user', true],
'enterprise users update': ['PATCH', id => `/user/${id}`, true],
'enterprise users delete': ['DELETE', id => `/user/${id}`, true],
'enterprise users get-auth-token': ['GET', id => `/user/${id}/get-auth-token`],
'enterprise addon email products': ['GET', '/addon/email/products'],
'enterprise addon email provider': ['GET', '/addon/email/provider'],
'enterprise addon email purchase': ['POST', '/addon/email/purchase', true],
'enterprise addon mailbox products': ['GET', '/addon/mailbox/products'],
'enterprise addon mailbox accounts': ['GET', '/addon/mailbox/accounts'],
'enterprise addon mailbox show': ['GET', id => `/addon/mailbox/${id}`],
'enterprise addon mailbox purchase': ['POST', '/addon/mailbox/purchase', true],
'enterprise addon mailbox update': ['PATCH', id => `/addon/mailbox/${id}`, true],
'enterprise addon mailbox delete': ['DELETE', id => `/addon/mailbox/${id}`, true],
'enterprise addon patchstack products': ['GET', '/addon/patchstack/products'],
'enterprise addon patchstack sites': ['GET', '/addon/patchstack/sites'],
'enterprise addon patchstack show': ['GET', id => `/addon/patchstack/${id}`],
'enterprise addon patchstack purchase': ['POST', '/addon/patchstack/purchase', true],
'enterprise addon patchstack delete': ['DELETE', id => `/addon/patchstack/${id}`, true]
}
};
export function resolveRoute(flavor, words) {
const table = flavor === 'enterprise' ? routes.enterprise : routes.public;
for (let i = words.length; i > 0; i--) {
const key = words.slice(0, i).join(' ');
if (table[key]) return { route: table[key], params: words.slice(i), key };
}
return null;
}
export function buildRoutePath(route, params = [], key = 'command') {
const target = route[1];
if (typeof target !== 'function') {
if (params.length) throw new Error(`unexpected argument(s) for command: ${key}`);
return target;
}
const required = target.length;
if (params.length < required) {
const suffix = required === 1 ? 'id' : `${required} ids`;
throw new Error(`missing required ${suffix} for command: ${key}`);
}
if (params.length > required) throw new Error(`unexpected argument(s) for command: ${key}`);
return target(...params);
}
export function buildURL(baseURL, flavor, path, query = []) {
const cleanBase = String(baseURL || '').replace(/\/+$/, '');
const prefix = apiPrefix(flavor).replace(/^\/+|\/+$/g, '');
const cleanPath = String(path || '').replace(/^\/+/, '');
const url = new URL(`${cleanBase}/${prefix}/${cleanPath}`);
for (const pair of query) {
const idx = pair.indexOf('=');
if (idx === -1) url.searchParams.append(pair, '');
else url.searchParams.append(pair.slice(0, idx), pair.slice(idx + 1));
}
return url.toString();
}