-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85f9ef6
commit ee7ef71
Showing
12 changed files
with
437 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 { merge } from 'lodash'; | ||
import Boom from 'boom'; | ||
import { InternalCoreSetup } from 'src/core/server'; | ||
import { Request } from 'hapi'; | ||
import * as t from 'io-ts'; | ||
import { PathReporter } from 'io-ts/lib/PathReporter'; | ||
import { ServerAPI, RouteFactoryFunction, RouteParams } from './typings'; | ||
import { removeUndefinedProps } from '../../public/context/UrlParamsContext/helpers'; | ||
|
||
type GenericRouteFactoryFunction = RouteFactoryFunction< | ||
any, | ||
any, | ||
{ [key in keyof RouteParams]?: RouteParams[key] }, | ||
any | ||
>; | ||
|
||
export function createApi() { | ||
const factoryFns: GenericRouteFactoryFunction[] = []; | ||
const router = { | ||
add(fn: GenericRouteFactoryFunction) { | ||
factoryFns.push(fn); | ||
return router; | ||
}, | ||
init: (core: InternalCoreSetup) => { | ||
const { server } = core.http; | ||
factoryFns.forEach(fn => { | ||
const { params = {}, ...route } = fn(core); | ||
server.route( | ||
merge( | ||
{ | ||
options: { | ||
tags: ['access:apm'] | ||
} | ||
}, | ||
route, | ||
{ | ||
handler: async (request: Request) => { | ||
const paramMap = { | ||
path: request.params, | ||
body: request.payload, | ||
query: request.query | ||
}; | ||
|
||
const parsedParams = (Object.keys(params) as Array< | ||
keyof typeof params | ||
>).reduce( | ||
(acc, key) => { | ||
let codec = params[key]; | ||
if (!codec) return acc; | ||
|
||
if ('_tag' in codec) { | ||
codec = t.exact(codec); | ||
} | ||
|
||
const result = codec.decode(paramMap[key]); | ||
if (result.isLeft()) { | ||
throw Boom.badRequest(PathReporter.report(result)[0]); | ||
} | ||
return { | ||
...acc, | ||
[key]: result.value | ||
}; | ||
}, | ||
{} as Record<keyof typeof params, any> | ||
); | ||
|
||
return route.handler( | ||
request, | ||
removeUndefinedProps(parsedParams) | ||
); | ||
} | ||
} | ||
) | ||
); | ||
}); | ||
} | ||
}; | ||
|
||
return router as ServerAPI<{}>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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 * as t from 'io-ts'; | ||
import { | ||
RouteFactoryFunction, | ||
HttpMethod, | ||
RouteParams, | ||
RouteRequestHandler | ||
} from './typings'; | ||
|
||
export function createRoute< | ||
T extends string, | ||
U extends HttpMethod, | ||
V extends RouteParams, | ||
W, | ||
X extends RouteRequestHandler<V, W> | ||
>(cb: RouteFactoryFunction<T, U, V, W>) { | ||
return cb; | ||
} | ||
|
||
createRoute(core => ({ | ||
path: '/foo', | ||
method: 'GET' as const, | ||
params: { | ||
query: t.type({ | ||
serviceName: t.string | ||
}) | ||
}, | ||
handler: async (req, params) => { | ||
return null; | ||
} | ||
})); |
30 changes: 30 additions & 0 deletions
30
x-pack/legacy/plugins/apm/server/routes/default_api_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 * as t from 'io-ts'; | ||
import { either } from 'fp-ts/lib/Either'; | ||
|
||
const tDate = new t.Type<string, string, unknown>( | ||
'DateAsString', | ||
(u): u is string => typeof u === 'string', | ||
(u, c) => | ||
either.chain(t.string.validate(u, c), s => { | ||
const d = new Date(s); | ||
return isNaN(d.getTime()) ? t.failure(u, c) : t.success(s); | ||
}), | ||
a => a | ||
); | ||
|
||
export const defaultApiTypes = t.intersection([ | ||
t.type({ | ||
start: tDate, | ||
end: tDate, | ||
uiFilters: t.string | ||
}), | ||
t.partial({ | ||
_debug: t.boolean | ||
}) | ||
]); |
Oops, something went wrong.