1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.config = void 0;
|
8 | const binding_key_1 = require("./binding-key");
|
9 | const context_view_1 = require("./context-view");
|
10 | const inject_1 = require("./inject");
|
11 | const value_promise_1 = require("./value-promise");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function config(propertyPath, metadata) {
|
46 | propertyPath = propertyPath !== null && propertyPath !== void 0 ? propertyPath : '';
|
47 | if (typeof propertyPath === 'object') {
|
48 | metadata = propertyPath;
|
49 | propertyPath = '';
|
50 | }
|
51 | metadata = Object.assign({ propertyPath, decorator: '@config', optional: true }, metadata);
|
52 | return (0, inject_1.inject)('', metadata, resolveFromConfig);
|
53 | }
|
54 | exports.config = config;
|
55 | (function (config) {
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 | config.getter = function injectConfigGetter(propertyPath, metadata) {
|
62 | propertyPath = propertyPath !== null && propertyPath !== void 0 ? propertyPath : '';
|
63 | if (typeof propertyPath === 'object') {
|
64 | metadata = propertyPath;
|
65 | propertyPath = '';
|
66 | }
|
67 | metadata = Object.assign({ propertyPath, decorator: '@config.getter', optional: true }, metadata);
|
68 | return (0, inject_1.inject)('', metadata, resolveAsGetterFromConfig);
|
69 | };
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | config.view = function injectConfigView(propertyPath, metadata) {
|
77 | propertyPath = propertyPath !== null && propertyPath !== void 0 ? propertyPath : '';
|
78 | if (typeof propertyPath === 'object') {
|
79 | metadata = propertyPath;
|
80 | propertyPath = '';
|
81 | }
|
82 | metadata = Object.assign({ propertyPath, decorator: '@config.view', optional: true }, metadata);
|
83 | return (0, inject_1.inject)('', metadata, resolveAsViewFromConfig);
|
84 | };
|
85 | })(config || (exports.config = config = {}));
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function getCurrentBindingKey(session) {
|
92 | var _a;
|
93 |
|
94 | return (_a = session.currentBinding) === null || _a === void 0 ? void 0 : _a.key;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function getTargetBindingKey(injection, session) {
|
102 | return injection.metadata.fromBinding || getCurrentBindingKey(session);
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function resolveFromConfig(ctx, injection, session) {
|
111 | const bindingKey = getTargetBindingKey(injection, session);
|
112 |
|
113 | if (!bindingKey)
|
114 | return undefined;
|
115 | const meta = injection.metadata;
|
116 | return ctx.getConfigAsValueOrPromise(bindingKey, meta.propertyPath, {
|
117 | session,
|
118 | optional: meta.optional,
|
119 | });
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function resolveAsGetterFromConfig(ctx, injection, session) {
|
128 | (0, inject_1.assertTargetType)(injection, Function, 'Getter function');
|
129 | const bindingKey = getTargetBindingKey(injection, session);
|
130 | const meta = injection.metadata;
|
131 | return async function getter() {
|
132 |
|
133 | if (!bindingKey)
|
134 | return undefined;
|
135 | return ctx.getConfigAsValueOrPromise(bindingKey, meta.propertyPath, {
|
136 |
|
137 |
|
138 |
|
139 | session: undefined,
|
140 | optional: meta.optional,
|
141 | });
|
142 | };
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function resolveAsViewFromConfig(ctx, injection, session) {
|
151 | (0, inject_1.assertTargetType)(injection, context_view_1.ContextView);
|
152 | const bindingKey = getTargetBindingKey(injection, session);
|
153 |
|
154 | if (!bindingKey)
|
155 | return undefined;
|
156 | const view = new ConfigView(ctx, binding => binding.key === binding_key_1.BindingKey.buildKeyForConfig(bindingKey).toString(), injection.metadata.propertyPath);
|
157 | view.open();
|
158 | return view;
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | class ConfigView extends context_view_1.ContextView {
|
165 | constructor(ctx, filter, propertyPath) {
|
166 | super(ctx, filter);
|
167 | this.propertyPath = propertyPath;
|
168 | }
|
169 | |
170 |
|
171 |
|
172 |
|
173 | async values(session) {
|
174 | const configValues = await super.values(session);
|
175 | const propertyPath = this.propertyPath;
|
176 | if (!propertyPath)
|
177 | return configValues;
|
178 | return configValues.map(v => (0, value_promise_1.getDeepProperty)(v, propertyPath));
|
179 | }
|
180 | }
|
181 |
|
\ | No newline at end of file |