@aws-lambda-powertools/jmespath
Version:
A type safe and modern jmespath module to parse and extract data from JSON documents using JMESPath
71 lines (70 loc) • 2.91 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Functions = exports.PowertoolsFunctions = void 0;
const node_zlib_1 = require("node:zlib");
const base64_1 = require("@aws-lambda-powertools/commons/utils/base64");
const Functions_js_1 = require("./Functions.js");
Object.defineProperty(exports, "Functions", { enumerable: true, get: function () { return Functions_js_1.Functions; } });
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_js_1.Functions {
funcPowertoolsBase64(value) {
return decoder.decode((0, base64_1.fromBase64)(value, 'base64'));
}
funcPowertoolsBase64Gzip(value) {
const encoded = (0, base64_1.fromBase64)(value, 'base64');
const uncompressed = (0, node_zlib_1.gunzipSync)(encoded);
return uncompressed.toString();
}
funcPowertoolsJson(value) {
return JSON.parse(value);
}
}
exports.PowertoolsFunctions = PowertoolsFunctions;
__decorate([
Functions_js_1.Functions.signature({
argumentsSpecs: [['string']],
})
], PowertoolsFunctions.prototype, "funcPowertoolsBase64", null);
__decorate([
Functions_js_1.Functions.signature({
argumentsSpecs: [['string']],
})
], PowertoolsFunctions.prototype, "funcPowertoolsBase64Gzip", null);
__decorate([
Functions_js_1.Functions.signature({
argumentsSpecs: [['string']],
})
], PowertoolsFunctions.prototype, "funcPowertoolsJson", null);