From faa806900c6e28455934cd0975764cac25e745fb Mon Sep 17 00:00:00 2001 From: Xavier Fournet <461943+xfournet@users.noreply.github.com> Date: Fri, 10 Jan 2025 18:09:24 +0100 Subject: [PATCH] Component types export (#957) Generate api/_types/index.d.ts (#955) Signed-off-by: Xavier Fournet <461943+xfournet@users.noreply.github.com> --- .gitattributes | 1 + CHANGELOG.md | 3 +- api/_types/index.d.ts | 233 ++++++++++++++++++ api_generator/src/Generator.ts | 8 + .../render_types/ComponentTypesRenderer.ts | 40 +++ .../renderers/render_types/TypesContainer.ts | 6 +- .../templates/types.component_types.mustache | 12 + index.d.ts | 2 + 8 files changed, 301 insertions(+), 4 deletions(-) create mode 100644 .gitattributes create mode 100644 api/_types/index.d.ts 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/.gitattributes b/.gitattributes new file mode 100644 index 000000000..6313b56c5 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cd490003..82b2fe5c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] ### Added +- Added export of component types ([#955]) ### Dependencies ### Changed ### Deprecated @@ -312,4 +313,4 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Bumps `tsd` from 0.22.0 to 0.24.1 - Bumps `semver` from 7.3.7 to 7.3.8 ### Fixed -- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) \ No newline at end of file +- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) diff --git a/api/_types/index.d.ts b/api/_types/index.d.ts new file mode 100644 index 000000000..ec3671879 --- /dev/null +++ b/api/_types/index.d.ts @@ -0,0 +1,233 @@ +/* + * 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. + * + */ + +/* + * This file was generated from the OpenSearch API Spec. Do NOT edit it + * manually. If you want to make changes, either update the spec or + * modify the API generator. + */ + +import * as Common from './_common' +import * as Common_Aggregations from './_common.aggregations' +import * as Common_Analysis from './_common.analysis' +import * as Common_Mapping from './_common.mapping' +import * as Common_QueryDsl from './_common.query_dsl' +import * as Core_Common from './_core._common' +import * as Core_Bulk from './_core.bulk' +import * as Core_Explain from './_core.explain' +import * as Core_FieldCaps from './_core.field_caps' +import * as Core_GetScriptContext from './_core.get_script_context' +import * as Core_GetScriptLanguages from './_core.get_script_languages' +import * as Core_Get from './_core.get' +import * as Core_Mget from './_core.mget' +import * as Core_MsearchTemplate from './_core.msearch_template' +import * as Core_Msearch from './_core.msearch' +import * as Core_Mtermvectors from './_core.mtermvectors' +import * as Core_RankEval from './_core.rank_eval' +import * as Core_ReindexRethrottle from './_core.reindex_rethrottle' +import * as Core_Reindex from './_core.reindex' +import * as Core_ScriptsPainlessExecute from './_core.scripts_painless_execute' +import * as Core_SearchShards from './_core.search_shards' +import * as Core_Search from './_core.search' +import * as Core_Termvectors from './_core.termvectors' +import * as Core_UpdateByQueryRethrottle from './_core.update_by_query_rethrottle' +import * as Core_Update from './_core.update' +import * as AsynchronousSearch_Common from './asynchronous_search._common' +import * as Cat_Aliases from './cat.aliases' +import * as Cat_Allocation from './cat.allocation' +import * as Cat_ClusterManager from './cat.cluster_manager' +import * as Cat_Count from './cat.count' +import * as Cat_Fielddata from './cat.fielddata' +import * as Cat_Health from './cat.health' +import * as Cat_Indices from './cat.indices' +import * as Cat_Master from './cat.master' +import * as Cat_Nodeattrs from './cat.nodeattrs' +import * as Cat_Nodes from './cat.nodes' +import * as Cat_PendingTasks from './cat.pending_tasks' +import * as Cat_Plugins from './cat.plugins' +import * as Cat_Recovery from './cat.recovery' +import * as Cat_Repositories from './cat.repositories' +import * as Cat_SegmentReplication from './cat.segment_replication' +import * as Cat_Segments from './cat.segments' +import * as Cat_Shards from './cat.shards' +import * as Cat_Snapshots from './cat.snapshots' +import * as Cat_Tasks from './cat.tasks' +import * as Cat_Templates from './cat.templates' +import * as Cat_ThreadPool from './cat.thread_pool' +import * as Cluster_Common from './cluster._common' +import * as Cluster_AllocationExplain from './cluster.allocation_explain' +import * as Cluster_DecommissionAwareness from './cluster.decommission_awareness' +import * as Cluster_Health from './cluster.health' +import * as Cluster_PendingTasks from './cluster.pending_tasks' +import * as Cluster_RemoteInfo from './cluster.remote_info' +import * as Cluster_Reroute from './cluster.reroute' +import * as Cluster_State from './cluster.state' +import * as Cluster_Stats from './cluster.stats' +import * as Cluster_WeightedRouting from './cluster.weighted_routing' +import * as DanglingIndices_ListDanglingIndices from './dangling_indices.list_dangling_indices' +import * as FlowFramework_Common from './flow_framework.common' +import * as FlowFramework_Errors from './flow_framework.errors' +import * as Indices_Common from './indices._common' +import * as Indices_AddBlock from './indices.add_block' +import * as Indices_Analyze from './indices.analyze' +import * as Indices_Close from './indices.close' +import * as Indices_GetAlias from './indices.get_alias' +import * as Indices_GetFieldMapping from './indices.get_field_mapping' +import * as Indices_GetIndexTemplate from './indices.get_index_template' +import * as Indices_GetMapping from './indices.get_mapping' +import * as Indices_PutIndexTemplate from './indices.put_index_template' +import * as Indices_Recovery from './indices.recovery' +import * as Indices_ResolveIndex from './indices.resolve_index' +import * as Indices_Rollover from './indices.rollover' +import * as Indices_Segments from './indices.segments' +import * as Indices_ShardStores from './indices.shard_stores' +import * as Indices_SimulateTemplate from './indices.simulate_template' +import * as Indices_Stats from './indices.stats' +import * as Indices_UpdateAliases from './indices.update_aliases' +import * as Indices_ValidateQuery from './indices.validate_query' +import * as Ingest_Common from './ingest._common' +import * as Ingest_Simulate from './ingest.simulate' +import * as Insights_Common from './insights._common' +import * as Ism_Common from './ism._common' +import * as Knn_Common from './knn._common' +import * as Ml_Common from './ml._common' +import * as Nodes_Common from './nodes._common' +import * as Nodes_Info from './nodes.info' +import * as Nodes_ReloadSecureSettings from './nodes.reload_secure_settings' +import * as Nodes_Stats from './nodes.stats' +import * as Nodes_Usage from './nodes.usage' +import * as Notifications_Common from './notifications._common' +import * as Observability_Common from './observability._common' +import * as Query_Common from './query._common' +import * as RemoteStore_Common from './remote_store._common' +import * as Replication_Common from './replication._common' +import * as Rollups_Common from './rollups._common' +import * as SearchPipeline_Common from './search_pipeline._common' +import * as Security_Common from './security._common' +import * as Sm_Common from './sm._common' +import * as Snapshot_Common from './snapshot._common' +import * as Snapshot_CleanupRepository from './snapshot.cleanup_repository' +import * as Snapshot_Restore from './snapshot.restore' +import * as Snapshot_VerifyRepository from './snapshot.verify_repository' +import * as Sql_Common from './sql._common' +import * as Tasks_Common from './tasks._common' +import * as Transforms_Common from './transforms._common' +import * as Wlm_Common from './wlm._common' +import * as Global from './_global' + +export { + Common, + Common_Aggregations, + Common_Analysis, + Common_Mapping, + Common_QueryDsl, + Core_Common, + Core_Bulk, + Core_Explain, + Core_FieldCaps, + Core_GetScriptContext, + Core_GetScriptLanguages, + Core_Get, + Core_Mget, + Core_MsearchTemplate, + Core_Msearch, + Core_Mtermvectors, + Core_RankEval, + Core_ReindexRethrottle, + Core_Reindex, + Core_ScriptsPainlessExecute, + Core_SearchShards, + Core_Search, + Core_Termvectors, + Core_UpdateByQueryRethrottle, + Core_Update, + AsynchronousSearch_Common, + Cat_Aliases, + Cat_Allocation, + Cat_ClusterManager, + Cat_Count, + Cat_Fielddata, + Cat_Health, + Cat_Indices, + Cat_Master, + Cat_Nodeattrs, + Cat_Nodes, + Cat_PendingTasks, + Cat_Plugins, + Cat_Recovery, + Cat_Repositories, + Cat_SegmentReplication, + Cat_Segments, + Cat_Shards, + Cat_Snapshots, + Cat_Tasks, + Cat_Templates, + Cat_ThreadPool, + Cluster_Common, + Cluster_AllocationExplain, + Cluster_DecommissionAwareness, + Cluster_Health, + Cluster_PendingTasks, + Cluster_RemoteInfo, + Cluster_Reroute, + Cluster_State, + Cluster_Stats, + Cluster_WeightedRouting, + DanglingIndices_ListDanglingIndices, + FlowFramework_Common, + FlowFramework_Errors, + Indices_Common, + Indices_AddBlock, + Indices_Analyze, + Indices_Close, + Indices_GetAlias, + Indices_GetFieldMapping, + Indices_GetIndexTemplate, + Indices_GetMapping, + Indices_PutIndexTemplate, + Indices_Recovery, + Indices_ResolveIndex, + Indices_Rollover, + Indices_Segments, + Indices_ShardStores, + Indices_SimulateTemplate, + Indices_Stats, + Indices_UpdateAliases, + Indices_ValidateQuery, + Ingest_Common, + Ingest_Simulate, + Insights_Common, + Ism_Common, + Knn_Common, + Ml_Common, + Nodes_Common, + Nodes_Info, + Nodes_ReloadSecureSettings, + Nodes_Stats, + Nodes_Usage, + Notifications_Common, + Observability_Common, + Query_Common, + RemoteStore_Common, + Replication_Common, + Rollups_Common, + SearchPipeline_Common, + Security_Common, + Sm_Common, + Snapshot_Common, + Snapshot_CleanupRepository, + Snapshot_Restore, + Snapshot_VerifyRepository, + Sql_Common, + Tasks_Common, + Transforms_Common, + Wlm_Common, + Global, +}; 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/render_types/TypesContainer.ts b/api_generator/src/renderers/render_types/TypesContainer.ts index bc1c186e0..742fc33b3 100644 --- a/api_generator/src/renderers/render_types/TypesContainer.ts +++ b/api_generator/src/renderers/render_types/TypesContainer.ts @@ -38,7 +38,7 @@ export default class TypesContainer { } static import_path (from: { file_path: string }, to: { file_path: string }): string { - const relative = path.relative(path.dirname(to.file_path), from.file_path).replace('.d.ts', '') + const relative = path.relative(path.dirname(to.file_path), from.file_path).replace('.d.ts', '').replaceAll('\\', '/') return relative.startsWith('.') ? relative : `./${relative}` } @@ -58,10 +58,10 @@ export default class TypesContainer { let file_path: string = 'UNSET' if (ref.startsWith('#/components')) { const file_name = ref.split(SEPARATOR)[0].split('/').reverse()[0] - file_path = `${TYPE_COMPONENTS_FOLDER}/${file_name}.d.ts` + file_path = path.join(TYPE_COMPONENTS_FOLDER, `${file_name}.d.ts`) } else { const [folder_name, file_name] = ref.split(SEPARATOR)[0].split('/') - file_path = `${folder_name}/${file_name}.d.ts` + file_path = path.join(folder_name, `${file_name}.d.ts`) } const container = TypesContainer.REPO.get(file_path) if (!container) throw new Error(`Container not found for: ${ref} -> ${file_path}`) 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,