-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathupdateSyncTokenSample.ts
More file actions
86 lines (69 loc) · 3.33 KB
/
updateSyncTokenSample.ts
File metadata and controls
86 lines (69 loc) · 3.33 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @summary The AppConfiguration service supports EventGrid-based setting change notifications.
* This sample shows how to process these notifications. Due to the distributed nature of the
* AppConfiguration service, the synchronization token needs to be registered with the client
* to get the most up-to-date value of the setting. The ConfigurationClient.UpdateSyncToken
* is used to register the synchronization token.
* @azsdk-weight 70
*/
import { AppConfigurationClient } from "@azure/app-configuration";
import type { EventGridEvent } from "@azure/eventgrid";
import { isSystemEvent, EventGridDeserializer } from "@azure/eventgrid";
import { appConfigTestEvent } from "./testData.ts";
// Load the .env file if it exists
import * as dotenv from "dotenv";
import { DefaultAzureCredential } from "@azure/identity";
dotenv.config();
// Create an Event Grid Consumer which will decode a hard coded test object into an EventGridEvent object.
const consumer = new EventGridDeserializer();
/**
* For a full implementation, another service would act as a receiver for events {@link https://learn.microsoft.com/azure/event-grid/event-handlers}.
* However, to avoid additional complexity for this sample, a hardcoded test event is being used. For full EventGrid samples, see
* {@link https://github.com/Azure/azure-sdk-for-js/tree/ebbfcff02ca15b1792dc6c45d8ba10913891c530/sdk/eventgrid/eventgrid/samples-dev}.
*/
async function processEvent(): Promise<EventGridEvent<unknown>[]> {
return consumer.deserializeEventGridEvents(appConfigTestEvent);
}
export async function main(): Promise<void> {
// Set the following environment variable or edit the value on the following line.
const endpoint = process.env["AZ_CONFIG_ENDPOINT"] || "<endpoint>";
const credential = new DefaultAzureCredential();
const client = new AppConfigurationClient(endpoint, credential);
const greetingKey = "Samples:Greeting";
await cleanupSampleValues([greetingKey], client);
// creating a new setting
console.log(`Adding in new setting ${greetingKey}`);
await client.addConfigurationSetting({ key: greetingKey, value: "Hello!" });
// Simulate receiving events from EventGrid
const events = await processEvent();
// Iterate through events and log updated key-value pairs.
events.forEach(async (eventData) => {
if (isSystemEvent("Microsoft.AppConfiguration.KeyValueModified", eventData)) {
client.updateSyncToken(eventData.data.syncToken);
const newSetting = await client.getConfigurationSetting({
key: eventData.data.key,
label: eventData.data.label,
});
console.log(`Setting was updated. Key: ${newSetting.key} value ${newSetting.value}`);
}
});
// Run for 2 seconds, allowing events to be processed.
await new Promise((resolve) => {
setTimeout(resolve, 1000 * 2);
});
await cleanupSampleValues([greetingKey], client);
}
async function cleanupSampleValues(keys: string[], client: AppConfigurationClient) {
const settingsIterator = client.listConfigurationSettings({
keyFilter: keys.join(","),
});
for await (const setting of settingsIterator) {
await client.deleteConfigurationSetting({ key: setting.key, label: setting.label });
}
}
main().catch((err) => {
console.error("Failed to run sample:", err);
process.exit(1);
});