From 95a6741550ce24218bad2bd4443ac22e40184151 Mon Sep 17 00:00:00 2001 From: Xavier Fournet <461943+xfournet@users.noreply.github.com> Date: Thu, 9 Jan 2025 12:27:46 +0100 Subject: [PATCH] Generate api/_types/index.d.ts --- api_generator/src/Generator.ts | 8 ++++ .../render_types/ComponentTypesRenderer.ts | 40 +++++++++++++++++++ .../templates/types.component_types.mustache | 12 ++++++ index.d.ts | 2 + 4 files changed, 62 insertions(+) create mode 100644 api_generator/src/renderers/render_types/ComponentTypesRenderer.ts create mode 100644 api_generator/src/renderers/templates/types.component_types.mustache diff --git a/api_generator/src/Generator.ts b/api_generator/src/Generator.ts index d6d4512a8..332481665 100644 --- a/api_generator/src/Generator.ts +++ b/api_generator/src/Generator.ts @@ -27,6 +27,7 @@ import FunctionTypesContainer from './renderers/render_types/FunctionTypesContai import { clear_child_dirs, recreate_dir } from './helpers' import OpenSearchApiTypeRenderer from './renderers/render_types/OpensearchApiTypeRenderer' import ApiTypeRenderer from './renderers/render_types/ApiTypeRenderer' +import ComponentTypesRenderer from './renderers/render_types/ComponentTypesRenderer' const HTTP_METHODS = ['get', 'post', 'put', 'delete', 'head', 'patch', 'options', 'trace', 'connect'].sort() @@ -55,6 +56,7 @@ export default class Generator { this.#generate_namespace_modules() this.#generate_opensearch_api() this.#generate_types_containers() + this.#generate_types_index_type() this.#generate_api_index_type() this.#generate_opensearch_api_type() } @@ -117,4 +119,10 @@ export default class Generator { const renderder = new ApiTypeRenderer() fs.writeFileSync(file_path, renderder.render()) } + + #generate_types_index_type (): void { + const file_path = path.join(this.repo_folder, 'api', '_types', 'index.d.ts') + const renderer = new ComponentTypesRenderer() + fs.writeFileSync(file_path, renderer.render()) + } } diff --git a/api_generator/src/renderers/render_types/ComponentTypesRenderer.ts b/api_generator/src/renderers/render_types/ComponentTypesRenderer.ts new file mode 100644 index 000000000..7e54283da --- /dev/null +++ b/api_generator/src/renderers/render_types/ComponentTypesRenderer.ts @@ -0,0 +1,40 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + */ + +import BaseRenderer from '../BaseRenderer' +import FunctionTypesContainer from './FunctionTypesContainer' +import TypesContainer from './TypesContainer' + +export default class ComponentTypesRenderer extends BaseRenderer { + protected template_file = 'types.component_types.mustache' + private readonly containers: TypesContainer[] + + constructor () { + super() + this.containers = [...TypesContainer.REPO.values()].filter((container) => container.folder_name === '_types') + } + + view (): Record { + return { imports: this.#imports(), exports: this.#exports() } + } + + #imports (): Array<{ name: string, path: string }> { + return this.containers.map(container => { + return { + name: container.import_name, + path: FunctionTypesContainer.import_path(container, { file_path: './_types/index.d.ts' }) + } + }) + } + + #exports (): string[] { + return this.containers.map(container => container.import_name) + } +} diff --git a/api_generator/src/renderers/templates/types.component_types.mustache b/api_generator/src/renderers/templates/types.component_types.mustache new file mode 100644 index 000000000..5110fa740 --- /dev/null +++ b/api_generator/src/renderers/templates/types.component_types.mustache @@ -0,0 +1,12 @@ +{{{opensearch_license}}} +{{{generated_code_warning}}} + +{{#imports}} +import * as {{{name}}} from '{{{path}}}' +{{/imports}} + +export { +{{#exports}} + {{{.}}}, +{{/exports}} +}; diff --git a/index.d.ts b/index.d.ts index 84cabcb64..8fd759f4d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -43,6 +43,7 @@ import { import Serializer from './lib/Serializer'; import * as errors from './lib/errors'; import * as API from './api'; +import * as Types from './api/_types'; import { Client, ClientOptions, NodeOptions, ClientExtendsCallbackOptions } from './lib/Client'; declare const events: { @@ -56,6 +57,7 @@ declare const events: { export { API, + Types, Client, Transport, ConnectionPool,