/
schema.ts
559 lines (546 loc) · 15.7 KB
/
schema.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import { FastifySchema } from "fastify";
export interface SwaggerSchema extends FastifySchema {
description: string;
tags: string[];
summary: string;
security?: { [securityLabel: string]: string[] }[] | undefined;
consumes?: string[];
}
export interface Schema {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
preValidation?: any;
schema: SwaggerSchema;
}
interface Schemas {
[key: string]: Schema;
}
// ------------------------------------------------------------
// Responses
// ------------------------------------------------------------
function getAuthErrorSchema(): Object {
return {
description: "Unauthorized request",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
error: {
type: "object",
properties: {
code: { type: "string", example: "401" },
message: {
type: "string",
example: "A valid JWT auth bearer token is required for this route.",
},
},
},
},
};
}
function getSuccessfulSchema(): { description: string; type: string } {
return {
description: "successful response",
type: "object",
};
}
const schemas: Schemas = {
// ------------------------------------------------------------
// system
// ------------------------------------------------------------
readiness: {
schema: {
description:
"Returns '200 Ready' if the API is up and the Multichain service is reachable. " +
"'504 Gateway Timeout.' otherwise.",
tags: ["system"],
summary: "Check if the Multichain is reachable",
response: {
200: getSuccessfulSchema(),
401: getAuthErrorSchema(),
504: {
description: "Blockchain not ready",
type: "string",
example: "Not ready. Waiting for multichain.",
},
},
},
},
liveness: {
schema: {
description: "Returns '200' and uptime in seconds if the API is up.",
tags: ["system"],
summary: "Check if the API is up",
response: {
200: getSuccessfulSchema(),
},
},
},
version: {
schema: {
description: "Returns version of the current release.",
tags: ["system"],
summary: "Check version of current release.",
security: [
{
bearerToken: [],
},
],
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
properties: {
api: {
type: "object",
properties: {
release: { type: "string", example: "1.0.0" },
commit: { type: "string", example: "f48b2af8e44f6a6d46f512efc68de35cb7e44c00" },
buildTimeStamp: { type: "string", example: "1546950454" },
},
},
blockchain: {
type: "object",
properties: {
release: { type: "string", example: "1.0.0" },
commit: { type: "string", example: "f48b2af8e44f6a6d46f512efc68de35cb7e44c00" },
buildTimeStamp: { type: "string", example: "1546950454" },
ping: { type: "number", example: "32.476300001144409" },
},
},
multichain: {
type: "object",
properties: {
release: { type: "string", example: "1.0.0" },
ping: { type: "number", example: "32.476300001144409" },
},
},
storage: {
type: "object",
properties: {
release: { type: "string", example: "1.0.0" },
commit: { type: "string", example: "f48b2af8e44f6a6d46f512efc68de35cb7e44c00" },
buildTimeStamp: { type: "string", example: "1546950454" },
ping: { type: "number", example: "32.476300001144409" },
},
},
},
},
},
},
},
},
},
registerNode: {
schema: {
description: "Used by non-alpha MultiChain nodes to register their wallet address.",
tags: ["network"],
summary: "Register a node",
security: [
{
bearerToken: [],
},
],
body: {
type: "object",
properties: {
apiVersion: { type: "string" },
data: {
type: "object",
additionalProperties: false,
properties: {
address: { type: "string" },
organization: { type: "string" },
},
required: ["address", "organization"],
},
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example4: "1.0" },
data: {
type: "object",
},
},
},
401: getAuthErrorSchema(),
},
},
},
declineNode: {
schema: {
description: "A user declines a node's request to connect to the network",
tags: ["network"],
summary: "Decline a node's request",
security: [
{
bearerToken: [],
},
],
body: {
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
additionalProperties: false,
properties: {
node: {
type: "object",
properties: {
address: { type: "string", example: "1CaWV7nTVwAd8bTzcPBBSQRZgbXLd9K8faM9QM" },
organization: { type: "string", example: "Alice's Solutions & Co" },
},
required: ["address", "organization"],
},
},
required: ["node"],
},
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
},
},
},
401: getAuthErrorSchema(),
},
},
},
voteForPermission: {
schema: {
description:
"Votes for granting/revoking network-level permissions to/from a " +
"registered node (identified by its wallet addresses). After this call, the voted " +
"access level may or may not be in effect, depending on the consensus parameters of " +
"the underlying blockchain.",
tags: ["network"],
summary: "Vote for permission",
security: [
{
bearerToken: [],
},
],
body: {
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
additionalProperties: false,
properties: {
address: { type: "string", example: "1CaWV7nTVwAd8bTzcPBBSQRZgbXLd9K8faM9QM" },
vote: { type: "string", example: "admin" },
},
required: ["address", "vote"],
},
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
},
},
},
401: getAuthErrorSchema(),
},
},
},
approveNewOrganization: {
schema: {
description: "Approves a new organization if there are enough votes.",
tags: ["network"],
summary: "Approve a new organization",
security: [
{
bearerToken: [],
},
],
body: {
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
additionalProperties: false,
properties: {
organization: { type: "string", example: "Alice's Solutions & Co" },
},
required: ["organization"],
},
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
},
},
},
401: getAuthErrorSchema(),
},
},
},
approveNewNodeForExistingOrganization: {
schema: {
description:
"Approves a new node for an existing organization." +
" This organization doesn't have to go through the voting system again",
tags: ["network"],
summary: "Approve a new node",
security: [
{
bearerToken: [],
},
],
body: {
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
additionalProperties: false,
properties: {
address: { type: "string", example: "1CaWV7nTVwAd8bTzcPBBSQRZgbXLd9K8faM9QM" },
},
required: ["address"],
},
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
},
},
},
401: getAuthErrorSchema(),
409: {
description:
"Tells either your organization has already voted or the permissions are already assigned",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
error: {
type: "object",
properties: {
code: { type: "string", example: "409" },
message: { type: "string", example: "User already exists." },
},
},
},
},
},
},
},
networkList: {
schema: {
description: "List all nodes.",
tags: ["network"],
summary: "List all nodes",
security: [
{
bearerToken: [],
},
],
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
properties: {
nodes: {
type: "array",
items: {
type: "object",
properties: {
address: {
type: "object",
properties: {
address: {
type: "string",
example: "1CaWV7nTVwAd8bTzcPBBSQRZgbXLd9K8faM9QM",
},
organization: { type: "string", example: "Alice's Solutions & Co" },
},
},
myVote: { type: "string", example: "admin" },
isConnected: { type: "boolean", example: true },
currentAccess: {
type: "object",
properties: {
accessType: { type: "string", example: "admin" },
approvers: {
type: "array",
items: {
type: "object",
properties: {
address: {
type: "string",
example: "1CaWV7nTVwAd8bTzcPBBSQRZgbXLd9K8faM9QM",
},
organization: {
type: "string",
example: "Alice's Solutions & Co",
},
},
},
},
decliners: {
type: "array",
items: {
type: "object",
properties: {
address: {
type: "string",
example: "1CaWV7nTVwAd8bTzcPBBSQRZgbXLd9K8faM9QM",
},
organization: {
type: "string",
example: "Alice's Solutions & Co",
},
},
},
},
},
},
lastSeen: {
type: "string",
example: "2021-02-11",
},
},
},
},
},
},
},
},
401: getAuthErrorSchema(),
},
},
},
listActive: {
schema: {
description: "Get the number of all peers in the blockchain network.",
tags: ["network"],
summary: "List all active peers",
security: [
{
bearerToken: [],
},
],
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
properties: {
peers: { type: "string", example: "56" },
},
},
},
},
401: getAuthErrorSchema(),
},
},
},
createBackup: {
schema: {
description: "Create a backup",
tags: ["system"],
summary: "Create a Backup",
security: [
{
bearerToken: [],
},
],
response: {
200: {
description: "file download backup.gz",
type: "string",
// format: "binary",
example: "backup.gz",
},
},
},
},
restoreBackup: {
schema: {
description:
"To restore a backup send a valid backup.gz file as binary via an API-Testing-Tool like postman." +
"Use 'application/gzip' as content type header)",
tags: ["system"],
summary: "Restore a Backup",
security: [
{
bearerToken: [],
},
],
consumes: ["application/gzip"],
body: {
// type=string + format=binary is not supported by fastify-swagger.
// Instead, we use the `AnyValue` schema, which allows anything.
AnyValue: {
description: "backup.gz (binary gzip file)",
},
},
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
},
},
},
401: getAuthErrorSchema(),
},
},
},
};
export function getSchema(server, id): Schema {
const schema = schemas[id];
return {
preValidation: [server.authenticate],
...schema,
};
}
export function getSchemaWithoutAuth(id): Schema {
const schema = schemas[id];
return {
...schema,
};
}