|
| 1 | +import type { DesktopSize } from '../interfaces/DesktopSize'; |
| 2 | +import { Config } from './Config'; |
| 3 | +import type { ExtensionValue } from '../interfaces/ExtensionValue'; |
| 4 | + |
| 5 | +type ExtensionConstructor = (ident: string, value: unknown) => ExtensionValue; |
| 6 | + |
| 7 | +/** |
| 8 | + * Builder class for creating Config objects with a fluent interface. |
| 9 | + * |
| 10 | + * @example |
| 11 | + * ```typescript |
| 12 | + * const configBuilder = new ConfigBuilder(createExtensionFunction); |
| 13 | + * const config = configBuilder |
| 14 | + * .withDestination(destination) |
| 15 | + * .withProxyAddress(proxyAddress) |
| 16 | + * .withAuthToken(authToken) |
| 17 | + * ... |
| 18 | + * .build(); |
| 19 | + * ``` |
| 20 | + */ |
| 21 | +export class ConfigBuilder { |
| 22 | + private extensionConstructor: ExtensionConstructor; |
| 23 | + |
| 24 | + private username: string = ''; |
| 25 | + private password: string = ''; |
| 26 | + private destination: string = ''; |
| 27 | + private proxyAddress: string = ''; |
| 28 | + private serverDomain: string = ''; |
| 29 | + private authToken: string = ''; |
| 30 | + private desktopSize?: DesktopSize; |
| 31 | + |
| 32 | + private extensions: ExtensionValue[] = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a new ConfigBuilder instance. |
| 36 | + * |
| 37 | + * @param extensionConstructor - Function that creates extension values from identifiers and values. |
| 38 | + */ |
| 39 | + constructor(extensionConstructor: ExtensionConstructor) { |
| 40 | + this.extensionConstructor = extensionConstructor; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Optional parameter |
| 45 | + * |
| 46 | + * @param username - The username to use for authentication |
| 47 | + * @returns The builder instance for method chaining |
| 48 | + */ |
| 49 | + withUsername(username: string): ConfigBuilder { |
| 50 | + this.username = username; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Optional parameter |
| 56 | + * |
| 57 | + * @param password - The password for authentication |
| 58 | + * @returns The builder instance for method chaining |
| 59 | + */ |
| 60 | + withPassword(password: string): ConfigBuilder { |
| 61 | + this.password = password; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Required parameter |
| 67 | + * |
| 68 | + * @param destination - The destination address to connect to |
| 69 | + * @returns The builder instance for method chaining |
| 70 | + */ |
| 71 | + withDestination(destination: string): ConfigBuilder { |
| 72 | + this.destination = destination; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Required parameter |
| 78 | + * |
| 79 | + * @param proxyAddress - The address of the proxy server |
| 80 | + * @returns The builder instance for method chaining |
| 81 | + */ |
| 82 | + withProxyAddress(proxyAddress: string): ConfigBuilder { |
| 83 | + this.proxyAddress = proxyAddress; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Optional parameter |
| 89 | + * |
| 90 | + * @param serverDomain - The server domain to connect to |
| 91 | + * @returns The builder instance for method chaining |
| 92 | + */ |
| 93 | + withServerDomain(serverDomain: string): ConfigBuilder { |
| 94 | + this.serverDomain = serverDomain; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Required parameter |
| 100 | + * |
| 101 | + * @param authToken - JWT token to connect to the proxy |
| 102 | + * @returns The builder instance for method chaining |
| 103 | + */ |
| 104 | + withAuthToken(authToken: string): ConfigBuilder { |
| 105 | + this.authToken = authToken; |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Optional parameter |
| 111 | + * |
| 112 | + * @param ident - The identifier for the extension |
| 113 | + * @param value - The value for the extension |
| 114 | + * @returns The builder instance for method chaining |
| 115 | + */ |
| 116 | + withExtension(ident: string, value: unknown): ConfigBuilder { |
| 117 | + this.extensions.push(this.extensionConstructor(ident, value)); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Optional |
| 123 | + * |
| 124 | + * @param desktopSize - The desktop size configuration object |
| 125 | + * @returns The builder instance for method chaining |
| 126 | + */ |
| 127 | + withDesktopSize(desktopSize: DesktopSize): ConfigBuilder { |
| 128 | + this.desktopSize = desktopSize; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Builds a new Config instance. |
| 134 | + * |
| 135 | + * @throws {Error} If required parameters (destination, proxyAddress, authToken) are not set |
| 136 | + * @returns A new Config instance with the configured values |
| 137 | + */ |
| 138 | + build(): Config { |
| 139 | + if (this.destination === '') { |
| 140 | + throw new Error('destination has to be specified'); |
| 141 | + } |
| 142 | + if (this.proxyAddress === '') { |
| 143 | + throw new Error('proxy address has to be specified'); |
| 144 | + } |
| 145 | + if (this.authToken === '') { |
| 146 | + throw new Error('authentication token has to be specified'); |
| 147 | + } |
| 148 | + const userData = { username: this.username, password: this.password }; |
| 149 | + const proxyData = { address: this.proxyAddress, authToken: this.authToken }; |
| 150 | + |
| 151 | + const configOptions = { |
| 152 | + destination: this.destination, |
| 153 | + serverDomain: this.serverDomain, |
| 154 | + extensions: this.extensions, |
| 155 | + desktopSize: this.desktopSize, |
| 156 | + }; |
| 157 | + |
| 158 | + return new Config(userData, proxyData, configOptions); |
| 159 | + } |
| 160 | +} |
0 commit comments