@metacall/protocol 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,273 @@
1
+ /*
2
+
3
+ * About File:
4
+
5
+ this is just a client that implements all the rest API from the FaaS, so each function it contains is an endpoint in the FaaS for deploying and similar
6
+
7
+ refresh: updates the auth token
8
+ validate: validates the auth token
9
+ deployEnabled: checks if you're able to deploy
10
+ listSubscriptions: gives you a list of the subscription available
11
+ inspect: gives you are deploys with it's endpoints
12
+ upload: uploads a zip (package) into the faas
13
+ deploy: deploys the previously uploaded zip into the faas
14
+ deployDelete: deletes the deploy and the zip
15
+
16
+ */
17
+
18
+ import axios, { AxiosError, AxiosResponse } from 'axios';
19
+ import FormData from 'form-data';
20
+ import { Create, Deployment, LogType, MetaCallJSON } from './deployment';
21
+ import { Plans } from './plan';
22
+
23
+ export const isProtocolError = (err: unknown): boolean =>
24
+ axios.isAxiosError(err);
25
+
26
+ export { AxiosError as ProtocolError };
27
+
28
+ type SubscriptionMap = Record<string, number>;
29
+
30
+ export type ResourceType = 'Package' | 'Repository';
31
+
32
+ export interface AddResponse {
33
+ id: string;
34
+ }
35
+
36
+ export interface Branches {
37
+ branches: [string];
38
+ }
39
+
40
+ interface API {
41
+ refresh(): Promise<string>;
42
+ validate(): Promise<boolean>;
43
+ deployEnabled(): Promise<boolean>;
44
+ listSubscriptions(): Promise<SubscriptionMap>;
45
+ inspect(): Promise<Deployment[]>;
46
+ upload(
47
+ name: string,
48
+ blob: unknown,
49
+ jsons: MetaCallJSON[],
50
+ runners: string[]
51
+ ): Promise<string>;
52
+ add(
53
+ url: string,
54
+ branch: string,
55
+ jsons: MetaCallJSON[]
56
+ ): Promise<AddResponse>;
57
+ deploy(
58
+ name: string,
59
+ env: string[],
60
+ plan: Plans,
61
+ resourceType: ResourceType,
62
+ release?: string,
63
+ version?: string
64
+ ): Promise<Create>;
65
+ deployDelete(
66
+ prefix: string,
67
+ suffix: string,
68
+ version: string
69
+ ): Promise<string>;
70
+ logs(
71
+ container: string,
72
+ type: LogType,
73
+ suffix: string,
74
+ prefix: string,
75
+ version?: string
76
+ ): Promise<string>;
77
+ branchList(url: string): Promise<Branches>;
78
+ fileList(url: string, branch: string): Promise<string[]>;
79
+ }
80
+
81
+ export default (token: string, baseURL: string): API => {
82
+ const api: API = {
83
+ refresh: (): Promise<string> =>
84
+ axios
85
+ .get<string>(baseURL + '/api/account/refresh-token', {
86
+ headers: { Authorization: 'jwt ' + token }
87
+ })
88
+ .then(res => res.data),
89
+
90
+ validate: (): Promise<boolean> =>
91
+ axios
92
+ .get<boolean>(baseURL + '/validate', {
93
+ headers: { Authorization: 'jwt ' + token }
94
+ })
95
+ .then(res => res.data),
96
+
97
+ deployEnabled: (): Promise<boolean> =>
98
+ axios
99
+ .get<boolean>(baseURL + '/api/account/deploy-enabled', {
100
+ headers: { Authorization: 'jwt ' + token }
101
+ })
102
+ .then(res => res.data),
103
+
104
+ listSubscriptions: async (): Promise<SubscriptionMap> => {
105
+ const res = await axios.get<string[]>(
106
+ baseURL + '/api/billing/list-subscriptions',
107
+ {
108
+ headers: { Authorization: 'jwt ' + token }
109
+ }
110
+ );
111
+
112
+ const subscriptions: SubscriptionMap = {};
113
+
114
+ for (const id of res.data) {
115
+ if (subscriptions[id] === undefined) {
116
+ subscriptions[id] = 1;
117
+ } else {
118
+ ++subscriptions[id];
119
+ }
120
+ }
121
+
122
+ return subscriptions;
123
+ },
124
+
125
+ inspect: async (): Promise<Deployment[]> =>
126
+ axios
127
+ .get<Deployment[]>(baseURL + '/api/inspect', {
128
+ headers: { Authorization: 'jwt ' + token }
129
+ })
130
+ .then(res => res.data),
131
+
132
+ upload: async (
133
+ name: string,
134
+ blob: unknown,
135
+ jsons: MetaCallJSON[] = [],
136
+ runners: string[] = []
137
+ ): Promise<string> => {
138
+ const fd = new FormData();
139
+ fd.append('id', name);
140
+ fd.append('type', 'application/x-zip-compressed');
141
+ fd.append('jsons', JSON.stringify(jsons));
142
+ fd.append('runners', JSON.stringify(runners));
143
+ fd.append('raw', blob, {
144
+ filename: 'blob',
145
+ contentType: 'application/x-zip-compressed'
146
+ });
147
+ const res = await axios.post<string>(
148
+ baseURL + '/api/package/create',
149
+ fd,
150
+ {
151
+ headers: {
152
+ Authorization: 'jwt ' + token,
153
+ ...fd.getHeaders()
154
+ }
155
+ }
156
+ );
157
+ return res.data;
158
+ },
159
+ add: (
160
+ url: string,
161
+ branch: string,
162
+ jsons: MetaCallJSON[] = []
163
+ ): Promise<AddResponse> =>
164
+ axios
165
+ .post<string>(
166
+ baseURL + '/api/repository/add',
167
+ {
168
+ url,
169
+ branch,
170
+ jsons
171
+ },
172
+ {
173
+ headers: { Authorization: 'jwt ' + token }
174
+ }
175
+ )
176
+ .then((res: AxiosResponse) => res.data as AddResponse),
177
+ branchList: (url: string): Promise<Branches> =>
178
+ axios
179
+ .post<string>(
180
+ baseURL + '/api/repository/branchlist',
181
+ {
182
+ url
183
+ },
184
+ {
185
+ headers: { Authorization: 'jwt ' + token }
186
+ }
187
+ )
188
+ .then((res: AxiosResponse) => res.data as Branches),
189
+
190
+ deploy: (
191
+ name: string,
192
+ env: string[],
193
+ plan: Plans,
194
+ resourceType: ResourceType,
195
+ release: string = Date.now().toString(16),
196
+ version = 'v1'
197
+ ): Promise<Create> =>
198
+ axios
199
+ .post<Create>(
200
+ baseURL + '/api/deploy/create',
201
+ {
202
+ resourceType,
203
+ suffix: name,
204
+ release,
205
+ env,
206
+ plan,
207
+ version
208
+ },
209
+ {
210
+ headers: { Authorization: 'jwt ' + token }
211
+ }
212
+ )
213
+ .then(res => res.data),
214
+
215
+ deployDelete: (
216
+ prefix: string,
217
+ suffix: string,
218
+ version = 'v1'
219
+ ): Promise<string> =>
220
+ axios
221
+ .post<string>(
222
+ baseURL + '/api/deploy/delete',
223
+ {
224
+ prefix,
225
+ suffix,
226
+ version
227
+ },
228
+ {
229
+ headers: { Authorization: 'jwt ' + token }
230
+ }
231
+ )
232
+ .then(res => res.data),
233
+
234
+ logs: (
235
+ container: string,
236
+ type: LogType = LogType.Deploy,
237
+ suffix: string,
238
+ prefix: string,
239
+ version = 'v1'
240
+ ): Promise<string> =>
241
+ axios
242
+ .post<string>(
243
+ baseURL + '/api/deploy/logs',
244
+ {
245
+ container,
246
+ type,
247
+ suffix,
248
+ prefix,
249
+ version
250
+ },
251
+ {
252
+ headers: { Authorization: 'jwt ' + token }
253
+ }
254
+ )
255
+ .then(res => res.data),
256
+
257
+ fileList: (url: string, branch: string): Promise<string[]> =>
258
+ axios
259
+ .post<{ [k: string]: string[] }>(
260
+ baseURL + '/api/repository/filelist',
261
+ {
262
+ url,
263
+ branch
264
+ },
265
+ {
266
+ headers: { Authorization: 'jwt ' + token }
267
+ }
268
+ )
269
+ .then(res => res.data['files'])
270
+ };
271
+
272
+ return api;
273
+ };
package/src/token.ts ADDED
@@ -0,0 +1,10 @@
1
+ import jwt from 'jsonwebtoken';
2
+
3
+ export const expiresIn = (token: string): number => {
4
+ const decoded = jwt.decode(token);
5
+ if (typeof decoded === 'string') {
6
+ return 0;
7
+ }
8
+ const now = Date.now() / 1000;
9
+ return new Date((decoded?.['exp'] || now) * 1000).getTime() - now * 1000;
10
+ };