@backstage/backend-app-api 0.2.5-next.0 → 0.3.0-next.1

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.

Potentially problematic release.


This version of @backstage/backend-app-api might be problematic. Click here for more details.

package/dist/index.d.ts CHANGED
@@ -4,9 +4,17 @@
4
4
  * @packageDocumentation
5
5
  */
6
6
 
7
+ /// <reference types="node" />
8
+
7
9
  import { BackendFeature } from '@backstage/backend-plugin-api';
8
10
  import { Config } from '@backstage/config';
11
+ import { ConfigService } from '@backstage/backend-plugin-api';
12
+ import { CorsOptions } from 'cors';
13
+ import { ErrorRequestHandler } from 'express';
14
+ import { Express as Express_2 } from 'express';
9
15
  import { ExtensionPoint } from '@backstage/backend-plugin-api';
16
+ import { HelmetOptions } from 'helmet';
17
+ import * as http from 'http';
10
18
  import { HttpRouterService } from '@backstage/backend-plugin-api';
11
19
  import { LifecycleService } from '@backstage/backend-plugin-api';
12
20
  import { LoggerService } from '@backstage/backend-plugin-api';
@@ -14,10 +22,16 @@ import { PermissionsService } from '@backstage/backend-plugin-api';
14
22
  import { PluginCacheManager } from '@backstage/backend-common';
15
23
  import { PluginDatabaseManager } from '@backstage/backend-common';
16
24
  import { PluginEndpointDiscovery } from '@backstage/backend-common';
17
- import { PluginTaskScheduler } from '@backstage/backend-tasks';
25
+ import { RequestHandler } from 'express';
26
+ import { RequestListener } from 'http';
27
+ import { RootHttpRouterService } from '@backstage/backend-plugin-api';
28
+ import { RootLifecycleService } from '@backstage/backend-plugin-api';
29
+ import { RootLoggerService } from '@backstage/backend-plugin-api';
30
+ import { SchedulerService } from '@backstage/backend-plugin-api';
18
31
  import { ServiceFactory } from '@backstage/backend-plugin-api';
32
+ import { ServiceFactoryOrFunction } from '@backstage/backend-plugin-api';
19
33
  import { ServiceRef } from '@backstage/backend-plugin-api';
20
- import { TokenManager } from '@backstage/backend-common';
34
+ import { TokenManagerService } from '@backstage/backend-plugin-api';
21
35
  import { UrlReader } from '@backstage/backend-common';
22
36
 
23
37
  /**
@@ -33,7 +47,16 @@ export declare interface Backend {
33
47
  export declare const cacheFactory: (options?: undefined) => ServiceFactory<PluginCacheManager>;
34
48
 
35
49
  /** @public */
36
- export declare const configFactory: (options?: undefined) => ServiceFactory<Config>;
50
+ export declare const configFactory: (options?: undefined) => ServiceFactory<ConfigService>;
51
+
52
+ /**
53
+ * Creates a Node.js HTTP or HTTPS server instance.
54
+ *
55
+ * @public
56
+ */
57
+ export declare function createHttpServer(listener: RequestListener, options: HttpServerOptions, deps: {
58
+ logger: LoggerService;
59
+ }): Promise<ExtendedHttpServer>;
37
60
 
38
61
  /**
39
62
  * @public
@@ -44,7 +67,7 @@ export declare function createSpecializedBackend(options: CreateSpecializedBacke
44
67
  * @public
45
68
  */
46
69
  export declare interface CreateSpecializedBackendOptions {
47
- services: (ServiceFactory | (() => ServiceFactory))[];
70
+ services: ServiceFactoryOrFunction[];
48
71
  }
49
72
 
50
73
  /** @public */
@@ -53,6 +76,17 @@ export declare const databaseFactory: (options?: undefined) => ServiceFactory<Pl
53
76
  /** @public */
54
77
  export declare const discoveryFactory: (options?: undefined) => ServiceFactory<PluginEndpointDiscovery>;
55
78
 
79
+ /**
80
+ * An HTTP server extended with utility methods.
81
+ *
82
+ * @public
83
+ */
84
+ export declare interface ExtendedHttpServer extends http.Server {
85
+ start(): Promise<void>;
86
+ stop(): Promise<void>;
87
+ port(): number;
88
+ }
89
+
56
90
  /** @public */
57
91
  export declare const httpRouterFactory: (options?: HttpRouterFactoryOptions | undefined) => ServiceFactory<HttpRouterService>;
58
92
 
@@ -61,9 +95,38 @@ export declare const httpRouterFactory: (options?: HttpRouterFactoryOptions | un
61
95
  */
62
96
  export declare type HttpRouterFactoryOptions = {
63
97
  /**
64
- * The plugin ID used for the index route. Defaults to 'app'
98
+ * A callback used to generate the path for each plugin, defaults to `/api/{pluginId}`.
65
99
  */
66
- indexPlugin?: string;
100
+ getPath(pluginId: string): string;
101
+ };
102
+
103
+ /**
104
+ * Options for configuring HTTPS for an HTTP server.
105
+ *
106
+ * @public
107
+ */
108
+ export declare type HttpServerCertificateOptions = {
109
+ type: 'plain';
110
+ key: string;
111
+ cert: string;
112
+ } | {
113
+ type: 'generated';
114
+ hostname: string;
115
+ };
116
+
117
+ /**
118
+ * Options for starting up an HTTP server.
119
+ *
120
+ * @public
121
+ */
122
+ export declare type HttpServerOptions = {
123
+ listen: {
124
+ port: number;
125
+ host: string;
126
+ };
127
+ https?: {
128
+ certificate: HttpServerCertificateOptions;
129
+ };
67
130
  };
68
131
 
69
132
  /**
@@ -74,19 +137,212 @@ export declare const lifecycleFactory: (options?: undefined) => ServiceFactory<L
74
137
  /** @public */
75
138
  export declare const loggerFactory: (options?: undefined) => ServiceFactory<LoggerService>;
76
139
 
140
+ /**
141
+ * A utility to configure common middleware.
142
+ *
143
+ * @public
144
+ */
145
+ export declare class MiddlewareFactory {
146
+ #private;
147
+ /**
148
+ * Creates a new {@link MiddlewareFactory}.
149
+ */
150
+ static create(options: MiddlewareFactoryOptions): MiddlewareFactory;
151
+ private constructor();
152
+ /**
153
+ * Returns a middleware that unconditionally produces a 404 error response.
154
+ *
155
+ * @remarks
156
+ *
157
+ * Typically you want to place this middleware at the end of the chain, such
158
+ * that it's the last one attempted after no other routes matched.
159
+ *
160
+ * @returns An Express request handler
161
+ */
162
+ notFound(): RequestHandler;
163
+ /**
164
+ * Returns the compression middleware.
165
+ *
166
+ * @remarks
167
+ *
168
+ * The middleware will attempt to compress response bodies for all requests
169
+ * that traverse through the middleware.
170
+ */
171
+ compression(): RequestHandler;
172
+ /**
173
+ * Returns a request logging middleware.
174
+ *
175
+ * @remarks
176
+ *
177
+ * Typically you want to place this middleware at the start of the chain, such
178
+ * that it always logs requests whether they are "caught" by handlers farther
179
+ * down or not.
180
+ *
181
+ * @returns An Express request handler
182
+ */
183
+ logging(): RequestHandler;
184
+ /**
185
+ * Returns a middleware that implements the helmet library.
186
+ *
187
+ * @remarks
188
+ *
189
+ * This middleware applies security policies to incoming requests and outgoing
190
+ * responses. It is configured using config keys such as `backend.csp`.
191
+ *
192
+ * @see {@link https://helmetjs.github.io/}
193
+ *
194
+ * @returns An Express request handler
195
+ */
196
+ helmet(): RequestHandler;
197
+ /**
198
+ * Returns a middleware that implements the cors library.
199
+ *
200
+ * @remarks
201
+ *
202
+ * This middleware handles CORS. It is configured using the config key
203
+ * `backend.cors`.
204
+ *
205
+ * @see {@link https://github.com/expressjs/cors}
206
+ *
207
+ * @returns An Express request handler
208
+ */
209
+ cors(): RequestHandler;
210
+ /**
211
+ * Express middleware to handle errors during request processing.
212
+ *
213
+ * @remarks
214
+ *
215
+ * This is commonly the very last middleware in the chain.
216
+ *
217
+ * Its primary purpose is not to do translation of business logic exceptions,
218
+ * but rather to be a global catch-all for uncaught "fatal" errors that are
219
+ * expected to result in a 500 error. However, it also does handle some common
220
+ * error types (such as http-error exceptions, and the well-known error types
221
+ * in the `@backstage/errors` package) and returns the enclosed status code
222
+ * accordingly.
223
+ *
224
+ * It will also produce a response body with a serialized form of the error,
225
+ * unless a previous handler already did send a body. See
226
+ * {@link @backstage/errors#ErrorResponseBody} for the response shape used.
227
+ *
228
+ * @returns An Express error request handler
229
+ */
230
+ error(options?: MiddlewareFactoryErrorOptions): ErrorRequestHandler;
231
+ }
232
+
233
+ /**
234
+ * Options passed to the {@link MiddlewareFactory.error} middleware.
235
+ *
236
+ * @public
237
+ */
238
+ export declare interface MiddlewareFactoryErrorOptions {
239
+ /**
240
+ * Whether error response bodies should show error stack traces or not.
241
+ *
242
+ * If not specified, by default shows stack traces only in development mode.
243
+ */
244
+ showStackTraces?: boolean;
245
+ /**
246
+ * Whether any 4xx errors should be logged or not.
247
+ *
248
+ * If not specified, default to only logging 5xx errors.
249
+ */
250
+ logAllErrors?: boolean;
251
+ }
252
+
253
+ /**
254
+ * Options used to create a {@link MiddlewareFactory}.
255
+ *
256
+ * @public
257
+ */
258
+ export declare interface MiddlewareFactoryOptions {
259
+ config: ConfigService;
260
+ logger: LoggerService;
261
+ }
262
+
77
263
  /** @public */
78
264
  export declare const permissionsFactory: (options?: undefined) => ServiceFactory<PermissionsService>;
79
265
 
266
+ /**
267
+ * Attempts to read a CORS options object from the backend configuration object.
268
+ *
269
+ * @public
270
+ * @param config - The backend configuration object.
271
+ * @returns A CORS options object, or undefined if no cors configuration is present.
272
+ *
273
+ * @example
274
+ * ```ts
275
+ * const corsOptions = readCorsOptions(config.getConfig('backend'));
276
+ * ```
277
+ */
278
+ export declare function readCorsOptions(config?: Config): CorsOptions;
279
+
280
+ /**
281
+ * Attempts to read Helmet options from the backend configuration object.
282
+ *
283
+ * @public
284
+ * @param config - The backend configuration object.
285
+ * @returns A Helmet options object, or undefined if no Helmet configuration is present.
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * const helmetOptions = readHelmetOptions(config.getConfig('backend'));
290
+ * ```
291
+ */
292
+ export declare function readHelmetOptions(config?: Config): HelmetOptions;
293
+
294
+ /**
295
+ * Reads {@link HttpServerOptions} from a {@link @backstage/config#Config} object.
296
+ *
297
+ * @public
298
+ * @remarks
299
+ *
300
+ * The provided configuration object should contain the `listen` and
301
+ * additional keys directly.
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * const opts = readHttpServerOptions(config.getConfig('backend'));
306
+ * ```
307
+ */
308
+ export declare function readHttpServerOptions(config?: Config): HttpServerOptions;
309
+
310
+ /**
311
+ * @public
312
+ */
313
+ export declare interface RootHttpRouterConfigureOptions {
314
+ app: Express_2;
315
+ middleware: MiddlewareFactory;
316
+ routes: RequestHandler;
317
+ config: ConfigService;
318
+ logger: LoggerService;
319
+ lifecycle: LifecycleService;
320
+ }
321
+
322
+ /** @public */
323
+ export declare const rootHttpRouterFactory: (options?: RootHttpRouterFactoryOptions | undefined) => ServiceFactory<RootHttpRouterService>;
324
+
325
+ /**
326
+ * @public
327
+ */
328
+ export declare type RootHttpRouterFactoryOptions = {
329
+ /**
330
+ * The path to forward all unmatched requests to. Defaults to '/api/app'
331
+ */
332
+ indexPath?: string | false;
333
+ configure?(options: RootHttpRouterConfigureOptions): void;
334
+ };
335
+
80
336
  /**
81
337
  * Allows plugins to register shutdown hooks that are run when the process is about to exit.
82
338
  * @public */
83
- export declare const rootLifecycleFactory: (options?: undefined) => ServiceFactory<LifecycleService>;
339
+ export declare const rootLifecycleFactory: (options?: undefined) => ServiceFactory<RootLifecycleService>;
84
340
 
85
341
  /** @public */
86
- export declare const rootLoggerFactory: (options?: undefined) => ServiceFactory<LoggerService>;
342
+ export declare const rootLoggerFactory: (options?: undefined) => ServiceFactory<RootLoggerService>;
87
343
 
88
344
  /** @public */
89
- export declare const schedulerFactory: (options?: undefined) => ServiceFactory<PluginTaskScheduler>;
345
+ export declare const schedulerFactory: (options?: undefined) => ServiceFactory<SchedulerService>;
90
346
 
91
347
  /**
92
348
  * @public
@@ -94,7 +350,7 @@ export declare const schedulerFactory: (options?: undefined) => ServiceFactory<P
94
350
  export declare type ServiceOrExtensionPoint<T = unknown> = ExtensionPoint<T> | ServiceRef<T>;
95
351
 
96
352
  /** @public */
97
- export declare const tokenManagerFactory: (options?: undefined) => ServiceFactory<TokenManager>;
353
+ export declare const tokenManagerFactory: (options?: undefined) => ServiceFactory<TokenManagerService>;
98
354
 
99
355
  /** @public */
100
356
  export declare const urlReaderFactory: (options?: undefined) => ServiceFactory<UrlReader>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@backstage/backend-app-api",
3
3
  "description": "Core API used by Backstage backend apps",
4
- "version": "0.2.5-next.0",
4
+ "version": "0.3.0-next.1",
5
5
  "main": "dist/index.cjs.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "publishConfig": {
@@ -33,17 +33,37 @@
33
33
  "start": "backstage-cli package start"
34
34
  },
35
35
  "dependencies": {
36
- "@backstage/backend-common": "^0.18.0-next.0",
37
- "@backstage/backend-plugin-api": "^0.2.1-next.0",
38
- "@backstage/backend-tasks": "^0.4.1-next.0",
36
+ "@backstage/backend-common": "^0.18.0-next.1",
37
+ "@backstage/backend-plugin-api": "^0.3.0-next.1",
38
+ "@backstage/backend-tasks": "^0.4.1-next.1",
39
+ "@backstage/config": "^1.0.6-next.0",
39
40
  "@backstage/errors": "^1.1.4",
40
- "@backstage/plugin-permission-node": "^0.7.3-next.0",
41
+ "@backstage/plugin-permission-node": "^0.7.3-next.1",
42
+ "@types/cors": "^2.8.6",
43
+ "@types/express": "^4.17.6",
44
+ "compression": "^1.7.4",
45
+ "cors": "^2.8.5",
41
46
  "express": "^4.17.1",
42
47
  "express-promise-router": "^4.1.0",
48
+ "fs-extra": "10.1.0",
49
+ "helmet": "^6.0.0",
50
+ "minimatch": "^5.0.0",
51
+ "morgan": "^1.10.0",
52
+ "node-forge": "^1.3.1",
53
+ "selfsigned": "^2.0.0",
54
+ "stoppable": "^1.1.0",
43
55
  "winston": "^3.2.1"
44
56
  },
45
57
  "devDependencies": {
46
- "@backstage/cli": "^0.22.1-next.1"
58
+ "@backstage/cli": "^0.22.1-next.2",
59
+ "@types/compression": "^1.7.0",
60
+ "@types/fs-extra": "^9.0.3",
61
+ "@types/http-errors": "^2.0.0",
62
+ "@types/morgan": "^1.9.0",
63
+ "@types/node-forge": "^1.3.0",
64
+ "@types/stoppable": "^1.1.0",
65
+ "http-errors": "^2.0.0",
66
+ "supertest": "^6.1.3"
47
67
  },
48
68
  "files": [
49
69
  "dist",