1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {BindingFilter} from './binding-filter';
|
7 | import {BindingAddress, BindingKey} from './binding-key';
|
8 | import {Context} from './context';
|
9 | import {ContextView} from './context-view';
|
10 | import {assertTargetType, inject, Injection, InjectionMetadata} from './inject';
|
11 | import {ResolutionSession} from './resolution-session';
|
12 | import {getDeepProperty, ValueOrPromise} from './value-promise';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export interface ConfigInjectionMetadata extends InjectionMetadata {
|
18 | |
19 |
|
20 |
|
21 |
|
22 | propertyPath?: string;
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | fromBinding?: BindingAddress;
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export function config(
|
65 | propertyPath?: string | ConfigInjectionMetadata,
|
66 | metadata?: ConfigInjectionMetadata,
|
67 | ) {
|
68 | propertyPath = propertyPath ?? '';
|
69 | if (typeof propertyPath === 'object') {
|
70 | metadata = propertyPath;
|
71 | propertyPath = '';
|
72 | }
|
73 | metadata = Object.assign(
|
74 | {propertyPath, decorator: '@config', optional: true},
|
75 | metadata,
|
76 | );
|
77 | return inject('', metadata, resolveFromConfig);
|
78 | }
|
79 |
|
80 | export namespace config {
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 | export const getter = function injectConfigGetter(
|
87 | propertyPath?: string | ConfigInjectionMetadata,
|
88 | metadata?: ConfigInjectionMetadata,
|
89 | ) {
|
90 | propertyPath = propertyPath ?? '';
|
91 | if (typeof propertyPath === 'object') {
|
92 | metadata = propertyPath;
|
93 | propertyPath = '';
|
94 | }
|
95 | metadata = Object.assign(
|
96 | {propertyPath, decorator: '@config.getter', optional: true},
|
97 | metadata,
|
98 | );
|
99 | return inject('', metadata, resolveAsGetterFromConfig);
|
100 | };
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | export const view = function injectConfigView(
|
109 | propertyPath?: string | ConfigInjectionMetadata,
|
110 | metadata?: ConfigInjectionMetadata,
|
111 | ) {
|
112 | propertyPath = propertyPath ?? '';
|
113 | if (typeof propertyPath === 'object') {
|
114 | metadata = propertyPath;
|
115 | propertyPath = '';
|
116 | }
|
117 | metadata = Object.assign(
|
118 | {propertyPath, decorator: '@config.view', optional: true},
|
119 | metadata,
|
120 | );
|
121 | return inject('', metadata, resolveAsViewFromConfig);
|
122 | };
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function getCurrentBindingKey(session: ResolutionSession) {
|
131 |
|
132 | return session.currentBinding?.key;
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function getTargetBindingKey(injection: Injection, session: ResolutionSession) {
|
141 | return injection.metadata.fromBinding || getCurrentBindingKey(session);
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function resolveFromConfig(
|
151 | ctx: Context,
|
152 | injection: Injection,
|
153 | session: ResolutionSession,
|
154 | ): ValueOrPromise<unknown> {
|
155 | const bindingKey = getTargetBindingKey(injection, session);
|
156 |
|
157 | if (!bindingKey) return undefined;
|
158 | const meta = injection.metadata;
|
159 | return ctx.getConfigAsValueOrPromise(bindingKey, meta.propertyPath, {
|
160 | session,
|
161 | optional: meta.optional,
|
162 | });
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | function resolveAsGetterFromConfig(
|
172 | ctx: Context,
|
173 | injection: Injection,
|
174 | session: ResolutionSession,
|
175 | ) {
|
176 | assertTargetType(injection, Function, 'Getter function');
|
177 | const bindingKey = getTargetBindingKey(injection, session);
|
178 | const meta = injection.metadata;
|
179 | return async function getter() {
|
180 |
|
181 | if (!bindingKey) return undefined;
|
182 | return ctx.getConfigAsValueOrPromise(bindingKey, meta.propertyPath, {
|
183 |
|
184 |
|
185 |
|
186 | session: undefined,
|
187 | optional: meta.optional,
|
188 | });
|
189 | };
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function resolveAsViewFromConfig(
|
199 | ctx: Context,
|
200 | injection: Injection,
|
201 | session: ResolutionSession,
|
202 | ) {
|
203 | assertTargetType(injection, ContextView);
|
204 | const bindingKey = getTargetBindingKey(injection, session);
|
205 |
|
206 | if (!bindingKey) return undefined;
|
207 | const view = new ConfigView(
|
208 | ctx,
|
209 | binding =>
|
210 | binding.key === BindingKey.buildKeyForConfig(bindingKey).toString(),
|
211 | injection.metadata.propertyPath,
|
212 | );
|
213 | view.open();
|
214 | return view;
|
215 | }
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | class ConfigView extends ContextView {
|
222 | constructor(
|
223 | ctx: Context,
|
224 | filter: BindingFilter,
|
225 | private propertyPath?: string,
|
226 | ) {
|
227 | super(ctx, filter);
|
228 | }
|
229 |
|
230 | |
231 |
|
232 |
|
233 |
|
234 | async values(session?: ResolutionSession) {
|
235 | const configValues = await super.values(session);
|
236 | const propertyPath = this.propertyPath;
|
237 | if (!propertyPath) return configValues;
|
238 | return configValues.map(v => getDeepProperty(v, propertyPath));
|
239 | }
|
240 | }
|