1
+ import { Context } from 'aws-lambda' ;
2
+ import { DynamoDBProvider } from '../../src/dynamodb' ;
3
+ import { TinyLogger } from '../helpers/tinyLogger' ;
4
+ // # TODO: Uncomment code below once #1222 is fixed
5
+ /*
6
+ import { middleware } from '../helpers/sdkMiddlewareRequestCounter';
7
+ import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
8
+ */
9
+
10
+ const tableGet = process . env . TABLE_GET ?? 'my-table' ;
11
+ const tableGetMultiple = process . env . TABLE_GET_MULTIPLE ?? 'my-table' ;
12
+ const tableGetCustomkeys = process . env . TABLE_GET_CUSTOM_KEYS ?? 'my-table' ;
13
+ const tableGetMultipleCustomkeys = process . env . TABLE_GET_MULTIPLE_CUSTOM_KEYS ?? 'my-table' ;
14
+ const keyAttr = process . env . KEY_ATTR ?? 'id' ;
15
+ const sortAttr = process . env . SORT_ATTR ?? 'sk' ;
16
+ const valueAttr = process . env . VALUE_ATTR ?? 'value' ;
17
+
18
+ // We use a custom logger to log pure JSON objects to stdout
19
+ const logger = new TinyLogger ( ) ;
20
+
21
+ // Provider test 1, 5, 6
22
+ const providerGet = new DynamoDBProvider ( {
23
+ tableName : tableGet ,
24
+ } ) ;
25
+ // Provider test 2, 7
26
+ const providerGetMultiple = new DynamoDBProvider ( {
27
+ tableName : tableGetMultiple ,
28
+ } ) ;
29
+ // Provider test 3
30
+ const providerGetCustomKeys = new DynamoDBProvider ( {
31
+ tableName : tableGetCustomkeys ,
32
+ keyAttr,
33
+ valueAttr,
34
+ } ) ;
35
+ // Provider 4
36
+ const providerGetMultipleCustomKeys = new DynamoDBProvider ( {
37
+ tableName : tableGetMultipleCustomkeys ,
38
+ keyAttr,
39
+ sortAttr,
40
+ valueAttr,
41
+ } ) ;
42
+ // # TODO: Uncomment code below once #1222 is fixed
43
+ /*
44
+ // Provider test 8, 9
45
+ const customClient = new DynamoDBClient({});
46
+ providerWithMiddleware.middlewareStack.use(middleware);
47
+ const providerWithMiddleware = new DynamoDBProvider({
48
+ awsSdkV3Client: customClient
49
+ });
50
+ */
51
+
52
+ export const handler = async ( _event : unknown , _context : Context ) : Promise < void > => {
53
+ // Test 1 - get a single parameter with default options (keyAttr: 'id', valueAttr: 'value')
54
+ try {
55
+ const parameterValue = await providerGet . get ( 'my-param' ) ;
56
+ logger . log ( {
57
+ test : 'get' ,
58
+ value : parameterValue
59
+ } ) ;
60
+ } catch ( err ) {
61
+ logger . log ( {
62
+ test : 'get' ,
63
+ error : err . message
64
+ } ) ;
65
+ }
66
+
67
+ // Test 2 - get multiple parameters with default options (keyAttr: 'id', sortAttr: 'sk', valueAttr: 'value')
68
+ try {
69
+ const parametersValues = await providerGetMultiple . getMultiple ( 'my-params' ) ;
70
+ logger . log ( {
71
+ test : 'get-multiple' ,
72
+ value : parametersValues
73
+ } ) ;
74
+ } catch ( err ) {
75
+ logger . log ( {
76
+ test : 'get-multiple' ,
77
+ error : err . message
78
+ } ) ;
79
+ }
80
+
81
+ // Test 3 - get a single parameter with custom options (keyAttr: 'key', valueAttr: 'val')
82
+ try {
83
+ const parameterValueCustom = await providerGetCustomKeys . get ( 'my-param' ) ;
84
+ logger . log ( {
85
+ test : 'get-custom' ,
86
+ value : parameterValueCustom
87
+ } ) ;
88
+ } catch ( err ) {
89
+ logger . log ( {
90
+ test : 'get-custom' ,
91
+ error : err . message
92
+ } ) ;
93
+ }
94
+
95
+ // Test 4 - get multiple parameters with custom options (keyAttr: 'key', sortAttr: 'sort', valueAttr: 'val')
96
+ try {
97
+ const parametersValuesCustom = await providerGetMultipleCustomKeys . getMultiple ( 'my-params' ) ;
98
+ logger . log ( {
99
+ test : 'get-multiple-custom' ,
100
+ value : parametersValuesCustom
101
+ } ) ;
102
+ } catch ( err ) {
103
+ logger . log ( {
104
+ test : 'get-multiple-custom' ,
105
+ error : err . message
106
+ } ) ;
107
+ }
108
+
109
+ // Test 5 - get a single parameter with json transform
110
+ try {
111
+ const parameterValueJson = await providerGet . get ( 'my-param-json' , {
112
+ transform : 'json'
113
+ } ) ;
114
+ logger . log ( {
115
+ test : 'get-json-transform' ,
116
+ value : typeof parameterValueJson // should be object
117
+ } ) ;
118
+ } catch ( err ) {
119
+ logger . log ( {
120
+ test : 'get-json-transform' ,
121
+ error : err . message
122
+ } ) ;
123
+ }
124
+
125
+ // Test 6 - get a single parameter with binary transform
126
+ try {
127
+ const parameterValueBinary = await providerGet . get ( 'my-param-binary' , {
128
+ transform : 'binary'
129
+ } ) ;
130
+ logger . log ( {
131
+ test : 'get-binary-transform' ,
132
+ value : typeof parameterValueBinary // should be string
133
+ } ) ;
134
+ } catch ( err ) {
135
+ logger . log ( {
136
+ test : 'get-binary-transform' ,
137
+ error : err . message
138
+ } ) ;
139
+ }
140
+
141
+ // Test 7 - get multiple parameters with auto transform
142
+ try {
143
+ const parametersValuesAuto = await providerGetMultiple . getMultiple ( 'my-encoded-params' , {
144
+ transform : 'auto'
145
+ } ) ;
146
+ if ( ! parametersValuesAuto ) throw new Error ( 'parametersValuesAuto is undefined' ) ;
147
+
148
+ logger . log ( {
149
+ test : 'get-multiple-auto-transform' ,
150
+ value :
151
+ `${ typeof parametersValuesAuto [ 'config.json' ] } ,${ typeof parametersValuesAuto [ 'key.binary' ] } ` // should be object,string
152
+ } ) ;
153
+ } catch ( err ) {
154
+ logger . log ( {
155
+ test : 'get-multiple-auto-transform' ,
156
+ error : err . message
157
+ } ) ;
158
+ }
159
+
160
+ // # TODO: Uncomment code below once #1222 is fixed
161
+ /**
162
+ * Test 8 - get a parameter twice, second time should be cached
163
+ *
164
+ * Should only make 1 request, we use middleware to count requests
165
+ */
166
+ /*
167
+ try {
168
+ await providerWithMiddleware.get('my-param');
169
+ await providerWithMiddleware.get('my-param');
170
+ logger.log({
171
+ test: 'get-cache-request-count',
172
+ value: middleware.requestCount
173
+ });
174
+ } catch (err) {
175
+ logger.log({
176
+ test: 'get-cache-request-count',
177
+ error: err.message
178
+ });
179
+ }
180
+ */
181
+
182
+ /**
183
+ * Test 9 - get a parameter once more but with forceFetch = true
184
+ *
185
+ * Request count should increase to 2, we use middleware to count requests
186
+ */
187
+ /*
188
+ try {
189
+ await providerWithMiddleware.get('my-param', { forceFetch: true });
190
+ logger.log({
191
+ test: 'get-force-fetch-request-count',
192
+ value: middleware.requestCount
193
+ });
194
+ } catch (err) {
195
+ logger.log({
196
+ test: 'get-force-fetch-request-count',
197
+ error: err.message
198
+ });
199
+ }
200
+ */
201
+ } ;
0 commit comments