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

Convert uiSettings service to TypeScript #47018

Merged
merged 18 commits into from
Oct 4, 2019

Conversation

mshustov
Copy link
Contributor

@mshustov mshustov commented Oct 1, 2019

Summary

Convert service to TS and declare IUiSettingsService public interface.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@mshustov mshustov added chore Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes v7.5.0 labels Oct 1, 2019
@mshustov mshustov requested a review from a team as a code owner October 1, 2019 09:38
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform

@mshustov mshustov requested a review from a team October 1, 2019 09:39
getAll: <T extends SavedObjectAttribute = any>() => Promise<Record<string, T>>;
getUserProvided: () => Promise<UserProvided>;
setMany: <T extends SavedObjectAttribute = any>(changes: Record<string, T>) => Promise<void>;
set: <T extends SavedObjectAttribute = any>(key: string, value: T) => Promise<void>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

value must satisfy SavedObjectAttribute` constraints. @rudolf is it okay that we cannot set an array of primitives? according to SavedObjects interface

Copy link
Contributor

Choose a reason for hiding this comment

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

I've created #47317 but see #47334 for some of the issues consumers of these types have experienced with the SavedObjectsClient which will be similar for the UiSettingsService.

@@ -233,7 +231,7 @@ describe('ui settings', () => {
});

try {
await uiSettings.setMany(['bar', 'foo']);
await uiSettings.setMany({ baz: 'baz', foo: 'foo' });
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 believe it was used in unexpected way

// mock includeFrozen to return false
mockRequest.getUiSettingsService = () => ({ get: async () => false });
uiSettingsService.get.mockResolvedValue(false);
mockRequest.getUiSettingsService = () => uiSettingsService;
Copy link
Contributor Author

@mshustov mshustov Oct 1, 2019

Choose a reason for hiding this comment

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

had to update tests to fix a type error

@@ -489,7 +487,7 @@ describe('ui settings', () => {
it('pulls user configuration from ES', async () => {
const esDocSource = {};
const { uiSettings, assertGetQuery } = setup({ esDocSource });
await uiSettings.get();
await uiSettings.get('any');
Copy link
Contributor Author

@mshustov mshustov Oct 1, 2019

Choose a reason for hiding this comment

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

key is required argument

}

async getRaw() {
// NOTE: should be a private method
async getRaw(): Promise<UiSettingsRaw> {
Copy link
Contributor Author

@mshustov mshustov Oct 1, 2019

Choose a reason for hiding this comment

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

right now we used them in tests. I'd re-write test and hide them later

get: <T extends SavedObjectAttribute = any>(key: string) => Promise<T>;
getAll: <T extends SavedObjectAttribute = any>() => Promise<Record<string, T>>;
getUserProvided: () => Promise<UserProvided>;
setMany: <T extends SavedObjectAttribute = any>(changes: Record<string, T>) => Promise<void>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can use T extends SavedObjectAttributes = any but not sure we should. I thought it made it harder to read the code.

@mshustov mshustov added the review label Oct 1, 2019
await this._write({ changes });
}

async set(key, value) {
async set<T extends SavedObjectAttribute = any>(key: string, value: T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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


export interface IUiSettingsService {
getDefaults: () => Promise<Record<string, UiSettingsParams>>;
get: <T extends SavedObjectAttribute = any>(key: string) => Promise<T>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

used as

const value = uiSettings.get<string>('key');
// error
const value = uiSettings.get<string []>('key');

Copy link
Member

@sorenlouv sorenlouv left a comment

Choose a reason for hiding this comment

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

APM changes lgtm

* @property {Function} [options.log]
*/
constructor(options) {
export class UiSettingsService implements IUiSettingsService {
Copy link
Contributor Author

@mshustov mshustov Oct 1, 2019

Choose a reason for hiding this comment

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

Shall we rename it to UiSettingsClient? It's not a service but a designated object for the plugins to interact with.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think that makes sense, or even just UISettings?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, we probably need to decide on our convention for capitalization of acronyms. For example we use HttpServiceSetup instead of HTTPServiceSetup in core right now. Should this be UiSettingsClient? I prefer this pattern because then the I-prefix looks a little less strange: IUiSettingsClient

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💔 Build Failed

@mshustov
Copy link
Contributor Author

mshustov commented Oct 1, 2019

retest

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

export async function createOrUpgradeSavedConfig<T extends SavedObjectAttribute = any>(
options: Options
): Promise<Record<string, T> | undefined> {
const { savedObjectsClient, version, buildNum, logWithMetadata, onWriteError } = options;
Copy link
Contributor

Choose a reason for hiding this comment

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

Move object destructure to function arguments?

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'd prefer not to do. there are already too many params to use positional arguments.

@mshustov
Copy link
Contributor Author

mshustov commented Oct 4, 2019

retest

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@mshustov mshustov merged commit d1a99ea into elastic:master Oct 4, 2019
@mshustov mshustov deleted the ts-ui-settings-code branch October 4, 2019 10:48
mshustov added a commit to mshustov/kibana that referenced this pull request Oct 4, 2019
* tsify is_config_version_upgradeable

* tsify get_upgradeable_config

* tsify create_or_upgrade_saved_config

* tsify ui_settings_service

* tsify ui_settings_service_factory

* tsify ui_settings_service_for_request

* declare types on server object

* tsify set route

* tsify set_many route

* tsify get route

* tsify delete route

* tsify logWithMetadata

* improve ui_settings_service typings

* introduce uiService mocks

* remove private methods from public contract

* add types for server.uiSettingsServiceFactory

* rename IUiSettingsService --> IUiSettingsClient
mshustov added a commit that referenced this pull request Oct 4, 2019
* tsify is_config_version_upgradeable

* tsify get_upgradeable_config

* tsify create_or_upgrade_saved_config

* tsify ui_settings_service

* tsify ui_settings_service_factory

* tsify ui_settings_service_for_request

* declare types on server object

* tsify set route

* tsify set_many route

* tsify get route

* tsify delete route

* tsify logWithMetadata

* improve ui_settings_service typings

* introduce uiService mocks

* remove private methods from public contract

* add types for server.uiSettingsServiceFactory

* rename IUiSettingsService --> IUiSettingsClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backported chore Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants