Skip to content

Commit 9ebde2a

Browse files
committed
feat: uses extension-utils for git and vault parsing extensions
1 parent 21e2db8 commit 9ebde2a

File tree

6 files changed

+24
-36
lines changed

6 files changed

+24
-36
lines changed

app-config-git/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"dependencies": {
3333
"@app-config/core": "^2.1.5",
34+
"@app-config/extension-utils": "^2.1.5",
3435
"simple-git": "2"
3536
},
3637
"devDependencies": {

app-config-git/src/index.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import simpleGit from 'simple-git';
22
import { ParsingExtension, AppConfigError } from '@app-config/core';
3+
import { forKey, validateOptions } from '@app-config/extension-utils';
34

45
/** Access to the git branch and commit ref */
56
export default function gitRefDirectives(
67
getStatus: typeof gitStatus = gitStatus,
78
): ParsingExtension {
8-
return (value, [_, key]) => {
9-
if (key === '$git') {
10-
return async (parse) => {
11-
if (typeof value !== 'string') {
12-
throw new AppConfigError('$git directive should be passed a string');
13-
}
14-
9+
return forKey(
10+
'$git',
11+
validateOptions(
12+
(SchemaBuilder) => SchemaBuilder.stringSchema(),
13+
(value) => async (parse) => {
1514
switch (value) {
1615
case 'commit':
1716
return getStatus().then(({ commitRef }) => parse(commitRef, { shouldFlatten: true }));
@@ -41,11 +40,9 @@ export default function gitRefDirectives(
4140
default:
4241
throw new AppConfigError('$git directive was not passed a valid option');
4342
}
44-
};
45-
}
46-
47-
return false;
48-
};
43+
},
44+
),
45+
);
4946
}
5047

5148
interface GitStatus {

app-config-git/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"exclude": ["node_modules"],
99
"references": [
1010
{ "path": "../app-config-test-utils" },
11-
{ "path": "../app-config-core" }
11+
{ "path": "../app-config-core" },
12+
{ "path": "../app-config-extension-utils" }
1213
]
1314
}

app-config-vault/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"dependencies": {
3333
"@lcdev/fetch": "^0.1.10",
34+
"@app-config/extension-utils": "^2.1.5",
3435
"cross-fetch": "3",
3536
"node-vault": "0.9"
3637
},

app-config-vault/src/index.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch from 'cross-fetch';
22
import { api, buildPath, setGlobalFetch } from '@lcdev/fetch';
33
import type { JsonObject } from '@lcdev/ts';
44
import type { ParsingExtension, Json } from '@app-config/main';
5+
import { forKey, validateOptions } from '@app-config/extension-utils';
56

67
setGlobalFetch(fetch);
78

@@ -30,24 +31,12 @@ function vaultParsingExtension(options: Options = {}): ParsingExtension {
3031
return call.expectStatus(200);
3132
});
3233

33-
return (value, [_, objectKey]) => {
34-
if (objectKey === '$vault') {
35-
return async (parse) => {
36-
const parsed = await parse(value).then((v) => v.toJSON());
37-
38-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
39-
throw new Error('$vault requires a an object with options');
40-
}
41-
42-
const { secret, select } = parsed;
43-
44-
if (typeof secret !== 'string') {
45-
throw new Error('$vault requires a "secret" option');
46-
}
47-
48-
if (typeof select !== 'string') {
49-
throw new Error('$vault requires a "select" option');
50-
}
34+
return forKey(
35+
'$vault',
36+
validateOptions(
37+
(SchemaBuilder) => SchemaBuilder.emptySchema().addString('secret').addString('select'),
38+
(value) => async (parse) => {
39+
const { secret, select } = value;
5140

5241
type VaultResponse<T> = {
5342
data: {
@@ -74,11 +63,9 @@ function vaultParsingExtension(options: Options = {}): ParsingExtension {
7463
}
7564

7665
return parse(namedValue as Json, { shouldFlatten: true });
77-
};
78-
}
79-
80-
return false;
81-
};
66+
},
67+
),
68+
);
8269
}
8370

8471
export default vaultParsingExtension;

app-config-vault/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"include": ["src/**/*"],
99
"exclude": ["node_modules"],
1010
"references": [
11+
{ "path": "../app-config-extension-utils" },
1112
{ "path": "../app-config-main" }
1213
]
1314
}

0 commit comments

Comments
 (0)