From dad07dbd1b2040e9fc5eef1bde80f41887238d7c Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 21 Mar 2024 09:30:08 +0100 Subject: [PATCH] feat(jmespath): add powertools functions --- packages/jmespath/src/PowertoolsFunctions.ts | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 packages/jmespath/src/PowertoolsFunctions.ts diff --git a/packages/jmespath/src/PowertoolsFunctions.ts b/packages/jmespath/src/PowertoolsFunctions.ts new file mode 100644 index 0000000000..3cba848f73 --- /dev/null +++ b/packages/jmespath/src/PowertoolsFunctions.ts @@ -0,0 +1,61 @@ +import { gunzipSync } from 'node:zlib'; +import type { JSONValue } from '@aws-lambda-powertools/commons/types'; +import { fromBase64 } from '@aws-lambda-powertools/commons/utils/base64'; +import { Functions } from './Functions.js'; + +const decoder = new TextDecoder('utf-8'); + +/** + * Custom functions for the Powertools for AWS Lambda JMESPath module. + * + * Built-in JMESPath functions include: `powertools_json`, `powertools_base64`, `powertools_base64_gzip` + * + * You can use these functions to decode and/or deserialize JSON objects when using the {@link index.search | search} function. + * + * @example + * ```typescript + * import { search } from '@aws-lambda-powertools/jmespath'; + * import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions'; + * + * const data = { + * body: "{\"foo\": \"bar\"}" + * }; + * + * const result = search( + * 'powertools_json(body)', + * data, + * { customFunctions: new PowertoolsFunctions() } + * ); + * console.log(result); // { foo: 'bar' } + * ``` + * + * When using the {@link extractDataFromEnvelope} function, the PowertoolsFunctions class is automatically used. + * + */ +class PowertoolsFunctions extends Functions { + @Functions.signature({ + argumentsSpecs: [['string']], + }) + public funcPowertoolsBase64(value: string): string { + return decoder.decode(fromBase64(value, 'base64')); + } + + @Functions.signature({ + argumentsSpecs: [['string']], + }) + public funcPowertoolsBase64Gzip(value: string): string { + const encoded = fromBase64(value, 'base64'); + const uncompressed = gunzipSync(encoded); + + return uncompressed.toString(); + } + + @Functions.signature({ + argumentsSpecs: [['string']], + }) + public funcPowertoolsJson(value: string): JSONValue { + return JSON.parse(value); + } +} + +export { PowertoolsFunctions };