From 8975200f582942f4110f1e71230e4b921b54fe11 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 26 Jul 2023 16:25:10 +0000 Subject: [PATCH] [Console] Convert all non-autocomplete lib files to typescript (#4150) * Convert non-autocomplete part to TS Signed-off-by: Sirazh Gabdullin * Update CHANGELOG.md Signed-off-by: Sirazh Gabdullin * refactor and improve typing Signed-off-by: Sirazh Gabdullin --------- Signed-off-by: Sirazh Gabdullin (cherry picked from commit 5c2b22b1dc3e7474afd006885b7a17ef1170e315) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- .../__jest__/url_autocomplete.test.ts | 2 +- .../public/lib/autocomplete/body_completer.ts | 6 +- .../components/url_pattern_matcher.ts | 2 +- .../console/public/lib/autocomplete/types.ts | 44 +++++++++ ...l_parsing.test.js => curl_parsing.test.ts} | 8 +- .../lib/curl_parsing/{curl.js => curl.ts} | 30 +++--- ...tent_type.test.js => content_type.test.ts} | 0 .../osd/__tests__/{kb.test.js => kb.test.ts} | 93 ++++++++++--------- .../console/public/lib/osd/{api.js => api.ts} | 69 ++++++++------ .../public/lib/osd/{index.js => index.ts} | 0 .../console/public/lib/osd/{osd.js => osd.ts} | 77 ++++++++------- .../{utils.test.js => utils.test.ts} | 0 typings/index.d.ts | 6 ++ 13 files changed, 205 insertions(+), 132 deletions(-) rename src/plugins/console/public/lib/curl_parsing/__tests__/{curl_parsing.test.js => curl_parsing.test.ts} (91%) rename src/plugins/console/public/lib/curl_parsing/{curl.js => curl.ts} (89%) rename src/plugins/console/public/lib/opensearch/__tests__/{content_type.test.js => content_type.test.ts} (100%) rename src/plugins/console/public/lib/osd/__tests__/{kb.test.js => kb.test.ts} (72%) rename src/plugins/console/public/lib/osd/{api.js => api.ts} (63%) rename src/plugins/console/public/lib/osd/{index.js => index.ts} (100%) rename src/plugins/console/public/lib/osd/{osd.js => osd.ts} (74%) rename src/plugins/console/public/lib/utils/__tests__/{utils.test.js => utils.test.ts} (100%) diff --git a/src/plugins/console/public/lib/autocomplete/__jest__/url_autocomplete.test.ts b/src/plugins/console/public/lib/autocomplete/__jest__/url_autocomplete.test.ts index c8e4221f234d..cf39f0fb039f 100644 --- a/src/plugins/console/public/lib/autocomplete/__jest__/url_autocomplete.test.ts +++ b/src/plugins/console/public/lib/autocomplete/__jest__/url_autocomplete.test.ts @@ -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( diff --git a/src/plugins/console/public/lib/autocomplete/body_completer.ts b/src/plugins/console/public/lib/autocomplete/body_completer.ts index 62a473034613..66407864b58e 100644 --- a/src/plugins/console/public/lib/autocomplete/body_completer.ts +++ b/src/plugins/console/public/lib/autocomplete/body_completer.ts @@ -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'; @@ -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 }); } @@ -345,7 +345,7 @@ export function globalsOnlyAutocompleteComponents() { export function compileBodyDescription( endpointId: string, description: Description, - parametrizedComponentFactories: ParametrizedComponentFactories + parametrizedComponentFactories?: ParametrizedComponentFactories ) { return compileDescription( description, diff --git a/src/plugins/console/public/lib/autocomplete/components/url_pattern_matcher.ts b/src/plugins/console/public/lib/autocomplete/components/url_pattern_matcher.ts index 63d4daf751b8..f00280e3925c 100644 --- a/src/plugins/console/public/lib/autocomplete/components/url_pattern_matcher.ts +++ b/src/plugins/console/public/lib/autocomplete/components/url_pattern_matcher.ts @@ -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; diff --git a/src/plugins/console/public/lib/autocomplete/types.ts b/src/plugins/console/public/lib/autocomplete/types.ts index ce2af144dba0..3c15f0192fca 100644 --- a/src/plugins/console/public/lib/autocomplete/types.ts +++ b/src/plugins/console/public/lib/autocomplete/types.ts @@ -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 { @@ -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; +} diff --git a/src/plugins/console/public/lib/curl_parsing/__tests__/curl_parsing.test.js b/src/plugins/console/public/lib/curl_parsing/__tests__/curl_parsing.test.ts similarity index 91% rename from src/plugins/console/public/lib/curl_parsing/__tests__/curl_parsing.test.js rename to src/plugins/console/public/lib/curl_parsing/__tests__/curl_parsing.test.ts index dca1b4e3ace7..0f5b9d6e1d72 100644 --- a/src/plugins/console/public/lib/curl_parsing/__tests__/curl_parsing.test.js +++ b/src/plugins/console/public/lib/curl_parsing/__tests__/curl_parsing.test.ts @@ -44,10 +44,10 @@ describe('CURL', () => { if (fixture.trim() === '') { return; } - fixture = fixture.split(/^-+$/m); - const name = fixture[0].trim(); - const curlText = fixture[1]; - const response = fixture[2].trim(); + const fixtureParts = fixture.split(/^-+$/m); + const name = fixtureParts[0].trim(); + const curlText = fixtureParts[1]; + const response = fixtureParts[2].trim(); test('cURL Detection - ' + name, function () { expect(detectCURL(curlText)).toBe(true); diff --git a/src/plugins/console/public/lib/curl_parsing/curl.js b/src/plugins/console/public/lib/curl_parsing/curl.ts similarity index 89% rename from src/plugins/console/public/lib/curl_parsing/curl.js rename to src/plugins/console/public/lib/curl_parsing/curl.ts index 0f5f4c597c6b..89ee287039d5 100644 --- a/src/plugins/console/public/lib/curl_parsing/curl.js +++ b/src/plugins/console/public/lib/curl_parsing/curl.ts @@ -28,12 +28,12 @@ * under the License. */ -function detectCURLinLine(line) { +function detectCURLinLine(line: string) { // returns true if text matches a curl request return line.match(/^\s*?curl\s+(-X[A-Z]+)?\s*['"]?.*?['"]?(\s*$|\s+?-d\s*?['"])/); } -export function detectCURL(text) { +export function detectCURL(text: string) { // returns true if text matches a curl request if (!text) return false; for (const line of text.split('\n')) { @@ -44,10 +44,10 @@ export function detectCURL(text) { return false; } -export function parseCURL(text) { +export function parseCURL(text: string) { let state = 'NONE'; - const out = []; - let body = []; + const out: string[] = []; + let body: string[] = []; let line = ''; const lines = text.trim().split('\n'); let matches; @@ -79,12 +79,12 @@ export function parseCURL(text) { 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; } function unescapeLastBodyEl() { - const str = body.pop().replace(/\\([\\"'])/g, '$1'); + const str = (body.pop() as string).replace(/\\([\\"'])/g, '$1'); body.push(str); } @@ -115,11 +115,11 @@ export function parseCURL(text) { // If the pattern matches, then the state is about to change, // so add the capture to the body and detect the next state // Otherwise add the whole line - function consumeMatching(pattern) { - const matches = line.match(pattern); - if (matches) { - body.push(matches[1]); - line = line.substr(matches[0].length); + function consumeMatching(pattern: RegExp) { + const patternMatches = line.match(pattern); + if (patternMatches) { + body.push(patternMatches[1]); + line = line.substr(patternMatches[0].length); detectQuote(); } else { body.push(line); @@ -130,9 +130,9 @@ export function parseCURL(text) { function parseCurlLine() { let verb = 'GET'; let request = ''; - let matches; - if ((matches = line.match(CurlVerb))) { - verb = matches[1]; + let curlMatches: RegExpMatchArray | null; + if ((curlMatches = line.match(CurlVerb))) { + verb = curlMatches[1]; } // JS regexen don't support possessive quantifiers, so diff --git a/src/plugins/console/public/lib/opensearch/__tests__/content_type.test.js b/src/plugins/console/public/lib/opensearch/__tests__/content_type.test.ts similarity index 100% rename from src/plugins/console/public/lib/opensearch/__tests__/content_type.test.js rename to src/plugins/console/public/lib/opensearch/__tests__/content_type.test.ts diff --git a/src/plugins/console/public/lib/osd/__tests__/kb.test.js b/src/plugins/console/public/lib/osd/__tests__/kb.test.ts similarity index 72% rename from src/plugins/console/public/lib/osd/__tests__/kb.test.js rename to src/plugins/console/public/lib/osd/__tests__/kb.test.ts index ea26de73386c..c17ef895163d 100644 --- a/src/plugins/console/public/lib/osd/__tests__/kb.test.js +++ b/src/plugins/console/public/lib/osd/__tests__/kb.test.ts @@ -32,8 +32,10 @@ import _ from 'lodash'; import { populateContext } from '../../autocomplete/engine'; import '../../../application/models/sense_editor/sense_editor.test.mocks'; -import * as osd from '../../osd'; +import * as osd from '../osd'; import * as mappings from '../../mappings/mappings'; +import { PartialAutoCompleteContext } from '../../autocomplete/components/autocomplete_component'; +import { Term } from '../../autocomplete/types'; describe('Knowledge base', () => { beforeEach(() => { @@ -67,37 +69,41 @@ describe('Knowledge base', () => { }, }; - function testUrlContext(tokenPath, otherTokenValues, expectedContext) { + function testUrlContext( + tokenPath: Array, + otherTokenValues: string[], + expectedContext: PartialAutoCompleteContext + ) { if (expectedContext.autoCompleteSet) { - expectedContext.autoCompleteSet = _.map(expectedContext.autoCompleteSet, function (t) { - if (_.isString(t)) { - t = { name: t }; + expectedContext.autoCompleteSet = _.map(expectedContext.autoCompleteSet, function (term) { + if (_.isString(term)) { + term = { name: term }; } - return t; + return term; }); } - const context = { otherTokenValues: otherTokenValues }; + const context: PartialAutoCompleteContext = { otherTokenValues }; populateContext( tokenPath, context, null, - expectedContext.autoCompleteSet, + !!expectedContext.autoCompleteSet, osd.getTopLevelUrlCompleteComponents('GET') ); // override context to just check on id if (context.endpoint) { - context.endpoint = context.endpoint.id; + context.endpoint = (context as any).endpoint.id; } delete context.otherTokenValues; - function norm(t) { - if (_.isString(t)) { - return { name: t }; + function norm(term: Term) { + if (_.isString(term)) { + return { name: term }; } - return t; + return term; } if (context.autoCompleteSet) { @@ -113,34 +119,35 @@ describe('Knowledge base', () => { expect(context).toEqual(expectedContext); } - function t(term) { + function t(term: string) { return { name: term, meta: 'type' }; } - function i(term) { + function i(term: string) { return { name: term, meta: 'index' }; } - function indexTest(name, tokenPath, otherTokenValues, expectedContext) { + function indexTest( + name: string, + tokenPath: Array, + otherTokenValues: string[], + expectedContext: PartialAutoCompleteContext + ) { test(name, function () { - // eslint-disable-next-line new-cap - const testApi = new osd._test.loadApisFromJson( - { - indexTest: { - endpoints: { - _multi_indices: { - patterns: ['{indices}/_multi_indices'], - }, - _single_index: { patterns: ['{index}/_single_index'] }, - _no_index: { - // testing default patters - // patterns: ["_no_index"] - }, + const testApi = osd._test.loadApisFromJson({ + indexTest: { + endpoints: { + _multi_indices: { + patterns: ['{indices}/_multi_indices'], + }, + _single_index: { patterns: ['{index}/_single_index'] }, + _no_index: { + // testing default patters + // patterns: ["_no_index"] }, }, }, - osd._test.globalUrlComponentFactories - ); + }); osd.setActiveApi(testApi); @@ -171,20 +178,22 @@ describe('Knowledge base', () => { autoCompleteSet: ['_multi_indices'], }); - function typeTest(name, tokenPath, otherTokenValues, expectedContext) { + function typeTest( + name: string, + tokenPath: Array, + otherTokenValues: string[], + expectedContext: PartialAutoCompleteContext + ) { test(name, function () { - const testApi = osd._test.loadApisFromJson( - { - typeTest: { - endpoints: { - _multi_types: { patterns: ['{indices}/{types}/_multi_types'] }, - _single_type: { patterns: ['{indices}/{type}/_single_type'] }, - _no_types: { patterns: ['{indices}/_no_types'] }, - }, + const testApi = osd._test.loadApisFromJson({ + typeTest: { + endpoints: { + _multi_types: { patterns: ['{indices}/{types}/_multi_types'] }, + _single_type: { patterns: ['{indices}/{type}/_single_type'] }, + _no_types: { patterns: ['{indices}/_no_types'] }, }, }, - osd._test.globalUrlComponentFactories - ); + }); osd.setActiveApi(testApi); mappings.loadMappings(MAPPING); diff --git a/src/plugins/console/public/lib/osd/api.js b/src/plugins/console/public/lib/osd/api.ts similarity index 63% rename from src/plugins/console/public/lib/osd/api.js rename to src/plugins/console/public/lib/osd/api.ts index 7e0bc2749b75..ce75cde29f24 100644 --- a/src/plugins/console/public/lib/osd/api.js +++ b/src/plugins/console/public/lib/osd/api.ts @@ -29,12 +29,14 @@ */ import _ from 'lodash'; -import { UrlPatternMatcher } from '../autocomplete/components'; +import { SharedComponent, UrlPatternMatcher } from '../autocomplete/components'; import { UrlParams } from '../autocomplete/url_params'; import { globalsOnlyAutocompleteComponents, compileBodyDescription, } from '../autocomplete/body_completer'; +import { Endpoint } from '../autocomplete/types'; +import { ParametrizedComponentFactories } from '../autocomplete/types'; /** * @@ -44,33 +46,42 @@ import { * @constructor * @param bodyParametrizedComponentFactories same as urlParametrizedComponentFactories but used for body compilation */ -function Api(urlParametrizedComponentFactories, bodyParametrizedComponentFactories) { - this.globalRules = Object.create(null); - this.endpoints = Object.create(null); - this.urlPatternMatcher = new UrlPatternMatcher(urlParametrizedComponentFactories); - this.globalBodyComponentFactories = bodyParametrizedComponentFactories; - this.name = ''; -} +export class Api { + globalRules: Record; + endpoints: Record; + urlPatternMatcher: UrlPatternMatcher; + globalBodyComponentFactories: ParametrizedComponentFactories | undefined; + name: string; + + constructor( + urlParametrizedComponentFactories?: ParametrizedComponentFactories, + bodyParametrizedComponentFactories?: ParametrizedComponentFactories + ) { + this.globalRules = Object.create(null); + this.endpoints = Object.create(null); + this.urlPatternMatcher = new UrlPatternMatcher(urlParametrizedComponentFactories); + this.globalBodyComponentFactories = bodyParametrizedComponentFactories; + this.name = ''; + } -(function (cls) { - cls.addGlobalAutocompleteRules = function (parentNode, rules) { + addGlobalAutocompleteRules(parentNode: string, rules: Record): void { this.globalRules[parentNode] = compileBodyDescription( 'GLOBAL.' + parentNode, rules, this.globalBodyComponentFactories ); - }; + } - cls.getGlobalAutocompleteComponents = function (term, throwOnMissing) { - const result = this.globalRules[term]; + getGlobalAutocompleteComponents(term: string, throwOnMissing?: boolean) { + const result: SharedComponent[] = this.globalRules[term]; if (_.isUndefined(result) && (throwOnMissing || _.isUndefined(throwOnMissing))) { throw new Error("failed to resolve global components for ['" + term + "']"); } return result; - }; + } - cls.addEndpointDescription = function (endpoint, description) { - const copiedDescription = {}; + addEndpointDescription(endpoint: string, description: Record) { + const copiedDescription: Record = {}; _.assign(copiedDescription, description || {}); _.defaults(copiedDescription, { id: endpoint, @@ -78,7 +89,7 @@ function Api(urlParametrizedComponentFactories, bodyParametrizedComponentFactori methods: ['GET'], }); _.each(copiedDescription.patterns, (p) => { - this.urlPatternMatcher.addEndpoint(p, copiedDescription); + this.urlPatternMatcher.addEndpoint(p, copiedDescription as Endpoint); }); copiedDescription.paramsAutocomplete = new UrlParams(copiedDescription.url_params); @@ -88,25 +99,23 @@ function Api(urlParametrizedComponentFactories, bodyParametrizedComponentFactori this.globalBodyComponentFactories ); - this.endpoints[endpoint] = copiedDescription; - }; + this.endpoints[endpoint] = copiedDescription as Endpoint; + } - cls.getEndpointDescriptionByEndpoint = function (endpoint) { + getEndpointDescriptionByEndpoint(endpoint: string) { return this.endpoints[endpoint]; - }; + } - cls.getTopLevelUrlCompleteComponents = function (method) { + getTopLevelUrlCompleteComponents(method: string) { return this.urlPatternMatcher.getTopLevelComponents(method); - }; + } - cls.getUnmatchedEndpointComponents = function () { + getUnmatchedEndpointComponents() { return globalsOnlyAutocompleteComponents(); - }; + } - cls.clear = function () { + clear(): void { this.endpoints = {}; this.globalRules = {}; - }; -})(Api.prototype); - -export default Api; + } +} diff --git a/src/plugins/console/public/lib/osd/index.js b/src/plugins/console/public/lib/osd/index.ts similarity index 100% rename from src/plugins/console/public/lib/osd/index.js rename to src/plugins/console/public/lib/osd/index.ts diff --git a/src/plugins/console/public/lib/osd/osd.js b/src/plugins/console/public/lib/osd/osd.ts similarity index 74% rename from src/plugins/console/public/lib/osd/osd.js rename to src/plugins/console/public/lib/osd/osd.ts index 529fba754a93..30bb91968b26 100644 --- a/src/plugins/console/public/lib/osd/osd.js +++ b/src/plugins/console/public/lib/osd/osd.ts @@ -28,6 +28,8 @@ * under the License. */ +import $ from 'jquery'; +import _ from 'lodash'; import { TypeAutocompleteComponent, IdAutocompleteComponent, @@ -38,67 +40,69 @@ import { UsernameAutocompleteComponent, } from '../autocomplete/components'; -import $ from 'jquery'; -import _ from 'lodash'; - -import Api from './api'; +import { Api } from './api'; +import { + ComponentFactory, + IdAutocompleteComponentFactory, + ParametrizedComponentFactories, +} from '../autocomplete/types'; let ACTIVE_API = new Api(); -const isNotAnIndexName = (name) => name[0] === '_' && name !== '_all'; +const isNotAnIndexName = (name: string) => name[0] === '_' && name !== '_all'; -const idAutocompleteComponentFactory = (name, parent) => { +const idAutocompleteComponentFactory: IdAutocompleteComponentFactory = (name, parent) => { return new IdAutocompleteComponent(name, parent); }; -const parametrizedComponentFactories = { - getComponent: function (name, parent, provideDefault) { - if (this[name]) { - return this[name]; +const parametrizedComponentFactories: ParametrizedComponentFactories = { + getComponent(name, parent, provideDefault) { + if (this[name as keyof ParametrizedComponentFactories]) { + return this[name as keyof ParametrizedComponentFactories] as ComponentFactory; } else if (provideDefault) { - return idAutocompleteComponentFactory; + return idAutocompleteComponentFactory as ComponentFactory; } }, - index: function (name, parent) { + index(name, parent) { if (isNotAnIndexName(name)) return; return new IndexAutocompleteComponent(name, parent, false); }, - indices: function (name, parent) { + indices(name, parent) { if (isNotAnIndexName(name)) return; return new IndexAutocompleteComponent(name, parent, true); }, - type: function (name, parent) { + type(name, parent) { return new TypeAutocompleteComponent(name, parent, false); }, - types: function (name, parent) { + types(name, parent) { return new TypeAutocompleteComponent(name, parent, true); }, - id: function (name, parent) { + id(name, parent) { return idAutocompleteComponentFactory(name, parent); }, - transform_id: function (name, parent) { + transform_id(name, parent) { return idAutocompleteComponentFactory(name, parent); }, - username: function (name, parent) { + username(name, parent) { return new UsernameAutocompleteComponent(name, parent); }, - user: function (name, parent) { + user(name, parent) { return new UsernameAutocompleteComponent(name, parent); }, - template: function (name, parent) { + template(name, parent) { return new TemplateAutocompleteComponent(name, parent); }, - task_id: function (name, parent) { + task_id(name, parent) { return idAutocompleteComponentFactory(name, parent); }, - ids: function (name, parent) { + ids(name, parent) { return idAutocompleteComponentFactory(name, parent, true); }, - fields: function (name, parent) { + fields(name, parent) { return new FieldAutocompleteComponent(name, parent, true); }, - field: function (name, parent) { + field(name, parent) { return new FieldAutocompleteComponent(name, parent, false); }, - nodes: function (name, parent) { + nodes(name, parent) { return new ListComponent( name, [ @@ -115,7 +119,7 @@ const parametrizedComponentFactories = { parent ); }, - node: function (name, parent) { + node(name, parent) { return new ListComponent(name, [], parent, false); }, }; @@ -124,11 +128,11 @@ export function getUnmatchedEndpointComponents() { return ACTIVE_API.getUnmatchedEndpointComponents(); } -export function getEndpointDescriptionByEndpoint(endpoint) { +export function getEndpointDescriptionByEndpoint(endpoint: string) { return ACTIVE_API.getEndpointDescriptionByEndpoint(endpoint); } -export function getEndpointBodyCompleteComponents(endpoint) { +export function getEndpointBodyCompleteComponents(endpoint: string) { const desc = getEndpointDescriptionByEndpoint(endpoint); if (!desc) { throw new Error("failed to resolve endpoint ['" + endpoint + "']"); @@ -136,25 +140,25 @@ export function getEndpointBodyCompleteComponents(endpoint) { return desc.bodyAutocompleteRootComponents; } -export function getTopLevelUrlCompleteComponents(method) { +export function getTopLevelUrlCompleteComponents(method: string) { return ACTIVE_API.getTopLevelUrlCompleteComponents(method); } -export function getGlobalAutocompleteComponents(term, throwOnMissing) { +export function getGlobalAutocompleteComponents(term: string, throwOnMissing: boolean) { return ACTIVE_API.getGlobalAutocompleteComponents(term, throwOnMissing); } function loadApisFromJson( - json, - urlParametrizedComponentFactories, - bodyParametrizedComponentFactories + json: Record, + urlParametrizedComponentFactories?: ParametrizedComponentFactories, + bodyParametrizedComponentFactories?: ParametrizedComponentFactories ) { urlParametrizedComponentFactories = urlParametrizedComponentFactories || parametrizedComponentFactories; bodyParametrizedComponentFactories = bodyParametrizedComponentFactories || urlParametrizedComponentFactories; const api = new Api(urlParametrizedComponentFactories, bodyParametrizedComponentFactories); - const names = []; + const names: string[] = []; _.each(json, function (apiJson, name) { names.unshift(name); _.each(apiJson.globals || {}, function (globalJson, globalName) { @@ -172,7 +176,7 @@ function loadApisFromJson( // This function should be attached to a class that holds the current state, not setup // when the file is required. Also, jQuery should not be used to make network requests // like this, it looks like a minor security issue. -export function setActiveApi(api) { +export function setActiveApi(api?: Api) { if (!api) { $.ajax({ url: '../api/console/api_server', @@ -185,6 +189,7 @@ export function setActiveApi(api) { setActiveApi(loadApisFromJson(data)); }, function (jqXHR) { + // eslint-disable-next-line no-console console.log("failed to load API '" + api + "': " + jqXHR.responseText); } ); @@ -197,5 +202,5 @@ export function setActiveApi(api) { setActiveApi(); export const _test = { - loadApisFromJson: loadApisFromJson, + loadApisFromJson, }; diff --git a/src/plugins/console/public/lib/utils/__tests__/utils.test.js b/src/plugins/console/public/lib/utils/__tests__/utils.test.ts similarity index 100% rename from src/plugins/console/public/lib/utils/__tests__/utils.test.js rename to src/plugins/console/public/lib/utils/__tests__/utils.test.ts diff --git a/typings/index.d.ts b/typings/index.d.ts index 17a1b2139e34..ef7cf0239229 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -46,6 +46,12 @@ declare module '*.svg' { export default content; } +declare module '*.txt' { + const content: string; + // eslint-disable-next-line import/no-default-export + export default content; +} + type MethodKeysOf = { [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never; }[keyof T];