OpenSearch-Dashboards
OpenSearch-Dashboards copied to clipboard
Export correct types for `RequestHandlerContext.dataSource` for plugins
Is your feature request related to a problem? Please describe.
currently in plugins when we do context.dataSource in route handler, typescript complains Property 'dataSource' does not exist on type 'RequestHandlerContext'., and it's hard for developers to know the schema of available methods under it.
Describe the solution you'd like
Core to provide types correctly for RequestHandlerContext
Describe alternatives you've considered
I'm aware that plugins can add
declare module '../../../src/core/server' {
interface RequestHandlerContext {
dataSource: DataSourcePluginRequestContext;
}
}
But this is unreliable and duplicate work for all plugins, especially because plugins have to also add DataSourcePluginRequestContext definition since it's not a top level export
export interface DataSourcePluginRequestContext {
opensearch: {
getClient: (dataSourceId: string) => Promise<OpenSearchClient>;
legacy: {
getClient: (
dataSourceId: string
) => {
callAPI: (
endpoint: string,
clientParams: Record<string, any>,
options?: LegacyCallAPIOptions
) => Promise<unknown>;
};
};
};
}
this can cause issues if upstream API changes and types become inconsistent. For something like dataSource that should be universal rather than plugin specific, can this be made available to plugins in core?
Additional context
somewhat related https://github.com/opensearch-project/OpenSearch-Dashboards/issues/4274
@joshuali925 are you looking for export that interface here?
https://github.com/opensearch-project/OpenSearch-Dashboards/blob/6fea4c9195aa764a132fe4ecd531d65d33cb8f48/src/plugins/data_source/server/index.ts#L18
@Flyingliuhub @BionIT would you confirm with @joshuali925 and work with team to help plugin teams
@seraphjiang yes, or would be better to make dataSource available through core's RequestHandlerContext type so that
declare module '../../../src/core/server' {
interface RequestHandlerContext {
dataSource: DataSourcePluginRequestContext;
}
}
doesn't need to be added and maintained manually by each plugin
@zhyuanqi would you help this
Hi @joshuali925, since data source plugin is also a plugin which does not exist in src/core, core plugins can consume the merged declaration to consume data source context interface, while external plugins can not. We suggest each external plugin to use the following to consume the type, and in the PR, we have exported the DataSourcePluginRequestContext at top level and I saw that search relevance plugin uses the following as well to declare merge, and the change only involves adding data source with import from data source server path. Hope it makes sense.
export interface SearchRelevancePluginRequestContext {
metricsService: MetricsServiceSetup;
}
declare module '../../../src/core/server' {
interface RequestHandlerContext {
searchRelevance: SearchRelevancePluginRequestContext;
dataSource: DataSourcePluginRequestContext;
}
}