kibana/x-pack/examples/screenshotting_example/server/plugin.ts
Dario Gieselaar 98ce312ba3
More strict plugin type definitions (#205232)
Stricter defaults for plugin types: `Plugin` and `CoreSetup` now have
empty objects as defaults instead of `object` which is assignable to
anything basically. This catches some type errors, but my motivation for
this is to allow something like:

```ts
function createPlugin ():Plugin<MySetupContract, MyStartContract, MySetupDependencies, MyStartDependencies> {
	return {
		// look ma, no additional typing necessary
		setup ( coreSetup, pluginsSetup ) {
		},
		start ( coreStart, pluginsStart ) {
		}
	}
}
```
2025-01-07 16:41:15 +01:00

54 lines
1.7 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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { schema } from '@kbn/config-schema';
import type { CoreSetup, Plugin } from '@kbn/core/server';
import type { ScreenshottingStart } from '@kbn/screenshotting-plugin/server';
import { lastValueFrom } from 'rxjs';
import { API_ENDPOINT, ScreenshottingExpressionResponse } from '../common';
interface StartDeps {
screenshotting: ScreenshottingStart;
}
export class ScreenshottingExamplePlugin implements Plugin<void, void, {}, StartDeps> {
setup({ http, getStartServices }: CoreSetup<StartDeps>) {
const router = http.createRouter();
router.get(
{
path: API_ENDPOINT,
validate: {
query: schema.object({
expression: schema.string(),
}),
},
},
async (_context, request, response) => {
const [, { screenshotting }] = await getStartServices();
const { metrics, results } = await lastValueFrom(
screenshotting.getScreenshots({
request,
taskInstanceFields: { startedAt: null, retryAt: null },
expression: request.query.expression,
})
);
return response.ok({
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
metrics,
image: results[0]?.screenshots[0]?.data.toString('base64'),
errors: results[0]?.renderErrors,
} as ScreenshottingExpressionResponse),
});
}
);
}
start() {}
}