|
| 1 | +import express = require('express'); |
| 2 | + |
1 | 3 | import {MyPluginConfig, MyPluginConfigPublic} from '../../../shared/myPluginConfig'; |
2 | 4 | import {clone} from '../../../shared/utils'; |
3 | 5 | import {IntegrationModelPublic} from '../../../shared/integration/IntegrationModel'; |
4 | 6 | import {VendorIntegration} from '../../../shared/integration/vendorIntegration'; |
5 | 7 | import {STRINGS} from '../../../shared/strings'; |
| 8 | +import {API} from '../server/api'; |
| 9 | +import {ConfigOptions} from '../models/configOptions'; |
| 10 | +import {Vendors} from '../../../shared/vendor/vendors'; |
6 | 11 |
|
7 | 12 | import {Logger} from './logger'; |
8 | 13 |
|
| 14 | +type RequiredFields<T, K extends keyof T> = T & Required<Pick<T, K>>; |
| 15 | + |
| 16 | +type ValidConfig = RequiredFields<MyPluginConfig, 'integrations'>; |
| 17 | + |
9 | 18 | export class Configuration { |
10 | | - public readonly config: Required<MyPluginConfig>; |
| 19 | + private static MY_CONFIG_KEY = 'third-party-data'; |
| 20 | + private config: ValidConfig; |
11 | 21 | // @ts-ignore |
12 | 22 | private readonly logger: Logger; |
| 23 | + private readonly api: API; |
13 | 24 |
|
14 | | - constructor(config: MyPluginConfig, logger: Logger) { |
| 25 | + constructor(config: MyPluginConfig, api: API, logger: Logger) { |
15 | 26 | this.logger = logger; |
| 27 | + this.api = api; |
16 | 28 | this.config = Configuration.validate(config); |
17 | 29 | } |
18 | 30 |
|
19 | 31 | getPublicConfig(): MyPluginConfigPublic { |
20 | 32 | const configClone = clone(this.config); |
21 | 33 | return { |
22 | 34 | basePath: configClone.basePath, |
23 | | - integrations: configClone.integrations.map((integration) => ({ |
| 35 | + integrations: (configClone.integrations ?? []).map((integration) => ({ |
24 | 36 | ...integration, |
25 | 37 | adminSettings: undefined |
26 | 38 | })) as IntegrationModelPublic[] |
27 | 39 | }; |
28 | 40 | } |
29 | 41 |
|
| 42 | + getConfigFull(): MyPluginConfig { |
| 43 | + return this.config; |
| 44 | + } |
| 45 | + |
| 46 | + async setConfigFull(req: express.Request, config: ConfigOptions): Promise<void> { |
| 47 | + // validate |
| 48 | + const validConfig = Configuration.validate(config.data); |
| 49 | + |
| 50 | + // update the LKE config |
| 51 | + const pathToUpdate = await this.getLkeConfigPath(req); |
| 52 | + this.logger.info(`Updating plugin config in: ${pathToUpdate}`); |
| 53 | + await this.api.server(req).config.updateConfiguration({ |
| 54 | + path: pathToUpdate, |
| 55 | + configuration: validConfig |
| 56 | + }); |
| 57 | + |
| 58 | + // update the plugin's config cache |
| 59 | + this.config = validConfig; |
| 60 | + } |
| 61 | + |
| 62 | + private async getLkeConfigPath(req: express.Request): Promise<string> { |
| 63 | + const fullLkeConfigR = await this.api.server(req).config.getConfiguration(); |
| 64 | + if (!fullLkeConfigR.isSuccess()) { |
| 65 | + throw new Error('Cannot load Linkurious configuration'); |
| 66 | + } |
| 67 | + const configs = (fullLkeConfigR.body.plugins ?? {})[Configuration.MY_CONFIG_KEY]; |
| 68 | + if (Array.isArray(configs)) { |
| 69 | + if (configs.length === 1) { |
| 70 | + return `plugins.${Configuration.MY_CONFIG_KEY}.0`; |
| 71 | + } |
| 72 | + const basePath = this.config.basePath; |
| 73 | + const index = configs.findIndex((config: ValidConfig) => config.basePath === basePath); |
| 74 | + if (index < 0) { |
| 75 | + throw new Error(`Cannot find the configuration path for ${Configuration.MY_CONFIG_KEY}`); |
| 76 | + } |
| 77 | + return `plugins.${Configuration.MY_CONFIG_KEY}.${index}`; |
| 78 | + } else { |
| 79 | + return `plugins.${Configuration.MY_CONFIG_KEY}`; |
| 80 | + } |
| 81 | + } |
| 82 | + |
30 | 83 | /** |
31 | 84 | * Validate the plugin configuration parameters and add eventual default values. |
32 | 85 | * Terminate the plugin in case of errors. |
33 | 86 | */ |
34 | | - static validate(config: Partial<MyPluginConfig>): Required<MyPluginConfig> { |
| 87 | + static validate(config: Partial<MyPluginConfig>): ValidConfig { |
| 88 | + if (typeof config.basePath !== 'string') { |
| 89 | + throw new Error('Invalid configuration: basePath must be a string'); |
| 90 | + } |
35 | 91 | if (config.integrations === undefined) { |
36 | 92 | config.integrations = []; |
37 | 93 | } |
| 94 | + for (const integration of config.integrations) { |
| 95 | + const vendor = Vendors.getVendorByKey(integration.vendorKey); |
| 96 | + const as = integration.adminSettings; |
| 97 | + if (typeof as !== 'object' || as === null) { |
| 98 | + throw new Error(`${vendor.key}: "adminSettings" is invalid (must be an object)`); |
| 99 | + } |
| 100 | + for (const [key, value] of Object.entries(as)) { |
| 101 | + const field = vendor.adminFields.find((adminField) => adminField.key === key); |
| 102 | + if (!field) { |
| 103 | + throw new Error(`${vendor.key}: "adminSettings" field ${key} is unexpected`); |
| 104 | + } |
| 105 | + if (value === undefined) { |
| 106 | + if (field.required) { |
| 107 | + throw new Error(`${vendor.key}: adminSettings.${key} is required`); |
| 108 | + } else { |
| 109 | + // not required and undefined, okay |
| 110 | + } |
| 111 | + } else { |
| 112 | + // value is defined |
| 113 | + if (field.type === 'string' && typeof value !== 'string') { |
| 114 | + throw new Error(`${vendor.key}: adminSettings.${key} must be a string`); |
| 115 | + } |
| 116 | + if (field.type === 'boolean' && typeof value !== 'boolean') { |
| 117 | + throw new Error(`${vendor.key}: adminSettings.${key} must be a boolean`); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
38 | 122 | /* |
39 | 123 | if (config.mandatoryParam === null || config.mandatoryParam === undefined) { |
40 | 124 | logger.error('Missing mandatory parameter `mandatoryParam`.'); |
41 | 125 | // process.exit(1); |
42 | 126 | } |
43 | 127 | */ |
44 | | - return config as Required<MyPluginConfig>; |
| 128 | + return config as ValidConfig; |
45 | 129 | } |
46 | 130 |
|
47 | 131 | getIntegrationById(integrationId: string): VendorIntegration { |
|
0 commit comments