Skip to content

Commit 6c9acbd

Browse files
committed
chore: removes $ prefixed options in $substitute
1 parent 26fb69f commit 6c9acbd

File tree

2 files changed

+12
-176
lines changed

2 files changed

+12
-176
lines changed

app-config-extensions/src/index.test.ts

Lines changed: 6 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -995,162 +995,6 @@ describe('$substitute directive', () => {
995995
expect(parsed.toJSON()).toEqual({ foo: 'qa' });
996996
});
997997

998-
it('reads object with $name', async () => {
999-
process.env.FOO = 'foo';
1000-
1001-
const source = new LiteralSource({
1002-
foo: { $substitute: { $name: 'FOO' } },
1003-
});
1004-
1005-
const parsed = await source.read([substituteDirective()]);
1006-
1007-
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
1008-
});
1009-
1010-
it('fails with $name when not defined', async () => {
1011-
const source = new LiteralSource({
1012-
foo: { $substitute: { $name: 'FOO' } },
1013-
});
1014-
1015-
await expect(source.read([substituteDirective()])).rejects.toThrow();
1016-
});
1017-
1018-
it('uses $name when $fallback is defined', async () => {
1019-
process.env.FOO = 'foo';
1020-
1021-
const source = new LiteralSource({
1022-
foo: { $substitute: { $name: 'FOO', $fallback: 'bar' } },
1023-
});
1024-
1025-
const parsed = await source.read([substituteDirective()]);
1026-
1027-
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
1028-
});
1029-
1030-
it('uses $fallback when $name was not found', async () => {
1031-
const source = new LiteralSource({
1032-
foo: { $substitute: { $name: 'FOO', $fallback: 'bar' } },
1033-
});
1034-
1035-
const parsed = await source.read([substituteDirective()]);
1036-
1037-
expect(parsed.toJSON()).toEqual({ foo: 'bar' });
1038-
});
1039-
1040-
it('allows null value when $allowNull', async () => {
1041-
const source = new LiteralSource({
1042-
foo: { $substitute: { $name: 'FOO', $fallback: null, $allowNull: true } },
1043-
});
1044-
1045-
const parsed = await source.read([substituteDirective()]);
1046-
1047-
expect(parsed.toJSON()).toEqual({ foo: null });
1048-
});
1049-
1050-
it('does not allow number even when $allowNull', async () => {
1051-
const source = new LiteralSource({
1052-
foo: { $substitute: { $name: 'FOO', $fallback: 42, $allowNull: true } },
1053-
});
1054-
1055-
await expect(source.read([substituteDirective()])).rejects.toThrow();
1056-
});
1057-
1058-
it('parses ints', async () => {
1059-
process.env.FOO = '11';
1060-
1061-
const source = new LiteralSource({
1062-
$substitute: { $name: 'FOO', $parseInt: true },
1063-
});
1064-
1065-
expect(await source.readToJSON([substituteDirective()])).toEqual(11);
1066-
});
1067-
1068-
it('fails when int is invalid', async () => {
1069-
process.env.FOO = 'not a number';
1070-
1071-
const source = new LiteralSource({
1072-
$substitute: { $name: 'FOO', $parseInt: true },
1073-
});
1074-
1075-
await expect(source.read([substituteDirective()])).rejects.toThrow();
1076-
});
1077-
1078-
it('parses float', async () => {
1079-
process.env.FOO = '11.2';
1080-
1081-
const source = new LiteralSource({
1082-
$substitute: { $name: 'FOO', $parseFloat: true },
1083-
});
1084-
1085-
expect(await source.readToJSON([substituteDirective()])).toEqual(11.2);
1086-
});
1087-
1088-
it('fails when float is invalid', async () => {
1089-
process.env.FOO = 'not a number';
1090-
1091-
const source = new LiteralSource({
1092-
$substitute: { $name: 'FOO', $parseFloat: true },
1093-
});
1094-
1095-
await expect(source.read([substituteDirective()])).rejects.toThrow();
1096-
});
1097-
1098-
it('parses boolean = true', async () => {
1099-
process.env.FOO = 'true';
1100-
1101-
const source = new LiteralSource({
1102-
$substitute: { $name: 'FOO', $parseBool: true },
1103-
});
1104-
1105-
expect(await source.readToJSON([substituteDirective()])).toEqual(true);
1106-
});
1107-
1108-
it('parses boolean = 1', async () => {
1109-
process.env.FOO = '1';
1110-
1111-
const source = new LiteralSource({
1112-
$substitute: { $name: 'FOO', $parseBool: true },
1113-
});
1114-
1115-
expect(await source.readToJSON([substituteDirective()])).toEqual(true);
1116-
});
1117-
1118-
it('parses boolean = 0', async () => {
1119-
process.env.FOO = '0';
1120-
1121-
const source = new LiteralSource({
1122-
$substitute: { $name: 'FOO', $parseBool: true },
1123-
});
1124-
1125-
expect(await source.readToJSON([substituteDirective()])).toEqual(false);
1126-
});
1127-
1128-
it('parses boolean = false', async () => {
1129-
process.env.FOO = 'false';
1130-
1131-
const source = new LiteralSource({
1132-
$substitute: { $name: 'FOO', $parseBool: true },
1133-
});
1134-
1135-
expect(await source.readToJSON([substituteDirective()])).toEqual(false);
1136-
});
1137-
1138-
it('doesnt visit fallback if name is defined', async () => {
1139-
const failDirective = forKey('$fail', () => () => {
1140-
throw new Error();
1141-
});
1142-
1143-
process.env.FOO = 'foo';
1144-
1145-
const source = new LiteralSource({
1146-
foo: { $substitute: { $name: 'FOO', $fallback: { $fail: true } } },
1147-
});
1148-
1149-
const parsed = await source.read([substituteDirective(), failDirective]);
1150-
1151-
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
1152-
});
1153-
1154998
it('reads object with name', async () => {
1155999
process.env.FOO = 'foo';
11561000

@@ -1328,7 +1172,7 @@ describe('$envVar directive', () => {
13281172
expect(parsed.toJSON()).toEqual({ foo: 'foo', bar: 'bar' });
13291173
});
13301174

1331-
it('reads object with $name', async () => {
1175+
it('reads object with name', async () => {
13321176
process.env.FOO = 'foo';
13331177

13341178
const source = new LiteralSource({
@@ -1340,15 +1184,15 @@ describe('$envVar directive', () => {
13401184
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
13411185
});
13421186

1343-
it('fails with $name when not defined', async () => {
1187+
it('fails with name when not defined', async () => {
13441188
const source = new LiteralSource({
13451189
foo: { $envVar: { name: 'FOO' } },
13461190
});
13471191

13481192
await expect(source.read([envVarDirective()])).rejects.toThrow();
13491193
});
13501194

1351-
it('uses $name when $fallback is defined', async () => {
1195+
it('uses name when fallback is defined', async () => {
13521196
process.env.FOO = 'foo';
13531197

13541198
const source = new LiteralSource({
@@ -1360,7 +1204,7 @@ describe('$envVar directive', () => {
13601204
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
13611205
});
13621206

1363-
it('uses $fallback when $name was not found', async () => {
1207+
it('uses fallback when name was not found', async () => {
13641208
const source = new LiteralSource({
13651209
foo: { $envVar: { name: 'FOO', fallback: 'bar' } },
13661210
});
@@ -1370,7 +1214,7 @@ describe('$envVar directive', () => {
13701214
expect(parsed.toJSON()).toEqual({ foo: 'bar' });
13711215
});
13721216

1373-
it('allows null value when $allowNull', async () => {
1217+
it('allows null value when allowNull', async () => {
13741218
const source = new LiteralSource({
13751219
foo: { $envVar: { name: 'FOO', fallback: null, allowNull: true } },
13761220
});
@@ -1380,7 +1224,7 @@ describe('$envVar directive', () => {
13801224
expect(parsed.toJSON()).toEqual({ foo: null });
13811225
});
13821226

1383-
it('does not allow number even when $allowNull', async () => {
1227+
it('does not allow number even when allowNull', async () => {
13841228
const source = new LiteralSource({
13851229
foo: { $envVar: { name: 'FOO', fallback: 42, allowNull: true } },
13861230
});

app-config-extensions/src/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export function substituteDirective(
345345
validateObject(value, [...ctx, key]);
346346
if (Array.isArray(value)) throw new AppConfigError('$substitute was given an array');
347347

348-
const name = (await parse(selectDefined(value.name, value.$name))).toJSON();
348+
const name = (await parse(value.name)).toJSON();
349349

350350
validateString(name, [...ctx, key, [InObject, 'name']]);
351351

@@ -356,7 +356,7 @@ export function substituteDirective(
356356
}
357357

358358
if (resolvedValue) {
359-
const parseInt = (await parse(selectDefined(value.parseInt, value.$parseInt))).toJSON();
359+
const parseInt = (await parse(value.parseInt)).toJSON();
360360

361361
if (parseInt) {
362362
const parsed = Number.parseInt(resolvedValue, 10);
@@ -368,7 +368,7 @@ export function substituteDirective(
368368
return parse(parsed, { shouldFlatten: true });
369369
}
370370

371-
const parseFloat = (await parse(selectDefined(value.parseFloat, value.$parseFloat))).toJSON();
371+
const parseFloat = (await parse(value.parseFloat)).toJSON();
372372

373373
if (parseFloat) {
374374
const parsed = Number.parseFloat(resolvedValue);
@@ -380,7 +380,7 @@ export function substituteDirective(
380380
return parse(parsed, { shouldFlatten: true });
381381
}
382382

383-
const parseBool = (await parse(selectDefined(value.parseBool, value.$parseBool))).toJSON();
383+
const parseBool = (await parse(value.parseBool)).toJSON();
384384

385385
if (parseBool) {
386386
const parsed = resolvedValue.toLowerCase() !== 'false' && resolvedValue !== '0';
@@ -392,8 +392,8 @@ export function substituteDirective(
392392
}
393393

394394
if (value.fallback !== undefined || value.$fallback !== undefined) {
395-
const fallback = (await parse(selectDefined(value.fallback, value.$fallback))).toJSON();
396-
const allowNull = (await parse(selectDefined(value.allowNull, value.$allowNull))).toJSON();
395+
const fallback = (await parse(value.fallback)).toJSON();
396+
const allowNull = (await parse(value.allowNull)).toJSON();
397397

398398
if (allowNull) {
399399
validateStringOrNull(fallback, [...ctx, key, [InObject, 'fallback']]);
@@ -533,14 +533,6 @@ function performAllSubstitutions(text: string, envType?: string): string {
533533
return output;
534534
}
535535

536-
function selectDefined<T>(...args: (T | null | undefined)[]): T | null {
537-
for (const a of args) {
538-
if (a !== undefined) return a;
539-
}
540-
541-
return (undefined as any) as T;
542-
}
543-
544536
const validateObject: ValidationFunction<
545537
Record<string, any>
546538
> = validationFunction(({ emptySchema }) => emptySchema().addAdditionalProperties());

0 commit comments

Comments
 (0)