refactor(user-settings-backend): deprecate legacy create Router

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-08-08 11:00:36 +02:00
parent a428f5512b
commit 164ce3ee25
9 changed files with 108 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-user-settings-backend': minor
---
In preparation to stop supporting to the legacy backend system, the `createRouter` function is now deprecated and we strongly recommend you [migrate](https://backstage.io/docs/backend-system/building-backends/migrating) your backend to the new system.
+4 -7
View File
@@ -8,18 +8,15 @@ import express from 'express';
import { IdentityApi } from '@backstage/plugin-auth-node';
import { SignalsService } from '@backstage/plugin-signals-node';
// @public
// @public @deprecated
export function createRouter(options: RouterOptions): Promise<express.Router>;
// @public (undocumented)
export interface RouterOptions {
// (undocumented)
// @public @deprecated
export type RouterOptions = {
database: DatabaseService;
// (undocumented)
identity: IdentityApi;
// (undocumented)
signals?: SignalsService;
}
};
// (No @packageDocumentation comment for this package)
```
+2 -1
View File
@@ -52,7 +52,7 @@
"test": "backstage-cli package test"
},
"dependencies": {
"@backstage/backend-common": "workspace:^",
"@backstage/backend-defaults": "workspace:^",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/errors": "workspace:^",
@@ -64,6 +64,7 @@
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"knex": "^3.0.0",
"lodash": "^4.17.21",
"winston": "^3.2.1",
"yn": "^4.0.0"
},
@@ -14,8 +14,10 @@
* limitations under the License.
*/
import { PluginDatabaseManager } from '@backstage/backend-common';
import { resolvePackagePath } from '@backstage/backend-plugin-api';
import {
resolvePackagePath,
DatabaseService,
} from '@backstage/backend-plugin-api';
import { NotFoundError } from '@backstage/errors';
import { JsonValue } from '@backstage/types';
import { Knex } from 'knex';
@@ -43,7 +45,7 @@ export type RawDbUserSettingsRow = {
*/
export class DatabaseUserSettingsStore implements UserSettingsStore {
static async create(options: {
database: PluginDatabaseManager;
database: DatabaseService;
}): Promise<DatabaseUserSettingsStore> {
const { database } = options;
const client = await database.getClient();
@@ -0,0 +1,83 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import express from 'express';
import { merge } from 'lodash';
import * as winston from 'winston';
import { ConfigReader } from '@backstage/config';
import { DatabaseService } from '@backstage/backend-plugin-api';
import { WinstonLogger } from '@backstage/backend-defaults/rootLogger';
import { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter';
import { IdentityApi } from '@backstage/plugin-auth-node';
import { SignalsService } from '@backstage/plugin-signals-node';
import { createRouter as _createRouter } from './service';
/**
* Type for the options passed to the "createRouter" function.
*
* @public
* @deprecated This type is only exported for legacy reasons and will be removed in the future.
*/
export type RouterOptions = {
database: DatabaseService;
identity: IdentityApi;
signals?: SignalsService;
};
/**
* Create the user settings backend routes.
*
* @public
* @deprecated This function is only exported for legacy reasons and will be removed in the future.
* Please {@link https://backstage.io/docs/backend-system/building-backends/migrating | migrate } to use the new backend system and follow these {@link https://github.com/backstage/backstage/tree/master/plugins/user-settings-backend#new-backend | instructions } to install the user settings backend plugin.
*/
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const router = await _createRouter(options);
const config = new ConfigReader({});
const logger = winston
.createLogger(
merge<winston.LoggerOptions, winston.LoggerOptions>(
{
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
WinstonLogger.redacter().format,
process.env.NODE_ENV === 'production'
? winston.format.json()
: WinstonLogger.colorFormat(),
),
transports: [
new winston.transports.Console({
silent:
process.env.JEST_WORKER_ID !== undefined &&
!process.env.LOG_LEVEL,
}),
],
},
{},
),
)
.child({ service: 'backstage' });
const middleware = MiddlewareFactory.create({
config,
logger,
});
router.use(middleware.error());
return router;
}
+1 -1
View File
@@ -14,5 +14,5 @@
* limitations under the License.
*/
export * from './service';
export * from './deprecated';
export * from './database';
@@ -20,6 +20,7 @@ import { UserSettingsStore } from '../database/UserSettingsStore';
import { createRouterInternal } from './router';
import { SignalsService } from '@backstage/plugin-signals-node';
import { mockCredentials, mockServices } from '@backstage/backend-test-utils';
import { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter';
describe('createRouter', () => {
const userSettingsStore: jest.Mocked<UserSettingsStore> = {
@@ -40,7 +41,12 @@ describe('createRouter', () => {
signals: signalService as SignalsService,
});
app = express().use(router);
const errorHandler = MiddlewareFactory.create({
config: mockServices.rootConfig(),
logger: mockServices.rootLogger(),
}).error();
app = express().use(router).use(errorHandler);
});
afterEach(() => {
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import { errorHandler } from '@backstage/backend-common';
import { AuthenticationError, InputError } from '@backstage/errors';
import { IdentityApi } from '@backstage/plugin-auth-node';
import express, { Request } from 'express';
@@ -156,7 +155,5 @@ export async function createRouterInternal(
res.status(204).end();
});
router.use(errorHandler());
return router;
}
+1 -1
View File
@@ -7766,7 +7766,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/plugin-user-settings-backend@workspace:plugins/user-settings-backend"
dependencies:
"@backstage/backend-common": "workspace:^"
"@backstage/backend-defaults": "workspace:^"
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
@@ -7782,6 +7781,7 @@ __metadata:
express: ^4.17.1
express-promise-router: ^4.1.0
knex: ^3.0.0
lodash: ^4.17.21
supertest: ^6.1.3
winston: ^3.2.1
yn: ^4.0.0