Skip to content

Commit 45a57c8

Browse files
committed
feat(#111): adds support for $extendsSelf with 'select' & 'env'
1 parent 28a0ba3 commit 45a57c8

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

app-config-extensions/src/extends-directive.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,48 @@ export function extendsDirective(): ParsingExtension {
119119

120120
/** Lookup a property in the same file, and "copy" it */
121121
export function extendsSelfDirective(): ParsingExtension {
122-
const validate: ValidationFunction<string> = validationFunction(({ stringSchema }) =>
123-
stringSchema(),
122+
const validate: ValidationFunction<string> = validationFunction(
123+
({ oneOf, stringSchema, emptySchema }) =>
124+
oneOf(stringSchema(), emptySchema().addString('select').addString('env', {}, false)),
124125
);
125126

126127
return named(
127128
'$extendsSelf',
128-
forKey('$extendsSelf', (input, key, ctx) => async (parse, _, __, ___, root) => {
129+
forKey('$extendsSelf', (input, key, parentKeys, ctx) => async (parse, _, __, ___, root) => {
130+
let select: string;
131+
let env: string | undefined;
132+
129133
const value = (await parse(input)).toJSON();
130-
validate(value, [...ctx, key]);
134+
135+
validate(value, [...parentKeys, key]);
136+
137+
if (typeof value === 'string') {
138+
select = value;
139+
} else {
140+
({ select, env } = value);
141+
}
142+
143+
const environmentOptions = {
144+
...environmentOptionsFromContext(ctx),
145+
override: env,
146+
};
131147

132148
// we temporarily use a ParsedValue literal so that we get the same property lookup semantics
133-
const selected = ParsedValue.literal(root).property(value.split('.'));
149+
const selected = ParsedValue.literal(root).property(select.split('.'));
134150

135151
if (selected === undefined) {
136-
throw new AppConfigError(`$extendsSelf selector was not found (${value})`);
152+
throw new AppConfigError(`$extendsSelf selector was not found (${select})`);
137153
}
138154

139155
if (selected.asObject() !== undefined) {
140-
return parse(selected.toJSON(), { shouldMerge: true });
156+
return parse(selected.toJSON(), { shouldMerge: true }, undefined, undefined, {
157+
environmentOptions,
158+
});
141159
}
142160

143-
return parse(selected.toJSON(), { shouldFlatten: true });
161+
return parse(selected.toJSON(), { shouldFlatten: true }, undefined, undefined, {
162+
environmentOptions,
163+
});
144164
}),
145165
);
146166
}

0 commit comments

Comments
 (0)