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

Expose whitelisted config values to client-side plugin #50641

Merged
merged 20 commits into from Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/plugins/testbed/public/index.ts
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

import { PluginInitializer } from 'kibana/public';
import { PluginInitializer, PluginInitializerContext } from 'kibana/public';
import { TestbedPlugin, TestbedPluginSetup, TestbedPluginStart } from './plugin';

export const plugin: PluginInitializer<TestbedPluginSetup, TestbedPluginStart> = () =>
new TestbedPlugin();
export const plugin: PluginInitializer<TestbedPluginSetup, TestbedPluginStart> = (
initializerContext: PluginInitializerContext
) => new TestbedPlugin(initializerContext);
18 changes: 15 additions & 3 deletions src/plugins/testbed/public/plugin.ts
Expand Up @@ -17,12 +17,24 @@
* under the License.
*/

import { Plugin, CoreSetup } from 'kibana/public';
import { take } from 'rxjs/operators';
import { Plugin, CoreSetup, PluginInitializerContext } from 'kibana/public';

interface ConfigType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for simplicity.
optional: We can add a test that server side types can be reused.
server/config.ts

import { PluginConfigDescriptor } from 'kibana/server';

import { schema, TypeOf } from '@kbn/config-schema';

const configSchema = schema.object({
  secret: schema.string({ defaultValue: 'Not really a secret :/' }),
  uiProp: schema.string({ defaultValue: 'Accessible from client' }),
});

export const config: PluginConfigDescriptor<ConfigType> = {
  exposeToBrowser: ['uiProp'],
  schema: configSchema,
};

export type ConfigType = TypeOf<typeof configSchema>;
export type PublicConfigType = Pick<ConfigType, 'uiProp'>;

server/index.ts

import { config, ConfigType } from './config';
export { config };

public/plugin.ts

import { PublicConfigType } from '../server/config'; // or re-export it from /server/types.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great and ensure the PublicConfigType is correctly typed. Only question I have is regarding server-to-client imports: from what I understood, we needs to be extra careful on these imports to avoid polluting the client bundles with imports dependencies, so what should be the recommended way to do this? creates a types.ts files at the server plugin root folder level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creates a types.ts files at the server plugin root folder level?

sounds reasonable. @kbn/config-schema is used in node env., so we cannot recommend declaring types under public or common.

uiProp: string;
}

export class TestbedPlugin implements Plugin<TestbedPluginSetup, TestbedPluginStart> {
public setup(core: CoreSetup, deps: {}) {
constructor(private readonly initializerContext: PluginInitializerContext) {}

public async setup(core: CoreSetup, deps: {}) {
const config = await this.initializerContext.config
.create<ConfigType>()
.pipe(take(1))
.toPromise();

// eslint-disable-next-line no-console
console.log(`Testbed plugin set up`);
console.log(`Testbed plugin set up. uiProp: '${config.uiProp}'`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example usage in the testbed plugin. The commit is isolated, tell me if I should remove it.

return {
foo: 'bar',
};
Expand Down
25 changes: 18 additions & 7 deletions src/plugins/testbed/server/index.ts
Expand Up @@ -20,15 +20,26 @@
import { map, mergeMap } from 'rxjs/operators';
import { schema, TypeOf } from '@kbn/config-schema';

import { CoreSetup, CoreStart, Logger, PluginInitializerContext, PluginName } from 'kibana/server';
import {
CoreSetup,
CoreStart,
Logger,
PluginInitializerContext,
PluginConfigDescriptor,
PluginName,
} from 'kibana/server';

export const config = {
schema: schema.object({
secret: schema.string({ defaultValue: 'Not really a secret :/' }),
}),
};
const configSchema = schema.object({
secret: schema.string({ defaultValue: 'Not really a secret :/' }),
uiProp: schema.string({ defaultValue: 'Accessible from client' }),
});

type ConfigType = TypeOf<typeof configSchema>;

type ConfigType = TypeOf<typeof config.schema>;
export const config: PluginConfigDescriptor<ConfigType> = {
exposeToBrowser: ['uiProp'],
schema: configSchema,
};

class Plugin {
private readonly log: Logger;
Expand Down