-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtestFunctionRemoteConfigUpdate.ts
More file actions
82 lines (75 loc) · 2.74 KB
/
Copy pathtestFunctionRemoteConfigUpdate.ts
File metadata and controls
82 lines (75 loc) · 2.74 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
/*
*
* Testing tools for invertase/react-native-firebase use only.
*
* Copyright (C) 2018-present Invertase Limited <oss@invertase.io>
*
* See License file for more information.
*/
// import * as admin from 'firebase-admin';
import { getRemoteConfig } from 'firebase-admin/remote-config';
import { logger } from 'firebase-functions/v2';
import { CallableRequest, onCall } from 'firebase-functions/v2/https';
import { E2E_TEST_FUNCTION_TIMEOUT_SECONDS } from './e2eCallOptions';
import { getAdminApp } from '.';
export const testFunctionRemoteConfigUpdateV2 = onCall(
{
maxInstances: 1,
concurrency: 1,
timeoutSeconds: E2E_TEST_FUNCTION_TIMEOUT_SECONDS,
},
async (
req: CallableRequest<{
operations: {
delete: string[] | undefined;
add: { name: string; value: string }[] | undefined;
update: { name: string; value: string }[] | undefined;
};
}>,
) => {
let template = await getRemoteConfig(getAdminApp()).getTemplate();
logger.info('received template version: ' + JSON.stringify(template.version));
// logger.info('received template: ' + JSON.stringify(template, null, 2));
if (req.data !== undefined && req.data.operations !== undefined) {
modifyTemplate(req.data, template);
}
// validate the template
template = await getRemoteConfig(getAdminApp()).validateTemplate(template);
logger.info('template is valid after updates.');
template = await getRemoteConfig(getAdminApp()).publishTemplate(template);
logger.info('template published, new version: ' + JSON.stringify(template.version));
return { templateVersion: template.version?.versionNumber };
},
);
function modifyTemplate(data: any, template: any) {
if (data.operations['delete'] !== undefined) {
const deletions = data.operations['delete'];
deletions.forEach((deletion: string) => {
logger.info('deleting key: ' + deletion);
if (template.parameters[deletion] !== undefined) {
delete template.parameters[deletion];
}
});
}
if (data.operations['add'] !== undefined) {
const adds = data.operations['add'];
adds.forEach((add: { name: string; value: any }) => {
logger.info('adding key: ' + JSON.stringify(add));
template.parameters[add.name] = {
description: 'realtime test parameter',
defaultValue: {
value: add.value,
},
};
});
}
if (data.operations['update'] !== undefined) {
const updates = data.operations['update'];
updates.forEach((update: { name: string; value: any }) => {
logger.info('updating key: ' + JSON.stringify(update));
if (template.parameters[update.name] !== undefined) {
template.parameters[update.name].defaultValue = update.value;
}
});
}
}