From 26fc1469857092d922b60dbe0cd9555d9896a176 Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Tue, 27 Jun 2023 18:22:11 +0200 Subject: [PATCH] [Console] Implement url params conversion logic --- .../src/generate_console_definitions.ts | 8 +- .../src/generate_query_params.ts | 81 +++++++++++++------ .../types/autocomplete_definition_types.ts | 4 +- .../src/utils.ts | 17 ++++ 4 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 packages/kbn-generate-console-definitions/src/utils.ts diff --git a/packages/kbn-generate-console-definitions/src/generate_console_definitions.ts b/packages/kbn-generate-console-definitions/src/generate_console_definitions.ts index 2eaf066dd91a6..1d4918f44c6b1 100644 --- a/packages/kbn-generate-console-definitions/src/generate_console_definitions.ts +++ b/packages/kbn-generate-console-definitions/src/generate_console_definitions.ts @@ -16,6 +16,7 @@ import type { AutocompleteUrlParams, SpecificationTypes, } from './types'; +import { findTypeDefinition } from './utils'; const generateMethods = (endpoint: SpecificationTypes.Endpoint): string[] => { // this array consists of arrays of strings @@ -49,9 +50,7 @@ const generateParams = ( if (!request) { return; } - const requestType = schema.types.find( - ({ name: { name, namespace } }) => name === request.name && namespace === request.namespace - ); + const requestType = findTypeDefinition(schema, request); if (!requestType) { return; } @@ -87,11 +86,12 @@ const generateDefinition = ( const methods = generateMethods(endpoint); const patterns = generatePatterns(endpoint); const documentation = generateDocumentation(endpoint); - let definition: AutocompleteDefinition = { methods, patterns, documentation }; + let definition: AutocompleteDefinition = {}; const params = generateParams(endpoint, schema); if (params) { definition = addParams(definition, params); } + definition = { ...definition, methods, patterns, documentation }; return definition; }; diff --git a/packages/kbn-generate-console-definitions/src/generate_query_params.ts b/packages/kbn-generate-console-definitions/src/generate_query_params.ts index 91123913d17d8..8859dd9c2d104 100644 --- a/packages/kbn-generate-console-definitions/src/generate_query_params.ts +++ b/packages/kbn-generate-console-definitions/src/generate_query_params.ts @@ -8,6 +8,7 @@ import { UrlParamValue } from './types/autocomplete_definition_types'; import type { AutocompleteUrlParams, SpecificationTypes } from './types'; +import { findTypeDefinition } from './utils'; export const generateQueryParams = ( requestType: SpecificationTypes.Request, schema: SpecificationTypes.Model @@ -23,24 +24,24 @@ export const generateQueryParams = ( const behaviorType = foundBehavior as SpecificationTypes.Interface; // if there are any properties in the behavior type, iterate over each and add it to url params const { properties } = behaviorType; - urlParams = convertProperties(properties, urlParams); + urlParams = convertProperties(properties, urlParams, schema); } } } // iterate over properties in query - urlParams = convertProperties(query, urlParams); + urlParams = convertProperties(query, urlParams, schema); return urlParams; }; const convertInstanceOf = ( type: SpecificationTypes.InstanceOf, - serverDefault: SpecificationTypes.Property['serverDefault'] -): UrlParamValue => { - const { - type: { name: propertyName }, - } = type; + serverDefault: SpecificationTypes.Property['serverDefault'], + schema: SpecificationTypes.Model +): UrlParamValue | undefined => { + const { type: typeName } = type; + const { name: propertyName } = typeName; // text property if (propertyName === 'string') { // add default value if any @@ -58,33 +59,65 @@ const convertInstanceOf = ( // names else if (propertyName === 'Names') { return []; + } else { + // if it's a defined type, try to convert it + const definedType = findTypeDefinition(schema, typeName); + if (definedType) { + // if it's enum + if (definedType.kind === 'enum') { + return convertEnum(definedType as SpecificationTypes.Enum); + } else if (definedType.kind === 'type_alias') { + const aliasValueOf = definedType.type; + return convertValueOf(aliasValueOf, serverDefault, schema); + } + } } - return ''; }; const convertProperties = ( properties: SpecificationTypes.Property[], - urlParams: AutocompleteUrlParams + urlParams: AutocompleteUrlParams, + schema: SpecificationTypes.Model ): AutocompleteUrlParams => { for (const property of properties) { const { name, serverDefault, type } = property; - const { kind } = type; - if (kind === 'instance_of') { - urlParams[name] = convertInstanceOf(type, serverDefault); - } else if (kind === 'union_of') { - const { items } = type; - const itemValues = new Set(); - for (const item of items) { - if (item.kind === 'instance_of') { - itemValues.add(convertInstanceOf(item, serverDefault)); - } else if (item.kind === 'array_of') { - if (item.value.kind === 'instance_of') { - itemValues.add(convertInstanceOf(item.value, serverDefault)); - } + const convertedValue = convertValueOf(type, serverDefault, schema); + urlParams[name] = convertedValue ?? ''; + } + return urlParams; +}; + +const convertValueOf = ( + valueOf: SpecificationTypes.ValueOf, + serverDefault: SpecificationTypes.Property['serverDefault'], + schema: SpecificationTypes.Model +): UrlParamValue | undefined => { + const { kind } = valueOf; + if (kind === 'instance_of') { + return convertInstanceOf(valueOf, serverDefault, schema); + } else if (kind === 'union_of') { + const { items } = valueOf; + const itemValues = new Set(); + for (const item of items) { + if (item.kind === 'instance_of') { + const convertedValue = convertInstanceOf(item, serverDefault, schema); + if (convertedValue instanceof Array) { + convertedValue.forEach((v) => itemValues.add(v)); + } else itemValues.add(convertedValue); + } else if (item.kind === 'array_of') { + if (item.value.kind === 'instance_of') { + const convertedValue = convertInstanceOf(item.value, serverDefault, schema); + if (convertedValue instanceof Array) { + convertedValue.forEach((v) => itemValues.add(v)); + } else itemValues.add(convertedValue); } } - urlParams[name] = itemValues as unknown as UrlParamValue; } + return [...itemValues] as UrlParamValue; } - return urlParams; +}; + +const convertEnum = (enumDefinition: SpecificationTypes.Enum): UrlParamValue => { + const { members } = enumDefinition; + return members.map((member) => member.name); }; diff --git a/packages/kbn-generate-console-definitions/src/types/autocomplete_definition_types.ts b/packages/kbn-generate-console-definitions/src/types/autocomplete_definition_types.ts index 5f3dff755abcf..edbb9bd74d9a8 100644 --- a/packages/kbn-generate-console-definitions/src/types/autocomplete_definition_types.ts +++ b/packages/kbn-generate-console-definitions/src/types/autocomplete_definition_types.ts @@ -16,8 +16,8 @@ export interface AutocompleteBodyParams { export interface AutocompleteDefinition { documentation?: string; - methods: string[]; - patterns: string[]; + methods?: string[]; + patterns?: string[]; url_params?: AutocompleteUrlParams; data_autocomplete_rules?: AutocompleteBodyParams; } diff --git a/packages/kbn-generate-console-definitions/src/utils.ts b/packages/kbn-generate-console-definitions/src/utils.ts new file mode 100644 index 0000000000000..29c24e63fa58f --- /dev/null +++ b/packages/kbn-generate-console-definitions/src/utils.ts @@ -0,0 +1,17 @@ +/* + * 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 type { SpecificationTypes } from './types'; +export const findTypeDefinition = ( + schema: SpecificationTypes.Model, + typeName: SpecificationTypes.TypeName +): SpecificationTypes.TypeDefinition | undefined => { + return schema.types.find( + (type) => type.name.name === typeName.name && type.name.namespace === typeName.namespace + ); +};