-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjsonKeyValueAdapter.ts
More file actions
60 lines (54 loc) · 2.18 KB
/
jsonKeyValueAdapter.ts
File metadata and controls
60 lines (54 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ConfigurationSetting, featureFlagContentType, secretReferenceContentType } from "@azure/app-configuration";
import { stripComments } from "jsonc-parser";
import { parseContentType, isJsonContentType } from "./common/contentType.js";
import { IKeyValueAdapter } from "./keyValueAdapter.js";
export class JsonKeyValueAdapter implements IKeyValueAdapter {
static readonly #ExcludedJsonContentTypes: string[] = [
secretReferenceContentType,
featureFlagContentType
];
canProcess(setting: ConfigurationSetting): boolean {
if (!setting.contentType) {
return false;
}
if (JsonKeyValueAdapter.#ExcludedJsonContentTypes.includes(setting.contentType)) {
return false;
}
const contentType = parseContentType(setting.contentType);
return isJsonContentType(contentType);
}
async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> {
let parsedValue: unknown;
if (setting.value !== undefined) {
const parseResult = this.#tryParseJson(setting.value);
if (parseResult.success) {
parsedValue = parseResult.result;
} else {
// Try parsing with comments stripped
const parseWithoutCommentsResult = this.#tryParseJson(stripComments(setting.value));
if (parseWithoutCommentsResult.success) {
parsedValue = parseWithoutCommentsResult.result;
} else {
// If still not valid JSON, return the original value
parsedValue = setting.value;
}
}
}
return [setting.key, parsedValue];
}
async onChangeDetected(): Promise<void> {
return;
}
#tryParseJson(value: string): { success: true; result: unknown } | { success: false } {
try {
return { success: true, result: JSON.parse(value) };
} catch (error) {
if (error instanceof SyntaxError) {
return { success: false };
}
throw error;
}
}
}