-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
routes.ts
115 lines (112 loc) · 3.3 KB
/
routes.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import Joi from '@hapi/joi';
import Enjoi from 'enjoi';
import {
PLUGIN_ID,
API_INTEGRATIONS_LIST,
API_INTEGRATIONS_INFO,
API_SAVED_OBJECTS_DETAIL,
API_SAVED_OBJECTS_ROOT,
SAVED_OBJECT_TYPE,
} from '../common/constants';
import { Request, ServerRoute } from '../common/types';
import listSchema from '../schemas/consumed/list.schema.json';
import packageSchema from '../schemas/consumed/package.schema.json';
import { fetchInfo, fetchList, getArchiveInfo } from './registry';
import { getClient } from './saved_objects';
interface PostRequest extends Request {
payload: {
body: string;
};
}
// Manager public API paths (currently essentially a proxy to registry service)
export const routes: ServerRoute[] = [
{
method: 'GET',
path: API_INTEGRATIONS_LIST,
options: {
tags: [`access:${PLUGIN_ID}`, 'api'],
response: {
// TODO: Figure out why we need .meta and to spread
schema: Enjoi.schema({
...listSchema,
...listSchema.definitions.IntegrationListElement,
}).meta({ className: 'IntegrationListElement' }),
},
},
handler: fetchList,
},
{
method: 'GET',
path: API_INTEGRATIONS_INFO,
options: {
tags: [`access:${PLUGIN_ID}`, 'api'],
validate: {
params: {
pkgkey: Joi.string()
// TODO: real validation for name-semver
.regex(/\w+-\d+\.\d+\.\d+$/)
.required(),
},
},
response: {
// TODO: Figure out why we need .meta and to spread
schema: Enjoi.schema({
...packageSchema,
...packageSchema.definitions.Package,
}).meta({ className: 'Package' }),
},
},
handler: async (req: Request) => fetchInfo(req.params.pkgkey),
},
{
method: 'GET',
path: `${API_INTEGRATIONS_INFO}.zip`,
options: { tags: [`access:${PLUGIN_ID}`] },
handler: async (req: Request) => {
const { pkgkey } = req.params;
const paths = await getArchiveInfo(`${pkgkey}.zip`);
return { meta: { pkgkey, paths } };
},
},
{
method: 'GET',
path: `${API_INTEGRATIONS_INFO}.tar.gz`,
options: { tags: [`access:${PLUGIN_ID}`] },
handler: async (req: Request) => {
const { pkgkey } = req.params;
const paths = await getArchiveInfo(`${pkgkey}.tar.gz`);
return { meta: { pkgkey, paths } };
},
},
{
method: 'GET',
path: API_SAVED_OBJECTS_ROOT,
options: { tags: [`access:${PLUGIN_ID}`] },
handler: async (req: Request) => getClient(req).find({ type: SAVED_OBJECT_TYPE }),
},
{
method: 'GET',
path: API_SAVED_OBJECTS_DETAIL,
options: { tags: [`access:${PLUGIN_ID}`] },
handler: async (req: Request) => getClient(req).get(SAVED_OBJECT_TYPE, req.params.oid),
},
{
method: 'POST',
path: API_SAVED_OBJECTS_ROOT,
options: { tags: [`access:${PLUGIN_ID}`] },
handler: async (req: PostRequest) =>
getClient(req).create(
SAVED_OBJECT_TYPE,
{
is_working: true,
other: req.payload.body,
},
{ id: 'TBD', overwrite: true }
),
},
];