update usages of createServiceFactory

Co-authored-by: blam <ben@blam.sh>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-01-13 16:16:26 +01:00
parent eadc8f7df8
commit b8a14b3023
19 changed files with 101 additions and 96 deletions
@@ -27,10 +27,10 @@ export const cacheFactory = createServiceFactory({
config: coreServices.config,
plugin: coreServices.pluginMetadata,
},
async factory({ config }) {
const cacheManager = CacheManager.fromConfig(config);
return async ({ plugin }) => {
return cacheManager.forPlugin(plugin.getId());
};
async createRootContext({ config }) {
return CacheManager.fromConfig(config);
},
async factory({ plugin }, manager) {
return manager.forPlugin(plugin.getId());
},
});
@@ -28,8 +28,8 @@ export const databaseFactory = createServiceFactory({
config: coreServices.config,
plugin: coreServices.pluginMetadata,
},
async factory({ config }) {
const databaseManager = config.getOptional('backend.database')
async createRootContext({ config }) {
return config.getOptional('backend.database')
? DatabaseManager.fromConfig(config)
: DatabaseManager.fromConfig(
new ConfigReader({
@@ -38,9 +38,8 @@ export const databaseFactory = createServiceFactory({
},
}),
);
return async ({ plugin }) => {
return databaseManager.forPlugin(plugin.getId());
};
},
async factory({ plugin }, databaseManager) {
return databaseManager.forPlugin(plugin.getId());
},
});
@@ -26,10 +26,10 @@ export const discoveryFactory = createServiceFactory({
deps: {
config: coreServices.config,
},
async factory({ config }) {
const discovery = SingleHostDiscovery.fromConfig(config);
return async () => {
return discovery;
};
async createRootContext({ config }) {
return SingleHostDiscovery.fromConfig(config);
},
async factory(_, discovery) {
return discovery;
},
});
@@ -27,16 +27,27 @@ describe('httpRouterFactory', () => {
ServiceFactory<HttpRouterService>,
{ scope: 'root' }
>;
const innerFactory = await factory.factory({ rootHttpRouter });
const handler1 = () => {};
const router1 = await innerFactory({ plugin: { getId: () => 'test1' } });
const router1 = await factory.factory(
{
rootHttpRouter,
plugin: { getId: () => 'test1' },
},
undefined,
);
router1.use(handler1);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(1);
expect(rootHttpRouter.use).toHaveBeenCalledWith('/api/test1', handler1);
const handler2 = () => {};
const router2 = await innerFactory({ plugin: { getId: () => 'test2' } });
const router2 = await factory.factory(
{
rootHttpRouter,
plugin: { getId: () => 'test2' },
},
undefined,
);
router2.use(handler2);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(2);
expect(rootHttpRouter.use).toHaveBeenCalledWith('/api/test2', handler2);
@@ -47,10 +58,15 @@ describe('httpRouterFactory', () => {
const factory = httpRouterFactory({
getPath: id => `/some/${id}/path`,
}) as Exclude<ServiceFactory<HttpRouterService>, { scope: 'root' }>;
const innerFactory = await factory.factory({ rootHttpRouter });
const handler1 = () => {};
const router1 = await innerFactory({ plugin: { getId: () => 'test1' } });
const router1 = await factory.factory(
{
rootHttpRouter,
plugin: { getId: () => 'test1' },
},
undefined,
);
router1.use(handler1);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(1);
expect(rootHttpRouter.use).toHaveBeenCalledWith(
@@ -59,7 +75,13 @@ describe('httpRouterFactory', () => {
);
const handler2 = () => {};
const router2 = await innerFactory({ plugin: { getId: () => 'test2' } });
const router2 = await factory.factory(
{
rootHttpRouter,
plugin: { getId: () => 'test2' },
},
undefined,
);
router2.use(handler2);
expect(rootHttpRouter.use).toHaveBeenCalledTimes(2);
expect(rootHttpRouter.use).toHaveBeenCalledWith(
@@ -38,16 +38,13 @@ export const httpRouterFactory = createServiceFactory(
plugin: coreServices.pluginMetadata,
rootHttpRouter: coreServices.rootHttpRouter,
},
async factory({ rootHttpRouter }) {
async factory({ plugin, rootHttpRouter }) {
const getPath = options?.getPath ?? (id => `/api/${id}`);
return async ({ plugin }) => {
const path = getPath(plugin.getId());
return {
use(handler: Handler) {
rootHttpRouter.use(path, handler);
},
};
const path = getPath(plugin.getId());
return {
use(handler: Handler) {
rootHttpRouter.use(path, handler);
},
};
},
}),
@@ -34,17 +34,17 @@ export type IdentityFactoryOptions = {
};
/** @public */
export const identityFactory = createServiceFactory({
service: coreServices.identity,
deps: {
config: coreServices.config,
discovery: coreServices.discovery,
tokenManager: coreServices.tokenManager,
},
export const identityFactory = createServiceFactory(
(options?: IdentityFactoryOptions) => ({
service: coreServices.identity,
deps: {
config: coreServices.config,
discovery: coreServices.discovery,
tokenManager: coreServices.tokenManager,
},
async factory({}, options?: IdentityFactoryOptions) {
return async ({ discovery }) => {
async factory({ discovery }) {
return DefaultIdentityClient.create({ discovery, ...options });
};
},
});
},
}),
);
@@ -29,18 +29,16 @@ export const lifecycleFactory = createServiceFactory({
rootLifecycle: coreServices.rootLifecycle,
pluginMetadata: coreServices.pluginMetadata,
},
async factory({ rootLifecycle }) {
return async ({ logger, pluginMetadata }) => {
const plugin = pluginMetadata.getId();
return {
addShutdownHook(options: LifecycleServiceShutdownHook): void {
rootLifecycle.addShutdownHook({
...options,
async factory({ rootLifecycle, logger, pluginMetadata }) {
const plugin = pluginMetadata.getId();
return {
addShutdownHook(options: LifecycleServiceShutdownHook): void {
rootLifecycle.addShutdownHook({
...options,
logger: options.logger?.child({ plugin }) ?? logger,
});
},
};
logger: options.logger?.child({ plugin }) ?? logger,
});
},
};
},
});
@@ -26,9 +26,7 @@ export const loggerFactory = createServiceFactory({
rootLogger: coreServices.rootLogger,
plugin: coreServices.pluginMetadata,
},
async factory({ rootLogger }) {
return async ({ plugin }) => {
return rootLogger.child({ plugin: plugin.getId() });
};
async factory({ rootLogger, plugin }) {
return rootLogger.child({ plugin: plugin.getId() });
},
});
@@ -28,12 +28,10 @@ export const permissionsFactory = createServiceFactory({
discovery: coreServices.discovery,
tokenManager: coreServices.tokenManager,
},
async factory({ config }) {
return async ({ discovery, tokenManager }) => {
return ServerPermissionClient.fromConfig(config, {
discovery,
tokenManager,
});
};
async factory({ config, discovery, tokenManager }) {
return ServerPermissionClient.fromConfig(config, {
discovery,
tokenManager,
});
},
});
@@ -29,13 +29,11 @@ export const schedulerFactory = createServiceFactory({
databaseManager: coreServices.database,
logger: coreServices.logger,
},
async factory() {
return async ({ plugin, databaseManager, logger }) => {
return TaskScheduler.forPlugin({
pluginId: plugin.getId(),
databaseManager,
logger: loggerToWinstonLogger(logger),
});
};
async factory({ plugin, databaseManager, logger }) {
return TaskScheduler.forPlugin({
pluginId: plugin.getId(),
databaseManager,
logger: loggerToWinstonLogger(logger),
});
},
});
@@ -31,6 +31,6 @@ export const tokenManagerFactory = createServiceFactory({
const tokenManager = ServerTokenManager.fromConfig(config, {
logger,
});
return async () => tokenManager;
return tokenManager;
},
});
@@ -27,12 +27,10 @@ export const urlReaderFactory = createServiceFactory({
config: coreServices.config,
logger: coreServices.logger,
},
async factory() {
return async ({ config, logger }) => {
return UrlReaders.default({
config,
logger: loggerToWinstonLogger(logger),
});
};
async factory({ config, logger }) {
return UrlReaders.default({
config,
logger: loggerToWinstonLogger(logger),
});
},
});
@@ -53,7 +53,7 @@ describe('createSpecializedBackend', () => {
createServiceFactory({
service: coreServices.pluginMetadata,
deps: {},
factory: async () => async () => ({ getId: () => 'test' }),
factory: async () => ({ getId: () => 'test' }),
}),
],
}),
@@ -63,7 +63,7 @@ describe('createBackend', () => {
createServiceFactory({
service: coreServices.pluginMetadata,
deps: {},
factory: async () => async () => ({ getId: () => 'test' }),
factory: async () => ({ getId: () => 'test' }),
}),
],
}),
@@ -19,7 +19,8 @@ export type {
ServiceRefConfig,
TypesToServiceRef,
ServiceFactory,
ServiceFactoryConfig,
PluginServiceFactoryConfig,
RootServiceFactoryConfig,
ServiceFactoryOrFunction,
} from './types';
export { createServiceRef, createServiceFactory } from './types';
@@ -34,8 +34,6 @@ export const mockTokenManagerFactory = createServiceFactory({
service: coreServices.tokenManager,
deps: {},
async factory() {
return async () => {
return new TokenManagerMock();
};
return new TokenManagerMock();
},
});
@@ -106,7 +106,7 @@ describe('TestBackend', () => {
deps: {},
service: testRef,
factory: async () => {
return async () => testFn;
return testFn;
},
});
@@ -168,7 +168,7 @@ export async function startTestBackend<
backend: { baseUrl: `http://localhost:${port}`, listen: { port } },
}),
);
return async () => discovery;
return discovery;
},
});
@@ -179,13 +179,13 @@ export async function startTestBackend<
const [ref, impl] = serviceDef;
if (ref.scope === 'plugin') {
return createServiceFactory({
service: ref,
service: ref as ServiceRef<unknown, 'plugin'>,
deps: {},
factory: async () => async () => impl,
factory: async () => impl,
})();
}
return createServiceFactory({
service: ref,
service: ref as ServiceRef<unknown, 'root'>,
deps: {},
factory: async () => impl,
})();
+2 -4
View File
@@ -33,10 +33,8 @@ export const catalogServiceRef = createServiceRef<CatalogApi>({
deps: {
discoveryApi: coreServices.discovery,
},
async factory() {
return async ({ discoveryApi }) => {
return new CatalogClient({ discoveryApi });
};
async factory({ discoveryApi }) {
return new CatalogClient({ discoveryApi });
},
}),
});