Skip to content

Commit

Permalink
Generate api/_types/index.d.ts (opensearch-project#955)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Fournet <[email protected]>
  • Loading branch information
xfournet committed Jan 9, 2025
1 parent 075e3c7 commit 61f27a6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
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))
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)
}
}
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 61f27a6

Please sign in to comment.