backend-plugin-api: simplified ServiceFactory type

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-08-17 18:11:41 +02:00
parent 70343d6813
commit eef91a2558
12 changed files with 70 additions and 108 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-plugin-api': patch
---
Simplified the `ServiceFactory` type and removed `AnyServiceFactory`.
+11 -38
View File
@@ -3,7 +3,6 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { AnyServiceFactory } from '@backstage/backend-plugin-api';
import { BackendFeature } from '@backstage/backend-plugin-api';
import { Config } from '@backstage/config';
import { ExtensionPoint } from '@backstage/backend-plugin-api';
@@ -29,14 +28,10 @@ export interface Backend {
}
// @public (undocumented)
export const cacheFactory: ServiceFactory<
PluginCacheManager,
PluginCacheManager,
{}
>;
export const cacheFactory: ServiceFactory<PluginCacheManager>;
// @public (undocumented)
export const configFactory: ServiceFactory<Config, Config, {}>;
export const configFactory: ServiceFactory<Config>;
// @public (undocumented)
export function createSpecializedBackend(
@@ -46,46 +41,28 @@ export function createSpecializedBackend(
// @public (undocumented)
export interface CreateSpecializedBackendOptions {
// (undocumented)
services: AnyServiceFactory[];
services: ServiceFactory[];
}
// @public (undocumented)
export const databaseFactory: ServiceFactory<
PluginDatabaseManager,
PluginDatabaseManager,
{}
>;
export const databaseFactory: ServiceFactory<PluginDatabaseManager>;
// @public (undocumented)
export const discoveryFactory: ServiceFactory<
PluginEndpointDiscovery,
PluginEndpointDiscovery,
{}
>;
export const discoveryFactory: ServiceFactory<PluginEndpointDiscovery>;
// @public (undocumented)
export const httpRouterFactory: ServiceFactory<
HttpRouterService,
HttpRouterService,
{}
>;
export const httpRouterFactory: ServiceFactory<HttpRouterService>;
// @public (undocumented)
export const loggerFactory: ServiceFactory<Logger, Logger, {}>;
export const loggerFactory: ServiceFactory<Logger>;
// @public (undocumented)
export const permissionsFactory: ServiceFactory<
PermissionAuthorizer | PermissionEvaluator,
PermissionAuthorizer | PermissionEvaluator,
{}
PermissionAuthorizer | PermissionEvaluator
>;
// @public (undocumented)
export const schedulerFactory: ServiceFactory<
PluginTaskScheduler,
PluginTaskScheduler,
{}
>;
export const schedulerFactory: ServiceFactory<PluginTaskScheduler>;
// @public (undocumented)
export type ServiceOrExtensionPoint<T = unknown> =
@@ -93,12 +70,8 @@ export type ServiceOrExtensionPoint<T = unknown> =
| ServiceRef<T>;
// @public (undocumented)
export const tokenManagerFactory: ServiceFactory<
TokenManager,
TokenManager,
{}
>;
export const tokenManagerFactory: ServiceFactory<TokenManager>;
// @public (undocumented)
export const urlReaderFactory: ServiceFactory<UrlReader, UrlReader, {}>;
export const urlReaderFactory: ServiceFactory<UrlReader>;
```
@@ -108,7 +108,9 @@ export class BackendInitializer {
id: feature.id,
provides,
consumes: new Set(Object.values(registerOptions.deps)),
deps: registerOptions.deps,
deps: registerOptions.deps as {
[name: string]: ServiceOrExtensionPoint;
},
init: registerOptions.init as BackendRegisterInit['init'],
};
},
@@ -14,10 +14,7 @@
* limitations under the License.
*/
import {
AnyServiceFactory,
BackendFeature,
} from '@backstage/backend-plugin-api';
import { ServiceFactory, BackendFeature } from '@backstage/backend-plugin-api';
import { BackendInitializer } from './BackendInitializer';
import { ServiceRegistry } from './ServiceRegistry';
import { Backend } from './types';
@@ -26,7 +23,7 @@ export class BackstageBackend implements Backend {
#services: ServiceRegistry;
#initializer: BackendInitializer;
constructor(apiFactories: AnyServiceFactory[]) {
constructor(apiFactories: ServiceFactory[]) {
this.#services = new ServiceRegistry(apiFactories);
this.#initializer = new BackendInitializer(this.#services);
}
+2 -2
View File
@@ -15,7 +15,7 @@
*/
import {
AnyServiceFactory,
ServiceFactory,
BackendFeature,
ExtensionPoint,
FactoryFunc,
@@ -43,7 +43,7 @@ export interface BackendRegisterInit {
* @public
*/
export interface CreateSpecializedBackendOptions {
services: AnyServiceFactory[];
services: ServiceFactory[];
}
export type ServiceHolder = {
+2 -2
View File
@@ -3,8 +3,8 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { AnyServiceFactory } from '@backstage/backend-plugin-api';
import { Backend } from '@backstage/backend-app-api';
import { ServiceFactory } from '@backstage/backend-plugin-api';
// @public (undocumented)
export function createBackend(options?: CreateBackendOptions): Backend;
@@ -12,6 +12,6 @@ export function createBackend(options?: CreateBackendOptions): Backend;
// @public (undocumented)
export interface CreateBackendOptions {
// (undocumented)
services?: AnyServiceFactory[];
services?: ServiceFactory[];
}
```
@@ -28,7 +28,7 @@ import {
tokenManagerFactory,
urlReaderFactory,
} from '@backstage/backend-app-api';
import { AnyServiceFactory } from '@backstage/backend-plugin-api';
import { ServiceFactory } from '@backstage/backend-plugin-api';
export const defaultServiceFactories = [
cacheFactory,
@@ -47,15 +47,15 @@ export const defaultServiceFactories = [
* @public
*/
export interface CreateBackendOptions {
services?: AnyServiceFactory[];
services?: ServiceFactory[];
}
/**
* @public
*/
export function createBackend(options?: CreateBackendOptions): Backend {
const services = new Map<string, AnyServiceFactory>(
defaultServiceFactories.map(sf => [sf.service.id, sf]),
const services = new Map<string, ServiceFactory>(
defaultServiceFactories.map(sf => [sf.service.id, sf as ServiceFactory]),
);
if (options?.services) {
+21 -24
View File
@@ -16,15 +16,6 @@ import { TokenManager } from '@backstage/backend-common';
import { TransportStreamOptions } from 'winston-transport';
import { UrlReader } from '@backstage/backend-common';
// @public (undocumented)
export type AnyServiceFactory = ServiceFactory<
unknown,
unknown,
{
[key in string]: unknown;
}
>;
// @public (undocumented)
export interface BackendFeature {
// (undocumented)
@@ -101,15 +92,22 @@ export function createExtensionPoint<T>(options: {
// @public (undocumented)
export function createServiceFactory<
Api,
Impl extends Api,
Deps extends {
TService,
TImpl extends TService,
TDeps extends {
[name in string]: unknown;
},
>(factory: ServiceFactory<Api, Impl, Deps>): ServiceFactory<Api, Api, {}>;
>(factory: {
service: ServiceRef<TService>;
deps: TypesToServiceRef<TDeps>;
factory(deps: DepsToDepFactories<TDeps>): Promise<FactoryFunc<TImpl>>;
}): ServiceFactory<TService>;
// @public (undocumented)
export function createServiceRef<T>(options: { id: string }): ServiceRef<T>;
export function createServiceRef<T>(options: {
id: string;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
}): ServiceRef<T>;
// @public (undocumented)
export const databaseServiceRef: ServiceRef<PluginDatabaseManager>;
@@ -168,22 +166,21 @@ export const permissionsServiceRef: ServiceRef<
export const schedulerServiceRef: ServiceRef<PluginTaskScheduler>;
// @public (undocumented)
export type ServiceFactory<
TApi,
TImpl extends TApi,
TDeps extends {
[name in string]: unknown;
},
> = {
service: ServiceRef<TApi>;
deps: TypesToServiceRef<TDeps>;
factory(deps: DepsToDepFactories<TDeps>): Promise<FactoryFunc<TImpl>>;
export type ServiceFactory<TService = unknown> = {
service: ServiceRef<TService>;
deps: {
[key in string]: ServiceRef<unknown>;
};
factory(deps: {
[key in string]: unknown;
}): Promise<FactoryFunc<TService>>;
};
// @public
export type ServiceRef<T> = {
id: string;
T: T;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
toString(): string;
$$ref: 'service';
};
@@ -20,6 +20,5 @@ export type {
DepsToDepFactories,
FactoryFunc,
ServiceFactory,
AnyServiceFactory,
} from './types';
export { createServiceRef, createServiceFactory } from './types';
@@ -32,9 +32,7 @@ export type ServiceRef<T> = {
* The default factory that will be used to create service
* instances if not other factory is provided.
*/
defaultFactory?: (
service: ServiceRef<T>,
) => Promise<ServiceFactory<T, T, {}>>;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
toString(): string;
@@ -53,31 +51,18 @@ export type DepsToDepFactories<T> = {
export type FactoryFunc<Impl> = (pluginId: string) => Promise<Impl>;
/** @public */
export type ServiceFactory<
TApi,
TImpl extends TApi,
TDeps extends { [name in string]: unknown },
> = {
service: ServiceRef<TApi>;
deps: TypesToServiceRef<TDeps>;
factory(deps: DepsToDepFactories<TDeps>): Promise<FactoryFunc<TImpl>>;
export type ServiceFactory<TService = unknown> = {
service: ServiceRef<TService>;
deps: { [key in string]: ServiceRef<unknown> };
factory(deps: { [key in string]: unknown }): Promise<FactoryFunc<TService>>;
};
/** @public */
export type AnyServiceFactory = ServiceFactory<
unknown,
unknown,
{ [key in string]: unknown }
>;
/**
* @public
*/
export function createServiceRef<T>(options: {
id: string;
defaultFactory?: (
service: ServiceRef<T>,
) => Promise<ServiceFactory<T, T, {}>>;
defaultFactory?: (service: ServiceRef<T>) => Promise<ServiceFactory<T>>;
}): ServiceRef<T> {
const { id, defaultFactory } = options;
return {
@@ -97,9 +82,13 @@ export function createServiceRef<T>(options: {
* @public
*/
export function createServiceFactory<
Api,
Impl extends Api,
Deps extends { [name in string]: unknown },
>(factory: ServiceFactory<Api, Impl, Deps>): ServiceFactory<Api, Api, {}> {
return factory;
TService,
TImpl extends TService,
TDeps extends { [name in string]: unknown },
>(factory: {
service: ServiceRef<TService>;
deps: TypesToServiceRef<TDeps>;
factory(deps: DepsToDepFactories<TDeps>): Promise<FactoryFunc<TImpl>>;
}): ServiceFactory<TService> {
return factory as ServiceFactory<TService>;
}
+2 -2
View File
@@ -3,10 +3,10 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { AnyServiceFactory } from '@backstage/backend-plugin-api';
import { BackendFeature } from '@backstage/backend-plugin-api';
import { ExtensionPoint } from '@backstage/backend-plugin-api';
import { Knex } from 'knex';
import { ServiceFactory } from '@backstage/backend-plugin-api';
import { ServiceRef } from '@backstage/backend-plugin-api';
// @public (undocumented)
@@ -45,7 +45,7 @@ export interface TestBackendOptions<
services?: readonly [
...{
[index in keyof TServices]:
| AnyServiceFactory
| ServiceFactory<TServices[index]>
| [ServiceRef<TServices[index]>, Partial<TServices[index]>];
},
];
@@ -16,7 +16,7 @@
import { createSpecializedBackend } from '@backstage/backend-app-api';
import {
AnyServiceFactory,
ServiceFactory,
ServiceRef,
createServiceFactory,
BackendFeature,
@@ -31,7 +31,7 @@ export interface TestBackendOptions<
services?: readonly [
...{
[index in keyof TServices]:
| AnyServiceFactory
| ServiceFactory<TServices[index]>
| [ServiceRef<TServices[index]>, Partial<TServices[index]>];
},
];
@@ -68,7 +68,7 @@ export async function startTestBackend<
factory: async () => async () => serviceDef[1],
});
}
return serviceDef as AnyServiceFactory;
return serviceDef as ServiceFactory;
});
const backend = createSpecializedBackend({