Skip to content

Commit

Permalink
refactor and improve typing
Browse files Browse the repository at this point in the history
Signed-off-by: Sirazh Gabdullin <sirazh.gabdullin@nu.edu.kz>
  • Loading branch information
curq committed Jul 26, 2023
1 parent 0516224 commit ce40d95
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {

import { populateContext } from '../../autocomplete/engine';
import { PartialAutoCompleteContext } from '../components/autocomplete_component';
import { ComponentFactory, ParametrizedComponentFactories } from '../../osd';
import { ComponentFactory, ParametrizedComponentFactories } from '../types';

describe('Url autocomplete', () => {
function patternsTest(
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/console/public/lib/autocomplete/body_completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
ConditionalProxy,
GlobalOnlyComponent,
} from './components';
import { ParametrizedComponentFactories } from '../osd/osd';
import { ParametrizedComponentFactories } from './types';
import { AutoCompleteContext, Template, Term } from './types';
import { CoreEditor, Token } from '../../types';
import { MatchResult } from './components/autocomplete_component';
Expand Down Expand Up @@ -265,7 +265,7 @@ function compileParametrizedValue(
if (!componentFactory) {
throw new Error("no factory found for '" + value + "'");
}
let component = componentFactory(value, null, template);
let component = componentFactory(value, null, !!template);
if (!_.isUndefined(template)) {
component = wrapComponentWithDefaults(component, { template });
}
Expand Down Expand Up @@ -345,7 +345,7 @@ export function globalsOnlyAutocompleteComponents() {
export function compileBodyDescription(
endpointId: string,
description: Description,
parametrizedComponentFactories: ParametrizedComponentFactories
parametrizedComponentFactories?: ParametrizedComponentFactories
) {
return compileDescription(
description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {

import { FullRequestComponent } from './full_request_component';
import { Endpoint, UrlComponent, UrlObjectComponent } from '../types';
import { ComponentFactory, ParametrizedComponentFactories } from '../../osd/osd';
import { ComponentFactory, ParametrizedComponentFactories } from '../types';

interface MethodData {
rootComponent: SharedComponent;
Expand Down
44 changes: 44 additions & 0 deletions src/plugins/console/public/lib/autocomplete/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

import { Token, Position, Range } from '../../types';
import { Description } from './body_completer';
import {
FieldAutocompleteComponent,
IdAutocompleteComponent,
IndexAutocompleteComponent,
ListComponent,
TemplateAutocompleteComponent,
TypeAutocompleteComponent,
UsernameAutocompleteComponent,
} from './components';
import { SharedComponent } from './components/shared_component';

export interface UrlObjectComponent {
Expand Down Expand Up @@ -75,3 +84,38 @@ export interface TermObject {
}

export type Term = string | TermObject;

export type IdAutocompleteComponentFactory = (
name: string,
parent: SharedComponent,
multiValued?: boolean
) => IdAutocompleteComponent;

export type ComponentFactory = (
name: string,
parent: SharedComponent | null,
multiValued?: boolean
) => SharedComponent;

export interface ParametrizedComponentFactories {
getComponent: (
name: string,
parent?: SharedComponent | boolean,
provideDefault?: boolean
) => ComponentFactory | undefined;
index?: (name: string, parent: ListComponent) => IndexAutocompleteComponent | undefined;
indices?: (name: string, parent: ListComponent) => IndexAutocompleteComponent | undefined;
type?: (name: string, parent: ListComponent) => TypeAutocompleteComponent;
types?: (name: string, parent: ListComponent) => TypeAutocompleteComponent;
id?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
transform_id?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
username?: (name: string, parent: ListComponent) => UsernameAutocompleteComponent;
user?: (name: string, parent: ListComponent) => UsernameAutocompleteComponent;
template?: (name: string, parent: ListComponent) => TemplateAutocompleteComponent;
task_id?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
ids?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
fields?: (name: string, parent: ListComponent) => FieldAutocompleteComponent;
field?: (name: string, parent: ListComponent) => FieldAutocompleteComponent;
nodes?: (name: string, parent: SharedComponent) => ListComponent;
node?: (name: string, parent: SharedComponent) => ListComponent;
}
2 changes: 1 addition & 1 deletion src/plugins/console/public/lib/curl_parsing/curl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function parseCURL(text: string) {
if (lines.length === 0) {
return false;
}
line = lines.shift()?.replace(/[\r\n]+/g, '\n') + '\n';
line = lines.shift()!.replace(/[\r\n]+/g, '\n') + '\n';
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/console/public/lib/osd/__tests__/kb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Knowledge base', () => {
tokenPath,
context,
null,
expectedContext.autoCompleteSet,
!!expectedContext.autoCompleteSet,
osd.getTopLevelUrlCompleteComponents('GET')
);

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/console/public/lib/osd/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
compileBodyDescription,
} from '../autocomplete/body_completer';
import { Endpoint } from '../autocomplete/types';
import { ParametrizedComponentFactories } from './osd';
import { ParametrizedComponentFactories } from '../autocomplete/types';

/**
*
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Api {
return result;
}

addEndpointDescription(endpoint: string, description?: Record<string, any>) {
addEndpointDescription(endpoint: string, description: Record<string, any>) {
const copiedDescription: Record<string, any> = {};
_.assign(copiedDescription, description || {});
_.defaults(copiedDescription, {
Expand Down
47 changes: 8 additions & 39 deletions src/plugins/console/public/lib/osd/osd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,14 @@ import {
ListComponent,
TemplateAutocompleteComponent,
UsernameAutocompleteComponent,
SharedComponent,
} from '../autocomplete/components';

import { Api } from './api';

export type IdAutocompleteComponentFactory = (
name: string,
parent: SharedComponent,
multiValued?: boolean
) => IdAutocompleteComponent;

export type ComponentFactory = (
name: string,
parent: SharedComponent | null,
multiValued?: boolean | unknown
) => SharedComponent;

export interface ParametrizedComponentFactories {
getComponent: (
name: string,
parent?: SharedComponent | boolean,
provideDefault?: boolean
) => ComponentFactory;
index?: (name: string, parent: ListComponent) => IndexAutocompleteComponent | undefined;
indices?: (name: string, parent: ListComponent) => IndexAutocompleteComponent | undefined;
type?: (name: string, parent: ListComponent) => TypeAutocompleteComponent;
types?: (name: string, parent: ListComponent) => TypeAutocompleteComponent;
id?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
transform_id?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
username?: (name: string, parent: ListComponent) => UsernameAutocompleteComponent;
user?: (name: string, parent: ListComponent) => UsernameAutocompleteComponent;
template?: (name: string, parent: ListComponent) => TemplateAutocompleteComponent;
task_id?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
ids?: (name: string, parent: SharedComponent) => IdAutocompleteComponent;
fields?: (name: string, parent: ListComponent) => FieldAutocompleteComponent;
field?: (name: string, parent: ListComponent) => FieldAutocompleteComponent;
nodes?: (name: string, parent: SharedComponent) => ListComponent;
node?: (name: string, parent: SharedComponent) => ListComponent;
}
import {
ComponentFactory,
IdAutocompleteComponentFactory,
ParametrizedComponentFactories,
} from '../autocomplete/types';

let ACTIVE_API = new Api();
const isNotAnIndexName = (name: string) => name[0] === '_' && name !== '_all';
Expand All @@ -86,10 +55,10 @@ const idAutocompleteComponentFactory: IdAutocompleteComponentFactory = (name, pa
};
const parametrizedComponentFactories: ParametrizedComponentFactories = {
getComponent(name, parent, provideDefault) {
if ((this as any)[name]) {
return (this as any)[name];
if (this[name as keyof ParametrizedComponentFactories]) {
return this[name as keyof ParametrizedComponentFactories] as ComponentFactory;
} else if (provideDefault) {
return idAutocompleteComponentFactory;
return idAutocompleteComponentFactory as ComponentFactory;

Check warning on line 61 in src/plugins/console/public/lib/osd/osd.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/console/public/lib/osd/osd.ts#L61

Added line #L61 was not covered by tests
}
},
index(name, parent) {
Expand Down

0 comments on commit ce40d95

Please sign in to comment.