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 search function.

Example

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 extractDataFromEnvelope function, the PowertoolsFunctions class is automatically used.

Hierarchy (view full)

Constructors

Properties

methods: Set<string> = ...

A set of all the custom functions available in this and all child classes.

Methods

  • Get the absolute value of the provided number.

    Parameters

    • args: number

      The number to get the absolute value of

    Returns number

  • Calculate the average of the numbers in the provided array.

    Parameters

    • args: number[]

      The numbers to average

    Returns number

  • Get the ceiling of the provided number.

    Parameters

    • args: number

      The number to get the ceiling of

    Returns number

  • Determine if the given value is contained in the provided array or string.

    Parameters

    • haystack: string

      The array or string to check

    • needle: string

      The value to check for

    Returns boolean

  • Determines if the provided string ends with the provided suffix.

    Parameters

    • str: string

      The string to check

    • suffix: string

      The suffix to check for

    Returns boolean

  • Get the floor of the provided number.

    Parameters

    • args: number

      The number to get the floor of

    Returns number

  • Join the provided array into a single string.

    Parameters

    • separator: string

      The separator to use

    • items: string[]

      The array of itmes to join

    Returns string

  • Get the number of items in the provided item.

    Parameters

    • arg: string | unknown[] | Record<string, unknown>

      The array to get the length of

    Returns number

  • Map the provided function over the provided array.

    Parameters

    • expression: Expression

      The expression to map over the array

    • args: JSONArray

      The array to map the expression over

    Returns unknown[] | JSONArray

  • Get the maximum value in the provided array.

    Parameters

    • arg: (string | number)[]

      The array to get the maximum value of

    Returns null | string | number

  • Get the item in the provided array that has the maximum value when the provided expression is evaluated.

    Parameters

    • args: JSONObject[]

      The array of items to get the maximum value of

    • expression: Expression

      The expression to evaluate for each item in the array

    Returns null | JSONObject

  • Get the minimum value in the provided array.

    Parameters

    • arg: number[]

      The array to get the minimum value of

    Returns null | string | number

  • Get the item in the provided array that has the minimum value when the provided expression is evaluated.

    Parameters

    • args: JSONObject[]

      The array of items to get the minimum value of

    • expression: Expression

      The expression to evaluate for each item in the array

    Returns null | JSONObject

  • Reverses the provided string or array.

    Parameters

    • arg: string | unknown[]

      The string or array to reverse

    Returns string | unknown[]

  • Sort the provided array.

    Parameters

    • arg: string[] | number[]

      The array to sort

    Returns unknown[]

  • Sort the provided array by the provided expression.

    Parameters

    • args: JSONValue[]

      The array to sort

    • expression: Expression

      The expression to sort by

    Returns unknown[]

  • Determines if the provided string starts with the provided prefix.

    Parameters

    • str: string

      The string to check

    • prefix: string

      The prefix to check for

    Returns boolean

  • Sum the provided numbers.

    Parameters

    • args: number[]

      The numbers to sum

    Returns number

  • Convert the provided value to a number.

    If the provided value is a number, then it is returned. Otherwise, the value is converted to a number and returned.

    If the value cannot be converted to a number, then null is returned.

    Parameters

    • arg: JSONValue

      The value to convert to a number

    Returns null | number

  • Convert the provided value to a string.

    If the provided value is a string, then it is returned. Otherwise, the value is converted to a string and returned.

    Parameters

    • arg: JSONValue

      The value to convert to a string

    Returns string

  • Lazily introspects the methods of the class instance and all child classes to get the names of the methods that correspond to JMESPath functions.

    This method is used to get the names of the custom functions that are available in the class instance and all child classes. The names of the functions are used to create the custom function map that is passed to the JMESPath search function.

    The method traverses the inheritance chain going from the leaf class to the root class and stops when it reaches the Functions class, which is the root class.

    In doing so, it collects the names of the methods that start with func and adds them to the methods set. Finally, when the recursion collects back to the current instance, it adds the collected methods to the this.methods set so that they can be accessed later.

    Parameters

    • Optional scope: Functions

      The scope of the class instance to introspect

    Returns Set<string>

  • Decorator to enforce the signature of a function at runtime.

    The signature decorator enforces the arity and types of the arguments passed to a function at runtime. If the arguments do not match the expected arity or types errors are thrown.

    Parameters

    Returns FunctionSignatureDecorator

    Example

    import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';

    class MyFunctions extends Functions {
    ⁣@Functions.signature({
    argumentsSpecs: [['number'], ['number']],
    variadic: true,
    })
    public funcMyMethod(args: Array<number>): unknown {
    // ...
    }
    }