backend-plugin-api: backwards compat for service ref multiton field

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-08-06 14:13:23 +02:00
parent 7c5f3b0297
commit 6058a95955
3 changed files with 21 additions and 3 deletions
+1 -1
View File
@@ -703,7 +703,7 @@ export type ServiceRef<
> = {
id: string;
scope: TScope;
multiton: TInstances extends 'multiton' ? true : false;
multiton?: TInstances extends 'multiton' ? true : false;
T: TService;
$$type: '@backstage/ServiceRef';
};
@@ -324,6 +324,24 @@ describe('createServiceFactory', () => {
metaFactory();
});
it('should support old service refs without a multiton field', () => {
const oldPluginDep = pluginDep as Omit<typeof pluginDep, 'multiton'>; // Old refs don't have a multiton field
const metaFactory = createServiceFactory({
service: ref,
deps: {
plugin: oldPluginDep,
},
async factory({ plugin }) {
const plugin1: boolean = plugin;
// @ts-expect-error
const plugin2: number = plugin;
unused(plugin1, plugin2);
return 'x';
},
});
expect(metaFactory).toEqual(expect.any(Function));
});
it('should only allow objects as options', () => {
// @ts-expect-error
const metaFactory = createServiceFactory((_opts: string) => ({
@@ -39,7 +39,7 @@ export type ServiceRef<
*/
scope: TScope;
multiton: TInstances extends 'multiton' ? true : false;
multiton?: TInstances extends 'multiton' ? true : false;
/**
* Utility for getting the type of the service, using `typeof serviceRef.T`.
@@ -189,7 +189,7 @@ type ServiceRefsToInstances<
> = {
[key in keyof T as T[key]['scope'] extends TScope
? key
: never]: T[key]['multiton'] extends true
: never]: T[key]['multiton'] extends true | undefined
? Array<T[key]['T']>
: T[key]['T'];
};