Skip to content

Commit

Permalink
Component types export (#957)
Browse files Browse the repository at this point in the history
Generate api/_types/index.d.ts (#955)

Signed-off-by: Xavier Fournet <461943+xfournet@users.noreply.github.com>
  • Loading branch information
xfournet authored Jan 10, 2025
1 parent 180eaa0 commit faa8069
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291))
233 changes: 233 additions & 0 deletions api/_types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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,
};
8 changes: 8 additions & 0 deletions api_generator/src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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())
}
}
40 changes: 40 additions & 0 deletions api_generator/src/renderers/render_types/ComponentTypesRenderer.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> {
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)
}
}
6 changes: 3 additions & 3 deletions api_generator/src/renderers/render_types/TypesContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}

Expand All @@ -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}`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{{opensearch_license}}}
{{{generated_code_warning}}}

{{#imports}}
import * as {{{name}}} from '{{{path}}}'
{{/imports}}

export {
{{#exports}}
{{{.}}},
{{/exports}}
};
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -56,6 +57,7 @@ declare const events: {

export {
API,
Types,
Client,
Transport,
ConnectionPool,
Expand Down

0 comments on commit faa8069

Please sign in to comment.