Skip to content

Commit

Permalink
ref to json
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Adelöw <[email protected]>
  • Loading branch information
freben committed Sep 6, 2024
1 parent b4ae0c8 commit 4f73c95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rude-fireants-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/backend-plugin-api': patch
---

Add a `toJSON` on refs so that they can appear in expectations in jest tests
18 changes: 17 additions & 1 deletion packages/backend-plugin-api/src/services/system/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import { BackendFeature } from '../../types';

/**
* TODO
* A reference to a backend service. You can use these references to mark
* dependencies on services and having their implementations injected
* automatically.
*
* @public
*/
Expand All @@ -39,6 +41,11 @@ export type ServiceRef<
*/
scope: TScope;

/**
* Marks whether the service is a multiton or not. Multiton services the
* opposite of singletons - they can be provided many times, and when depended
* on, you receive an array of all provided instances.
*/
multiton?: TInstances extends 'multiton' ? true : false;

/**
Expand Down Expand Up @@ -142,6 +149,15 @@ export function createServiceRef<
toString() {
return `serviceRef{${options.id}}`;
},
toJSON() {
// This avoids accidental calls to T happening e.g. in tests
return {
$$type: '@backstage/ServiceRef',
id,
scope,
multiton,
};
},
$$type: '@backstage/ServiceRef',
__defaultFactory: defaultFactory,
} as ServiceRef<TService, typeof scope, TInstances> & {
Expand Down

0 comments on commit 4f73c95

Please sign in to comment.