kibana/x-pack/plugins/actions/server/routes/execute.ts
Mikhail Shustov b3a9754394
[Core] Explicit typings for request handler context (#88718)
* move context to server part. couple with RequestHandlerContext

Context implementation will be simplified in follow-up.

* adopt core code

* adopt bfetch code

* adopt data code

* adopt search examples

* adopt vis_type_timelion

* adopt vis_type_timeseries

* adopt plugin functional tests

* adopt actions

* adopt alerting plugin

* adopt APM plugin

* adopt beats_management

* adopt case plugin

* adopt cross_cluster_replication

* adopt data_enhanced

* adopt event_log

* adopt global_search

* adopt index_management

* adopt infra

* adopt licensing

* adopt lists

* adopt logstash

* adopt reporting

* adopt observability

* adopt monitoring

* adopt rollup

* adopt so tagging

* adopt security

* adopt security_solutions

* adopt watcher

* adopt uptime

* adopt spaces

* adopt snapshot_restore

* adopt features changes

* mute error when null used to extend context

* update docs

* small cleanup

* add type safety for return type

* refactor registerRouteHandlerContext type

* update docs

* update license header

* update docs

* fix type error. fetch body does not accept array of strings

* fix telemetry test

* remove unnecessary ts-ignore

* address comments

* update docs
2021-01-21 15:20:22 +01:00

64 lines
1.9 KiB
TypeScript

/*
* 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 { schema } from '@kbn/config-schema';
import { IRouter } from 'kibana/server';
import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../lib';
import { ActionTypeExecutorResult, ActionsRequestHandlerContext } from '../types';
import { BASE_ACTION_API_PATH } from '../../common';
import { asHttpRequestExecutionSource } from '../lib/action_execution_source';
const paramSchema = schema.object({
id: schema.string(),
});
const bodySchema = schema.object({
params: schema.recordOf(schema.string(), schema.any()),
});
export const executeActionRoute = (
router: IRouter<ActionsRequestHandlerContext>,
licenseState: ILicenseState
) => {
router.post(
{
path: `${BASE_ACTION_API_PATH}/action/{id}/_execute`,
validate: {
body: bodySchema,
params: paramSchema,
},
},
router.handleLegacyErrors(async function (context, req, res) {
verifyApiAccess(licenseState);
if (!context.actions) {
return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' });
}
const actionsClient = context.actions.getActionsClient();
const { params } = req.body;
const { id } = req.params;
try {
const body: ActionTypeExecutorResult<unknown> = await actionsClient.execute({
params,
actionId: id,
source: asHttpRequestExecutionSource(req),
});
return body
? res.ok({
body,
})
: res.noContent();
} catch (e) {
if (isErrorThatHandlesItsOwnResponse(e)) {
return e.sendResponse(res);
}
throw e;
}
})
);
};