feat(search): create search collator modules

Co-authored-by: Emma Indal <emma.indahl@gmail.com>
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2023-03-07 13:53:58 +01:00
parent 1473dd9eb6
commit 1adc2c787e
53 changed files with 1124 additions and 206 deletions
+6 -28
View File
@@ -3,18 +3,14 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
/// <reference types="node" />
import { Config } from '@backstage/config';
import { DocumentCollatorFactory } from '@backstage/plugin-search-common';
import { ExploreTool } from '@backstage/plugin-explore-common';
import express from 'express';
import { GetExploreToolsRequest } from '@backstage/plugin-explore-common';
import { GetExploreToolsResponse } from '@backstage/plugin-explore-common';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { Logger } from 'winston';
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { Readable } from 'stream';
import { ToolDocument } from '@backstage/plugin-search-backend-module-explore';
import { ToolDocumentCollatorFactory } from '@backstage/plugin-search-backend-module-explore';
import { ToolDocumentCollatorFactoryOptions } from '@backstage/plugin-search-backend-module-explore';
// @public (undocumented)
export function createRouter(options: RouterOptions): Promise<express.Router>;
@@ -40,27 +36,9 @@ export class StaticExploreToolProvider implements ExploreToolProvider {
getTools(request: GetExploreToolsRequest): Promise<GetExploreToolsResponse>;
}
// @public
export interface ToolDocument extends IndexableDocument, ExploreTool {}
export { ToolDocument };
// @public
export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
// (undocumented)
execute(): AsyncGenerator<ToolDocument>;
// (undocumented)
static fromConfig(
_config: Config,
options: ToolDocumentCollatorFactoryOptions,
): ToolDocumentCollatorFactory;
// (undocumented)
getCollator(): Promise<Readable>;
// (undocumented)
readonly type: string;
}
export { ToolDocumentCollatorFactory };
// @public
export type ToolDocumentCollatorFactoryOptions = {
discovery: PluginEndpointDiscovery;
logger: Logger;
};
export { ToolDocumentCollatorFactoryOptions };
```
+1
View File
@@ -25,6 +25,7 @@
"@backstage/backend-common": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/plugin-explore-common": "workspace:^",
"@backstage/plugin-search-backend-module-explore": "workspace:^",
"@backstage/plugin-search-common": "workspace:^",
"@types/express": "*",
"express": "^4.18.1",
+15 -1
View File
@@ -20,7 +20,6 @@
* @packageDocumentation
*/
export * from './search';
export * from './service';
export * from './tools';
@@ -28,3 +27,18 @@ export * from './tools';
* @internal Example only - do not use in production
*/
export { exampleTools } from './example/exampleTools';
/**
* @deprecated
* import from @backstage/plugin-search-backend-module-explore instead
*/
export { ToolDocumentCollatorFactory } from '@backstage/plugin-search-backend-module-explore';
/**
* @deprecated
* import from @backstage/plugin-search-backend-module-explore instead
*/
export type {
ToolDocument,
ToolDocumentCollatorFactoryOptions,
} from '@backstage/plugin-search-backend-module-explore';
@@ -1,101 +0,0 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { Config } from '@backstage/config';
import { ExploreTool } from '@backstage/plugin-explore-common';
import {
DocumentCollatorFactory,
IndexableDocument,
} from '@backstage/plugin-search-common';
import fetch from 'node-fetch';
import { Readable } from 'stream';
import { Logger } from 'winston';
/**
* Extended IndexableDocument with explore tool specific properties
*
* @public
*/
export interface ToolDocument extends IndexableDocument, ExploreTool {}
/**
* The options for the {@link ToolDocumentCollatorFactory}.
*
* @public
*/
export type ToolDocumentCollatorFactoryOptions = {
discovery: PluginEndpointDiscovery;
logger: Logger;
};
/**
* Search collator responsible for collecting explore tools to index.
*
* @public
*/
export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
public readonly type: string = 'tools';
private readonly discovery: PluginEndpointDiscovery;
private readonly logger: Logger;
private constructor(options: ToolDocumentCollatorFactoryOptions) {
this.discovery = options.discovery;
this.logger = options.logger;
}
static fromConfig(
_config: Config,
options: ToolDocumentCollatorFactoryOptions,
) {
return new ToolDocumentCollatorFactory(options);
}
async getCollator() {
return Readable.from(this.execute());
}
async *execute(): AsyncGenerator<ToolDocument> {
this.logger.info('Starting collation of explore tools');
const tools = await this.fetchTools();
for (const tool of tools) {
yield {
...tool,
text: tool.description,
location: tool.url,
};
}
this.logger.info('Finished collation of explore tools');
}
private async fetchTools() {
const baseUrl = await this.discovery.getBaseUrl('explore');
const response = await fetch(`${baseUrl}/tools`);
if (!response.ok) {
throw new Error(
`Failed to explore fetch tools, ${response.status}: ${response.statusText}`,
);
}
const data = await response.json();
return data.tools;
}
}
@@ -1,21 +0,0 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { ToolDocumentCollatorFactory } from './ToolDocumentCollatorFactory';
export type {
ToolDocument,
ToolDocumentCollatorFactoryOptions,
} from './ToolDocumentCollatorFactory';