@backstage/plugin-user-settings-backend 0.1.6-next.1 → 0.1.6-next.2
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 +14 -0
- package/alpha/package.json +6 -0
- package/dist/index.alpha.d.ts +28 -0
- package/dist/index.beta.d.ts +23 -0
- package/dist/index.cjs.js +18 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +14 -10
- package/package.json +12 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @backstage/plugin-user-settings-backend
|
|
2
2
|
|
|
3
|
+
## 0.1.6-next.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0ff03319be: Updated usage of `createBackendPlugin`.
|
|
8
|
+
- 4a6f38a535: Added a Backend System plugin feature
|
|
9
|
+
- Updated dependencies
|
|
10
|
+
- @backstage/backend-plugin-api@0.4.0-next.2
|
|
11
|
+
- @backstage/backend-common@0.18.2-next.2
|
|
12
|
+
- @backstage/catalog-model@1.2.0-next.1
|
|
13
|
+
- @backstage/plugin-auth-node@0.2.11-next.2
|
|
14
|
+
- @backstage/errors@1.1.4
|
|
15
|
+
- @backstage/types@1.0.2
|
|
16
|
+
|
|
3
17
|
## 0.1.6-next.1
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BackendFeature } from '@backstage/backend-plugin-api';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { IdentityApi } from '@backstage/plugin-auth-node';
|
|
4
|
+
import { PluginDatabaseManager } from '@backstage/backend-common';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create the user settings backend routes.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export declare function createRouter(options: RouterOptions): Promise<express.Router>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export declare interface RouterOptions {
|
|
17
|
+
database: PluginDatabaseManager;
|
|
18
|
+
identity: IdentityApi;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The user settings backend plugin.
|
|
23
|
+
*
|
|
24
|
+
* @alpha
|
|
25
|
+
*/
|
|
26
|
+
export declare const userSettingsPlugin: () => BackendFeature;
|
|
27
|
+
|
|
28
|
+
export { }
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BackendFeature } from '@backstage/backend-plugin-api';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { IdentityApi } from '@backstage/plugin-auth-node';
|
|
4
|
+
import { PluginDatabaseManager } from '@backstage/backend-common';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create the user settings backend routes.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export declare function createRouter(options: RouterOptions): Promise<express.Router>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export declare interface RouterOptions {
|
|
17
|
+
database: PluginDatabaseManager;
|
|
18
|
+
identity: IdentityApi;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* Excluded from this release type: userSettingsPlugin */
|
|
22
|
+
|
|
23
|
+
export { }
|
package/dist/index.cjs.js
CHANGED
|
@@ -6,6 +6,7 @@ var backendCommon = require('@backstage/backend-common');
|
|
|
6
6
|
var errors = require('@backstage/errors');
|
|
7
7
|
var express = require('express');
|
|
8
8
|
var Router = require('express-promise-router');
|
|
9
|
+
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
9
10
|
|
|
10
11
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
12
|
|
|
@@ -124,5 +125,22 @@ async function createRouterInternal(options) {
|
|
|
124
125
|
return router;
|
|
125
126
|
}
|
|
126
127
|
|
|
128
|
+
const userSettingsPlugin = backendPluginApi.createBackendPlugin({
|
|
129
|
+
pluginId: "userSettings",
|
|
130
|
+
register(env) {
|
|
131
|
+
env.registerInit({
|
|
132
|
+
deps: {
|
|
133
|
+
database: backendPluginApi.coreServices.database,
|
|
134
|
+
identity: backendPluginApi.coreServices.identity,
|
|
135
|
+
httpRouter: backendPluginApi.coreServices.httpRouter
|
|
136
|
+
},
|
|
137
|
+
async init({ database, identity, httpRouter }) {
|
|
138
|
+
httpRouter.use(await createRouter({ database, identity }));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
127
144
|
exports.createRouter = createRouter;
|
|
145
|
+
exports.userSettingsPlugin = userSettingsPlugin;
|
|
128
146
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/database/DatabaseUserSettingsStore.ts","../src/service/router.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 {\n PluginDatabaseManager,\n resolvePackagePath,\n} from '@backstage/backend-common';\nimport { NotFoundError } from '@backstage/errors';\nimport { JsonValue } from '@backstage/types';\nimport { Knex } from 'knex';\nimport { UserSettingsStore, type UserSetting } from './UserSettingsStore';\n\nconst migrationsDir = resolvePackagePath(\n '@backstage/plugin-user-settings-backend',\n 'migrations',\n);\n\n/**\n * @public\n */\nexport type RawDbUserSettingsRow = {\n user_entity_ref: string;\n bucket: string;\n key: string;\n value: string;\n};\n\n/**\n * Store to manage the user settings.\n *\n * @public\n */\nexport class DatabaseUserSettingsStore implements UserSettingsStore {\n static async create(options: {\n database: PluginDatabaseManager;\n }): Promise<DatabaseUserSettingsStore> {\n const { database } = options;\n const client = await database.getClient();\n\n if (!database.migrations?.skip) {\n await client.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n return new DatabaseUserSettingsStore(client);\n }\n\n private constructor(private readonly db: Knex) {}\n\n async get(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<UserSetting> {\n const rows = await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .select(['bucket', 'key', 'value']);\n\n if (!rows.length) {\n throw new NotFoundError(\n `Unable to find '${options.key}' in bucket '${options.bucket}'`,\n );\n }\n\n return {\n bucket: rows[0].bucket,\n key: rows[0].key,\n value: JSON.parse(rows[0].value),\n };\n }\n\n async set(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n value: JsonValue;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .insert({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n value: JSON.stringify(options.value),\n })\n .onConflict(['user_entity_ref', 'bucket', 'key'])\n .merge(['value']);\n }\n\n async delete(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .delete();\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 { errorHandler, PluginDatabaseManager } from '@backstage/backend-common';\nimport { AuthenticationError, InputError } from '@backstage/errors';\nimport { IdentityApi } from '@backstage/plugin-auth-node';\nimport express, { Request } from 'express';\nimport Router from 'express-promise-router';\nimport { DatabaseUserSettingsStore } from '../database/DatabaseUserSettingsStore';\nimport { UserSettingsStore } from '../database/UserSettingsStore';\n\n/**\n * @public\n */\nexport interface RouterOptions {\n database: PluginDatabaseManager;\n identity: IdentityApi;\n}\n\n/**\n * Create the user settings backend routes.\n *\n * @public\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const userSettingsStore = await DatabaseUserSettingsStore.create({\n database: options.database,\n });\n\n return await createRouterInternal({\n userSettingsStore,\n identity: options.identity,\n });\n}\n\nexport async function createRouterInternal(options: {\n identity: IdentityApi;\n userSettingsStore: UserSettingsStore;\n}): Promise<express.Router> {\n const router = Router();\n router.use(express.json());\n\n /**\n * Helper method to extract the userEntityRef from the request.\n */\n const getUserEntityRef = async (req: Request): Promise<string> => {\n // throws an AuthenticationError in case the token exists but is invalid\n const identity = await options.identity.getIdentity({ request: req });\n if (!identity) {\n throw new AuthenticationError(`Missing token in 'authorization' header`);\n }\n\n return identity.identity.userEntityRef;\n };\n\n // get a single value\n router.get('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n res.json(setting);\n });\n\n // set a single value\n router.put('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n const { value } = req.body;\n\n if (value === undefined) {\n throw new InputError('Missing required field \"value\"');\n }\n\n await options.userSettingsStore.set({\n userEntityRef,\n bucket,\n key,\n value,\n });\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n res.json(setting);\n });\n\n // get a single value\n router.delete('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n await options.userSettingsStore.delete({ userEntityRef, bucket, key });\n\n res.status(204).end();\n });\n\n router.use(errorHandler());\n\n return router;\n}\n"],"names":["resolvePackagePath","NotFoundError","Router","express","AuthenticationError","InputError","errorHandler"],"mappings":";;;;;;;;;;;;;;AAyBA,MAAM,aAAgB,GAAAA,gCAAA;AAAA,EACpB,yCAAA;AAAA,EACA,YAAA;AACF,CAAA,CAAA;AAiBO,MAAM,yBAAuD,CAAA;AAAA,EAgB1D,YAA6B,EAAU,EAAA;AAAV,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA,CAAA;AAAA,GAAW;AAAA,EAfhD,aAAa,OAAO,OAEmB,EAAA;AAhDzC,IAAA,IAAA,EAAA,CAAA;AAiDI,IAAM,MAAA,EAAE,UAAa,GAAA,OAAA,CAAA;AACrB,IAAM,MAAA,MAAA,GAAS,MAAM,QAAA,CAAS,SAAU,EAAA,CAAA;AAExC,IAAA,IAAI,EAAC,CAAA,EAAA,GAAA,QAAA,CAAS,UAAT,KAAA,IAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAqB,IAAM,CAAA,EAAA;AAC9B,MAAM,MAAA,MAAA,CAAO,QAAQ,MAAO,CAAA;AAAA,QAC1B,SAAW,EAAA,aAAA;AAAA,OACZ,CAAA,CAAA;AAAA,KACH;AAEA,IAAO,OAAA,IAAI,0BAA0B,MAAM,CAAA,CAAA;AAAA,GAC7C;AAAA,EAIA,MAAM,IAAI,OAIe,EAAA;AACvB,IAAA,MAAM,OAAO,MAAM,IAAA,CAAK,EAAyB,CAAA,eAAe,EAC7D,KAAM,CAAA;AAAA,MACL,iBAAiB,OAAQ,CAAA,aAAA;AAAA,MACzB,QAAQ,OAAQ,CAAA,MAAA;AAAA,MAChB,KAAK,OAAQ,CAAA,GAAA;AAAA,KACd,CACA,CAAA,MAAA,CAAO,CAAC,QAAU,EAA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AA,CAAA;AAEzB,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/database/DatabaseUserSettingsStore.ts","../src/service/router.ts","../src/plugin.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 {\n PluginDatabaseManager,\n resolvePackagePath,\n} from '@backstage/backend-common';\nimport { NotFoundError } from '@backstage/errors';\nimport { JsonValue } from '@backstage/types';\nimport { Knex } from 'knex';\nimport { UserSettingsStore, type UserSetting } from './UserSettingsStore';\n\nconst migrationsDir = resolvePackagePath(\n '@backstage/plugin-user-settings-backend',\n 'migrations',\n);\n\n/**\n * @public\n */\nexport type RawDbUserSettingsRow = {\n user_entity_ref: string;\n bucket: string;\n key: string;\n value: string;\n};\n\n/**\n * Store to manage the user settings.\n *\n * @public\n */\nexport class DatabaseUserSettingsStore implements UserSettingsStore {\n static async create(options: {\n database: PluginDatabaseManager;\n }): Promise<DatabaseUserSettingsStore> {\n const { database } = options;\n const client = await database.getClient();\n\n if (!database.migrations?.skip) {\n await client.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n return new DatabaseUserSettingsStore(client);\n }\n\n private constructor(private readonly db: Knex) {}\n\n async get(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<UserSetting> {\n const rows = await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .select(['bucket', 'key', 'value']);\n\n if (!rows.length) {\n throw new NotFoundError(\n `Unable to find '${options.key}' in bucket '${options.bucket}'`,\n );\n }\n\n return {\n bucket: rows[0].bucket,\n key: rows[0].key,\n value: JSON.parse(rows[0].value),\n };\n }\n\n async set(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n value: JsonValue;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .insert({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n value: JSON.stringify(options.value),\n })\n .onConflict(['user_entity_ref', 'bucket', 'key'])\n .merge(['value']);\n }\n\n async delete(options: {\n userEntityRef: string;\n bucket: string;\n key: string;\n }): Promise<void> {\n await this.db<RawDbUserSettingsRow>('user_settings')\n .where({\n user_entity_ref: options.userEntityRef,\n bucket: options.bucket,\n key: options.key,\n })\n .delete();\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 { errorHandler, PluginDatabaseManager } from '@backstage/backend-common';\nimport { AuthenticationError, InputError } from '@backstage/errors';\nimport { IdentityApi } from '@backstage/plugin-auth-node';\nimport express, { Request } from 'express';\nimport Router from 'express-promise-router';\nimport { DatabaseUserSettingsStore } from '../database/DatabaseUserSettingsStore';\nimport { UserSettingsStore } from '../database/UserSettingsStore';\n\n/**\n * @public\n */\nexport interface RouterOptions {\n database: PluginDatabaseManager;\n identity: IdentityApi;\n}\n\n/**\n * Create the user settings backend routes.\n *\n * @public\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const userSettingsStore = await DatabaseUserSettingsStore.create({\n database: options.database,\n });\n\n return await createRouterInternal({\n userSettingsStore,\n identity: options.identity,\n });\n}\n\nexport async function createRouterInternal(options: {\n identity: IdentityApi;\n userSettingsStore: UserSettingsStore;\n}): Promise<express.Router> {\n const router = Router();\n router.use(express.json());\n\n /**\n * Helper method to extract the userEntityRef from the request.\n */\n const getUserEntityRef = async (req: Request): Promise<string> => {\n // throws an AuthenticationError in case the token exists but is invalid\n const identity = await options.identity.getIdentity({ request: req });\n if (!identity) {\n throw new AuthenticationError(`Missing token in 'authorization' header`);\n }\n\n return identity.identity.userEntityRef;\n };\n\n // get a single value\n router.get('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n res.json(setting);\n });\n\n // set a single value\n router.put('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n const { value } = req.body;\n\n if (value === undefined) {\n throw new InputError('Missing required field \"value\"');\n }\n\n await options.userSettingsStore.set({\n userEntityRef,\n bucket,\n key,\n value,\n });\n const setting = await options.userSettingsStore.get({\n userEntityRef,\n bucket,\n key,\n });\n\n res.json(setting);\n });\n\n // get a single value\n router.delete('/buckets/:bucket/keys/:key', async (req, res) => {\n const userEntityRef = await getUserEntityRef(req);\n const { bucket, key } = req.params;\n\n await options.userSettingsStore.delete({ userEntityRef, bucket, key });\n\n res.status(204).end();\n });\n\n router.use(errorHandler());\n\n return router;\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 {\n createBackendPlugin,\n coreServices,\n} from '@backstage/backend-plugin-api';\nimport { createRouter } from './service/router';\n\n/**\n * The user settings backend plugin.\n *\n * @alpha\n */\nexport const userSettingsPlugin = createBackendPlugin({\n pluginId: 'userSettings',\n register(env) {\n env.registerInit({\n deps: {\n database: coreServices.database,\n identity: coreServices.identity,\n httpRouter: coreServices.httpRouter,\n },\n async init({ database, identity, httpRouter }) {\n httpRouter.use(await createRouter({ database, identity }));\n },\n });\n },\n});\n"],"names":["resolvePackagePath","NotFoundError","Router","express","AuthenticationError","InputError","errorHandler","createBackendPlugin","coreServices"],"mappings":";;;;;;;;;;;;;;;AAyBA,MAAM,aAAgB,GAAAA,gCAAA;AAAA,EACpB,yCAAA;AAAA,EACA,YAAA;AACF,CAAA,CAAA;AAiBO,MAAM,yBAAuD,CAAA;AAAA,EAgB1D,YAA6B,EAAU,EAAA;AAAV,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA,CAAA;AAAA,GAAW;AAAA,EAfhD,aAAa,OAAO,OAEmB,EAAA;AAhDzC,IAAA,IAAA,EAAA,CAAA;AAiDI,IAAM,MAAA,EAAE,UAAa,GAAA,OAAA,CAAA;AACrB,IAAM,MAAA,MAAA,GAAS,MAAM,QAAA,CAAS,SAAU,EAAA,CAAA;AAExC,IAAA,IAAI,EAAC,CAAA,EAAA,GAAA,QAAA,CAAS,UAAT,KAAA,IAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAqB,IAAM,CAAA,EAAA;AAC9B,MAAM,MAAA,MAAA,CAAO,QAAQ,MAAO,CAAA;AAAA,QAC1B,SAAW,EAAA,aAAA;AAAA,OACZ,CAAA,CAAA;AAAA,KACH;AAEA,IAAO,OAAA,IAAI,0BAA0B,MAAM,CAAA,CAAA;AAAA,GAC7C;AAAA,EAIA,MAAM,IAAI,OAIe,EAAA;AACvB,IAAA,MAAM,OAAO,MAAM,IAAA,CAAK,EAAyB,CAAA,eAAe,EAC7D,KAAM,CAAA;AAAA,MACL,iBAAiB,OAAQ,CAAA,aAAA;AAAA,MACz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AA,CAAO,GAAG,CAAA,CAAE,GAAI,EAAA,CAAA;AAAA,GACrB,CAAA,CAAA;AAED,EAAO,MAAA,CAAA,GAAA,CAAIC,4BAAc,CAAA,CAAA;AAEzB,EAAO,OAAA,MAAA,CAAA;AACT;;AC/FO,MAAM,qBAAqBC,oCAAoB,CAAA;AAAA,EACpD,QAAU,EAAA,cAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,UAAUC,6BAAa,CAAA,QAAA;AAAA,QACvB,UAAUA,6BAAa,CAAA,QAAA;AAAA,QACvB,YAAYA,6BAAa,CAAA,UAAA;AAAA,OAC3B;AAAA,MACA,MAAM,IAAK,CAAA,EAAE,QAAU,EAAA,QAAA,EAAU,YAAc,EAAA;AAC7C,QAAA,UAAA,CAAW,IAAI,MAAM,YAAA,CAAa,EAAE,QAAU,EAAA,QAAA,EAAU,CAAC,CAAA,CAAA;AAAA,OAC3D;AAAA,KACD,CAAA,CAAA;AAAA,GACH;AACF,CAAC;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IdentityApi } from '@backstage/plugin-auth-node';
|
|
1
|
+
import { BackendFeature } from '@backstage/backend-plugin-api';
|
|
3
2
|
import express from 'express';
|
|
3
|
+
import { IdentityApi } from '@backstage/plugin-auth-node';
|
|
4
|
+
import { PluginDatabaseManager } from '@backstage/backend-common';
|
|
4
5
|
|
|
5
6
|
/**
|
|
7
|
+
* Create the user settings backend routes.
|
|
8
|
+
*
|
|
6
9
|
* @public
|
|
7
10
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
identity: IdentityApi;
|
|
11
|
-
}
|
|
11
|
+
export declare function createRouter(options: RouterOptions): Promise<express.Router>;
|
|
12
|
+
|
|
12
13
|
/**
|
|
13
|
-
* Create the user settings backend routes.
|
|
14
|
-
*
|
|
15
14
|
* @public
|
|
16
15
|
*/
|
|
17
|
-
declare
|
|
16
|
+
export declare interface RouterOptions {
|
|
17
|
+
database: PluginDatabaseManager;
|
|
18
|
+
identity: IdentityApi;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* Excluded from this release type: userSettingsPlugin */
|
|
18
22
|
|
|
19
|
-
export {
|
|
23
|
+
export { }
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-user-settings-backend",
|
|
3
3
|
"description": "The Backstage backend plugin to manage user settings",
|
|
4
|
-
"version": "0.1.6-next.
|
|
4
|
+
"version": "0.1.6-next.2",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public",
|
|
13
13
|
"main": "dist/index.cjs.js",
|
|
14
|
-
"types": "dist/index.d.ts"
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"alphaTypes": "dist/index.alpha.d.ts"
|
|
15
16
|
},
|
|
16
17
|
"repository": {
|
|
17
18
|
"type": "git",
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
},
|
|
21
22
|
"scripts": {
|
|
22
23
|
"start": "backstage-cli package start",
|
|
23
|
-
"build": "backstage-cli package build",
|
|
24
|
+
"build": "backstage-cli package build --experimental-type-build",
|
|
24
25
|
"lint": "backstage-cli package lint",
|
|
25
26
|
"test": "backstage-cli package test",
|
|
26
27
|
"prepack": "backstage-cli package prepack",
|
|
@@ -28,10 +29,11 @@
|
|
|
28
29
|
"clean": "backstage-cli package clean"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@backstage/backend-common": "^0.18.2-next.
|
|
32
|
-
"@backstage/
|
|
32
|
+
"@backstage/backend-common": "^0.18.2-next.2",
|
|
33
|
+
"@backstage/backend-plugin-api": "^0.4.0-next.2",
|
|
34
|
+
"@backstage/catalog-model": "^1.2.0-next.1",
|
|
33
35
|
"@backstage/errors": "^1.1.4",
|
|
34
|
-
"@backstage/plugin-auth-node": "^0.2.11-next.
|
|
36
|
+
"@backstage/plugin-auth-node": "^0.2.11-next.2",
|
|
35
37
|
"@backstage/types": "^1.0.2",
|
|
36
38
|
"@types/express": "^4.17.6",
|
|
37
39
|
"express": "^4.17.1",
|
|
@@ -41,13 +43,14 @@
|
|
|
41
43
|
"yn": "^4.0.0"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
44
|
-
"@backstage/backend-test-utils": "^0.1.34-next.
|
|
45
|
-
"@backstage/cli": "^0.22.2-next.
|
|
46
|
+
"@backstage/backend-test-utils": "^0.1.34-next.2",
|
|
47
|
+
"@backstage/cli": "^0.22.2-next.1",
|
|
46
48
|
"@types/supertest": "^2.0.8",
|
|
47
49
|
"supertest": "^6.1.3"
|
|
48
50
|
},
|
|
49
51
|
"files": [
|
|
50
52
|
"dist",
|
|
51
|
-
"migrations"
|
|
53
|
+
"migrations",
|
|
54
|
+
"alpha"
|
|
52
55
|
]
|
|
53
56
|
}
|