Skip to content

Commit

Permalink
[Console] Implement url params conversion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliacech committed Jun 27, 2023
1 parent e4b864c commit 26fc146
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 17 additions & 0 deletions packages/kbn-generate-console-definitions/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
);
};

0 comments on commit 26fc146

Please sign in to comment.