Skip to content

Commit 7e3ca16

Browse files
authored
[Core] Simplify context typings (#93579)
* simplify context typings in core * add tests for context types * update docs
1 parent cad2653 commit 7e3ca16

12 files changed

+144
-110
lines changed

api_docs/core.json

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7985,22 +7985,11 @@
79857985
"id": "def-server.IContextContainer",
79867986
"type": "Interface",
79877987
"label": "IContextContainer",
7988-
"signature": [
7989-
{
7990-
"pluginId": "core",
7991-
"scope": "server",
7992-
"docId": "kibCorePluginApi",
7993-
"section": "def-server.IContextContainer",
7994-
"text": "IContextContainer"
7995-
},
7996-
"<THandler>"
7997-
],
79987988
"description": [
79997989
"\nAn object that handles registration of context providers and configuring handlers with context.\n"
80007990
],
80017991
"tags": [
80027992
"remarks",
8003-
"typeParam",
80047993
"public"
80057994
],
80067995
"children": [
@@ -8043,8 +8032,8 @@
80438032
],
80448033
"source": {
80458034
"path": "src/core/server/context/container/context.ts",
8046-
"lineNumber": 154,
8047-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L154"
8035+
"lineNumber": 151,
8036+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L151"
80488037
}
80498038
},
80508039
{
@@ -8059,8 +8048,8 @@
80598048
],
80608049
"source": {
80618050
"path": "src/core/server/context/container/context.ts",
8062-
"lineNumber": 155,
8063-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L155"
8051+
"lineNumber": 152,
8052+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L152"
80648053
}
80658054
},
80668055
{
@@ -8082,8 +8071,8 @@
80828071
],
80838072
"source": {
80848073
"path": "src/core/server/context/container/context.ts",
8085-
"lineNumber": 156,
8086-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L156"
8074+
"lineNumber": 153,
8075+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L153"
80878076
}
80888077
}
80898078
],
@@ -8095,26 +8084,49 @@
80958084
],
80968085
"source": {
80978086
"path": "src/core/server/context/container/context.ts",
8098-
"lineNumber": 153,
8099-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L153"
8087+
"lineNumber": 150,
8088+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L150"
81008089
}
81018090
},
81028091
{
81038092
"id": "def-server.IContextContainer.createHandler",
81048093
"type": "Function",
81058094
"label": "createHandler",
81068095
"signature": [
8107-
"(pluginOpaqueId: symbol, handler: THandler) => (...rest: ",
8096+
"(pluginOpaqueId: symbol, handler: ",
8097+
{
8098+
"pluginId": "core",
8099+
"scope": "server",
8100+
"docId": "kibCoreHttpPluginApi",
8101+
"section": "def-server.RequestHandler",
8102+
"text": "RequestHandler"
8103+
},
8104+
"<unknown, unknown, unknown, ",
81088105
{
81098106
"pluginId": "core",
81108107
"scope": "server",
81118108
"docId": "kibCorePluginApi",
8112-
"section": "def-server.HandlerParameters",
8113-
"text": "HandlerParameters"
8109+
"section": "def-server.RequestHandlerContext",
8110+
"text": "RequestHandlerContext"
8111+
},
8112+
", any, { custom: <T extends string | Record<string, any> | Error | { message: string | Error; attributes?: Record<string, any> | undefined; } | Buffer | ",
8113+
"Stream",
8114+
" | undefined>(options: ",
8115+
{
8116+
"pluginId": "core",
8117+
"scope": "server",
8118+
"docId": "kibCoreHttpPluginApi",
8119+
"section": "def-server.CustomHttpResponseOptions",
8120+
"text": "CustomHttpResponseOptions"
81148121
},
8115-
"<THandler>) => ",
8116-
"ShallowPromise",
8117-
"<ReturnType<THandler>>"
8122+
"<T>) => ",
8123+
{
8124+
"pluginId": "core",
8125+
"scope": "server",
8126+
"docId": "kibCoreHttpPluginApi",
8127+
"section": "def-server.KibanaResponse",
8128+
"text": "KibanaResponse"
8129+
}
81188130
],
81198131
"description": [
81208132
"\nCreate a new handler function pre-wired to context for the plugin.\n"
@@ -8132,42 +8144,74 @@
81328144
],
81338145
"source": {
81348146
"path": "src/core/server/context/container/context.ts",
8135-
"lineNumber": 168,
8136-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L168"
8147+
"lineNumber": 165,
8148+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L165"
81378149
}
81388150
},
81398151
{
81408152
"type": "Function",
81418153
"label": "handler",
81428154
"isRequired": true,
81438155
"signature": [
8144-
"THandler"
8156+
{
8157+
"pluginId": "core",
8158+
"scope": "server",
8159+
"docId": "kibCoreHttpPluginApi",
8160+
"section": "def-server.RequestHandler",
8161+
"text": "RequestHandler"
8162+
},
8163+
"<unknown, unknown, unknown, ",
8164+
{
8165+
"pluginId": "core",
8166+
"scope": "server",
8167+
"docId": "kibCorePluginApi",
8168+
"section": "def-server.RequestHandlerContext",
8169+
"text": "RequestHandlerContext"
8170+
},
8171+
", any, { custom: <T extends string | Record<string, any> | Error | { message: string | Error; attributes?: Record<string, any> | undefined; } | Buffer | ",
8172+
"Stream",
8173+
" | undefined>(options: ",
8174+
{
8175+
"pluginId": "core",
8176+
"scope": "server",
8177+
"docId": "kibCoreHttpPluginApi",
8178+
"section": "def-server.CustomHttpResponseOptions",
8179+
"text": "CustomHttpResponseOptions"
8180+
},
8181+
"<T>) => ",
8182+
{
8183+
"pluginId": "core",
8184+
"scope": "server",
8185+
"docId": "kibCoreHttpPluginApi",
8186+
"section": "def-server.KibanaResponse",
8187+
"text": "KibanaResponse"
8188+
}
81458189
],
81468190
"description": [
81478191
"- Handler function to pass context object to."
81488192
],
81498193
"source": {
81508194
"path": "src/core/server/context/container/context.ts",
8151-
"lineNumber": 169,
8152-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L169"
8195+
"lineNumber": 166,
8196+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L166"
81538197
}
81548198
}
81558199
],
81568200
"tags": [],
81578201
"returnComment": [
8158-
"A function that takes `THandlerParameters`, calls `handler` with a new context, and returns a Promise of\nthe `handler` return value."
8202+
"A function that takes `RequestHandler` parameters, calls `handler` with a new context, and returns a Promise of\nthe `handler` return value."
81598203
],
81608204
"source": {
81618205
"path": "src/core/server/context/container/context.ts",
8162-
"lineNumber": 167,
8163-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L167"
8206+
"lineNumber": 164,
8207+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L164"
81648208
}
81658209
}
81668210
],
81678211
"source": {
81688212
"path": "src/core/server/context/container/context.ts",
8169-
"lineNumber": 138,
8170-
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L138"
8213+
"lineNumber": 135,
8214+
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/context/container/context.ts#L135"
81718215
},
81728216
"initialIsOpen": false
81738217
},
@@ -16631,23 +16675,14 @@
1663116675
"type": "Function",
1663216676
"label": "createContextContainer",
1663316677
"signature": [
16634-
"<THandler extends ",
16635-
{
16636-
"pluginId": "core",
16637-
"scope": "server",
16638-
"docId": "kibCorePluginApi",
16639-
"section": "def-server.HandlerFunction",
16640-
"text": "HandlerFunction"
16641-
},
16642-
"<any>>() => ",
16678+
"() => ",
1664316679
{
1664416680
"pluginId": "core",
1664516681
"scope": "server",
1664616682
"docId": "kibCorePluginApi",
1664716683
"section": "def-server.IContextContainer",
1664816684
"text": "IContextContainer"
16649-
},
16650-
"<THandler>"
16685+
}
1665116686
],
1665216687
"description": [
1665316688
"\nCreates a new {@link IContextContainer} for a service owner."

api_docs/core_http.json

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,9 +3725,9 @@
37253725
{
37263726
"pluginId": "core",
37273727
"scope": "server",
3728-
"docId": "kibCoreHttpPluginApi",
3729-
"section": "def-server.RequestHandlerContextContainer",
3730-
"text": "RequestHandlerContextContainer"
3728+
"docId": "kibCorePluginApi",
3729+
"section": "def-server.IContextContainer",
3730+
"text": "IContextContainer"
37313731
}
37323732
]
37333733
},
@@ -6509,28 +6509,7 @@
65096509
"link": "https://github.com/elastic/kibana/tree/mastersrc/core/server/http/types.ts#L30"
65106510
},
65116511
"signature": [
6512-
"IContextContainer<RequestHandler<unknown, unknown, unknown, RequestHandlerContext, any, { custom: <T extends string | Record<string, any> | Error | { message: string | Error; attributes?: Record<string, any> | undefined; } | Buffer | ",
6513-
"Stream",
6514-
" | undefined>(options: ",
6515-
{
6516-
"pluginId": "core",
6517-
"scope": "server",
6518-
"docId": "kibCorePluginApi",
6519-
"section": "def-server.CustomHttpResponseOptions",
6520-
"text": "CustomHttpResponseOptions"
6521-
},
6522-
"<T>) => ",
6523-
"KibanaResponse",
6524-
"<T>; badRequest: (options?: ",
6525-
{
6526-
"pluginId": "core",
6527-
"scope": "server",
6528-
"docId": "kibCorePluginApi",
6529-
"section": "def-server.ErrorHttpResponseOptions",
6530-
"text": "ErrorHttpResponseOptions"
6531-
},
6532-
") => ",
6533-
"KibanaResponse"
6512+
"IContextContainer"
65346513
],
65356514
"initialIsOpen": false
65366515
},

docs/development/core/server/kibana-plugin-core-server.contextsetup.createcontextcontainer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Creates a new [IContextContainer](./kibana-plugin-core-server.icontextcontainer.
99
<b>Signature:</b>
1010

1111
```typescript
12-
createContextContainer<THandler extends HandlerFunction<any>>(): IContextContainer<THandler>;
12+
createContextContainer(): IContextContainer;
1313
```
1414
<b>Returns:</b>
1515

16-
`IContextContainer<THandler>`
16+
`IContextContainer`
1717

docs/development/core/server/kibana-plugin-core-server.icontextcontainer.createhandler.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ Create a new handler function pre-wired to context for the plugin.
99
<b>Signature:</b>
1010

1111
```typescript
12-
createHandler(pluginOpaqueId: PluginOpaqueId, handler: THandler): (...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>;
12+
createHandler(pluginOpaqueId: PluginOpaqueId, handler: RequestHandler): (...rest: HandlerParameters<RequestHandler>) => ShallowPromise<ReturnType<RequestHandler>>;
1313
```
1414

1515
## Parameters
1616

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

2222
<b>Returns:</b>
2323

24-
`(...rest: HandlerParameters<THandler>) => ShallowPromise<ReturnType<THandler>>`
24+
`(...rest: HandlerParameters<RequestHandler>) => ShallowPromise<ReturnType<RequestHandler>>`
2525

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

docs/development/core/server/kibana-plugin-core-server.icontextcontainer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ An object that handles registration of context providers and configuring handler
99
<b>Signature:</b>
1010

1111
```typescript
12-
export interface IContextContainer<THandler extends RequestHandler>
12+
export interface IContextContainer
1313
```
1414

1515
## Remarks

docs/development/core/server/kibana-plugin-core-server.requesthandlercontextcontainer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ An object that handles registration of http request context providers.
99
<b>Signature:</b>
1010

1111
```typescript
12-
export declare type RequestHandlerContextContainer = IContextContainer<RequestHandler>;
12+
export declare type RequestHandlerContextContainer = IContextContainer;
1313
```

src/core/server/context/container/context.mock.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
import { IContextContainer } from './context';
1010

11-
export type ContextContainerMock = jest.Mocked<IContextContainer<any>>;
11+
export type ContextContainerMock = jest.Mocked<IContextContainer>;
1212

13-
const createContextMock = (mockContext = {}) => {
13+
const createContextMock = (mockContext: any = {}) => {
1414
const contextMock: ContextContainerMock = {
15-
// @ts-expect-error tsc cannot infer ContextName and uses never
15+
// @ts-expect-error since ContextContainerMock cannot infer ContextName and fallsback to never
1616
registerContext: jest.fn(),
1717
createHandler: jest.fn(),
1818
};
1919
contextMock.createHandler.mockImplementation((pluginId, handler) => (...args) =>
20-
handler(mockContext, ...args)
20+
Promise.resolve(handler(mockContext, ...args))
2121
);
2222
return contextMock;
2323
};

src/core/server/context/container/context.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ describe('ContextContainer', () => {
6565
`"Cannot register context for unknown plugin: Symbol(unknown)"`
6666
);
6767
});
68+
69+
it('reports a TS error if returned contract does not satisfy the Context interface', async () => {
70+
const contextContainer = new ContextContainer(plugins, coreId);
71+
await expect(() =>
72+
contextContainer.registerContext<{ ctxFromA: string; core: any }, 'ctxFromA'>(
73+
pluginA,
74+
'ctxFromA',
75+
// @ts-expect-error expected string, returned number
76+
async () => 1
77+
)
78+
).not.toThrow();
79+
});
80+
81+
it('reports a TS error if registers a context for unknown property name', async () => {
82+
const contextContainer = new ContextContainer(plugins, coreId);
83+
await expect(() =>
84+
// @ts-expect-error expects ctxFromB, but given ctxFromC
85+
contextContainer.registerContext<{ ctxFromB: string; core: any }, 'ctxFromC'>(
86+
pluginB,
87+
'ctxFromC',
88+
async () => 1
89+
)
90+
).not.toThrow();
91+
});
6892
});
6993

7094
describe('context building', () => {

0 commit comments

Comments
 (0)