@aws-lambda-powertools/jmespath
Version:
A type safe and modern jmespath module to parse and extract data from JSON documents using JMESPath
67 lines (66 loc) • 2.53 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { gunzipSync } from 'node:zlib';
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 envelopes.extractDataFromEnvelope} function, the PowertoolsFunctions class is automatically used.
*
*/
class PowertoolsFunctions extends Functions {
funcPowertoolsBase64(value) {
return decoder.decode(fromBase64(value, 'base64'));
}
funcPowertoolsBase64Gzip(value) {
const encoded = fromBase64(value, 'base64');
const uncompressed = gunzipSync(encoded);
return uncompressed.toString();
}
funcPowertoolsJson(value) {
return JSON.parse(value);
}
}
__decorate([
Functions.signature({
argumentsSpecs: [['string']],
})
], PowertoolsFunctions.prototype, "funcPowertoolsBase64", null);
__decorate([
Functions.signature({
argumentsSpecs: [['string']],
})
], PowertoolsFunctions.prototype, "funcPowertoolsBase64Gzip", null);
__decorate([
Functions.signature({
argumentsSpecs: [['string']],
})
], PowertoolsFunctions.prototype, "funcPowertoolsJson", null);
export { PowertoolsFunctions, Functions };