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
+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>;
}