load js-yaml lazily (#79092)

This commit is contained in:
Mikhail Shustov 2020-10-04 22:10:19 +03:00 committed by GitHub
parent acedb35033
commit 5cc945f9e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 13 deletions

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import _ from 'lodash';
import { get } from 'lodash';
import React from 'react';
import { EuiLink } from '@elastic/eui';
@ -31,7 +31,7 @@ export const ConnectedLinkComponent = ({
}
// Shorthand for pathname
const pathname = path || _.get(props.to, 'pathname') || location.pathname;
const pathname = path || get(props.to, 'pathname') || location.pathname;
return (
<Link

View file

@ -20,7 +20,7 @@ describe('Tags Client Domain Lib', () => {
});
it('should use helper function to convert users yaml in tag to config object', async () => {
const convertedBlocks = lib.userConfigsToJson([
const convertedBlocks = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
@ -42,7 +42,7 @@ describe('Tags Client Domain Lib', () => {
});
it('should use helper function to convert user config to json with undefined `other`', async () => {
const convertedTag = lib.userConfigsToJson([
const convertedTag = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
@ -61,7 +61,7 @@ describe('Tags Client Domain Lib', () => {
});
it('should use helper function to convert users yaml in tag to config object, where empty other leads to no other fields saved', async () => {
const convertedTag = lib.userConfigsToJson([
const convertedTag = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
@ -83,7 +83,7 @@ describe('Tags Client Domain Lib', () => {
});
it('should convert tokenized fields to JSON', async () => {
const convertedTag = lib.userConfigsToJson([
const convertedTag = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
@ -106,7 +106,7 @@ describe('Tags Client Domain Lib', () => {
});
it('should use helper function to convert config object to users yaml', async () => {
const convertedTag = lib.jsonConfigToUserYaml([
const convertedTag = await lib.jsonConfigToUserYaml([
{
id: 'foo',
tag: 'basic',
@ -127,7 +127,7 @@ describe('Tags Client Domain Lib', () => {
});
it('should use helper function to convert config object to users yaml with empty `other`', async () => {
const convertedTag = lib.jsonConfigToUserYaml([
const convertedTag = await lib.jsonConfigToUserYaml([
{
id: 'foo',
tag: 'basic',

View file

@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/
import yaml from 'js-yaml';
import { set } from '@elastic/safer-lodash-set';
import { get, has, omit } from 'lodash';
import { ConfigBlockSchema, ConfigurationBlock } from '../../common/domain_types';
@ -19,16 +18,17 @@ export class ConfigBlocksLib {
) {}
public upsert = async (blocks: ConfigurationBlock[]) => {
return await this.adapter.upsert(this.userConfigsToJson(blocks));
return await this.adapter.upsert(await this.userConfigsToJson(blocks));
};
public getForTags = async (tagIds: string[], page: number) => {
const result = await this.adapter.getForTags(tagIds, page);
result.list = this.jsonConfigToUserYaml(result.list);
result.list = await this.jsonConfigToUserYaml(result.list);
return result;
};
public jsonConfigToUserYaml(blocks: ConfigurationBlock[]): ConfigurationBlock[] {
public async jsonConfigToUserYaml(blocks: ConfigurationBlock[]): Promise<ConfigurationBlock[]> {
const yaml = await import('js-yaml');
// configuration_blocks yaml, JS cant read YAML so we parse it into JS,
// because beats flattens all fields, and we need more structure.
// we take tagConfigs, grab the config that applies here, render what we can into
@ -73,7 +73,8 @@ export class ConfigBlocksLib {
});
}
public userConfigsToJson(blocks: ConfigurationBlock[]): ConfigurationBlock[] {
public async userConfigsToJson(blocks: ConfigurationBlock[]): Promise<ConfigurationBlock[]> {
const yaml = await import('js-yaml');
// configurations is the JS representation of the config yaml,
// so here we take that JS and convert it into a YAML string.
// we do so while also flattening "other" into the flat yaml beats expect