-
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.
Allow nested declaration for
exposeToBrowser
(#128864)
* Allow nested declaration for `exposeToBrowser` * update generated doc * add utest
- Loading branch information
1 parent
3f4aa49
commit dd0a190
Showing
11 changed files
with
341 additions
and
24 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...development/core/server/kibana-plugin-core-server.exposedtobrowserdescriptor.md
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,16 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [ExposedToBrowserDescriptor](./kibana-plugin-core-server.exposedtobrowserdescriptor.md) | ||
|
||
## ExposedToBrowserDescriptor type | ||
|
||
Type defining the list of configuration properties that will be exposed on the client-side Object properties can either be fully exposed | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type ExposedToBrowserDescriptor<T> = { | ||
[Key in keyof T]?: T[Key] extends Maybe<any[]> ? boolean : T[Key] extends Maybe<object> ? // can be nested for objects | ||
ExposedToBrowserDescriptor<T[Key]> | boolean : boolean; | ||
}; | ||
``` |
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,162 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ExposedToBrowserDescriptor } from './types'; | ||
import { createBrowserConfig } from './create_browser_config'; | ||
|
||
describe('createBrowserConfig', () => { | ||
it('picks nothing by default', () => { | ||
const config = { | ||
foo: 'bar', | ||
nested: { | ||
str: 'string', | ||
num: 42, | ||
}, | ||
}; | ||
const descriptor: ExposedToBrowserDescriptor<typeof config> = {}; | ||
|
||
const browserConfig = createBrowserConfig(config, descriptor); | ||
|
||
expect(browserConfig).toEqual({}); | ||
}); | ||
|
||
it('picks all the nested properties when using `true`', () => { | ||
const config = { | ||
foo: 'bar', | ||
nested: { | ||
str: 'string', | ||
num: 42, | ||
}, | ||
}; | ||
|
||
const descriptor: ExposedToBrowserDescriptor<typeof config> = { | ||
foo: true, | ||
nested: true, | ||
}; | ||
|
||
const browserConfig = createBrowserConfig(config, descriptor); | ||
|
||
expect(browserConfig).toEqual({ | ||
foo: 'bar', | ||
nested: { | ||
str: 'string', | ||
num: 42, | ||
}, | ||
}); | ||
}); | ||
|
||
it('picks specific nested properties when using a nested declaration', () => { | ||
const config = { | ||
foo: 'bar', | ||
nested: { | ||
str: 'string', | ||
num: 42, | ||
}, | ||
}; | ||
|
||
const descriptor: ExposedToBrowserDescriptor<typeof config> = { | ||
foo: true, | ||
nested: { | ||
str: true, | ||
num: false, | ||
}, | ||
}; | ||
|
||
const browserConfig = createBrowserConfig(config, descriptor); | ||
|
||
expect(browserConfig).toEqual({ | ||
foo: 'bar', | ||
nested: { | ||
str: 'string', | ||
}, | ||
}); | ||
}); | ||
|
||
it('accepts deeply nested structures', () => { | ||
const config = { | ||
foo: 'bar', | ||
deeply: { | ||
str: 'string', | ||
nested: { | ||
hello: 'dolly', | ||
structure: { | ||
propA: 'propA', | ||
propB: 'propB', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const descriptor: ExposedToBrowserDescriptor<typeof config> = { | ||
foo: false, | ||
deeply: { | ||
str: false, | ||
nested: { | ||
hello: true, | ||
structure: { | ||
propA: true, | ||
propB: false, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const browserConfig = createBrowserConfig(config, descriptor); | ||
|
||
expect(browserConfig).toEqual({ | ||
deeply: { | ||
nested: { | ||
hello: 'dolly', | ||
structure: { | ||
propA: 'propA', | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('only includes leaf properties that are `true` when in nested structures', () => { | ||
const config = { | ||
foo: 'bar', | ||
deeply: { | ||
str: 'string', | ||
nested: { | ||
hello: 'dolly', | ||
structure: { | ||
propA: 'propA', | ||
propB: 'propB', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const descriptor: ExposedToBrowserDescriptor<typeof config> = { | ||
deeply: { | ||
nested: { | ||
hello: true, | ||
structure: { | ||
propA: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const browserConfig = createBrowserConfig(config, descriptor); | ||
|
||
expect(browserConfig).toEqual({ | ||
deeply: { | ||
nested: { | ||
hello: 'dolly', | ||
structure: { | ||
propA: 'propA', | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
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,32 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ExposedToBrowserDescriptor } from './types'; | ||
|
||
export const createBrowserConfig = <T = unknown>( | ||
config: T, | ||
descriptor: ExposedToBrowserDescriptor<T> | ||
): unknown => { | ||
return recursiveCreateConfig(config, descriptor); | ||
}; | ||
|
||
const recursiveCreateConfig = <T = unknown>( | ||
config: T, | ||
descriptor: ExposedToBrowserDescriptor<T> = {} | ||
): unknown => { | ||
return Object.entries(config || {}).reduce((browserConfig, [key, value]) => { | ||
const exposedConfig = descriptor[key as keyof ExposedToBrowserDescriptor<T>]; | ||
if (exposedConfig && typeof exposedConfig === 'object') { | ||
browserConfig[key] = recursiveCreateConfig(value, exposedConfig); | ||
} | ||
if (exposedConfig === true) { | ||
browserConfig[key] = value; | ||
} | ||
return browserConfig; | ||
}, {} as Record<string, unknown>); | ||
}; |
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,90 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ExposedToBrowserDescriptor } from './types'; | ||
|
||
describe('ExposedToBrowserDescriptor', () => { | ||
interface ConfigType { | ||
str: string; | ||
array: number[]; | ||
obj: { | ||
sub1: string; | ||
sub2: number; | ||
}; | ||
deep: { | ||
foo: number; | ||
nested: { | ||
str: string; | ||
arr: number[]; | ||
}; | ||
}; | ||
} | ||
|
||
it('allows to use recursion on objects', () => { | ||
const exposeToBrowser: ExposedToBrowserDescriptor<ConfigType> = { | ||
obj: { | ||
sub1: true, | ||
}, | ||
}; | ||
expect(exposeToBrowser).toBeDefined(); | ||
}); | ||
|
||
it('allows to use recursion at multiple levels', () => { | ||
const exposeToBrowser: ExposedToBrowserDescriptor<ConfigType> = { | ||
deep: { | ||
foo: true, | ||
nested: { | ||
str: true, | ||
}, | ||
}, | ||
}; | ||
expect(exposeToBrowser).toBeDefined(); | ||
}); | ||
|
||
it('does not allow to use recursion on arrays', () => { | ||
const exposeToBrowser: ExposedToBrowserDescriptor<ConfigType> = { | ||
// @ts-expect-error Type '{ 0: true; }' is not assignable to type 'boolean | undefined'. | ||
array: { | ||
0: true, | ||
}, | ||
}; | ||
expect(exposeToBrowser).toBeDefined(); | ||
}); | ||
|
||
it('does not allow to use recursion on arrays at lower levels', () => { | ||
const exposeToBrowser: ExposedToBrowserDescriptor<ConfigType> = { | ||
deep: { | ||
nested: { | ||
// @ts-expect-error Type '{ 0: true; }' is not assignable to type 'boolean | undefined'. | ||
arr: { | ||
0: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
expect(exposeToBrowser).toBeDefined(); | ||
}); | ||
|
||
it('allows to specify all the properties', () => { | ||
const exposeToBrowser: ExposedToBrowserDescriptor<ConfigType> = { | ||
str: true, | ||
array: false, | ||
obj: { | ||
sub1: true, | ||
}, | ||
deep: { | ||
foo: true, | ||
nested: { | ||
arr: false, | ||
str: true, | ||
}, | ||
}, | ||
}; | ||
expect(exposeToBrowser).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.