Skip to content

Commit

Permalink
Generate api/_types/index.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
xfournet committed Jan 9, 2025
1 parent b911c49 commit 95a6741
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
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 95a6741

Please sign in to comment.