Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Simplify context typings #93579

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 81 additions & 46 deletions api_docs/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -7985,22 +7985,11 @@
"id": "def-server.IContextContainer",
"type": "Interface",
"label": "IContextContainer",
"signature": [
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.IContextContainer",
"text": "IContextContainer"
},
"<THandler>"
],
"description": [
"\nAn object that handles registration of context providers and configuring handlers with context.\n"
],
"tags": [
"remarks",
"typeParam",
"public"
],
"children": [
Expand Down Expand Up @@ -8043,8 +8032,8 @@
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 154,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L154"
"lineNumber": 151,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L151"
}
},
{
Expand All @@ -8059,8 +8048,8 @@
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 155,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L155"
"lineNumber": 152,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L152"
}
},
{
Expand All @@ -8082,8 +8071,8 @@
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 156,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L156"
"lineNumber": 153,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L153"
}
}
],
Expand All @@ -8095,26 +8084,49 @@
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 153,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L153"
"lineNumber": 150,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L150"
}
},
{
"id": "def-server.IContextContainer.createHandler",
"type": "Function",
"label": "createHandler",
"signature": [
"(pluginOpaqueId: symbol, handler: THandler) => (...rest: ",
"(pluginOpaqueId: symbol, handler: ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.RequestHandler",
"text": "RequestHandler"
},
"<unknown, unknown, unknown, ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.HandlerParameters",
"text": "HandlerParameters"
"section": "def-server.RequestHandlerContext",
"text": "RequestHandlerContext"
},
", any, { custom: <T extends string | Record<string, any> | Error | { message: string | Error; attributes?: Record<string, any> | undefined; } | Buffer | ",
"Stream",
" | undefined>(options: ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.CustomHttpResponseOptions",
"text": "CustomHttpResponseOptions"
},
"<THandler>) => ",
"ShallowPromise",
"<ReturnType<THandler>>"
"<T>) => ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.KibanaResponse",
"text": "KibanaResponse"
}
],
"description": [
"\nCreate a new handler function pre-wired to context for the plugin.\n"
Expand All @@ -8132,42 +8144,74 @@
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 168,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L168"
"lineNumber": 165,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L165"
}
},
{
"type": "Function",
"label": "handler",
"isRequired": true,
"signature": [
"THandler"
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.RequestHandler",
"text": "RequestHandler"
},
"<unknown, unknown, unknown, ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.RequestHandlerContext",
"text": "RequestHandlerContext"
},
", any, { custom: <T extends string | Record<string, any> | Error | { message: string | Error; attributes?: Record<string, any> | undefined; } | Buffer | ",
"Stream",
" | undefined>(options: ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.CustomHttpResponseOptions",
"text": "CustomHttpResponseOptions"
},
"<T>) => ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.KibanaResponse",
"text": "KibanaResponse"
}
],
"description": [
"- Handler function to pass context object to."
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 169,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L169"
"lineNumber": 166,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L166"
}
}
],
"tags": [],
"returnComment": [
"A function that takes `THandlerParameters`, calls `handler` with a new context, and returns a Promise of\nthe `handler` return value."
"A function that takes `RequestHandler` parameters, calls `handler` with a new context, and returns a Promise of\nthe `handler` return value."
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 167,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L167"
"lineNumber": 164,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L164"
}
}
],
"source": {
"path": "src/core/server/context/container/context.ts",
"lineNumber": 138,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L138"
"lineNumber": 135,
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L135"
},
"initialIsOpen": false
},
Expand Down Expand Up @@ -16631,23 +16675,14 @@
"type": "Function",
"label": "createContextContainer",
"signature": [
"<THandler extends ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.HandlerFunction",
"text": "HandlerFunction"
},
"<any>>() => ",
"() => ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.IContextContainer",
"text": "IContextContainer"
},
"<THandler>"
}
],
"description": [
"\nCreates a new {@link IContextContainer} for a service owner."
Expand Down
29 changes: 4 additions & 25 deletions api_docs/core_http.json
Original file line number Diff line number Diff line change
Expand Up @@ -3725,9 +3725,9 @@
{
"pluginId": "core",
"scope": "server",
"docId": "kibCoreHttpPluginApi",
"section": "def-server.RequestHandlerContextContainer",
"text": "RequestHandlerContextContainer"
"docId": "kibCorePluginApi",
"section": "def-server.IContextContainer",
"text": "IContextContainer"
}
]
},
Expand Down Expand Up @@ -6509,28 +6509,7 @@
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/http/types.ts#L30"
},
"signature": [
"IContextContainer<RequestHandler<unknown, unknown, unknown, RequestHandlerContext, any, { custom: <T extends string | Record<string, any> | Error | { message: string | Error; attributes?: Record<string, any> | undefined; } | Buffer | ",
"Stream",
" | undefined>(options: ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.CustomHttpResponseOptions",
"text": "CustomHttpResponseOptions"
},
"<T>) => ",
"KibanaResponse",
"<T>; badRequest: (options?: ",
{
"pluginId": "core",
"scope": "server",
"docId": "kibCorePluginApi",
"section": "def-server.ErrorHttpResponseOptions",
"text": "ErrorHttpResponseOptions"
},
") => ",
"KibanaResponse"
"IContextContainer"
],
"initialIsOpen": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Creates a new [IContextContainer](./kibana-plugin-core-server.icontextcontainer.
<b>Signature:</b>

```typescript
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
createContextContainer(): IContextContainer;
```
<b>Returns:</b>

`IContextContainer<THandler>`
`IContextContainer`

Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Create a new handler function pre-wired to context for the plugin.
<b>Signature:</b>

```typescript
createHandler(pluginOpaqueId: PluginOpaqueId, handler: THandler): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
createHandler(pluginOpaqueId: PluginOpaqueId, handler: RequestHandler): (...rest: HandlerParameters<RequestHandler>) => ShallowPromise<ReturnType<RequestHandler>>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| pluginOpaqueId | <code>PluginOpaqueId</code> | The plugin opaque ID for the plugin that registers this handler. |
| handler | <code>THandler</code> | Handler function to pass context object to. |
| handler | <code>RequestHandler</code> | Handler function to pass context object to. |

<b>Returns:</b>

`(...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>`
`(...rest: HandlerParameters<RequestHandler>) => ShallowPromise<ReturnType<RequestHandler>>`

A function that takes `THandlerParameters`<!-- -->, calls `handler` with a new context, and returns a Promise of the `handler` return value.
A function that takes `RequestHandler` parameters, calls `handler` with a new context, and returns a Promise of the `handler` return value.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ An object that handles registration of context providers and configuring handler
<b>Signature:</b>

```typescript
export interface IContextContainer<THandler extends RequestHandler>
export interface IContextContainer
```

## Remarks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ An object that handles registration of http request context providers.
<b>Signature:</b>

```typescript
export declare type RequestHandlerContextContainer = IContextContainer<RequestHandler>;
export declare type RequestHandlerContextContainer = IContextContainer;
```
8 changes: 4 additions & 4 deletions src/core/server/context/container/context.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

import { IContextContainer } from './context';

export type ContextContainerMock = jest.Mocked<IContextContainer<any>>;
export type ContextContainerMock = jest.Mocked<IContextContainer>;

const createContextMock = (mockContext = {}) => {
const createContextMock = (mockContext: any = {}) => {
const contextMock: ContextContainerMock = {
// @ts-expect-error tsc cannot infer ContextName and uses never
// @ts-expect-error since ContextContainerMock cannot infer ContextName and fallsback to never
registerContext: jest.fn(),
createHandler: jest.fn(),
};
contextMock.createHandler.mockImplementation((pluginId, handler) => (...args) =>
handler(mockContext, ...args)
Promise.resolve(handler(mockContext, ...args))
);
return contextMock;
};
Expand Down
24 changes: 24 additions & 0 deletions src/core/server/context/container/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ describe('ContextContainer', () => {
`"Cannot register context for unknown plugin: Symbol(unknown)"`
);
});

it('reports a TS error if returned contract does not satisfy the Context interface', async () => {
const contextContainer = new ContextContainer(plugins, coreId);
await expect(() =>
contextContainer.registerContext<{ ctxFromA: string; core: any }, 'ctxFromA'>(
pluginA,
'ctxFromA',
// @ts-expect-error expected string, returned number
async () => 1
)
).not.toThrow();
});

it('reports a TS error if registers a context for unknown property name', async () => {
const contextContainer = new ContextContainer(plugins, coreId);
await expect(() =>
// @ts-expect-error expects ctxFromB, but given ctxFromC
contextContainer.registerContext<{ ctxFromB: string; core: any }, 'ctxFromC'>(
pluginB,
'ctxFromC',
async () => 1
)
).not.toThrow();
});
});

describe('context building', () => {
Expand Down
Loading