@aws-lambda-powertools/jmespath 0.0.0-alpha.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +14 -0
- package/README.md +214 -5
- package/lib/cjs/Expression.d.ts +19 -0
- package/lib/cjs/Expression.d.ts.map +1 -0
- package/lib/cjs/Expression.js +38 -0
- package/lib/cjs/Functions.d.ts +262 -0
- package/lib/cjs/Functions.d.ts.map +1 -0
- package/lib/cjs/Functions.js +619 -0
- package/lib/cjs/Lexer.d.ts +19 -0
- package/lib/cjs/Lexer.d.ts.map +1 -0
- package/lib/cjs/Lexer.js +271 -0
- package/lib/cjs/ParsedResult.d.ts +15 -0
- package/lib/cjs/ParsedResult.d.ts.map +1 -0
- package/lib/cjs/ParsedResult.js +33 -0
- package/lib/cjs/Parser.d.ts +35 -0
- package/lib/cjs/Parser.d.ts.map +1 -0
- package/lib/cjs/Parser.js +506 -0
- package/lib/cjs/PowertoolsFunctions.d.ts +36 -0
- package/lib/cjs/PowertoolsFunctions.d.ts.map +1 -0
- package/lib/cjs/PowertoolsFunctions.js +69 -0
- package/lib/cjs/TreeInterpreter.d.ts +28 -0
- package/lib/cjs/TreeInterpreter.d.ts.map +1 -0
- package/lib/cjs/TreeInterpreter.js +313 -0
- package/lib/cjs/ast.d.ts +185 -0
- package/lib/cjs/ast.d.ts.map +1 -0
- package/lib/cjs/ast.js +275 -0
- package/lib/cjs/constants.d.ts +60 -0
- package/lib/cjs/constants.d.ts.map +1 -0
- package/lib/cjs/constants.js +96 -0
- package/lib/cjs/envelopes.d.ts +69 -0
- package/lib/cjs/envelopes.d.ts.map +1 -0
- package/lib/cjs/envelopes.js +90 -0
- package/lib/cjs/errors.d.ts +183 -0
- package/lib/cjs/errors.d.ts.map +1 -0
- package/lib/cjs/errors.js +206 -0
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.d.ts.map +1 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/search.d.ts +50 -0
- package/lib/cjs/search.d.ts.map +1 -0
- package/lib/cjs/search.js +55 -0
- package/lib/cjs/types.d.ts +84 -0
- package/lib/cjs/types.d.ts.map +1 -0
- package/lib/cjs/types.js +2 -0
- package/lib/cjs/utils.d.ts +87 -0
- package/lib/cjs/utils.d.ts.map +1 -0
- package/lib/cjs/utils.js +282 -0
- package/lib/esm/Expression.d.ts +19 -0
- package/lib/esm/Expression.d.ts.map +1 -0
- package/lib/esm/Expression.js +35 -0
- package/lib/esm/Functions.d.ts +262 -0
- package/lib/esm/Functions.d.ts.map +1 -0
- package/lib/esm/Functions.js +616 -0
- package/lib/esm/Lexer.d.ts +19 -0
- package/lib/esm/Lexer.d.ts.map +1 -0
- package/lib/esm/Lexer.js +268 -0
- package/lib/esm/ParsedResult.d.ts +15 -0
- package/lib/esm/ParsedResult.d.ts.map +1 -0
- package/lib/esm/ParsedResult.js +30 -0
- package/lib/esm/Parser.d.ts +35 -0
- package/lib/esm/Parser.d.ts.map +1 -0
- package/lib/esm/Parser.js +503 -0
- package/lib/esm/PowertoolsFunctions.d.ts +36 -0
- package/lib/esm/PowertoolsFunctions.d.ts.map +1 -0
- package/lib/esm/PowertoolsFunctions.js +66 -0
- package/lib/esm/TreeInterpreter.d.ts +28 -0
- package/lib/esm/TreeInterpreter.d.ts.map +1 -0
- package/lib/esm/TreeInterpreter.js +310 -0
- package/lib/esm/ast.d.ts +185 -0
- package/lib/esm/ast.d.ts.map +1 -0
- package/lib/esm/ast.js +251 -0
- package/lib/esm/constants.d.ts +60 -0
- package/lib/esm/constants.d.ts.map +1 -0
- package/lib/esm/constants.js +88 -0
- package/lib/esm/envelopes.d.ts +69 -0
- package/lib/esm/envelopes.d.ts.map +1 -0
- package/lib/esm/envelopes.js +74 -0
- package/lib/esm/errors.d.ts +183 -0
- package/lib/esm/errors.d.ts.map +1 -0
- package/lib/esm/errors.js +195 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.d.ts.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/search.d.ts +50 -0
- package/lib/esm/search.d.ts.map +1 -0
- package/lib/esm/search.js +52 -0
- package/lib/esm/types.d.ts +84 -0
- package/lib/esm/types.d.ts.map +1 -0
- package/lib/esm/types.js +1 -0
- package/lib/esm/utils.d.ts +87 -0
- package/lib/esm/utils.d.ts.map +1 -0
- package/lib/esm/utils.js +275 -0
- package/package.json +51 -12
- package/lib/index.d.ts +0 -0
- package/lib/index.js +0 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to
|
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
10
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
11
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
12
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
13
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
14
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,9 +1,218 @@
|
|
|
1
1
|
# Powertools for AWS Lambda (TypeScript) - JMESPath Utility <!-- omit in toc -->
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
5
|
-
| **This package is currently released as placeholder for the imminent launch**. <br /> The package is not intended for production usage, for additional info please reach out via our GitHub repository. |
|
|
3
|
+
Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless [best practices and increase developer velocity](https://docs.powertools.aws.dev/lambda/typescript/latest/#features).
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
You can use the package in both TypeScript and JavaScript code bases.
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
- [Intro](#intro)
|
|
8
|
+
- [Usage](#usage)
|
|
9
|
+
- [Basic usage](#basic-usage)
|
|
10
|
+
- [Extract data from envelopes](#extract-data-from-envelopes)
|
|
11
|
+
- [JMESPath custom functions](#jmespath-custom-functions)
|
|
12
|
+
- [Contribute](#contribute)
|
|
13
|
+
- [Roadmap](#roadmap)
|
|
14
|
+
- [Connect](#connect)
|
|
15
|
+
- [How to support Powertools for AWS Lambda (TypeScript)?](#how-to-support-powertools-for-aws-lambda-typescript)
|
|
16
|
+
- [Becoming a reference customer](#becoming-a-reference-customer)
|
|
17
|
+
- [Sharing your work](#sharing-your-work)
|
|
18
|
+
- [Using Lambda Layer](#using-lambda-layer)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
|
|
21
|
+
## Intro
|
|
22
|
+
|
|
23
|
+
The JMESPath utility is a high-level function to parse and extract data from JSON objects using JMESPath expressions.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
To get started, install the library by running:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm i @aws-lambda-powertools/jmespath
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Basic usage
|
|
34
|
+
|
|
35
|
+
At its core, the library provides a utility function to extract data from a JSON object using a JMESPath expression.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { search } from '@aws-lambda-powertools/jmespath';
|
|
39
|
+
import { Logger } from '@aws-lambda-powertools/logger';
|
|
40
|
+
|
|
41
|
+
const logger = new Logger();
|
|
42
|
+
|
|
43
|
+
type MyEvent = {
|
|
44
|
+
foo: {
|
|
45
|
+
bar: string;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const handler = async (event: MyEvent): Promise<void> => {
|
|
50
|
+
const result = search(event, 'foo.bar');
|
|
51
|
+
logger.info(result); // "baz"
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Extract data from envelopes
|
|
56
|
+
|
|
57
|
+
In some cases, you may want to extract data from an envelope. The library provides a utility function to help you work with envelopes and extract data from them.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { extractDataFromEnvelope } from '@aws-lambda-powertools/jmespath/envelopes';
|
|
61
|
+
|
|
62
|
+
type MyEvent = {
|
|
63
|
+
body: string; // "{\"customerId\":\"dd4649e6-2484-4993-acb8-0f9123103394\"}"
|
|
64
|
+
deeplyNested: Array<{ someData: number[] }>;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type MessageBody = {
|
|
68
|
+
customerId: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const handler = async (event: MyEvent): Promise<unknown> => {
|
|
72
|
+
const payload = extractDataFromEnvelope<MessageBody>(
|
|
73
|
+
event,
|
|
74
|
+
'powertools_json(body)'
|
|
75
|
+
);
|
|
76
|
+
const { customerId } = payload; // now deserialized
|
|
77
|
+
|
|
78
|
+
// also works for fetching and flattening deeply nested data
|
|
79
|
+
const someData = extractDataFromEnvelope<number[]>(
|
|
80
|
+
event,
|
|
81
|
+
'deeplyNested[*].someData[]'
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
customerId,
|
|
86
|
+
message: 'success',
|
|
87
|
+
context: someData,
|
|
88
|
+
statusCode: 200,
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The library provides [a set of built-in envelopes](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/jmespath/#built-in-envelopes) to help you extract data from common event sources, such as S3, SQS, and SNS, and more.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import {
|
|
97
|
+
extractDataFromEnvelope,
|
|
98
|
+
SQS,
|
|
99
|
+
} from '@aws-lambda-powertools/jmespath/envelopes';
|
|
100
|
+
import { Logger } from '@aws-lambda-powertools/logger';
|
|
101
|
+
import type { SQSEvent } from 'aws-lambda';
|
|
102
|
+
|
|
103
|
+
const logger = new Logger();
|
|
104
|
+
|
|
105
|
+
type MessageBody = {
|
|
106
|
+
customerId: string;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const handler = async (event: SQSEvent): Promise<void> => {
|
|
110
|
+
const records = extractDataFromEnvelope<Array<MessageBody>>(event, SQS);
|
|
111
|
+
for (const record of records) {
|
|
112
|
+
// records is now a list containing the deserialized body of each message
|
|
113
|
+
const { customerId } = record;
|
|
114
|
+
logger.appendKeys({ customerId });
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### JMESPath custom functions
|
|
120
|
+
|
|
121
|
+
In addition to all the [built-in JMESPath functions](https://jmespath.org/specification.html#built-in-functions), the library provides custom functions to help you work with complex data structures. For example, you can use the `powertools_json` function to parse a JSON string, or the `powertools_base64` function to decode a base64-encoded string:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { extractDataFromEnvelope } from '@aws-lambda-powertools/jmespath/envelopes';
|
|
125
|
+
import { Logger } from '@aws-lambda-powertools/logger';
|
|
126
|
+
|
|
127
|
+
const logger = new Logger();
|
|
128
|
+
|
|
129
|
+
export const handler = async (event: { payload: string }): Promise<void> => {
|
|
130
|
+
const data = extractDataFromEnvelope<string>(
|
|
131
|
+
event,
|
|
132
|
+
'powertools_json(powertools_base64(payload))'
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
logger.info('Decoded payload', { data });
|
|
136
|
+
};
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Finally, you can also extend the library with your own custom functions. Below an example of how to create a custom function to decode a Brotli-compressed string.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { fromBase64 } from '@aws-lambda-powertools/commons/utils/base64';
|
|
143
|
+
import { extractDataFromEnvelope } from '@aws-lambda-powertools/jmespath/envelopes';
|
|
144
|
+
import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
145
|
+
import { Logger } from '@aws-lambda-powertools/logger';
|
|
146
|
+
import { brotliDecompressSync } from 'node:zlib';
|
|
147
|
+
|
|
148
|
+
const logger = new Logger();
|
|
149
|
+
|
|
150
|
+
class CustomFunctions extends PowertoolsFunctions {
|
|
151
|
+
@PowertoolsFunctions.signature({
|
|
152
|
+
argumentsSpecs: [['string']],
|
|
153
|
+
variadic: false,
|
|
154
|
+
})
|
|
155
|
+
public funcDecodeBrotliCompression(value: string): string {
|
|
156
|
+
const encoded = fromBase64(value, 'base64');
|
|
157
|
+
const uncompressed = brotliDecompressSync(encoded);
|
|
158
|
+
|
|
159
|
+
return uncompressed.toString();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const handler = async (event: { payload: string }): Promise<void> => {
|
|
164
|
+
const message = extractDataFromEnvelope<string>(
|
|
165
|
+
event,
|
|
166
|
+
'Records[*].decode_brotli_compression(notification) | [*].powertools_json(@).message',
|
|
167
|
+
{ customFunctions: new CustomFunctions() }
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
logger.info('Decoded message', { message });
|
|
171
|
+
};
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Contribute
|
|
175
|
+
|
|
176
|
+
If you are interested in contributing to this project, please refer to our [Contributing Guidelines](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/CONTRIBUTING.md).
|
|
177
|
+
|
|
178
|
+
## Roadmap
|
|
179
|
+
|
|
180
|
+
The roadmap of Powertools for AWS Lambda (TypeScript) is driven by customers’ demand.
|
|
181
|
+
Help us prioritize upcoming functionalities or utilities by [upvoting existing RFCs and feature requests](https://github.com/aws-powertools/powertools-lambda-typescript/issues), or [creating new ones](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new/choose), in this GitHub repository.
|
|
182
|
+
|
|
183
|
+
## Connect
|
|
184
|
+
|
|
185
|
+
* **Powertools for AWS Lambda on Discord**: `#typescript` - **[Invite link](https://discord.gg/B8zZKbbyET)**
|
|
186
|
+
* **Email**: aws-lambda-powertools-feedback@amazon.com
|
|
187
|
+
|
|
188
|
+
## How to support Powertools for AWS Lambda (TypeScript)?
|
|
189
|
+
|
|
190
|
+
### Becoming a reference customer
|
|
191
|
+
|
|
192
|
+
Knowing which companies are using this library is important to help prioritize the project internally. If your company is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by raising a [Support Powertools for AWS Lambda (TypeScript) (become a reference)](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new?assignees=&labels=customer-reference&template=support_powertools.yml&title=%5BSupport+Lambda+Powertools%5D%3A+%3Cyour+organization+name%3E) issue.
|
|
193
|
+
|
|
194
|
+
The following companies, among others, use Powertools:
|
|
195
|
+
|
|
196
|
+
* [Hashnode](https://hashnode.com/)
|
|
197
|
+
* [Trek10](https://www.trek10.com/)
|
|
198
|
+
* [Elva](https://elva-group.com)
|
|
199
|
+
* [globaldatanet](https://globaldatanet.com/)
|
|
200
|
+
* [Bailey Nelson](https://www.baileynelson.com.au)
|
|
201
|
+
* [Perfect Post](https://www.perfectpost.fr)
|
|
202
|
+
* [Sennder](https://sennder.com/)
|
|
203
|
+
* [Certible](https://www.certible.com/)
|
|
204
|
+
* [tecRacer GmbH & Co. KG](https://www.tecracer.com/)
|
|
205
|
+
* [AppYourself](https://appyourself.net)
|
|
206
|
+
* [Alma Media](https://www.almamedia.fi)
|
|
207
|
+
|
|
208
|
+
### Sharing your work
|
|
209
|
+
|
|
210
|
+
Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog post, workshops, presentation, sample apps and others. Check out what the community has already shared about Powertools for AWS Lambda (TypeScript) [here](https://docs.powertools.aws.dev/lambda/typescript/latest/we_made_this).
|
|
211
|
+
|
|
212
|
+
### Using Lambda Layer
|
|
213
|
+
|
|
214
|
+
This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When [using Layers](https://docs.powertools.aws.dev/lambda/typescript/latest/#lambda-layer), you can add Powertools as a dev dependency to not impact the development process.
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
This library is licensed under the MIT-0 License. See the LICENSE file.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { TreeInterpreter } from './TreeInterpreter.js';
|
|
2
|
+
import type { JSONObject, Node } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Apply a JMESPath expression to a JSON value.
|
|
5
|
+
*/
|
|
6
|
+
declare class Expression {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(expression: Node, interpreter: TreeInterpreter);
|
|
9
|
+
/**
|
|
10
|
+
* Evaluate the expression against a JSON value.
|
|
11
|
+
*
|
|
12
|
+
* @param value The JSON value to apply the expression to.
|
|
13
|
+
* @param node The node to visit.
|
|
14
|
+
* @returns The result of applying the expression to the value.
|
|
15
|
+
*/
|
|
16
|
+
visit(value: JSONObject, node?: Node): JSONObject;
|
|
17
|
+
}
|
|
18
|
+
export { Expression };
|
|
19
|
+
//# sourceMappingURL=Expression.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Expression.d.ts","sourceRoot":"","sources":["../../src/Expression.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEnD;;GAEG;AACH,cAAM,UAAU;;gBAIK,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe;IAKjE;;;;;;OAMG;IACI,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,UAAU;CAGzD;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _Expression_expression, _Expression_interpreter;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Expression = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* Apply a JMESPath expression to a JSON value.
|
|
18
|
+
*/
|
|
19
|
+
class Expression {
|
|
20
|
+
constructor(expression, interpreter) {
|
|
21
|
+
_Expression_expression.set(this, void 0);
|
|
22
|
+
_Expression_interpreter.set(this, void 0);
|
|
23
|
+
__classPrivateFieldSet(this, _Expression_expression, expression, "f");
|
|
24
|
+
__classPrivateFieldSet(this, _Expression_interpreter, interpreter, "f");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Evaluate the expression against a JSON value.
|
|
28
|
+
*
|
|
29
|
+
* @param value The JSON value to apply the expression to.
|
|
30
|
+
* @param node The node to visit.
|
|
31
|
+
* @returns The result of applying the expression to the value.
|
|
32
|
+
*/
|
|
33
|
+
visit(value, node) {
|
|
34
|
+
return __classPrivateFieldGet(this, _Expression_interpreter, "f").visit(node ?? __classPrivateFieldGet(this, _Expression_expression, "f"), value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.Expression = Expression;
|
|
38
|
+
_Expression_expression = new WeakMap(), _Expression_interpreter = new WeakMap();
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type { JSONArray, JSONObject, JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { Expression } from './Expression.js';
|
|
3
|
+
import type { FunctionSignatureDecorator, FunctionSignatureOptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A class that contains the built-in JMESPath functions.
|
|
6
|
+
*
|
|
7
|
+
* The built-in functions are implemented as methods on the Functions class.
|
|
8
|
+
* Each method is decorated with the `@Function.signature()` decorator to enforce the
|
|
9
|
+
* arity and types of the arguments passed to the function at runtime.
|
|
10
|
+
*
|
|
11
|
+
* You can extend the Functions class to add custom functions by creating a new class
|
|
12
|
+
* that extends Functions and adding new methods to it.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
17
|
+
* import { Functions } from '@aws-lambda-powertools/jmespath/functions';
|
|
18
|
+
*
|
|
19
|
+
* class MyFunctions extends Functions {
|
|
20
|
+
* @Functions.signature({
|
|
21
|
+
* argumentsSpecs: [['number'], ['number']],
|
|
22
|
+
* variadic: true,
|
|
23
|
+
* })
|
|
24
|
+
* public funcMyMethod(args: Array<number>): unknown {
|
|
25
|
+
* // ...
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* const myFunctions = new MyFunctions();
|
|
30
|
+
*
|
|
31
|
+
* search('myMethod(@)', {}, { customFunctions: new MyFunctions() });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare class Functions {
|
|
35
|
+
/**
|
|
36
|
+
* A set of all the custom functions available in this and all child classes.
|
|
37
|
+
*/
|
|
38
|
+
methods: Set<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Get the absolute value of the provided number.
|
|
41
|
+
*
|
|
42
|
+
* @param args The number to get the absolute value of
|
|
43
|
+
*/
|
|
44
|
+
funcAbs(args: number): number;
|
|
45
|
+
/**
|
|
46
|
+
* Calculate the average of the numbers in the provided array.
|
|
47
|
+
*
|
|
48
|
+
* @param args The numbers to average
|
|
49
|
+
*/
|
|
50
|
+
funcAvg(args: Array<number>): number;
|
|
51
|
+
/**
|
|
52
|
+
* Get the ceiling of the provided number.
|
|
53
|
+
*
|
|
54
|
+
* @param args The number to get the ceiling of
|
|
55
|
+
*/
|
|
56
|
+
funcCeil(args: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* Determine if the given value is contained in the provided array or string.
|
|
59
|
+
*
|
|
60
|
+
* @param haystack The array or string to check
|
|
61
|
+
* @param needle The value to check for
|
|
62
|
+
*/
|
|
63
|
+
funcContains(haystack: string, needle: string): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Determines if the provided string ends with the provided suffix.
|
|
66
|
+
*
|
|
67
|
+
* @param str The string to check
|
|
68
|
+
* @param suffix The suffix to check for
|
|
69
|
+
*/
|
|
70
|
+
funcEndsWith(str: string, suffix: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Get the floor of the provided number.
|
|
73
|
+
*
|
|
74
|
+
* @param args The number to get the floor of
|
|
75
|
+
*/
|
|
76
|
+
funcFloor(args: number): number;
|
|
77
|
+
/**
|
|
78
|
+
* Join the provided array into a single string.
|
|
79
|
+
*
|
|
80
|
+
* @param separator The separator to use
|
|
81
|
+
* @param items The array of itmes to join
|
|
82
|
+
*/
|
|
83
|
+
funcJoin(separator: string, items: Array<string>): string;
|
|
84
|
+
/**
|
|
85
|
+
* Get the keys of the provided object.
|
|
86
|
+
*
|
|
87
|
+
* @param arg The object to get the keys of
|
|
88
|
+
*/
|
|
89
|
+
funcKeys(arg: JSONObject): string[];
|
|
90
|
+
/**
|
|
91
|
+
* Get the number of items in the provided item.
|
|
92
|
+
*
|
|
93
|
+
* @param arg The array to get the length of
|
|
94
|
+
*/
|
|
95
|
+
funcLength(arg: string | Array<unknown> | Record<string, unknown>): number;
|
|
96
|
+
/**
|
|
97
|
+
* Map the provided function over the provided array.
|
|
98
|
+
*
|
|
99
|
+
* @param expression The expression to map over the array
|
|
100
|
+
* @param args The array to map the expression over
|
|
101
|
+
*/
|
|
102
|
+
funcMap(expression: Expression, args: JSONArray): JSONArray | Array<unknown>;
|
|
103
|
+
/**
|
|
104
|
+
* Get the maximum value in the provided array.
|
|
105
|
+
*
|
|
106
|
+
* @param arg The array to get the maximum value of
|
|
107
|
+
*/
|
|
108
|
+
funcMax(arg: Array<number | string>): number | string | null;
|
|
109
|
+
/**
|
|
110
|
+
* Get the item in the provided array that has the maximum value when the provided expression is evaluated.
|
|
111
|
+
*
|
|
112
|
+
* @param args The array of items to get the maximum value of
|
|
113
|
+
* @param expression The expression to evaluate for each item in the array
|
|
114
|
+
*/
|
|
115
|
+
funcMaxBy(args: Array<JSONObject>, expression: Expression): JSONObject | null;
|
|
116
|
+
/**
|
|
117
|
+
* Merge the provided objects into a single object.
|
|
118
|
+
*
|
|
119
|
+
* Note that this is a shallow merge and will not merge nested objects.
|
|
120
|
+
*
|
|
121
|
+
* @param args The objects to merge
|
|
122
|
+
*/
|
|
123
|
+
funcMerge(...args: Array<JSONObject>): JSONObject;
|
|
124
|
+
/**
|
|
125
|
+
* Get the minimum value in the provided array.
|
|
126
|
+
*
|
|
127
|
+
* @param arg The array to get the minimum value of
|
|
128
|
+
*/
|
|
129
|
+
funcMin(arg: Array<number>): number | string | null;
|
|
130
|
+
/**
|
|
131
|
+
* Get the item in the provided array that has the minimum value when the provided expression is evaluated.
|
|
132
|
+
*
|
|
133
|
+
* @param args The array of items to get the minimum value of
|
|
134
|
+
* @param expression The expression to evaluate for each item in the array
|
|
135
|
+
*/
|
|
136
|
+
funcMinBy(args: Array<JSONObject>, expression: Expression): JSONObject | null;
|
|
137
|
+
/**
|
|
138
|
+
* Get the first argument that does not evaluate to null.
|
|
139
|
+
* If all arguments evaluate to null, then null is returned.
|
|
140
|
+
*
|
|
141
|
+
* @param args The keys of the items to check
|
|
142
|
+
*/
|
|
143
|
+
funcNotNull(...args: Array<JSONValue>): JSONValue | null;
|
|
144
|
+
/**
|
|
145
|
+
* Reverses the provided string or array.
|
|
146
|
+
*
|
|
147
|
+
* @param arg The string or array to reverse
|
|
148
|
+
*/
|
|
149
|
+
funcReverse(arg: string | Array<unknown>): string | Array<unknown>;
|
|
150
|
+
/**
|
|
151
|
+
* Sort the provided array.
|
|
152
|
+
*
|
|
153
|
+
* @param arg The array to sort
|
|
154
|
+
*/
|
|
155
|
+
funcSort(arg: Array<string> | Array<number>): Array<unknown>;
|
|
156
|
+
/**
|
|
157
|
+
* Sort the provided array by the provided expression.
|
|
158
|
+
*
|
|
159
|
+
* @param args The array to sort
|
|
160
|
+
* @param expression The expression to sort by
|
|
161
|
+
*/
|
|
162
|
+
funcSortBy(args: Array<JSONValue>, expression: Expression): Array<unknown>;
|
|
163
|
+
/**
|
|
164
|
+
* Determines if the provided string starts with the provided prefix.
|
|
165
|
+
*
|
|
166
|
+
* @param str The string to check
|
|
167
|
+
* @param prefix The prefix to check for
|
|
168
|
+
*/
|
|
169
|
+
funcStartsWith(str: string, prefix: string): boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Sum the provided numbers.
|
|
172
|
+
*
|
|
173
|
+
* @param args The numbers to sum
|
|
174
|
+
*/
|
|
175
|
+
funcSum(args: Array<number>): number;
|
|
176
|
+
/**
|
|
177
|
+
* Convert the provided value to an array.
|
|
178
|
+
*
|
|
179
|
+
* If the provided value is an array, then it is returned.
|
|
180
|
+
* Otherwise, the value is wrapped in an array and returned.
|
|
181
|
+
*
|
|
182
|
+
* @param arg The items to convert to an array
|
|
183
|
+
*/
|
|
184
|
+
funcToArray(arg: JSONArray | Array<JSONValue>): Array<JSONValue> | JSONArray;
|
|
185
|
+
/**
|
|
186
|
+
* Convert the provided value to a number.
|
|
187
|
+
*
|
|
188
|
+
* If the provided value is a number, then it is returned.
|
|
189
|
+
* Otherwise, the value is converted to a number and returned.
|
|
190
|
+
*
|
|
191
|
+
* If the value cannot be converted to a number, then null is returned.
|
|
192
|
+
*
|
|
193
|
+
* @param arg The value to convert to a number
|
|
194
|
+
*/
|
|
195
|
+
funcToNumber(arg: JSONValue): number | null;
|
|
196
|
+
/**
|
|
197
|
+
* Convert the provided value to a string.
|
|
198
|
+
*
|
|
199
|
+
* If the provided value is a string, then it is returned.
|
|
200
|
+
* Otherwise, the value is converted to a string and returned.
|
|
201
|
+
*
|
|
202
|
+
* @param arg The value to convert to a string
|
|
203
|
+
*/
|
|
204
|
+
funcToString(arg: JSONValue): string;
|
|
205
|
+
/**
|
|
206
|
+
* Get the type of the provided value.
|
|
207
|
+
*
|
|
208
|
+
* @param arg The value to check the type of
|
|
209
|
+
*/
|
|
210
|
+
funcType(arg: JSONValue): string;
|
|
211
|
+
/**
|
|
212
|
+
* Get the values of the provided object.
|
|
213
|
+
*
|
|
214
|
+
* @param arg The object to get the values of
|
|
215
|
+
*/
|
|
216
|
+
funcValues(arg: JSONObject): JSONValue[];
|
|
217
|
+
/**
|
|
218
|
+
* Lazily introspects the methods of the class instance and all child classes
|
|
219
|
+
* to get the names of the methods that correspond to JMESPath functions.
|
|
220
|
+
*
|
|
221
|
+
* This method is used to get the names of the custom functions that are available
|
|
222
|
+
* in the class instance and all child classes. The names of the functions are used
|
|
223
|
+
* to create the custom function map that is passed to the JMESPath search function.
|
|
224
|
+
*
|
|
225
|
+
* The method traverses the inheritance chain going from the leaf class to the root class
|
|
226
|
+
* and stops when it reaches the `Functions` class, which is the root class.
|
|
227
|
+
*
|
|
228
|
+
* In doing so, it collects the names of the methods that start with `func` and adds them
|
|
229
|
+
* to the `methods` set. Finally, when the recursion collects back to the current instance,
|
|
230
|
+
* it adds the collected methods to the `this.methods` set so that they can be accessed later.
|
|
231
|
+
*
|
|
232
|
+
* @param scope The scope of the class instance to introspect
|
|
233
|
+
*/
|
|
234
|
+
introspectMethods(scope?: Functions): Set<string>;
|
|
235
|
+
/**
|
|
236
|
+
* Decorator to enforce the signature of a function at runtime.
|
|
237
|
+
*
|
|
238
|
+
* The signature decorator enforces the arity and types of the arguments
|
|
239
|
+
* passed to a function at runtime. If the arguments do not match the
|
|
240
|
+
* expected arity or types errors are thrown.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
245
|
+
*
|
|
246
|
+
* class MyFunctions extends Functions {
|
|
247
|
+
* @Functions.signature({
|
|
248
|
+
* argumentsSpecs: [['number'], ['number']],
|
|
249
|
+
* variadic: true,
|
|
250
|
+
* })
|
|
251
|
+
* public funcMyMethod(args: Array<number>): unknown {
|
|
252
|
+
* // ...
|
|
253
|
+
* }
|
|
254
|
+
* }
|
|
255
|
+
* ```
|
|
256
|
+
*
|
|
257
|
+
* @param options The options for the signature decorator
|
|
258
|
+
*/
|
|
259
|
+
static signature(options: FunctionSignatureOptions): FunctionSignatureDecorator;
|
|
260
|
+
}
|
|
261
|
+
export { Functions };
|
|
262
|
+
//# sourceMappingURL=Functions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Functions.d.ts","sourceRoot":"","sources":["../../src/Functions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,UAAU,EACV,SAAS,EACV,MAAM,sCAAsC,CAAC;AAM9C,OAAO,KAA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