Skip to content

feat(jmespath): add powertools functions #2264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/jmespath/src/PowertoolsFunctions.ts
Original file line number Diff line number Diff line change
@@ -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 };