Rename item/items -> component/components

This commit is contained in:
Fredrik Adelöw
2020-05-11 14:20:08 +02:00
parent 2955b4e862
commit 2955884047
5 changed files with 25 additions and 19 deletions
@@ -18,17 +18,19 @@ import { NotFoundError } from '@backstage/backend-common';
import { Component, Inventory } from './types';
export class AggregatorInventory implements Inventory {
inventories: Inventory[] = [];
#inventories: Inventory[] = [];
async list(): Promise<Component[]> {
const lists = await Promise.all(this.inventories.map((i) => i.list()));
async components(): Promise<Component[]> {
const lists = await Promise.all(
this.#inventories.map((i) => i.components()),
);
return lists.flat();
}
item(id: string): Promise<Component> {
component(id: string): Promise<Component> {
return new Promise((resolve, reject) => {
const promises = this.inventories.map((i) =>
i.item(id).then(resolve, () => {
const promises = this.#inventories.map((i) =>
i.component(id).then(resolve, () => {
// For now, just swallow errors in individual inventories;
// should handle partial errors better
}),
@@ -40,6 +42,6 @@ export class AggregatorInventory implements Inventory {
}
enlist(inventory: Inventory) {
this.inventories.push(inventory);
this.#inventories.push(inventory);
}
}
@@ -18,14 +18,18 @@ import { NotFoundError } from '@backstage/backend-common';
import { Component, Inventory } from './types';
export class StaticInventory implements Inventory {
constructor(private components: Component[]) {}
#components: Component[];
async list(): Promise<Component[]> {
return this.components.slice();
constructor(components: Component[]) {
this.#components = components;
}
async item(id: string): Promise<Component> {
const item = this.components.find((i) => i.id === id);
async components(): Promise<Component[]> {
return this.#components.slice();
}
async component(id: string): Promise<Component> {
const item = this.#components.find((i) => i.id === id);
if (!item) {
throw new NotFoundError(`Found no component with ID ${id}`);
}
@@ -19,6 +19,6 @@ export type Component = {
};
export type Inventory = {
list(): Promise<Component[]>;
item(id: string): Promise<Component>;
components(): Promise<Component[]>;
component(id: string): Promise<Component>;
};
@@ -32,13 +32,13 @@ export async function createRouter(
const router = Router();
router
.get('/', async (req, res) => {
const components = await inventory.list();
.get('/components', async (req, res) => {
const components = await inventory.components();
res.status(200).send(components);
})
.get('/:id', async (req, res) => {
.get('/components/:id', async (req, res) => {
const { id } = req.params;
const component = await inventory.item(id);
const component = await inventory.component(id);
res.status(200).send(component);
});
+1 -1
View File
@@ -6,7 +6,7 @@
"sourceMap": true,
"declaration": true,
"strict": true,
"target": "es5",
"target": "es2019",
"module": "commonjs",
"esModuleInterop": true,
"lib": ["es2019"],