@backstage/backend-plugin-api 0.3.0-next.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,85 @@
1
1
  # @backstage/backend-plugin-api
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 8e06f3cf00: Moved `loggerToWinstonLogger` to `@backstage/backend-common`.
8
+ - ecbec4ec4c: Updated all factory function creators to accept options as a top-level callback rather than extra parameter to the main factory function.
9
+
10
+ ### Patch Changes
11
+
12
+ - 6cfd4d7073: Added `RootLifecycleService` and `rootLifecycleServiceRef`, as well as added a `logger` option to the existing `LifecycleServiceShutdownHook`.
13
+ - ecc6bfe4c9: Added `ServiceFactoryOrFunction` type, for use when either a `ServiceFactory` or `() => ServiceFactory` can be used.
14
+ - 5b7bcd3c5e: Added `createSharedEnvironment` for creating a shared environment containing commonly used services in a split backend setup of the backend.
15
+ - 02b119ff93: Added a new `rootHttpRouterServiceRef` and `RootHttpRouterService` interface.
16
+ - 5e2cebe9a3: Migrate `UrlReader` into this package to gradually remove the dependency on backend-common.
17
+ - 843a0a158c: Added new core identity service.
18
+ - 5437fe488f: Migrated types related to `TokenManagerService`, `CacheService` and `DatabaseService` into backend-plugin-api.
19
+ - 6f02d23b01: Moved `PluginEndpointDiscovery` type from backend-common to backend-plugin-api.
20
+ - 483e907eaf: The `createServiceFactory` function has been updated to no longer use a duplicate callback pattern for plugin scoped services. The outer callback is now replaced by an optional `createRootContext` method. This change was made in order to support TypeScript 4.9, but it also simplifies the API surface a bit, especially for plugin scoped service factories that don't need to create a root context. In addition, the factory and root context functions can now be synchronous.
21
+
22
+ A factory that previously would have looked like this:
23
+
24
+ ```ts
25
+ createServiceFactory({
26
+ service: coreServices.cache,
27
+ deps: {
28
+ config: coreServices.config,
29
+ plugin: coreServices.pluginMetadata,
30
+ },
31
+ async factory({ config }) {
32
+ const cacheManager = CacheManager.fromConfig(config);
33
+ return async ({ plugin }) => {
34
+ return cacheManager.forPlugin(plugin.getId());
35
+ };
36
+ },
37
+ });
38
+ ```
39
+
40
+ Now instead looks like this:
41
+
42
+ ```ts
43
+ createServiceFactory({
44
+ service: coreServices.cache,
45
+ deps: {
46
+ config: coreServices.config,
47
+ plugin: coreServices.pluginMetadata,
48
+ },
49
+ async createRootContext({ config }) {
50
+ return CacheManager.fromConfig(config);
51
+ },
52
+ async factory({ plugin }, manager) {
53
+ return manager.forPlugin(plugin.getId());
54
+ },
55
+ });
56
+ ```
57
+
58
+ Although in many cases the `createRootContext` isn't needed, for example:
59
+
60
+ ```ts
61
+ createServiceFactory({
62
+ service: coreServices.logger,
63
+ deps: {
64
+ rootLogger: coreServices.rootLogger,
65
+ plugin: coreServices.pluginMetadata,
66
+ },
67
+ factory({ rootLogger, plugin }) {
68
+ return rootLogger.child({ plugin: plugin.getId() });
69
+ },
70
+ });
71
+ ```
72
+
73
+ - 16054afdec: Documented `coreServices` an all of its members.
74
+ - 0e63aab311: Updated the `RootLoggerService` to also have an `addRedactions` method.
75
+ - 62b04bb865: Updates all `create*` methods to simplify their type definitions and ensure they all have configuration interfaces.
76
+ - Updated dependencies
77
+ - @backstage/backend-tasks@0.4.1
78
+ - @backstage/config@1.0.6
79
+ - @backstage/types@1.0.2
80
+ - @backstage/plugin-auth-node@0.2.9
81
+ - @backstage/plugin-permission-common@0.7.3
82
+
3
83
  ## 0.3.0-next.1
4
84
 
5
85
  ### Minor Changes
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/backend-plugin-api",
3
- "version": "0.3.0-next.1",
3
+ "version": "0.3.0",
4
4
  "main": "../dist/index.cjs.js",
5
5
  "types": "../dist/index.alpha.d.ts"
6
6
  }
@@ -8,6 +8,7 @@
8
8
 
9
9
  import { Config } from '@backstage/config';
10
10
  import { Handler } from 'express';
11
+ import { IdentityApi } from '@backstage/plugin-auth-node';
11
12
  import { JsonValue } from '@backstage/types';
12
13
  import { Knex } from 'knex';
13
14
  import { PermissionEvaluator } from '@backstage/plugin-permission-common';
@@ -21,16 +22,16 @@ export declare interface BackendFeature {
21
22
  }
22
23
 
23
24
  /** @public */
24
- export declare interface BackendModuleConfig<TOptions> {
25
+ export declare interface BackendModuleConfig {
25
26
  pluginId: string;
26
27
  moduleId: string;
27
- register(reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>, options: TOptions): void;
28
+ register(reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>): void;
28
29
  }
29
30
 
30
31
  /** @public */
31
- export declare interface BackendPluginConfig<TOptions> {
32
+ export declare interface BackendPluginConfig {
32
33
  id: string;
33
- register(reg: BackendRegistrationPoints, options: TOptions): void;
34
+ register(reg: BackendRegistrationPoints): void;
34
35
  }
35
36
 
36
37
  /** @public */
@@ -217,6 +218,12 @@ export declare namespace coreServices {
217
218
  * @public
218
219
  */
219
220
  const urlReader: ServiceRef<UrlReaderService, "plugin">;
221
+ /**
222
+ * The service reference for the plugin scoped {@link IdentityService}.
223
+ *
224
+ * @public
225
+ */
226
+ const identity: ServiceRef<IdentityService, "plugin">;
220
227
  }
221
228
 
222
229
  /**
@@ -233,20 +240,73 @@ export declare namespace coreServices {
233
240
  *
234
241
  * The `pluginId` should exactly match the `id` of the plugin that the module extends.
235
242
  */
236
- export declare function createBackendModule<TOptions extends MaybeOptions = undefined>(config: BackendModuleConfig<TOptions>): FactoryFunctionWithOptions<BackendFeature, TOptions>;
243
+ export declare function createBackendModule<TOptions extends [options?: object] = []>(config: BackendModuleConfig | ((...params: TOptions) => BackendModuleConfig)): (...params: TOptions) => BackendFeature;
237
244
 
238
245
  /** @public */
239
- export declare function createBackendPlugin<TOptions extends MaybeOptions = undefined>(config: BackendPluginConfig<TOptions>): FactoryFunctionWithOptions<BackendFeature, TOptions>;
246
+ export declare function createBackendPlugin<TOptions extends [options?: object] = []>(config: BackendPluginConfig | ((...params: TOptions) => BackendPluginConfig)): (...params: TOptions) => BackendFeature;
240
247
 
241
248
  /** @public */
242
249
  export declare function createExtensionPoint<T>(config: ExtensionPointConfig): ExtensionPoint<T>;
243
250
 
244
251
  /**
252
+ * Creates a root scoped service factory without options.
253
+ *
254
+ * @public
255
+ * @param config - The service factory configuration.
256
+ */
257
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
258
+ [name in string]: ServiceRef<unknown>;
259
+ }, TOpts extends object | undefined = undefined>(config: RootServiceFactoryConfig<TService, TImpl, TDeps>): () => ServiceFactory<TService>;
260
+
261
+ /**
262
+ * Creates a root scoped service factory with optional options.
263
+ *
264
+ * @public
265
+ * @param config - The service factory configuration.
266
+ */
267
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
268
+ [name in string]: ServiceRef<unknown>;
269
+ }, TOpts extends object | undefined = undefined>(config: (options?: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>): (options?: TOpts) => ServiceFactory<TService>;
270
+
271
+ /**
272
+ * Creates a root scoped service factory with required options.
273
+ *
274
+ * @public
275
+ * @param config - The service factory configuration.
276
+ */
277
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
278
+ [name in string]: ServiceRef<unknown>;
279
+ }, TOpts extends object | undefined = undefined>(config: (options: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>): (options: TOpts) => ServiceFactory<TService>;
280
+
281
+ /**
282
+ * Creates a plugin scoped service factory without options.
283
+ *
284
+ * @public
285
+ * @param config - The service factory configuration.
286
+ */
287
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
288
+ [name in string]: ServiceRef<unknown>;
289
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>): () => ServiceFactory<TService>;
290
+
291
+ /**
292
+ * Creates a plugin scoped service factory with optional options.
293
+ *
294
+ * @public
295
+ * @param config - The service factory configuration.
296
+ */
297
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
298
+ [name in string]: ServiceRef<unknown>;
299
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: (options?: TOpts) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>): (options?: TOpts) => ServiceFactory<TService>;
300
+
301
+ /**
302
+ * Creates a plugin scoped service factory with required options.
303
+ *
245
304
  * @public
305
+ * @param config - The service factory configuration.
246
306
  */
247
- export declare function createServiceFactory<TService, TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends {
307
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
248
308
  [name in string]: ServiceRef<unknown>;
249
- }, TOpts extends MaybeOptions = undefined>(config: ServiceFactoryConfig<TService, TScope, TImpl, TDeps, TOpts>): FactoryFunctionWithOptions<ServiceFactory<TService>, TOpts>;
309
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps> | ((options: TOpts) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>)): (options: TOpts) => ServiceFactory<TService>;
250
310
 
251
311
  /**
252
312
  * Creates a new service definition. This overload is used to create plugin scoped services.
@@ -262,9 +322,15 @@ export declare function createServiceRef<TService>(config: ServiceRefConfig<TSer
262
322
  */
263
323
  export declare function createServiceRef<TService>(config: ServiceRefConfig<TService, 'root'>): ServiceRef<TService, 'root'>;
264
324
 
325
+ /**
326
+ * Creates a shared backend environment which can be used to create multiple backends
327
+ * @public
328
+ */
329
+ export declare function createSharedEnvironment<TOptions extends [options?: object] = []>(config: SharedBackendEnvironmentConfig | ((...params: TOptions) => SharedBackendEnvironmentConfig)): (...options: TOptions) => SharedBackendEnvironment;
330
+
265
331
  /**
266
332
  * The DatabaseService manages access to databases that Plugins get.
267
- *gs
333
+ *
268
334
  * @public
269
335
  */
270
336
  export declare interface DatabaseService {
@@ -356,13 +422,6 @@ export declare interface ExtensionPointConfig {
356
422
  id: string;
357
423
  }
358
424
 
359
- /**
360
- * Helper type that makes the options argument optional if options are not required.
361
- *
362
- * @ignore
363
- */
364
- declare type FactoryFunctionWithOptions<TResult, TOptions> = undefined extends TOptions ? (options?: TOptions) => TResult : (options: TOptions) => TResult;
365
-
366
425
  /**
367
426
  * @public
368
427
  */
@@ -370,6 +429,10 @@ export declare interface HttpRouterService {
370
429
  use(handler: Handler): void;
371
430
  }
372
431
 
432
+ /** @public */
433
+ export declare interface IdentityService extends IdentityApi {
434
+ }
435
+
373
436
  /**
374
437
  * @public
375
438
  **/
@@ -411,13 +474,6 @@ export declare type LogMeta = {
411
474
  [name: string]: unknown;
412
475
  };
413
476
 
414
- /**
415
- * Base type for options objects that aren't required.
416
- *
417
- * @ignore
418
- */
419
- declare type MaybeOptions = object | undefined;
420
-
421
477
  /** @public */
422
478
  export declare interface PermissionsService extends PermissionEvaluator {
423
479
  }
@@ -429,6 +485,16 @@ export declare interface PluginMetadataService {
429
485
  getId(): string;
430
486
  }
431
487
 
488
+ /** @public */
489
+ export declare interface PluginServiceFactoryConfig<TService, TContext, TImpl extends TService, TDeps extends {
490
+ [name in string]: ServiceRef<unknown>;
491
+ }> {
492
+ service: ServiceRef<TService, 'plugin'>;
493
+ deps: TDeps;
494
+ createRootContext?(deps: ServiceRefsToInstances<TDeps, 'root'>): TContext | Promise<TContext>;
495
+ factory(deps: ServiceRefsToInstances<TDeps>, context: TContext): TImpl | Promise<TImpl>;
496
+ }
497
+
432
498
  /**
433
499
  * An options object for {@link UrlReaderService.readTree} operations.
434
500
  *
@@ -621,6 +687,15 @@ export declare interface RootLifecycleService extends LifecycleService {
621
687
  export declare interface RootLoggerService extends LoggerService {
622
688
  }
623
689
 
690
+ /** @public */
691
+ export declare interface RootServiceFactoryConfig<TService, TImpl extends TService, TDeps extends {
692
+ [name in string]: ServiceRef<unknown>;
693
+ }> {
694
+ service: ServiceRef<TService, 'root'>;
695
+ deps: TDeps;
696
+ factory(deps: ServiceRefsToInstances<TDeps, 'root'>): TImpl | Promise<TImpl>;
697
+ }
698
+
624
699
  /** @public */
625
700
  export declare interface SchedulerService extends PluginTaskScheduler {
626
701
  }
@@ -701,22 +776,14 @@ export declare type ServiceFactory<TService = unknown> = {
701
776
  deps: {
702
777
  [key in string]: ServiceRef<unknown>;
703
778
  };
704
- factory(deps: {
779
+ createRootContext?(deps: {
705
780
  [key in string]: unknown;
706
- }): Promise<(deps: {
781
+ }): Promise<unknown>;
782
+ factory(deps: {
707
783
  [key in string]: unknown;
708
- }) => Promise<TService>>;
784
+ }, context: unknown): Promise<TService>;
709
785
  };
710
786
 
711
- /** @public */
712
- export declare interface ServiceFactoryConfig<TService, TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends {
713
- [name in string]: ServiceRef<unknown>;
714
- }, TOpts extends MaybeOptions = undefined> {
715
- service: ServiceRef<TService, TScope>;
716
- deps: TDeps;
717
- factory(deps: ServiceRefsToInstances<TDeps, 'root'>, options: TOpts): TScope extends 'root' ? Promise<TImpl> : Promise<(deps: ServiceRefsToInstances<TDeps>) => Promise<TImpl>>;
718
- }
719
-
720
787
  /**
721
788
  * Represents either a {@link ServiceFactory} or a function that returns one.
722
789
  *
@@ -761,11 +828,21 @@ export declare interface ServiceRefConfig<TService, TScope extends 'root' | 'plu
761
828
  declare type ServiceRefsToInstances<T extends {
762
829
  [key in string]: ServiceRef<unknown>;
763
830
  }, TScope extends 'root' | 'plugin' = 'root' | 'plugin'> = {
764
- [name in {
765
- [key in keyof T]: T[key] extends ServiceRef<unknown, TScope> ? key : never;
766
- }[keyof T]]: T[name] extends ServiceRef<infer TImpl> ? TImpl : never;
831
+ [key in keyof T as T[key]['scope'] extends TScope ? key : never]: T[key]['T'];
767
832
  };
768
833
 
834
+ /**
835
+ * @public
836
+ */
837
+ export declare interface SharedBackendEnvironment {
838
+ $$type: 'SharedBackendEnvironment';
839
+ }
840
+
841
+ /** @public */
842
+ export declare interface SharedBackendEnvironmentConfig {
843
+ services?: ServiceFactoryOrFunction[];
844
+ }
845
+
769
846
  /**
770
847
  * Interface for creating and validating tokens.
771
848
  *
@@ -8,6 +8,7 @@
8
8
 
9
9
  import { Config } from '@backstage/config';
10
10
  import { Handler } from 'express';
11
+ import { IdentityApi } from '@backstage/plugin-auth-node';
11
12
  import { JsonValue } from '@backstage/types';
12
13
  import { Knex } from 'knex';
13
14
  import { PermissionEvaluator } from '@backstage/plugin-permission-common';
@@ -21,16 +22,16 @@ export declare interface BackendFeature {
21
22
  }
22
23
 
23
24
  /** @public */
24
- export declare interface BackendModuleConfig<TOptions> {
25
+ export declare interface BackendModuleConfig {
25
26
  pluginId: string;
26
27
  moduleId: string;
27
- register(reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>, options: TOptions): void;
28
+ register(reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>): void;
28
29
  }
29
30
 
30
31
  /** @public */
31
- export declare interface BackendPluginConfig<TOptions> {
32
+ export declare interface BackendPluginConfig {
32
33
  id: string;
33
- register(reg: BackendRegistrationPoints, options: TOptions): void;
34
+ register(reg: BackendRegistrationPoints): void;
34
35
  }
35
36
 
36
37
  /** @public */
@@ -217,6 +218,12 @@ export declare namespace coreServices {
217
218
  * @public
218
219
  */
219
220
  const urlReader: ServiceRef<UrlReaderService, "plugin">;
221
+ /**
222
+ * The service reference for the plugin scoped {@link IdentityService}.
223
+ *
224
+ * @public
225
+ */
226
+ const identity: ServiceRef<IdentityService, "plugin">;
220
227
  }
221
228
 
222
229
  /**
@@ -233,20 +240,73 @@ export declare namespace coreServices {
233
240
  *
234
241
  * The `pluginId` should exactly match the `id` of the plugin that the module extends.
235
242
  */
236
- export declare function createBackendModule<TOptions extends MaybeOptions = undefined>(config: BackendModuleConfig<TOptions>): FactoryFunctionWithOptions<BackendFeature, TOptions>;
243
+ export declare function createBackendModule<TOptions extends [options?: object] = []>(config: BackendModuleConfig | ((...params: TOptions) => BackendModuleConfig)): (...params: TOptions) => BackendFeature;
237
244
 
238
245
  /** @public */
239
- export declare function createBackendPlugin<TOptions extends MaybeOptions = undefined>(config: BackendPluginConfig<TOptions>): FactoryFunctionWithOptions<BackendFeature, TOptions>;
246
+ export declare function createBackendPlugin<TOptions extends [options?: object] = []>(config: BackendPluginConfig | ((...params: TOptions) => BackendPluginConfig)): (...params: TOptions) => BackendFeature;
240
247
 
241
248
  /** @public */
242
249
  export declare function createExtensionPoint<T>(config: ExtensionPointConfig): ExtensionPoint<T>;
243
250
 
244
251
  /**
252
+ * Creates a root scoped service factory without options.
253
+ *
254
+ * @public
255
+ * @param config - The service factory configuration.
256
+ */
257
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
258
+ [name in string]: ServiceRef<unknown>;
259
+ }, TOpts extends object | undefined = undefined>(config: RootServiceFactoryConfig<TService, TImpl, TDeps>): () => ServiceFactory<TService>;
260
+
261
+ /**
262
+ * Creates a root scoped service factory with optional options.
263
+ *
264
+ * @public
265
+ * @param config - The service factory configuration.
266
+ */
267
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
268
+ [name in string]: ServiceRef<unknown>;
269
+ }, TOpts extends object | undefined = undefined>(config: (options?: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>): (options?: TOpts) => ServiceFactory<TService>;
270
+
271
+ /**
272
+ * Creates a root scoped service factory with required options.
273
+ *
274
+ * @public
275
+ * @param config - The service factory configuration.
276
+ */
277
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
278
+ [name in string]: ServiceRef<unknown>;
279
+ }, TOpts extends object | undefined = undefined>(config: (options: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>): (options: TOpts) => ServiceFactory<TService>;
280
+
281
+ /**
282
+ * Creates a plugin scoped service factory without options.
283
+ *
284
+ * @public
285
+ * @param config - The service factory configuration.
286
+ */
287
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
288
+ [name in string]: ServiceRef<unknown>;
289
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>): () => ServiceFactory<TService>;
290
+
291
+ /**
292
+ * Creates a plugin scoped service factory with optional options.
293
+ *
294
+ * @public
295
+ * @param config - The service factory configuration.
296
+ */
297
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
298
+ [name in string]: ServiceRef<unknown>;
299
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: (options?: TOpts) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>): (options?: TOpts) => ServiceFactory<TService>;
300
+
301
+ /**
302
+ * Creates a plugin scoped service factory with required options.
303
+ *
245
304
  * @public
305
+ * @param config - The service factory configuration.
246
306
  */
247
- export declare function createServiceFactory<TService, TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends {
307
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
248
308
  [name in string]: ServiceRef<unknown>;
249
- }, TOpts extends MaybeOptions = undefined>(config: ServiceFactoryConfig<TService, TScope, TImpl, TDeps, TOpts>): FactoryFunctionWithOptions<ServiceFactory<TService>, TOpts>;
309
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps> | ((options: TOpts) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>)): (options: TOpts) => ServiceFactory<TService>;
250
310
 
251
311
  /**
252
312
  * Creates a new service definition. This overload is used to create plugin scoped services.
@@ -262,9 +322,15 @@ export declare function createServiceRef<TService>(config: ServiceRefConfig<TSer
262
322
  */
263
323
  export declare function createServiceRef<TService>(config: ServiceRefConfig<TService, 'root'>): ServiceRef<TService, 'root'>;
264
324
 
325
+ /**
326
+ * Creates a shared backend environment which can be used to create multiple backends
327
+ * @public
328
+ */
329
+ export declare function createSharedEnvironment<TOptions extends [options?: object] = []>(config: SharedBackendEnvironmentConfig | ((...params: TOptions) => SharedBackendEnvironmentConfig)): (...options: TOptions) => SharedBackendEnvironment;
330
+
265
331
  /**
266
332
  * The DatabaseService manages access to databases that Plugins get.
267
- *gs
333
+ *
268
334
  * @public
269
335
  */
270
336
  export declare interface DatabaseService {
@@ -356,13 +422,6 @@ export declare interface ExtensionPointConfig {
356
422
  id: string;
357
423
  }
358
424
 
359
- /**
360
- * Helper type that makes the options argument optional if options are not required.
361
- *
362
- * @ignore
363
- */
364
- declare type FactoryFunctionWithOptions<TResult, TOptions> = undefined extends TOptions ? (options?: TOptions) => TResult : (options: TOptions) => TResult;
365
-
366
425
  /**
367
426
  * @public
368
427
  */
@@ -370,6 +429,10 @@ export declare interface HttpRouterService {
370
429
  use(handler: Handler): void;
371
430
  }
372
431
 
432
+ /** @public */
433
+ export declare interface IdentityService extends IdentityApi {
434
+ }
435
+
373
436
  /**
374
437
  * @public
375
438
  **/
@@ -411,13 +474,6 @@ export declare type LogMeta = {
411
474
  [name: string]: unknown;
412
475
  };
413
476
 
414
- /**
415
- * Base type for options objects that aren't required.
416
- *
417
- * @ignore
418
- */
419
- declare type MaybeOptions = object | undefined;
420
-
421
477
  /** @public */
422
478
  export declare interface PermissionsService extends PermissionEvaluator {
423
479
  }
@@ -429,6 +485,16 @@ export declare interface PluginMetadataService {
429
485
  getId(): string;
430
486
  }
431
487
 
488
+ /** @public */
489
+ export declare interface PluginServiceFactoryConfig<TService, TContext, TImpl extends TService, TDeps extends {
490
+ [name in string]: ServiceRef<unknown>;
491
+ }> {
492
+ service: ServiceRef<TService, 'plugin'>;
493
+ deps: TDeps;
494
+ createRootContext?(deps: ServiceRefsToInstances<TDeps, 'root'>): TContext | Promise<TContext>;
495
+ factory(deps: ServiceRefsToInstances<TDeps>, context: TContext): TImpl | Promise<TImpl>;
496
+ }
497
+
432
498
  /**
433
499
  * An options object for {@link UrlReaderService.readTree} operations.
434
500
  *
@@ -621,6 +687,15 @@ export declare interface RootLifecycleService extends LifecycleService {
621
687
  export declare interface RootLoggerService extends LoggerService {
622
688
  }
623
689
 
690
+ /** @public */
691
+ export declare interface RootServiceFactoryConfig<TService, TImpl extends TService, TDeps extends {
692
+ [name in string]: ServiceRef<unknown>;
693
+ }> {
694
+ service: ServiceRef<TService, 'root'>;
695
+ deps: TDeps;
696
+ factory(deps: ServiceRefsToInstances<TDeps, 'root'>): TImpl | Promise<TImpl>;
697
+ }
698
+
624
699
  /** @public */
625
700
  export declare interface SchedulerService extends PluginTaskScheduler {
626
701
  }
@@ -701,22 +776,14 @@ export declare type ServiceFactory<TService = unknown> = {
701
776
  deps: {
702
777
  [key in string]: ServiceRef<unknown>;
703
778
  };
704
- factory(deps: {
779
+ createRootContext?(deps: {
705
780
  [key in string]: unknown;
706
- }): Promise<(deps: {
781
+ }): Promise<unknown>;
782
+ factory(deps: {
707
783
  [key in string]: unknown;
708
- }) => Promise<TService>>;
784
+ }, context: unknown): Promise<TService>;
709
785
  };
710
786
 
711
- /** @public */
712
- export declare interface ServiceFactoryConfig<TService, TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends {
713
- [name in string]: ServiceRef<unknown>;
714
- }, TOpts extends MaybeOptions = undefined> {
715
- service: ServiceRef<TService, TScope>;
716
- deps: TDeps;
717
- factory(deps: ServiceRefsToInstances<TDeps, 'root'>, options: TOpts): TScope extends 'root' ? Promise<TImpl> : Promise<(deps: ServiceRefsToInstances<TDeps>) => Promise<TImpl>>;
718
- }
719
-
720
787
  /**
721
788
  * Represents either a {@link ServiceFactory} or a function that returns one.
722
789
  *
@@ -761,11 +828,21 @@ export declare interface ServiceRefConfig<TService, TScope extends 'root' | 'plu
761
828
  declare type ServiceRefsToInstances<T extends {
762
829
  [key in string]: ServiceRef<unknown>;
763
830
  }, TScope extends 'root' | 'plugin' = 'root' | 'plugin'> = {
764
- [name in {
765
- [key in keyof T]: T[key] extends ServiceRef<unknown, TScope> ? key : never;
766
- }[keyof T]]: T[name] extends ServiceRef<infer TImpl> ? TImpl : never;
831
+ [key in keyof T as T[key]['scope'] extends TScope ? key : never]: T[key]['T'];
767
832
  };
768
833
 
834
+ /**
835
+ * @public
836
+ */
837
+ export declare interface SharedBackendEnvironment {
838
+ $$type: 'SharedBackendEnvironment';
839
+ }
840
+
841
+ /** @public */
842
+ export declare interface SharedBackendEnvironmentConfig {
843
+ services?: ServiceFactoryOrFunction[];
844
+ }
845
+
769
846
  /**
770
847
  * Interface for creating and validating tokens.
771
848
  *
package/dist/index.cjs.js CHANGED
@@ -19,14 +19,21 @@ function createServiceRef(config) {
19
19
  };
20
20
  }
21
21
  function createServiceFactory(config) {
22
- return (options) => ({
23
- scope: config.service.scope,
24
- service: config.service,
25
- deps: config.deps,
26
- factory(deps) {
27
- return config.factory(deps, options);
28
- }
29
- });
22
+ const configCallback = typeof config === "function" ? config : () => config;
23
+ return (options) => {
24
+ const c = configCallback(options);
25
+ return {
26
+ ...c,
27
+ ..."createRootContext" in c ? {
28
+ createRootContext: async (deps) => {
29
+ var _a;
30
+ return (_a = c == null ? void 0 : c.createRootContext) == null ? void 0 : _a.call(c, deps);
31
+ }
32
+ } : {},
33
+ factory: async (deps, ctx) => c.factory(deps, ctx),
34
+ scope: c.service.scope
35
+ };
36
+ };
30
37
  }
31
38
 
32
39
  exports.coreServices = void 0;
@@ -48,8 +55,41 @@ exports.coreServices = void 0;
48
55
  coreServices2.scheduler = createServiceRef({ id: "core.scheduler" });
49
56
  coreServices2.tokenManager = createServiceRef({ id: "core.tokenManager" });
50
57
  coreServices2.urlReader = createServiceRef({ id: "core.urlReader" });
58
+ coreServices2.identity = createServiceRef({ id: "core.identity" });
51
59
  })(exports.coreServices || (exports.coreServices = {}));
52
60
 
61
+ function createSharedEnvironment(config) {
62
+ const configCallback = typeof config === "function" ? config : () => config;
63
+ return (...options) => {
64
+ var _a;
65
+ const actualConfig = configCallback(...options);
66
+ const services = (_a = actualConfig == null ? void 0 : actualConfig.services) == null ? void 0 : _a.map(
67
+ (sf) => typeof sf === "function" ? sf() : sf
68
+ );
69
+ const exists = /* @__PURE__ */ new Set();
70
+ const duplicates = /* @__PURE__ */ new Set();
71
+ for (const { service } of services != null ? services : []) {
72
+ if (exists.has(service.id)) {
73
+ duplicates.add(service.id);
74
+ } else {
75
+ exists.add(service.id);
76
+ }
77
+ }
78
+ if (duplicates.size > 0) {
79
+ const dupStr = [...duplicates].map((id) => `'${id}'`).join(", ");
80
+ throw new Error(
81
+ `Duplicate service implementations provided in shared environment for ${dupStr}`
82
+ );
83
+ }
84
+ const env = {
85
+ $$type: "SharedBackendEnvironment",
86
+ version: "v1",
87
+ services
88
+ };
89
+ return env;
90
+ };
91
+ }
92
+
53
93
  function createExtensionPoint(config) {
54
94
  return {
55
95
  id: config.id,
@@ -64,18 +104,25 @@ function createExtensionPoint(config) {
64
104
  };
65
105
  }
66
106
  function createBackendPlugin(config) {
67
- return (options) => ({
68
- id: config.id,
69
- register(register) {
70
- return config.register(register, options);
71
- }
72
- });
107
+ if (typeof config === "function") {
108
+ return config;
109
+ }
110
+ return () => config;
73
111
  }
74
112
  function createBackendModule(config) {
75
- return (options) => ({
113
+ if (typeof config === "function") {
114
+ return (...options) => {
115
+ const c = config(...options);
116
+ return {
117
+ id: `${c.pluginId}.${c.moduleId}`,
118
+ register: c.register
119
+ };
120
+ };
121
+ }
122
+ return () => ({
76
123
  id: `${config.pluginId}.${config.moduleId}`,
77
124
  register(register) {
78
- return config.register(register, options);
125
+ return config.register(register);
79
126
  }
80
127
  });
81
128
  }
@@ -85,4 +132,5 @@ exports.createBackendPlugin = createBackendPlugin;
85
132
  exports.createExtensionPoint = createExtensionPoint;
86
133
  exports.createServiceFactory = createServiceFactory;
87
134
  exports.createServiceRef = createServiceRef;
135
+ exports.createSharedEnvironment = createSharedEnvironment;
88
136
  //# sourceMappingURL=index.cjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/services/system/types.ts","../src/services/definitions/coreServices.ts","../src/wiring/factories.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { FactoryFunctionWithOptions, MaybeOptions } from '../../types';\n\n/**\n * TODO\n *\n * @public\n */\nexport type ServiceRef<\n TService,\n TScope extends 'root' | 'plugin' = 'root' | 'plugin',\n> = {\n id: string;\n\n /**\n * This determines the scope at which this service is available.\n *\n * Root scoped services are available to all other services but\n * may only depend on other root scoped services.\n *\n * Plugin scoped services are only available to other plugin scoped\n * services but may depend on all other services.\n */\n scope: TScope;\n\n /**\n * Utility for getting the type of the service, using `typeof serviceRef.T`.\n * Attempting to actually read this value will result in an exception.\n */\n T: TService;\n\n toString(): string;\n\n $$ref: 'service';\n};\n\n/** @public */\nexport type TypesToServiceRef<T> = { [key in keyof T]: ServiceRef<T[key]> };\n\n/** @public */\nexport type ServiceFactory<TService = unknown> =\n | {\n // This scope prop is needed in addition to the service ref, as TypeScript\n // can't properly discriminate the two factory types otherwise.\n scope: 'root';\n service: ServiceRef<TService, 'root'>;\n deps: { [key in string]: ServiceRef<unknown> };\n factory(deps: { [key in string]: unknown }): Promise<TService>;\n }\n | {\n scope: 'plugin';\n service: ServiceRef<TService, 'plugin'>;\n deps: { [key in string]: ServiceRef<unknown> };\n factory(deps: { [key in string]: unknown }): Promise<\n (deps: { [key in string]: unknown }) => Promise<TService>\n >;\n };\n\n/**\n * Represents either a {@link ServiceFactory} or a function that returns one.\n *\n * @public\n */\nexport type ServiceFactoryOrFunction<TService = unknown> =\n | ServiceFactory<TService>\n | (() => ServiceFactory<TService>);\n\n/** @public */\nexport interface ServiceRefConfig<TService, TScope extends 'root' | 'plugin'> {\n id: string;\n scope?: TScope;\n defaultFactory?: (\n service: ServiceRef<TService, TScope>,\n ) => Promise<ServiceFactoryOrFunction<TService>>;\n}\n\n/**\n * Creates a new service definition. This overload is used to create plugin scoped services.\n *\n * @public\n */\nexport function createServiceRef<TService>(\n config: ServiceRefConfig<TService, 'plugin'>,\n): ServiceRef<TService, 'plugin'>;\n\n/**\n * Creates a new service definition. This overload is used to create root scoped services.\n *\n * @public\n */\nexport function createServiceRef<TService>(\n config: ServiceRefConfig<TService, 'root'>,\n): ServiceRef<TService, 'root'>;\nexport function createServiceRef<TService>(\n config: ServiceRefConfig<TService, any>,\n): ServiceRef<TService, any> {\n const { id, scope = 'plugin', defaultFactory } = config;\n return {\n id,\n scope,\n get T(): TService {\n throw new Error(`tried to read ServiceRef.T of ${this}`);\n },\n toString() {\n return `serviceRef{${config.id}}`;\n },\n $$ref: 'service', // TODO: declare\n __defaultFactory: defaultFactory,\n } as ServiceRef<TService, typeof scope> & {\n __defaultFactory?: (\n service: ServiceRef<TService>,\n ) => Promise<ServiceFactory<TService> | (() => ServiceFactory<TService>)>;\n };\n}\n\n/** @ignore */\ntype ServiceRefsToInstances<\n T extends { [key in string]: ServiceRef<unknown> },\n TScope extends 'root' | 'plugin' = 'root' | 'plugin',\n> = {\n [name in {\n [key in keyof T]: T[key] extends ServiceRef<unknown, TScope> ? key : never;\n }[keyof T]]: T[name] extends ServiceRef<infer TImpl> ? TImpl : never;\n};\n\n/** @public */\nexport interface ServiceFactoryConfig<\n TService,\n TScope extends 'root' | 'plugin',\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TOpts extends MaybeOptions = undefined,\n> {\n service: ServiceRef<TService, TScope>;\n deps: TDeps;\n factory(\n deps: ServiceRefsToInstances<TDeps, 'root'>,\n options: TOpts,\n ): TScope extends 'root'\n ? Promise<TImpl>\n : Promise<(deps: ServiceRefsToInstances<TDeps>) => Promise<TImpl>>;\n}\n\n/**\n * @public\n */\nexport function createServiceFactory<\n TService,\n TScope extends 'root' | 'plugin',\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TOpts extends MaybeOptions = undefined,\n>(\n config: ServiceFactoryConfig<TService, TScope, TImpl, TDeps, TOpts>,\n): FactoryFunctionWithOptions<ServiceFactory<TService>, TOpts> {\n return (options?: TOpts) =>\n ({\n scope: config.service.scope,\n service: config.service,\n deps: config.deps,\n factory(deps: ServiceRefsToInstances<TDeps, 'root'>) {\n return config.factory(deps, options!);\n },\n } as ServiceFactory<TService>);\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createServiceRef } from '../system';\n\n/**\n * All core services references\n *\n * @public\n */\nexport namespace coreServices {\n /**\n * The service reference for the plugin scoped {@link CacheService}.\n *\n * @public\n */\n export const cache = createServiceRef<import('./CacheService').CacheService>({\n id: 'core.cache',\n });\n\n /**\n * The service reference for the root scoped {@link ConfigService}.\n *\n * @public\n */\n export const config = createServiceRef<\n import('./ConfigService').ConfigService\n >({ id: 'core.config', scope: 'root' });\n\n /**\n * The service reference for the plugin scoped {@link DatabaseService}.\n *\n * @public\n */\n export const database = createServiceRef<\n import('./DatabaseService').DatabaseService\n >({ id: 'core.database' });\n\n /**\n * The service reference for the plugin scoped {@link DiscoveryService}.\n *\n * @public\n */\n export const discovery = createServiceRef<\n import('./DiscoveryService').DiscoveryService\n >({ id: 'core.discovery' });\n\n /**\n * The service reference for the plugin scoped {@link HttpRouterService}.\n *\n * @public\n */\n export const httpRouter = createServiceRef<\n import('./HttpRouterService').HttpRouterService\n >({ id: 'core.httpRouter' });\n\n /**\n * The service reference for the plugin scoped {@link LifecycleService}.\n *\n * @public\n */\n export const lifecycle = createServiceRef<\n import('./LifecycleService').LifecycleService\n >({ id: 'core.lifecycle' });\n\n /**\n * The service reference for the plugin scoped {@link LoggerService}.\n *\n * @public\n */\n export const logger = createServiceRef<\n import('./LoggerService').LoggerService\n >({ id: 'core.logger' });\n\n /**\n * The service reference for the plugin scoped {@link PermissionsService}.\n *\n * @public\n */\n export const permissions = createServiceRef<\n import('./PermissionsService').PermissionsService\n >({ id: 'core.permissions' });\n\n /**\n * The service reference for the plugin scoped {@link PluginMetadataService}.\n *\n * @public\n */\n export const pluginMetadata = createServiceRef<\n import('./PluginMetadataService').PluginMetadataService\n >({ id: 'core.pluginMetadata' });\n\n /**\n * The service reference for the root scoped {@link RootHttpRouterService}.\n *\n * @public\n */\n export const rootHttpRouter = createServiceRef<\n import('./RootHttpRouterService').RootHttpRouterService\n >({ id: 'core.rootHttpRouter', scope: 'root' });\n\n /**\n * The service reference for the root scoped {@link RootLifecycleService}.\n *\n * @public\n */\n export const rootLifecycle = createServiceRef<\n import('./RootLifecycleService').RootLifecycleService\n >({ id: 'core.rootLifecycle', scope: 'root' });\n\n /**\n * The service reference for the root scoped {@link RootLoggerService}.\n *\n * @public\n */\n export const rootLogger = createServiceRef<\n import('./RootLoggerService').RootLoggerService\n >({ id: 'core.rootLogger', scope: 'root' });\n\n /**\n * The service reference for the plugin scoped {@link SchedulerService}.\n *\n * @public\n */\n export const scheduler = createServiceRef<\n import('./SchedulerService').SchedulerService\n >({ id: 'core.scheduler' });\n\n /**\n * The service reference for the plugin scoped {@link TokenManagerService}.\n *\n * @public\n */\n export const tokenManager = createServiceRef<\n import('./TokenManagerService').TokenManagerService\n >({ id: 'core.tokenManager' });\n\n /**\n * The service reference for the plugin scoped {@link UrlReaderService}.\n *\n * @public\n */\n export const urlReader = createServiceRef<\n import('./UrlReaderService').UrlReaderService\n >({ id: 'core.urlReader' });\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { FactoryFunctionWithOptions, MaybeOptions } from '../types';\nimport {\n BackendRegistrationPoints,\n BackendFeature,\n ExtensionPoint,\n} from './types';\n\n/** @public */\nexport interface ExtensionPointConfig {\n id: string;\n}\n\n/** @public */\nexport function createExtensionPoint<T>(\n config: ExtensionPointConfig,\n): ExtensionPoint<T> {\n return {\n id: config.id,\n get T(): T {\n throw new Error(`tried to read ExtensionPoint.T of ${this}`);\n },\n toString() {\n return `extensionPoint{${config.id}}`;\n },\n $$ref: 'extension-point', // TODO: declare\n };\n}\n\n/** @public */\nexport interface BackendPluginConfig<TOptions> {\n id: string;\n register(reg: BackendRegistrationPoints, options: TOptions): void;\n}\n\n/** @public */\nexport function createBackendPlugin<TOptions extends MaybeOptions = undefined>(\n config: BackendPluginConfig<TOptions>,\n): FactoryFunctionWithOptions<BackendFeature, TOptions> {\n return (options?: TOptions) => ({\n id: config.id,\n register(register: BackendRegistrationPoints) {\n return config.register(register, options!);\n },\n });\n}\n\n/** @public */\nexport interface BackendModuleConfig<TOptions> {\n pluginId: string;\n moduleId: string;\n register(\n reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>,\n options: TOptions,\n ): void;\n}\n\n/**\n * Creates a new backend module for a given plugin.\n *\n * @public\n *\n * @remarks\n *\n * The `moduleId` should be equal to the module-specific prefix of the exported name, such\n * that the full name is `moduleId + PluginId + \"Module\"`. For example, a GitHub entity\n * provider module for the `catalog` plugin might have the module ID `'githubEntityProvider'`,\n * and the full exported name would be `githubEntityProviderCatalogModule`.\n *\n * The `pluginId` should exactly match the `id` of the plugin that the module extends.\n */\nexport function createBackendModule<TOptions extends MaybeOptions = undefined>(\n config: BackendModuleConfig<TOptions>,\n): FactoryFunctionWithOptions<BackendFeature, TOptions> {\n return (options?: TOptions) => ({\n id: `${config.pluginId}.${config.moduleId}`,\n register(register: BackendRegistrationPoints) {\n // TODO: Hide registerExtensionPo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;AAAA,IACX,IAAI,CAAO,GAAA;AACT,MAAM,MAAA,IAAI,KAAM,CAAA,CAAA,kCAAA,EAAqC,IAAM,CAAA,CAAA,CAAA,CAAA;AAAA,KAC7D;AAAA,IACA,QAAW,GAAA;AACT,MAAA,OAAO,kBAAkB,MAAO,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA;AAAA,KAClC;AAAA,IACA,KAAO,EAAA,iBAAA;AAAA;AAAA,GACT,CAAA;AACF,CAAA;AASO,SAAS,oBACd,MACsD,EAAA;AACtD,EAAA,OAAO,CAAC,OAAwB,MAAA;AAAA,IAC9B,IAAI,MAAO,CAAA,EAAA;AAAA,IACX,SAAS,QAAqC,EAAA;AAC5C,MAAO,OAAA,MAAA,CAAO,QAAS,CAAA,QAAA,EAAU,OAAQ,CAAA,CAAA;AAAA,KAC3C;AAAA,GACF,CAAA,CAAA;AACF,CAAA;AA0BO,SAAS,oBACd,MACsD,EAAA;AACtD,EAAA,OAAO,CAAC,OAAwB,MAAA;AAAA,IAC9B,EAAI,EAAA,CAAA,EAAG,MAAO,CAAA,QAAA,CAAA,CAAA,EAAY,MAAO,CAAA,QAAA,CAAA,CAAA;AAAA,IACjC,SAAS,QAAqC,EAAA;AAE5C,MAAO,OAAA,MAAA,CAAO,QAAS,CAAA,QAAA,EAAU,OAAQ,CAAA,CAAA;AAAA,KAC3C;AAAA,GACF,CAAA,CAAA;AACF;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/services/system/types.ts","../src/services/definitions/coreServices.ts","../src/wiring/createSharedEnvironment.ts","../src/wiring/factories.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * TODO\n *\n * @public\n */\nexport type ServiceRef<\n TService,\n TScope extends 'root' | 'plugin' = 'root' | 'plugin',\n> = {\n id: string;\n\n /**\n * This determines the scope at which this service is available.\n *\n * Root scoped services are available to all other services but\n * may only depend on other root scoped services.\n *\n * Plugin scoped services are only available to other plugin scoped\n * services but may depend on all other services.\n */\n scope: TScope;\n\n /**\n * Utility for getting the type of the service, using `typeof serviceRef.T`.\n * Attempting to actually read this value will result in an exception.\n */\n T: TService;\n\n toString(): string;\n\n $$ref: 'service';\n};\n\n/** @public */\nexport type TypesToServiceRef<T> = { [key in keyof T]: ServiceRef<T[key]> };\n\n/** @public */\nexport type ServiceFactory<TService = unknown> =\n | {\n // This scope prop is needed in addition to the service ref, as TypeScript\n // can't properly discriminate the two factory types otherwise.\n scope: 'root';\n service: ServiceRef<TService, 'root'>;\n deps: { [key in string]: ServiceRef<unknown> };\n factory(deps: { [key in string]: unknown }): Promise<TService>;\n }\n | {\n scope: 'plugin';\n service: ServiceRef<TService, 'plugin'>;\n deps: { [key in string]: ServiceRef<unknown> };\n createRootContext?(deps: { [key in string]: unknown }): Promise<unknown>;\n factory(\n deps: { [key in string]: unknown },\n context: unknown,\n ): Promise<TService>;\n };\n\n/**\n * Represents either a {@link ServiceFactory} or a function that returns one.\n *\n * @public\n */\nexport type ServiceFactoryOrFunction<TService = unknown> =\n | ServiceFactory<TService>\n | (() => ServiceFactory<TService>);\n\n/** @public */\nexport interface ServiceRefConfig<TService, TScope extends 'root' | 'plugin'> {\n id: string;\n scope?: TScope;\n defaultFactory?: (\n service: ServiceRef<TService, TScope>,\n ) => Promise<ServiceFactoryOrFunction<TService>>;\n}\n\n/**\n * Creates a new service definition. This overload is used to create plugin scoped services.\n *\n * @public\n */\nexport function createServiceRef<TService>(\n config: ServiceRefConfig<TService, 'plugin'>,\n): ServiceRef<TService, 'plugin'>;\n\n/**\n * Creates a new service definition. This overload is used to create root scoped services.\n *\n * @public\n */\nexport function createServiceRef<TService>(\n config: ServiceRefConfig<TService, 'root'>,\n): ServiceRef<TService, 'root'>;\nexport function createServiceRef<TService>(\n config: ServiceRefConfig<TService, any>,\n): ServiceRef<TService, any> {\n const { id, scope = 'plugin', defaultFactory } = config;\n return {\n id,\n scope,\n get T(): TService {\n throw new Error(`tried to read ServiceRef.T of ${this}`);\n },\n toString() {\n return `serviceRef{${config.id}}`;\n },\n $$ref: 'service', // TODO: declare\n __defaultFactory: defaultFactory,\n } as ServiceRef<TService, typeof scope> & {\n __defaultFactory?: (\n service: ServiceRef<TService>,\n ) => Promise<ServiceFactory<TService> | (() => ServiceFactory<TService>)>;\n };\n}\n\n/** @ignore */\ntype ServiceRefsToInstances<\n T extends { [key in string]: ServiceRef<unknown> },\n TScope extends 'root' | 'plugin' = 'root' | 'plugin',\n> = {\n [key in keyof T as T[key]['scope'] extends TScope ? key : never]: T[key]['T'];\n};\n\n/** @public */\nexport interface RootServiceFactoryConfig<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n> {\n service: ServiceRef<TService, 'root'>;\n deps: TDeps;\n factory(deps: ServiceRefsToInstances<TDeps, 'root'>): TImpl | Promise<TImpl>;\n}\n\n/** @public */\nexport interface PluginServiceFactoryConfig<\n TService,\n TContext,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n> {\n service: ServiceRef<TService, 'plugin'>;\n deps: TDeps;\n createRootContext?(\n deps: ServiceRefsToInstances<TDeps, 'root'>,\n ): TContext | Promise<TContext>;\n factory(\n deps: ServiceRefsToInstances<TDeps>,\n context: TContext,\n ): TImpl | Promise<TImpl>;\n}\n\n/**\n * Creates a root scoped service factory without options.\n *\n * @public\n * @param config - The service factory configuration.\n */\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TOpts extends object | undefined = undefined,\n>(\n config: RootServiceFactoryConfig<TService, TImpl, TDeps>,\n): () => ServiceFactory<TService>;\n/**\n * Creates a root scoped service factory with optional options.\n *\n * @public\n * @param config - The service factory configuration.\n */\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TOpts extends object | undefined = undefined,\n>(\n config: (options?: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>,\n): (options?: TOpts) => ServiceFactory<TService>;\n/**\n * Creates a root scoped service factory with required options.\n *\n * @public\n * @param config - The service factory configuration.\n */\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TOpts extends object | undefined = undefined,\n>(\n config: (options: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>,\n): (options: TOpts) => ServiceFactory<TService>;\n/**\n * Creates a plugin scoped service factory without options.\n *\n * @public\n * @param config - The service factory configuration.\n */\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TContext = undefined,\n TOpts extends object | undefined = undefined,\n>(\n config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>,\n): () => ServiceFactory<TService>;\n/**\n * Creates a plugin scoped service factory with optional options.\n *\n * @public\n * @param config - The service factory configuration.\n */\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TContext = undefined,\n TOpts extends object | undefined = undefined,\n>(\n config: (\n options?: TOpts,\n ) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>,\n): (options?: TOpts) => ServiceFactory<TService>;\n/**\n * Creates a plugin scoped service factory with required options.\n *\n * @public\n * @param config - The service factory configuration.\n */\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TContext = undefined,\n TOpts extends object | undefined = undefined,\n>(\n config:\n | PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>\n | ((\n options: TOpts,\n ) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>),\n): (options: TOpts) => ServiceFactory<TService>;\nexport function createServiceFactory<\n TService,\n TImpl extends TService,\n TDeps extends { [name in string]: ServiceRef<unknown> },\n TContext,\n TOpts extends object | undefined = undefined,\n>(\n config:\n | RootServiceFactoryConfig<TService, TImpl, TDeps>\n | PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>\n | ((options: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>)\n | ((\n options: TOpts,\n ) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>)\n | (() => RootServiceFactoryConfig<TService, TImpl, TDeps>)\n | (() => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>),\n): (options: TOpts) => ServiceFactory<TService> {\n const configCallback = typeof config === 'function' ? config : () => config;\n return (options: TOpts) => {\n const c = configCallback(options);\n return {\n ...c,\n ...('createRootContext' in c\n ? {\n createRootContext: async (deps: TDeps) =>\n c?.createRootContext?.(deps),\n }\n : {}),\n factory: async (deps: TDeps, ctx: TContext) => c.factory(deps, ctx),\n scope: c.service.scope,\n } as ServiceFactory<TService>;\n };\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createServiceRef } from '../system';\n\n/**\n * All core services references\n *\n * @public\n */\nexport namespace coreServices {\n /**\n * The service reference for the plugin scoped {@link CacheService}.\n *\n * @public\n */\n export const cache = createServiceRef<import('./CacheService').CacheService>({\n id: 'core.cache',\n });\n\n /**\n * The service reference for the root scoped {@link ConfigService}.\n *\n * @public\n */\n export const config = createServiceRef<\n import('./ConfigService').ConfigService\n >({ id: 'core.config', scope: 'root' });\n\n /**\n * The service reference for the plugin scoped {@link DatabaseService}.\n *\n * @public\n */\n export const database = createServiceRef<\n import('./DatabaseService').DatabaseService\n >({ id: 'core.database' });\n\n /**\n * The service reference for the plugin scoped {@link DiscoveryService}.\n *\n * @public\n */\n export const discovery = createServiceRef<\n import('./DiscoveryService').DiscoveryService\n >({ id: 'core.discovery' });\n\n /**\n * The service reference for the plugin scoped {@link HttpRouterService}.\n *\n * @public\n */\n export const httpRouter = createServiceRef<\n import('./HttpRouterService').HttpRouterService\n >({ id: 'core.httpRouter' });\n\n /**\n * The service reference for the plugin scoped {@link LifecycleService}.\n *\n * @public\n */\n export const lifecycle = createServiceRef<\n import('./LifecycleService').LifecycleService\n >({ id: 'core.lifecycle' });\n\n /**\n * The service reference for the plugin scoped {@link LoggerService}.\n *\n * @public\n */\n export const logger = createServiceRef<\n import('./LoggerService').LoggerService\n >({ id: 'core.logger' });\n\n /**\n * The service reference for the plugin scoped {@link PermissionsService}.\n *\n * @public\n */\n export const permissions = createServiceRef<\n import('./PermissionsService').PermissionsService\n >({ id: 'core.permissions' });\n\n /**\n * The service reference for the plugin scoped {@link PluginMetadataService}.\n *\n * @public\n */\n export const pluginMetadata = createServiceRef<\n import('./PluginMetadataService').PluginMetadataService\n >({ id: 'core.pluginMetadata' });\n\n /**\n * The service reference for the root scoped {@link RootHttpRouterService}.\n *\n * @public\n */\n export const rootHttpRouter = createServiceRef<\n import('./RootHttpRouterService').RootHttpRouterService\n >({ id: 'core.rootHttpRouter', scope: 'root' });\n\n /**\n * The service reference for the root scoped {@link RootLifecycleService}.\n *\n * @public\n */\n export const rootLifecycle = createServiceRef<\n import('./RootLifecycleService').RootLifecycleService\n >({ id: 'core.rootLifecycle', scope: 'root' });\n\n /**\n * The service reference for the root scoped {@link RootLoggerService}.\n *\n * @public\n */\n export const rootLogger = createServiceRef<\n import('./RootLoggerService').RootLoggerService\n >({ id: 'core.rootLogger', scope: 'root' });\n\n /**\n * The service reference for the plugin scoped {@link SchedulerService}.\n *\n * @public\n */\n export const scheduler = createServiceRef<\n import('./SchedulerService').SchedulerService\n >({ id: 'core.scheduler' });\n\n /**\n * The service reference for the plugin scoped {@link TokenManagerService}.\n *\n * @public\n */\n export const tokenManager = createServiceRef<\n import('./TokenManagerService').TokenManagerService\n >({ id: 'core.tokenManager' });\n\n /**\n * The service reference for the plugin scoped {@link UrlReaderService}.\n *\n * @public\n */\n export const urlReader = createServiceRef<\n import('./UrlReaderService').UrlReaderService\n >({ id: 'core.urlReader' });\n\n /**\n * The service reference for the plugin scoped {@link IdentityService}.\n *\n * @public\n */\n export const identity = createServiceRef<\n import('./IdentityService').IdentityService\n >({ id: 'core.identity' });\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ServiceFactory, ServiceFactoryOrFunction } from '../services';\n\n/** @public */\nexport interface SharedBackendEnvironmentConfig {\n services?: ServiceFactoryOrFunction[];\n}\n\n// This type is opaque in order to allow for future API evolution without\n// cluttering the external API. For example we might want to add support\n// for more powerful callback based backend modifications.\n//\n// By making this opaque we also ensure that the type doesn't become an input\n// type that we need to care about, as it would otherwise be possible to pass\n// a custom environment definition to `createBackend`, which we don't want.\n/**\n * @public\n */\nexport interface SharedBackendEnvironment {\n $$type: 'SharedBackendEnvironment';\n}\n\n/**\n * This type is NOT supposed to be used by anyone except internally by the backend-app-api package.\n * @internal */\nexport interface InternalSharedBackendEnvironment {\n version: 'v1';\n services?: ServiceFactory[];\n}\n\n/**\n * Creates a shared backend environment which can be used to create multiple backends\n * @public\n */\nexport function createSharedEnvironment<\n TOptions extends [options?: object] = [],\n>(\n config:\n | SharedBackendEnvironmentConfig\n | ((...params: TOptions) => SharedBackendEnvironmentConfig),\n): (...options: TOptions) => SharedBackendEnvironment {\n const configCallback = typeof config === 'function' ? config : () => config;\n\n return (...options) => {\n const actualConfig = configCallback(...options);\n const services = actualConfig?.services?.map(sf =>\n typeof sf === 'function' ? sf() : sf,\n );\n\n const exists = new Set<string>();\n const duplicates = new Set<string>();\n for (const { service } of services ?? []) {\n if (exists.has(service.id)) {\n duplicates.add(service.id);\n } else {\n exists.add(service.id);\n }\n }\n\n if (duplicates.size > 0) {\n const dupStr = [...duplicates].map(id => `'${id}'`).join(', ');\n throw new Error(\n `Duplicate service implementations provided in shared environment for ${dupStr}`,\n );\n }\n\n // Here to ensure type safety in this internal implementation.\n const env: SharedBackendEnvironment & InternalSharedBackendEnvironment = {\n $$type: 'SharedBackendEnvironment',\n version: 'v1',\n services,\n };\n return env;\n };\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n BackendRegistrationPoints,\n BackendFeature,\n ExtensionPoint,\n} from './types';\n\n/** @public */\nexport interface ExtensionPointConfig {\n id: string;\n}\n\n/** @public */\nexport function createExtensionPoint<T>(\n config: ExtensionPointConfig,\n): ExtensionPoint<T> {\n return {\n id: config.id,\n get T(): T {\n throw new Error(`tried to read ExtensionPoint.T of ${this}`);\n },\n toString() {\n return `extensionPoint{${config.id}}`;\n },\n $$ref: 'extension-point', // TODO: declare\n };\n}\n\n/** @public */\nexport interface BackendPluginConfig {\n id: string;\n register(reg: BackendRegistrationPoints): void;\n}\n\n/** @public */\nexport function createBackendPlugin<TOptions extends [options?: object] = []>(\n config: BackendPluginConfig | ((...params: TOptions) => BackendPluginConfig),\n): (...params: TOptions) => BackendFeature {\n if (typeof config === 'function') {\n return config;\n }\n\n return () => config;\n}\n\n/** @public */\nexport interface BackendModuleConfig {\n pluginId: string;\n moduleId: string;\n register(\n reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>,\n ): void;\n}\n\n/**\n * Creates a new backend module for a given plugin.\n *\n * @public\n *\n * @remarks\n *\n * The `moduleId` should be equal to the module-specific prefix of the exported name, such\n * that the full name is `moduleId + PluginId + \"Module\"`. For example, a GitHub entity\n * provider module for the `catalog` plugin might have the module ID `'githubEntityProvider'`,\n * and the full exported name would be `githubEntityProviderCatalogModule`.\n *\n * The `pluginId` should exactly match the `id` of the plugin that the module extends.\n */\nexport function createBackendModule<TOptions extends [options?: object] = []>(\n config: BackendModuleConfig | ((...params: TOptions) => BackendModuleConfig),\n): (...params: TOptions) => BackendFeature {\n if (typeof config === 'function') {\n return (...options: TOptions) => {\n const c = config(...options);\n return {\n id: `${c.pluginId}.${c.moduleId}`,\n register: c.register,\n };\n };\n }\n return () => ({\n id: `${config.pluginId}.${config.moduleId}`,\n register(register: BackendRegistrationPoints) {\n // TODO: Hide registerExtensionPoint\n return config.register(register);\n },\n });\n}\n"],"names":["coreServices"],"mappings":";;;;AA4GO,SAAS,iBACd,MAC2B,EAAA;AAC3B,EAAA,MAAM,EAAE,EAAA,EAAI,KAAQ,GAAA,QAAA,EAAU,gBAAmB,GAAA,MAAA,CAAA;AACjD,EAAO,OAAA;AAAA,IACL,EAAA;AAAA,IACA,KAAA;AAAA,IACA,IAAI,CAAc,GAAA;AAChB,MAAM,MAAA,IAAI,KAAM,CAAA,CAAA,8BAAA,EAAiC,IAAM,CAAA,CAAA,CAAA,CAAA;AAAA,KACzD;AAAA,IACA,QAAW,GAAA;AACT,MAAA,OAAO,cAAc,MAAO,CAAA,EAAA,CAAA,CAAA,CAAA,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AO,CAAA,QAAA,CAAA,CAAA;AAAA,IACjC,SAAS,QAAqC,EAAA;AAE5C,MAAO,OAAA,MAAA,CAAO,SAAS,QAAQ,CAAA,CAAA;AAAA,KACjC;AAAA,GACF,CAAA,CAAA;AACF;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@
8
8
 
9
9
  import { Config } from '@backstage/config';
10
10
  import { Handler } from 'express';
11
+ import { IdentityApi } from '@backstage/plugin-auth-node';
11
12
  import { JsonValue } from '@backstage/types';
12
13
  import { Knex } from 'knex';
13
14
  import { PermissionEvaluator } from '@backstage/plugin-permission-common';
@@ -21,16 +22,16 @@ export declare interface BackendFeature {
21
22
  }
22
23
 
23
24
  /** @public */
24
- export declare interface BackendModuleConfig<TOptions> {
25
+ export declare interface BackendModuleConfig {
25
26
  pluginId: string;
26
27
  moduleId: string;
27
- register(reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>, options: TOptions): void;
28
+ register(reg: Omit<BackendRegistrationPoints, 'registerExtensionPoint'>): void;
28
29
  }
29
30
 
30
31
  /** @public */
31
- export declare interface BackendPluginConfig<TOptions> {
32
+ export declare interface BackendPluginConfig {
32
33
  id: string;
33
- register(reg: BackendRegistrationPoints, options: TOptions): void;
34
+ register(reg: BackendRegistrationPoints): void;
34
35
  }
35
36
 
36
37
  /** @public */
@@ -217,6 +218,12 @@ export declare namespace coreServices {
217
218
  * @public
218
219
  */
219
220
  const urlReader: ServiceRef<UrlReaderService, "plugin">;
221
+ /**
222
+ * The service reference for the plugin scoped {@link IdentityService}.
223
+ *
224
+ * @public
225
+ */
226
+ const identity: ServiceRef<IdentityService, "plugin">;
220
227
  }
221
228
 
222
229
  /**
@@ -233,20 +240,73 @@ export declare namespace coreServices {
233
240
  *
234
241
  * The `pluginId` should exactly match the `id` of the plugin that the module extends.
235
242
  */
236
- export declare function createBackendModule<TOptions extends MaybeOptions = undefined>(config: BackendModuleConfig<TOptions>): FactoryFunctionWithOptions<BackendFeature, TOptions>;
243
+ export declare function createBackendModule<TOptions extends [options?: object] = []>(config: BackendModuleConfig | ((...params: TOptions) => BackendModuleConfig)): (...params: TOptions) => BackendFeature;
237
244
 
238
245
  /** @public */
239
- export declare function createBackendPlugin<TOptions extends MaybeOptions = undefined>(config: BackendPluginConfig<TOptions>): FactoryFunctionWithOptions<BackendFeature, TOptions>;
246
+ export declare function createBackendPlugin<TOptions extends [options?: object] = []>(config: BackendPluginConfig | ((...params: TOptions) => BackendPluginConfig)): (...params: TOptions) => BackendFeature;
240
247
 
241
248
  /** @public */
242
249
  export declare function createExtensionPoint<T>(config: ExtensionPointConfig): ExtensionPoint<T>;
243
250
 
244
251
  /**
252
+ * Creates a root scoped service factory without options.
253
+ *
254
+ * @public
255
+ * @param config - The service factory configuration.
256
+ */
257
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
258
+ [name in string]: ServiceRef<unknown>;
259
+ }, TOpts extends object | undefined = undefined>(config: RootServiceFactoryConfig<TService, TImpl, TDeps>): () => ServiceFactory<TService>;
260
+
261
+ /**
262
+ * Creates a root scoped service factory with optional options.
263
+ *
264
+ * @public
265
+ * @param config - The service factory configuration.
266
+ */
267
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
268
+ [name in string]: ServiceRef<unknown>;
269
+ }, TOpts extends object | undefined = undefined>(config: (options?: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>): (options?: TOpts) => ServiceFactory<TService>;
270
+
271
+ /**
272
+ * Creates a root scoped service factory with required options.
273
+ *
274
+ * @public
275
+ * @param config - The service factory configuration.
276
+ */
277
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
278
+ [name in string]: ServiceRef<unknown>;
279
+ }, TOpts extends object | undefined = undefined>(config: (options: TOpts) => RootServiceFactoryConfig<TService, TImpl, TDeps>): (options: TOpts) => ServiceFactory<TService>;
280
+
281
+ /**
282
+ * Creates a plugin scoped service factory without options.
283
+ *
284
+ * @public
285
+ * @param config - The service factory configuration.
286
+ */
287
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
288
+ [name in string]: ServiceRef<unknown>;
289
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>): () => ServiceFactory<TService>;
290
+
291
+ /**
292
+ * Creates a plugin scoped service factory with optional options.
293
+ *
294
+ * @public
295
+ * @param config - The service factory configuration.
296
+ */
297
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
298
+ [name in string]: ServiceRef<unknown>;
299
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: (options?: TOpts) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>): (options?: TOpts) => ServiceFactory<TService>;
300
+
301
+ /**
302
+ * Creates a plugin scoped service factory with required options.
303
+ *
245
304
  * @public
305
+ * @param config - The service factory configuration.
246
306
  */
247
- export declare function createServiceFactory<TService, TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends {
307
+ export declare function createServiceFactory<TService, TImpl extends TService, TDeps extends {
248
308
  [name in string]: ServiceRef<unknown>;
249
- }, TOpts extends MaybeOptions = undefined>(config: ServiceFactoryConfig<TService, TScope, TImpl, TDeps, TOpts>): FactoryFunctionWithOptions<ServiceFactory<TService>, TOpts>;
309
+ }, TContext = undefined, TOpts extends object | undefined = undefined>(config: PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps> | ((options: TOpts) => PluginServiceFactoryConfig<TService, TContext, TImpl, TDeps>)): (options: TOpts) => ServiceFactory<TService>;
250
310
 
251
311
  /**
252
312
  * Creates a new service definition. This overload is used to create plugin scoped services.
@@ -262,9 +322,15 @@ export declare function createServiceRef<TService>(config: ServiceRefConfig<TSer
262
322
  */
263
323
  export declare function createServiceRef<TService>(config: ServiceRefConfig<TService, 'root'>): ServiceRef<TService, 'root'>;
264
324
 
325
+ /**
326
+ * Creates a shared backend environment which can be used to create multiple backends
327
+ * @public
328
+ */
329
+ export declare function createSharedEnvironment<TOptions extends [options?: object] = []>(config: SharedBackendEnvironmentConfig | ((...params: TOptions) => SharedBackendEnvironmentConfig)): (...options: TOptions) => SharedBackendEnvironment;
330
+
265
331
  /**
266
332
  * The DatabaseService manages access to databases that Plugins get.
267
- *gs
333
+ *
268
334
  * @public
269
335
  */
270
336
  export declare interface DatabaseService {
@@ -356,13 +422,6 @@ export declare interface ExtensionPointConfig {
356
422
  id: string;
357
423
  }
358
424
 
359
- /**
360
- * Helper type that makes the options argument optional if options are not required.
361
- *
362
- * @ignore
363
- */
364
- declare type FactoryFunctionWithOptions<TResult, TOptions> = undefined extends TOptions ? (options?: TOptions) => TResult : (options: TOptions) => TResult;
365
-
366
425
  /**
367
426
  * @public
368
427
  */
@@ -370,6 +429,10 @@ export declare interface HttpRouterService {
370
429
  use(handler: Handler): void;
371
430
  }
372
431
 
432
+ /** @public */
433
+ export declare interface IdentityService extends IdentityApi {
434
+ }
435
+
373
436
  /**
374
437
  * @public
375
438
  **/
@@ -411,13 +474,6 @@ export declare type LogMeta = {
411
474
  [name: string]: unknown;
412
475
  };
413
476
 
414
- /**
415
- * Base type for options objects that aren't required.
416
- *
417
- * @ignore
418
- */
419
- declare type MaybeOptions = object | undefined;
420
-
421
477
  /** @public */
422
478
  export declare interface PermissionsService extends PermissionEvaluator {
423
479
  }
@@ -429,6 +485,16 @@ export declare interface PluginMetadataService {
429
485
  getId(): string;
430
486
  }
431
487
 
488
+ /** @public */
489
+ export declare interface PluginServiceFactoryConfig<TService, TContext, TImpl extends TService, TDeps extends {
490
+ [name in string]: ServiceRef<unknown>;
491
+ }> {
492
+ service: ServiceRef<TService, 'plugin'>;
493
+ deps: TDeps;
494
+ createRootContext?(deps: ServiceRefsToInstances<TDeps, 'root'>): TContext | Promise<TContext>;
495
+ factory(deps: ServiceRefsToInstances<TDeps>, context: TContext): TImpl | Promise<TImpl>;
496
+ }
497
+
432
498
  /**
433
499
  * An options object for {@link UrlReaderService.readTree} operations.
434
500
  *
@@ -621,6 +687,15 @@ export declare interface RootLifecycleService extends LifecycleService {
621
687
  export declare interface RootLoggerService extends LoggerService {
622
688
  }
623
689
 
690
+ /** @public */
691
+ export declare interface RootServiceFactoryConfig<TService, TImpl extends TService, TDeps extends {
692
+ [name in string]: ServiceRef<unknown>;
693
+ }> {
694
+ service: ServiceRef<TService, 'root'>;
695
+ deps: TDeps;
696
+ factory(deps: ServiceRefsToInstances<TDeps, 'root'>): TImpl | Promise<TImpl>;
697
+ }
698
+
624
699
  /** @public */
625
700
  export declare interface SchedulerService extends PluginTaskScheduler {
626
701
  }
@@ -701,22 +776,14 @@ export declare type ServiceFactory<TService = unknown> = {
701
776
  deps: {
702
777
  [key in string]: ServiceRef<unknown>;
703
778
  };
704
- factory(deps: {
779
+ createRootContext?(deps: {
705
780
  [key in string]: unknown;
706
- }): Promise<(deps: {
781
+ }): Promise<unknown>;
782
+ factory(deps: {
707
783
  [key in string]: unknown;
708
- }) => Promise<TService>>;
784
+ }, context: unknown): Promise<TService>;
709
785
  };
710
786
 
711
- /** @public */
712
- export declare interface ServiceFactoryConfig<TService, TScope extends 'root' | 'plugin', TImpl extends TService, TDeps extends {
713
- [name in string]: ServiceRef<unknown>;
714
- }, TOpts extends MaybeOptions = undefined> {
715
- service: ServiceRef<TService, TScope>;
716
- deps: TDeps;
717
- factory(deps: ServiceRefsToInstances<TDeps, 'root'>, options: TOpts): TScope extends 'root' ? Promise<TImpl> : Promise<(deps: ServiceRefsToInstances<TDeps>) => Promise<TImpl>>;
718
- }
719
-
720
787
  /**
721
788
  * Represents either a {@link ServiceFactory} or a function that returns one.
722
789
  *
@@ -761,11 +828,21 @@ export declare interface ServiceRefConfig<TService, TScope extends 'root' | 'plu
761
828
  declare type ServiceRefsToInstances<T extends {
762
829
  [key in string]: ServiceRef<unknown>;
763
830
  }, TScope extends 'root' | 'plugin' = 'root' | 'plugin'> = {
764
- [name in {
765
- [key in keyof T]: T[key] extends ServiceRef<unknown, TScope> ? key : never;
766
- }[keyof T]]: T[name] extends ServiceRef<infer TImpl> ? TImpl : never;
831
+ [key in keyof T as T[key]['scope'] extends TScope ? key : never]: T[key]['T'];
767
832
  };
768
833
 
834
+ /**
835
+ * @public
836
+ */
837
+ export declare interface SharedBackendEnvironment {
838
+ $$type: 'SharedBackendEnvironment';
839
+ }
840
+
841
+ /** @public */
842
+ export declare interface SharedBackendEnvironmentConfig {
843
+ services?: ServiceFactoryOrFunction[];
844
+ }
845
+
769
846
  /**
770
847
  * Interface for creating and validating tokens.
771
848
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@backstage/backend-plugin-api",
3
3
  "description": "Core API used by Backstage backend plugins",
4
- "version": "0.3.0-next.1",
4
+ "version": "0.3.0",
5
5
  "main": "dist/index.cjs.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "publishConfig": {
@@ -33,16 +33,17 @@
33
33
  "start": "backstage-cli package start"
34
34
  },
35
35
  "dependencies": {
36
- "@backstage/backend-tasks": "^0.4.1-next.1",
37
- "@backstage/config": "^1.0.6-next.0",
38
- "@backstage/plugin-permission-common": "^0.7.3-next.0",
36
+ "@backstage/backend-tasks": "^0.4.1",
37
+ "@backstage/config": "^1.0.6",
38
+ "@backstage/plugin-auth-node": "^0.2.9",
39
+ "@backstage/plugin-permission-common": "^0.7.3",
39
40
  "@backstage/types": "^1.0.2",
40
41
  "@types/express": "^4.17.6",
41
42
  "express": "^4.17.1",
42
43
  "knex": "^2.0.0"
43
44
  },
44
45
  "devDependencies": {
45
- "@backstage/cli": "^0.22.1-next.2"
46
+ "@backstage/cli": "^0.22.1"
46
47
  },
47
48
  "files": [
48
49
  "dist",