Skip to content

Commit a8dc5fa

Browse files
dreamorosisthulb
andauthored
feat(jmespath): add powertools functions (#2264)
Co-authored-by: Simon Thulbourn <[email protected]>
1 parent 24f8993 commit a8dc5fa

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { gunzipSync } from 'node:zlib';
2+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
3+
import { fromBase64 } from '@aws-lambda-powertools/commons/utils/base64';
4+
import { Functions } from './Functions.js';
5+
6+
const decoder = new TextDecoder('utf-8');
7+
8+
/**
9+
* Custom functions for the Powertools for AWS Lambda JMESPath module.
10+
*
11+
* Built-in JMESPath functions include: `powertools_json`, `powertools_base64`, `powertools_base64_gzip`
12+
*
13+
* You can use these functions to decode and/or deserialize JSON objects when using the {@link index.search | search} function.
14+
*
15+
* @example
16+
* ```typescript
17+
* import { search } from '@aws-lambda-powertools/jmespath';
18+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
19+
*
20+
* const data = {
21+
* body: "{\"foo\": \"bar\"}"
22+
* };
23+
*
24+
* const result = search(
25+
* 'powertools_json(body)',
26+
* data,
27+
* { customFunctions: new PowertoolsFunctions() }
28+
* );
29+
* console.log(result); // { foo: 'bar' }
30+
* ```
31+
*
32+
* When using the {@link extractDataFromEnvelope} function, the PowertoolsFunctions class is automatically used.
33+
*
34+
*/
35+
class PowertoolsFunctions extends Functions {
36+
@Functions.signature({
37+
argumentsSpecs: [['string']],
38+
})
39+
public funcPowertoolsBase64(value: string): string {
40+
return decoder.decode(fromBase64(value, 'base64'));
41+
}
42+
43+
@Functions.signature({
44+
argumentsSpecs: [['string']],
45+
})
46+
public funcPowertoolsBase64Gzip(value: string): string {
47+
const encoded = fromBase64(value, 'base64');
48+
const uncompressed = gunzipSync(encoded);
49+
50+
return uncompressed.toString();
51+
}
52+
53+
@Functions.signature({
54+
argumentsSpecs: [['string']],
55+
})
56+
public funcPowertoolsJson(value: string): JSONValue {
57+
return JSON.parse(value);
58+
}
59+
}
60+
61+
export { PowertoolsFunctions };

0 commit comments

Comments
 (0)