1
+ import { Context } from 'aws-lambda' ;
2
+ import {
3
+ AppConfigProvider ,
4
+ } from '../../src/appconfig' ;
5
+ import {
6
+ AppConfigGetOptionsInterface ,
7
+ } from '../../src/types' ;
8
+ import { TinyLogger } from '../helpers/tinyLogger' ;
9
+ import { middleware } from '../helpers/sdkMiddlewareRequestCounter' ;
10
+ import { AppConfigDataClient } from '@aws-sdk/client-appconfigdata' ;
11
+
12
+ // We use a custom logger to log pure JSON objects to stdout
13
+ const logger = new TinyLogger ( ) ;
14
+
15
+ const application = process . env . APPLICATION_NAME || 'my-app' ;
16
+ const environment = process . env . ENVIRONMENT_NAME || 'my-env' ;
17
+ const freeFormJsonName = process . env . FREEFORM_JSON_NAME || 'freeform-json' ;
18
+ const freeFormYamlName = process . env . FREEFORM_YAML_NAME || 'freeform-yaml' ;
19
+ const freeFormPlainTextNameA = process . env . FREEFORM_PLAIN_TEXT_NAME_A || 'freeform-plain-text' ;
20
+ const freeFormPlainTextNameB = process . env . FREEFORM_PLAIN_TEXT_NAME_B || 'freeform-plain-text' ;
21
+ const featureFlagName = process . env . FEATURE_FLAG_NAME || 'feature-flag' ;
22
+
23
+ const defaultProvider = new AppConfigProvider ( {
24
+ application,
25
+ environment,
26
+ } ) ;
27
+ // Provider test
28
+ const customClient = new AppConfigDataClient ( { } ) ;
29
+ customClient . middlewareStack . use ( middleware ) ;
30
+ const providerWithMiddleware = new AppConfigProvider ( {
31
+ awsSdkV3Client : customClient ,
32
+ application,
33
+ environment,
34
+ } ) ;
35
+
36
+ // Use provider specified, or default to main one & return it with cache cleared
37
+ const resolveProvider = ( provider ?: AppConfigProvider ) : AppConfigProvider => {
38
+ const resolvedProvider = provider ? provider : defaultProvider ;
39
+ resolvedProvider . clearCache ( ) ;
40
+
41
+ return resolvedProvider ;
42
+ } ;
43
+
44
+ // Helper function to call get() and log the result
45
+ const _call_get = async (
46
+ paramName : string ,
47
+ testName : string ,
48
+ options ?: AppConfigGetOptionsInterface ,
49
+ provider ?: AppConfigProvider ,
50
+ ) : Promise < void > => {
51
+ try {
52
+ const currentProvider = resolveProvider ( provider ) ;
53
+
54
+ const parameterValue = await currentProvider . get ( paramName , options ) ;
55
+ logger . log ( {
56
+ test : testName ,
57
+ value : parameterValue
58
+ } ) ;
59
+ } catch ( err ) {
60
+ logger . log ( {
61
+ test : testName ,
62
+ error : err . message
63
+ } ) ;
64
+ }
65
+ } ;
66
+
67
+ export const handler = async ( _event : unknown , _context : Context ) : Promise < void > => {
68
+ // Test 1 - get a single parameter as-is (no transformation)
69
+ await _call_get ( freeFormPlainTextNameA , 'get' ) ;
70
+
71
+ // Test 2 - get a free-form JSON and apply binary transformation (should return a stringified JSON)
72
+ await _call_get ( freeFormJsonName , 'get-freeform-json-binary' , { transform : 'binary' } ) ;
73
+
74
+ // Test 3 - get a free-form YAML and apply binary transformation (should return a string-encoded YAML)
75
+ await _call_get ( freeFormYamlName , 'get-freeform-yaml-binary' , { transform : 'binary' } ) ;
76
+
77
+ // Test 4 - get a free-form plain text and apply binary transformation (should return a string)
78
+ await _call_get ( freeFormPlainTextNameB , 'get-freeform-plain-text-binary' , { transform : 'binary' } ) ;
79
+
80
+ // Test 5 - get a feature flag and apply binary transformation (should return a stringified JSON)
81
+ await _call_get ( featureFlagName , 'get-feature-flag-binary' , { transform : 'binary' } ) ;
82
+
83
+ // Test 6
84
+ // get parameter twice with middleware, which counts the number of requests, we check later if we only called AppConfig API once
85
+ try {
86
+ providerWithMiddleware . clearCache ( ) ;
87
+ middleware . counter = 0 ;
88
+ await providerWithMiddleware . get ( freeFormPlainTextNameA ) ;
89
+ await providerWithMiddleware . get ( freeFormPlainTextNameA ) ;
90
+ logger . log ( {
91
+ test : 'get-cached' ,
92
+ value : middleware . counter // should be 1
93
+ } ) ;
94
+ } catch ( err ) {
95
+ logger . log ( {
96
+ test : 'get-cached' ,
97
+ error : err . message
98
+ } ) ;
99
+ }
100
+
101
+ // Test 7
102
+ // get parameter twice, but force fetch 2nd time, we count number of SDK requests and check that we made two API calls
103
+ try {
104
+ providerWithMiddleware . clearCache ( ) ;
105
+ middleware . counter = 0 ;
106
+ await providerWithMiddleware . get ( freeFormPlainTextNameA ) ;
107
+ await providerWithMiddleware . get ( freeFormPlainTextNameA , { forceFetch : true } ) ;
108
+ logger . log ( {
109
+ test : 'get-forced' ,
110
+ value : middleware . counter // should be 2
111
+ } ) ;
112
+ } catch ( err ) {
113
+ logger . log ( {
114
+ test : 'get-forced' ,
115
+ error : err . message
116
+ } ) ;
117
+ }
118
+ } ;
0 commit comments