-
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.
extract greeting implementations into separate plugin to showcase acc…
…essing statically
- Loading branch information
1 parent
6b55c41
commit 41f1cc9
Showing
13 changed files
with
308 additions
and
78 deletions.
There are no files selected for viewing
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,23 @@ | ||
## Custom Greetings | ||
|
||
This plugin is an example of adding custom implementations to a registry supplied by another plugin. | ||
|
||
It follows the best practice of also emitting direct accessors to these implementations on this | ||
plugin's start contract. | ||
|
||
This | ||
```ts | ||
const casualGreeter = customGreetings.getCasualGreeter(); | ||
``` | ||
|
||
should be preferred to | ||
|
||
```ts | ||
const casualGreeter = greeting.getGreeter('CASUAL_GREETER'); | ||
``` | ||
|
||
becuase: | ||
- the accessing plugin doesn't need to handle the possibility of `casualGreeter` being undefined | ||
- it's more obvious that the plugin accessing this greeter should list `customGreetings` as a plugin dependency | ||
- if the specific implementation had a specialized type, it can be accessed this way, in lieu of supporting | ||
typescript generics on the generic getter (e.g. `greeting.getGreeter<T>(id)`), which is error prone. |
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,10 @@ | ||
{ | ||
"id": "customGreetings", | ||
"version": "0.0.1", | ||
"kibanaVersion": "kibana", | ||
"configPath": ["custom_greetings"], | ||
"server": false, | ||
"ui": true, | ||
"requiredPlugins": ["greeting"], | ||
"optionalPlugins": [] | ||
} |
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,17 @@ | ||
{ | ||
"name": "custom_greetings", | ||
"version": "1.0.0", | ||
"main": "target/examples/custom_greetings", | ||
"kibana": { | ||
"version": "kibana", | ||
"templateVersion": "1.0.0" | ||
}, | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"kbn": "node ../../scripts/kbn.js", | ||
"build": "rm -rf './target' && tsc" | ||
}, | ||
"devDependencies": { | ||
"typescript": "3.7.2" | ||
} | ||
} |
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,24 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { CustomGreetingsPlugin } from './plugin'; | ||
|
||
export const plugin = () => new CustomGreetingsPlugin(); | ||
|
||
export { CustomGreetingsStart } from './plugin'; |
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,79 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { CoreSetup, Plugin } from 'kibana/public'; | ||
|
||
import { GreetingStart, GreetingSetup, Greeting } from 'examples/greeting/public'; | ||
|
||
interface SetupDependencies { | ||
greeting: GreetingSetup; | ||
} | ||
|
||
interface StartDependencies { | ||
greeting: GreetingStart; | ||
} | ||
|
||
/** | ||
* Expose direct access to specific greeter implementations on the start contract. | ||
* If a plugin knows ahead of time which specific implementation they would like to access | ||
* they should access it directly off this plugin as opposed to retrieving it off the | ||
* generic registry, via `greeting.getGreeter('Casual')`. | ||
*/ | ||
export interface CustomGreetingsStart { | ||
getCasualGreeter: () => Greeting; | ||
getExcitedGreeter: () => Greeting; | ||
getFormalGreeter: () => Greeting; | ||
} | ||
|
||
export class CustomGreetingsPlugin | ||
implements Plugin<void, CustomGreetingsStart, SetupDependencies, StartDependencies> { | ||
private casualGreeterProvider?: () => Greeting; | ||
private excitedGreeterProvider?: () => Greeting; | ||
private formalGreeterProvider?: () => Greeting; | ||
|
||
setup(core: CoreSetup<StartDependencies>, { greeting }: SetupDependencies) { | ||
this.casualGreeterProvider = greeting.registerGreetingDefinition({ | ||
id: 'Casual', | ||
salutation: 'Hey there', | ||
punctuation: '.', | ||
}); | ||
this.excitedGreeterProvider = greeting.registerGreetingDefinition({ | ||
id: 'Excited', | ||
salutation: 'Hi', | ||
punctuation: '!!', | ||
}); | ||
this.formalGreeterProvider = greeting.registerGreetingDefinition({ | ||
id: 'Formal', | ||
salutation: 'Hello ', | ||
punctuation: '.', | ||
}); | ||
} | ||
|
||
start() { | ||
const { casualGreeterProvider, excitedGreeterProvider, formalGreeterProvider } = this; | ||
if (!casualGreeterProvider || !excitedGreeterProvider || !formalGreeterProvider) { | ||
throw new Error('Something unexpected went wrong. Greeters should be defined by now.'); | ||
} | ||
return { | ||
getCasualGreeter: () => casualGreeterProvider(), | ||
getExcitedGreeter: () => casualGreeterProvider(), | ||
getFormalGreeter: () => casualGreeterProvider(), | ||
}; | ||
} | ||
} |
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,35 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { GreetingStart, Greeting } from '../../greeting/public'; | ||
|
||
export interface Services { | ||
greetWithGreeter: (greeting: Greeting, name: string) => void; | ||
getGreeters: () => Greeting[]; | ||
} | ||
|
||
/** | ||
* Rather than pass down depencies directly, we add some indirection with this services file, to help decouple and | ||
* buffer this plugin from any changes in dependency contracts. | ||
* @param dependencies | ||
*/ | ||
export const getServices = (dependencies: { greetingServices: GreetingStart }): Services => ({ | ||
greetWithGreeter: (greeting: Greeting, name: string) => greeting.greetMe(name), | ||
getGreeters: () => dependencies.greetingServices.getGreeters(), | ||
}); |
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,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./target", | ||
"skipLibCheck": true | ||
}, | ||
"include": [ | ||
"index.ts", | ||
"public/**/*.ts", | ||
"public/**/*.tsx", | ||
"../../typings/**/*", | ||
], | ||
"exclude": [] | ||
} |
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
Oops, something went wrong.