UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/context
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.BindingKey = void 0;
8const unique_id_1 = require("./unique-id");
9// eslint-disable-next-line @typescript-eslint/no-unused-vars
10class BindingKey {
11 /**
12 * Create a new key for a binding bound to a value of type `ValueType`.
13 *
14 * @example
15 *
16 * ```ts
17 * BindingKey.create<string>('application.name');
18 * BindingKey.create<number>('config', 'rest.port);
19 * BindingKey.create<number>('config#rest.port');
20 * ```
21 *
22 * @param key - The binding key. When propertyPath is not provided, the key
23 * is allowed to contain propertyPath as encoded via `BindingKey#toString()`
24 * @param propertyPath - Optional path to a deep property of the bound value.
25 */
26 static create(key, propertyPath) {
27 // TODO(bajtos) allow chaining of propertyPaths, e.g.
28 // BindingKey.create('config#rest', 'port')
29 // should create {key: 'config', path: 'rest.port'}
30 if (propertyPath) {
31 BindingKey.validate(key);
32 return new BindingKey(key, propertyPath);
33 }
34 return BindingKey.parseKeyWithPath(key);
35 }
36 constructor(key, propertyPath) {
37 this.key = key;
38 this.propertyPath = propertyPath;
39 }
40 toString() {
41 return this.propertyPath
42 ? `${this.key}${BindingKey.PROPERTY_SEPARATOR}${this.propertyPath}`
43 : this.key;
44 }
45 /**
46 * Get a binding address for retrieving a deep property of the object
47 * bound to the current binding key.
48 *
49 * @param propertyPath - A dot-separated path to a (deep) property, e.g. "server.port".
50 */
51 deepProperty(propertyPath) {
52 // TODO(bajtos) allow chaining of propertyPaths, e.g.
53 // BindingKey.create('config', 'rest').deepProperty('port')
54 // should create {key: 'config', path: 'rest.port'}
55 return BindingKey.create(this.key, propertyPath);
56 }
57 /**
58 * Validate the binding key format. Please note that `#` is reserved.
59 * Returns a string representation of the binding key.
60 *
61 * @param key - Binding key, such as `a`, `a.b`, `a:b`, or `a/b`
62 */
63 static validate(key) {
64 if (!key)
65 throw new Error('Binding key must be provided.');
66 key = key.toString();
67 if (key.includes(BindingKey.PROPERTY_SEPARATOR)) {
68 throw new Error(`Binding key ${key} cannot contain` +
69 ` '${BindingKey.PROPERTY_SEPARATOR}'.`);
70 }
71 return key;
72 }
73 /**
74 * Parse a string containing both the binding key and the path to the deeply
75 * nested property to retrieve.
76 *
77 * @param keyWithPath - The key with an optional path,
78 * e.g. "application.instance" or "config#rest.port".
79 */
80 static parseKeyWithPath(keyWithPath) {
81 if (typeof keyWithPath !== 'string') {
82 return BindingKey.create(keyWithPath.key, keyWithPath.propertyPath);
83 }
84 const index = keyWithPath.indexOf(BindingKey.PROPERTY_SEPARATOR);
85 if (index === -1) {
86 return new BindingKey(keyWithPath);
87 }
88 return BindingKey.create(keyWithPath.slice(0, index).trim(), keyWithPath.slice(index + 1));
89 }
90 /**
91 * Build a binding key for the configuration of the given binding.
92 * The format is `<key>:$config`
93 *
94 * @param key - Key of the target binding to be configured
95 */
96 static buildKeyForConfig(key = '') {
97 const suffix = BindingKey.CONFIG_NAMESPACE;
98 const bindingKey = key ? `${key}:${suffix}` : suffix;
99 return bindingKey;
100 }
101 /**
102 * Generate a universally unique binding key.
103 *
104 * Please note the format of they generated key is not specified, you must
105 * not rely on any specific formatting (e.g. UUID style).
106 *
107 * @param namespace - Namespace for the binding
108 */
109 static generate(namespace = '') {
110 const prefix = namespace ? `${namespace}.` : '';
111 const name = (0, unique_id_1.generateUniqueId)();
112 return BindingKey.create(`${prefix}${name}`);
113 }
114}
115exports.BindingKey = BindingKey;
116BindingKey.PROPERTY_SEPARATOR = '#';
117/**
118 * Name space for configuration binding keys
119 */
120BindingKey.CONFIG_NAMESPACE = '$config';
121//# sourceMappingURL=binding-key.js.map
\No newline at end of file