@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.
Files changed (97) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +214 -5
  3. package/lib/cjs/Expression.d.ts +19 -0
  4. package/lib/cjs/Expression.d.ts.map +1 -0
  5. package/lib/cjs/Expression.js +38 -0
  6. package/lib/cjs/Functions.d.ts +262 -0
  7. package/lib/cjs/Functions.d.ts.map +1 -0
  8. package/lib/cjs/Functions.js +619 -0
  9. package/lib/cjs/Lexer.d.ts +19 -0
  10. package/lib/cjs/Lexer.d.ts.map +1 -0
  11. package/lib/cjs/Lexer.js +271 -0
  12. package/lib/cjs/ParsedResult.d.ts +15 -0
  13. package/lib/cjs/ParsedResult.d.ts.map +1 -0
  14. package/lib/cjs/ParsedResult.js +33 -0
  15. package/lib/cjs/Parser.d.ts +35 -0
  16. package/lib/cjs/Parser.d.ts.map +1 -0
  17. package/lib/cjs/Parser.js +506 -0
  18. package/lib/cjs/PowertoolsFunctions.d.ts +36 -0
  19. package/lib/cjs/PowertoolsFunctions.d.ts.map +1 -0
  20. package/lib/cjs/PowertoolsFunctions.js +69 -0
  21. package/lib/cjs/TreeInterpreter.d.ts +28 -0
  22. package/lib/cjs/TreeInterpreter.d.ts.map +1 -0
  23. package/lib/cjs/TreeInterpreter.js +313 -0
  24. package/lib/cjs/ast.d.ts +185 -0
  25. package/lib/cjs/ast.d.ts.map +1 -0
  26. package/lib/cjs/ast.js +275 -0
  27. package/lib/cjs/constants.d.ts +60 -0
  28. package/lib/cjs/constants.d.ts.map +1 -0
  29. package/lib/cjs/constants.js +96 -0
  30. package/lib/cjs/envelopes.d.ts +69 -0
  31. package/lib/cjs/envelopes.d.ts.map +1 -0
  32. package/lib/cjs/envelopes.js +90 -0
  33. package/lib/cjs/errors.d.ts +183 -0
  34. package/lib/cjs/errors.d.ts.map +1 -0
  35. package/lib/cjs/errors.js +206 -0
  36. package/lib/cjs/index.d.ts +3 -0
  37. package/lib/cjs/index.d.ts.map +1 -0
  38. package/lib/cjs/index.js +15 -0
  39. package/lib/cjs/package.json +1 -0
  40. package/lib/cjs/search.d.ts +50 -0
  41. package/lib/cjs/search.d.ts.map +1 -0
  42. package/lib/cjs/search.js +55 -0
  43. package/lib/cjs/types.d.ts +84 -0
  44. package/lib/cjs/types.d.ts.map +1 -0
  45. package/lib/cjs/types.js +2 -0
  46. package/lib/cjs/utils.d.ts +87 -0
  47. package/lib/cjs/utils.d.ts.map +1 -0
  48. package/lib/cjs/utils.js +282 -0
  49. package/lib/esm/Expression.d.ts +19 -0
  50. package/lib/esm/Expression.d.ts.map +1 -0
  51. package/lib/esm/Expression.js +35 -0
  52. package/lib/esm/Functions.d.ts +262 -0
  53. package/lib/esm/Functions.d.ts.map +1 -0
  54. package/lib/esm/Functions.js +616 -0
  55. package/lib/esm/Lexer.d.ts +19 -0
  56. package/lib/esm/Lexer.d.ts.map +1 -0
  57. package/lib/esm/Lexer.js +268 -0
  58. package/lib/esm/ParsedResult.d.ts +15 -0
  59. package/lib/esm/ParsedResult.d.ts.map +1 -0
  60. package/lib/esm/ParsedResult.js +30 -0
  61. package/lib/esm/Parser.d.ts +35 -0
  62. package/lib/esm/Parser.d.ts.map +1 -0
  63. package/lib/esm/Parser.js +503 -0
  64. package/lib/esm/PowertoolsFunctions.d.ts +36 -0
  65. package/lib/esm/PowertoolsFunctions.d.ts.map +1 -0
  66. package/lib/esm/PowertoolsFunctions.js +66 -0
  67. package/lib/esm/TreeInterpreter.d.ts +28 -0
  68. package/lib/esm/TreeInterpreter.d.ts.map +1 -0
  69. package/lib/esm/TreeInterpreter.js +310 -0
  70. package/lib/esm/ast.d.ts +185 -0
  71. package/lib/esm/ast.d.ts.map +1 -0
  72. package/lib/esm/ast.js +251 -0
  73. package/lib/esm/constants.d.ts +60 -0
  74. package/lib/esm/constants.d.ts.map +1 -0
  75. package/lib/esm/constants.js +88 -0
  76. package/lib/esm/envelopes.d.ts +69 -0
  77. package/lib/esm/envelopes.d.ts.map +1 -0
  78. package/lib/esm/envelopes.js +74 -0
  79. package/lib/esm/errors.d.ts +183 -0
  80. package/lib/esm/errors.d.ts.map +1 -0
  81. package/lib/esm/errors.js +195 -0
  82. package/lib/esm/index.d.ts +3 -0
  83. package/lib/esm/index.d.ts.map +1 -0
  84. package/lib/esm/index.js +2 -0
  85. package/lib/esm/package.json +1 -0
  86. package/lib/esm/search.d.ts +50 -0
  87. package/lib/esm/search.d.ts.map +1 -0
  88. package/lib/esm/search.js +52 -0
  89. package/lib/esm/types.d.ts +84 -0
  90. package/lib/esm/types.d.ts.map +1 -0
  91. package/lib/esm/types.js +1 -0
  92. package/lib/esm/utils.d.ts +87 -0
  93. package/lib/esm/utils.d.ts.map +1 -0
  94. package/lib/esm/utils.js +275 -0
  95. package/package.json +51 -12
  96. package/lib/index.d.ts +0 -0
  97. package/lib/index.js +0 -4
package/lib/esm/ast.js ADDED
@@ -0,0 +1,251 @@
1
+ /**
2
+ * AST node representing a comparator expression.
3
+ *
4
+ * A comparator expression is a binary expression that compares two values.
5
+ *
6
+ * @param name The name of the comparator
7
+ * @param first The left-hand side of the comparator
8
+ * @param second The right-hand side of the comparator
9
+ */
10
+ const comparator = (name, first, second) => ({
11
+ type: 'comparator',
12
+ children: [first, second],
13
+ value: name,
14
+ });
15
+ /**
16
+ * AST node representing the current node.
17
+ *
18
+ * The current node is a reference to the current value being processed.
19
+ * In JMESPath, the current node is represented by the `@` symbol.
20
+ */
21
+ const currentNode = () => ({
22
+ type: 'current',
23
+ children: [],
24
+ });
25
+ /**
26
+ * AST node representing an expression reference.
27
+ *
28
+ * An expression reference is a reference to another expression.
29
+ * In JMESPath, an expression reference is represented by the `&` symbol.
30
+ *
31
+ * @param expression The expression to reference
32
+ */
33
+ const expref = (expression) => ({
34
+ type: 'expref',
35
+ children: [expression],
36
+ });
37
+ /**
38
+ * AST node representing a function expression.
39
+ *
40
+ * A function expression is a reference to a function and its arguments.
41
+ * The JMESPath specification defines a set of built-in functions that can
42
+ * be used in expressions like `length(@)`, `map(@, &foo)`, etc.
43
+ *
44
+ * Custom functions can be added by extending the `Functions` class.
45
+ *
46
+ * @param name The name of the function
47
+ * @param args The arguments to the function
48
+ */
49
+ const functionExpression = (name, args) => ({
50
+ type: 'function_expression',
51
+ children: args,
52
+ value: name,
53
+ });
54
+ /**
55
+ * AST node representing a field reference.
56
+ *
57
+ * A field reference is a reference to a field in an object.
58
+ */
59
+ const field = (name) => ({
60
+ type: 'field',
61
+ children: [],
62
+ value: name,
63
+ });
64
+ /**
65
+ * AST node representing a filter projection.
66
+ *
67
+ * A filter projection is a binary expression that filters the left-hand side
68
+ * based on the right-hand side.
69
+ *
70
+ * In JMESPath, a filter projection is represented by the `[]` operator.
71
+ * For example, `people[?age > 18]` filters the `people` array based on the
72
+ * `age` field.
73
+ *
74
+ * @param left The left-hand side of the filter projection
75
+ * @param right The right-hand side of the filter projection
76
+ * @param comparator The comparator to use for the filter
77
+ */
78
+ const filterProjection = (left, right, comparator) => ({
79
+ type: 'filter_projection',
80
+ children: [left, right, comparator],
81
+ });
82
+ /**
83
+ * AST node representing a flatten expression.
84
+ *
85
+ * A flatten expression is a unary expression that flattens an array of arrays
86
+ * into a single array.
87
+ *
88
+ * In JMESPath, a flatten expression is represented by the `[]` operator.
89
+ * For example, `people[].name` flattens the `people` array and returns the
90
+ * `name` field of each object in the array.
91
+ *
92
+ * @param node The node to flatten
93
+ */
94
+ const flatten = (node) => ({
95
+ type: 'flatten',
96
+ children: [node],
97
+ });
98
+ /**
99
+ * AST node representing an identity expression.
100
+ */
101
+ const identity = () => ({ type: 'identity', children: [] });
102
+ /**
103
+ * AST node representing an index reference.
104
+ *
105
+ * An index reference is a reference to an index in an array.
106
+ * For example, `people[0]` references the first element in the `people` array.
107
+ *
108
+ * @param index The index to reference
109
+ */
110
+ const index = (index) => ({
111
+ type: 'index',
112
+ value: index,
113
+ children: [],
114
+ });
115
+ /**
116
+ * AST node representing an index expression.
117
+ *
118
+ * An index expression holds the index and the children of the expression.
119
+ *
120
+ * @param children The children of the index expression
121
+ */
122
+ const indexExpression = (children) => ({
123
+ type: 'index_expression',
124
+ children: children,
125
+ });
126
+ /**
127
+ * AST node representing a key-value pair.
128
+ *
129
+ * @param keyName The name of the key
130
+ * @param node The value of the key
131
+ */
132
+ const keyValPair = (keyName, node) => ({
133
+ type: 'key_val_pair',
134
+ children: [node],
135
+ value: keyName,
136
+ });
137
+ /**
138
+ * AST node representing a literal value.
139
+ *
140
+ * A literal value is a value that is not a reference to another node.
141
+ *
142
+ * @param literalValue The value of the literal
143
+ */
144
+ const literal = (literalValue) => ({
145
+ type: 'literal',
146
+ value: literalValue,
147
+ children: [],
148
+ });
149
+ /**
150
+ * AST node representing a multi-select object.
151
+ *
152
+ * A multi-select object is a reference to multiple nodes in an object.
153
+ *
154
+ * @param nodes
155
+ */
156
+ const multiSelectObject = (nodes) => ({
157
+ type: 'multi_select_object',
158
+ children: nodes,
159
+ });
160
+ /**
161
+ * AST node representing a multi-select list.
162
+ *
163
+ * @param nodes
164
+ */
165
+ const multiSelectList = (nodes) => ({
166
+ type: 'multi_select_list',
167
+ children: nodes,
168
+ });
169
+ /**
170
+ * AST node representing an or expression.
171
+ *
172
+ * @param left The left-hand side of the or expression
173
+ * @param right The right-hand side of the or expression
174
+ */
175
+ const orExpression = (left, right) => ({
176
+ type: 'or_expression',
177
+ children: [left, right],
178
+ });
179
+ /**
180
+ * AST node representing an and expression.
181
+ *
182
+ * @param left The left-hand side of the and expression
183
+ * @param right The right-hand side of the and expression
184
+ */
185
+ const andExpression = (left, right) => ({
186
+ type: 'and_expression',
187
+ children: [left, right],
188
+ });
189
+ /**
190
+ * AST node representing a not expression.
191
+ *
192
+ * @param left The left-hand side of the not expression
193
+ * @param right The right-hand side of the not expression
194
+ */
195
+ const notExpression = (expr) => ({
196
+ type: 'not_expression',
197
+ children: [expr],
198
+ });
199
+ /**
200
+ * AST node representing a pipe expression.
201
+ *
202
+ * @param left The left-hand side of the pipe expression
203
+ * @param right The right-hand side of the pipe expression
204
+ */
205
+ const pipe = (left, right) => ({
206
+ type: 'pipe',
207
+ children: [left, right],
208
+ });
209
+ /**
210
+ * AST node representing a projection.
211
+ *
212
+ * @param left The left-hand side of the projection
213
+ * @param right The right-hand side of the projection
214
+ */
215
+ const projection = (left, right) => ({
216
+ type: 'projection',
217
+ children: [left, right],
218
+ });
219
+ /**
220
+ * AST node representing a subexpression.
221
+ *
222
+ * @param children The children of the subexpression
223
+ */
224
+ const subexpression = (children) => ({
225
+ type: 'subexpression',
226
+ children: children,
227
+ });
228
+ /**
229
+ * AST node representing a slice.
230
+ *
231
+ * A slice is a reference to a range of values in an array.
232
+ *
233
+ * @param start The start of the slice
234
+ * @param end The end of the slice
235
+ * @param step The step of the slice
236
+ */
237
+ const slice = (start, end, step) => ({
238
+ type: 'slice',
239
+ children: [start, end, step],
240
+ });
241
+ /**
242
+ * AST node representing a value projection.
243
+ *
244
+ * @param left The left-hand side of the value projection
245
+ * @param right The right-hand side of the value projection
246
+ */
247
+ const valueProjection = (left, right) => ({
248
+ type: 'value_projection',
249
+ children: [left, right],
250
+ });
251
+ export { andExpression, comparator, currentNode, expref, field, filterProjection, flatten, functionExpression, identity, index, indexExpression, keyValPair, literal, multiSelectObject, multiSelectList, notExpression, orExpression, pipe, projection, slice, subexpression, valueProjection, };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The binding powers for the various tokens in the JMESPath grammar.
3
+ *
4
+ * The binding powers are used to determine the order of operations for
5
+ * the parser. The higher the binding power, the more tightly the token
6
+ * binds to its arguments.
7
+ */
8
+ declare const BINDING_POWER: {
9
+ readonly eof: 0;
10
+ readonly unquoted_identifier: 0;
11
+ readonly quoted_identifier: 0;
12
+ readonly literal: 0;
13
+ readonly rbracket: 0;
14
+ readonly rparen: 0;
15
+ readonly comma: 0;
16
+ readonly rbrace: 0;
17
+ readonly number: 0;
18
+ readonly current: 0;
19
+ readonly expref: 0;
20
+ readonly colon: 0;
21
+ readonly pipe: 1;
22
+ readonly or: 2;
23
+ readonly and: 3;
24
+ readonly eq: 5;
25
+ readonly gt: 5;
26
+ readonly lt: 5;
27
+ readonly gte: 5;
28
+ readonly lte: 5;
29
+ readonly ne: 5;
30
+ readonly flatten: 9;
31
+ readonly star: 20;
32
+ readonly filter: 21;
33
+ readonly dot: 40;
34
+ readonly not: 45;
35
+ readonly lbrace: 50;
36
+ readonly lbracket: 55;
37
+ readonly lparen: 60;
38
+ };
39
+ /**
40
+ * The set of ASCII letters and digits allowed in JMESPath identifiers.
41
+ */
42
+ declare const START_IDENTIFIER: Set<string>;
43
+ /**
44
+ * The set of ASCII letters and digits allowed in JMESPath identifiers.
45
+ */
46
+ declare const VALID_IDENTIFIER: Set<string>;
47
+ /**
48
+ * The set of ASCII digits allowed in JMESPath identifiers.
49
+ */
50
+ declare const VALID_NUMBER: Set<string>;
51
+ /**
52
+ * The set of ASCII whitespace characters allowed in JMESPath identifiers.
53
+ */
54
+ declare const WHITESPACE: Set<string>;
55
+ /**
56
+ * The set of simple tokens in the JMESPath grammar.
57
+ */
58
+ declare const SIMPLE_TOKENS: Map<string, keyof typeof BINDING_POWER>;
59
+ export { BINDING_POWER, SIMPLE_TOKENS, START_IDENTIFIER, VALID_IDENTIFIER, VALID_NUMBER, WHITESPACE, };
60
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,QAAA,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BT,CAAC;AAkBX;;GAEG;AACH,QAAA,MAAM,gBAAgB,aAA+B,CAAC;AACtD;;GAEG;AACH,QAAA,MAAM,gBAAgB,aAAwC,CAAC;AAC/D;;GAEG;AACH,QAAA,MAAM,YAAY,aAAkB,CAAC;AACrC;;GAEG;AACH,QAAA,MAAM,UAAU,aAAqB,CAAC;AACtC;;GAEG;AACH,QAAA,MAAM,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,OAAO,aAAa,CAYzD,CAAC;AAEH,OAAO,EACL,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,UAAU,GACX,CAAC"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The binding powers for the various tokens in the JMESPath grammar.
3
+ *
4
+ * The binding powers are used to determine the order of operations for
5
+ * the parser. The higher the binding power, the more tightly the token
6
+ * binds to its arguments.
7
+ */
8
+ const BINDING_POWER = {
9
+ eof: 0,
10
+ unquoted_identifier: 0,
11
+ quoted_identifier: 0,
12
+ literal: 0,
13
+ rbracket: 0,
14
+ rparen: 0,
15
+ comma: 0,
16
+ rbrace: 0,
17
+ number: 0,
18
+ current: 0,
19
+ expref: 0,
20
+ colon: 0,
21
+ pipe: 1,
22
+ or: 2,
23
+ and: 3,
24
+ eq: 5,
25
+ gt: 5,
26
+ lt: 5,
27
+ gte: 5,
28
+ lte: 5,
29
+ ne: 5,
30
+ flatten: 9,
31
+ // Everything above stops a projection.
32
+ star: 20,
33
+ filter: 21,
34
+ dot: 40,
35
+ not: 45,
36
+ lbrace: 50,
37
+ lbracket: 55,
38
+ lparen: 60,
39
+ };
40
+ /**
41
+ * The set of ASCII lowercase letters allowed in JMESPath identifiers.
42
+ */
43
+ const ASCII_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
44
+ /**
45
+ * The set of ASCII uppercase letters allowed in JMESPath identifiers.
46
+ */
47
+ const ASCII_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
48
+ /**
49
+ * The set of ASCII letters allowed in JMESPath identifiers.
50
+ */
51
+ const ASCII_LETTERS = ASCII_LOWERCASE + ASCII_UPPERCASE;
52
+ /**
53
+ * The set of ASCII digits allowed in JMESPath identifiers.
54
+ */
55
+ const DIGITS = '0123456789';
56
+ /**
57
+ * The set of ASCII letters and digits allowed in JMESPath identifiers.
58
+ */
59
+ const START_IDENTIFIER = new Set(ASCII_LETTERS + '_');
60
+ /**
61
+ * The set of ASCII letters and digits allowed in JMESPath identifiers.
62
+ */
63
+ const VALID_IDENTIFIER = new Set(ASCII_LETTERS + DIGITS + '_');
64
+ /**
65
+ * The set of ASCII digits allowed in JMESPath identifiers.
66
+ */
67
+ const VALID_NUMBER = new Set(DIGITS);
68
+ /**
69
+ * The set of ASCII whitespace characters allowed in JMESPath identifiers.
70
+ */
71
+ const WHITESPACE = new Set(' \t\n\r');
72
+ /**
73
+ * The set of simple tokens in the JMESPath grammar.
74
+ */
75
+ const SIMPLE_TOKENS = new Map([
76
+ ['.', 'dot'],
77
+ ['*', 'star'],
78
+ [':', 'colon'],
79
+ [']', 'rbracket'],
80
+ [',', 'comma'],
81
+ [':', 'colon'],
82
+ ['@', 'current'],
83
+ ['(', 'lparen'],
84
+ [')', 'rparen'],
85
+ ['{', 'lbrace'],
86
+ ['}', 'rbrace'],
87
+ ]);
88
+ export { BINDING_POWER, SIMPLE_TOKENS, START_IDENTIFIER, VALID_IDENTIFIER, VALID_NUMBER, WHITESPACE, };
@@ -0,0 +1,69 @@
1
+ import type { JMESPathParsingOptions, JSONObject } from './types.js';
2
+ /**
3
+ * Searches and extracts data using JMESPath
4
+ *
5
+ * Envelope being the JMESPath expression to extract the data you're after
6
+ *
7
+ * Built-in JMESPath functions include: `powertools_json`, `powertools_base64`, `powertools_base64_gzip`
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { extractDataFromEnvelope } from '@aws-lambda-powertools/jmespath/envelopes';
12
+ *
13
+ * type CustomEvent = {
14
+ * body: string; // "{\"customerId\":\"dd4649e6-2484-4993-acb8-0f9123103394\"}"
15
+ * };
16
+ *
17
+ * type EventBody = {
18
+ * customerId: string;
19
+ * };
20
+ *
21
+ * export const handler = async (event: CustomEvent): Promise<unknown> => {
22
+ * const payload = extractDataFromEnvelope<EventBody>(event, "powertools_json(body)");
23
+ * const { customerId } = payload; // now deserialized
24
+ * // ...
25
+ * };
26
+ * ```
27
+ *
28
+ * We provide built-in envelopes for popular AWS Lambda event sources to easily decode and/or deserialize JSON objects.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import {
33
+ * extractDataFromEnvelope,
34
+ * SQS,
35
+ * } from '@aws-lambda-powertools/jmespath/envelopes';
36
+ * import type { SQSEvent } from 'aws-lambda';
37
+ *
38
+ * type MessageBody = {
39
+ * customerId: string;
40
+ * };
41
+ *
42
+ * export const handler = async (event: SQSEvent): Promise<unknown> => {
43
+ * const records = extractDataFromEnvelope<Array<MessageBody>>(event, SQS);
44
+ * for (const record in records) { // records is now a list containing the deserialized body of each message
45
+ * const { customerId } = record;
46
+ * }
47
+ * };
48
+ * ```
49
+ *
50
+ * @param data The JSON object to search
51
+ * @param envelope The JMESPath expression to use
52
+ * @param options The parsing options to use
53
+ */
54
+ declare const extractDataFromEnvelope: <T>(data: JSONObject, envelope: string, options?: JMESPathParsingOptions) => T;
55
+ declare const API_GATEWAY_REST = "powertools_json(body)";
56
+ declare const API_GATEWAY_HTTP = "powertools_json(body)";
57
+ declare const SQS = "Records[*].powertools_json(body)";
58
+ declare const SNS = "Records[0].Sns.Message | powertools_json(@)";
59
+ declare const EVENTBRIDGE = "detail";
60
+ declare const CLOUDWATCH_EVENTS_SCHEDULED = "detail";
61
+ declare const KINESIS_DATA_STREAM = "Records[*].kinesis.powertools_json(powertools_base64(data))";
62
+ declare const CLOUDWATCH_LOGS = "awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]";
63
+ declare const S3_SNS_SQS = "Records[*].powertools_json(body).powertools_json(Message).Records[0]";
64
+ declare const S3_SQS = "Records[*].powertools_json(body).Records[0]";
65
+ declare const S3_SNS_KINESIS_FIREHOSE = "records[*].powertools_json(powertools_base64(data)).powertools_json(Message).Records[0]";
66
+ declare const S3_KINESIS_FIREHOSE = "records[*].powertools_json(powertools_base64(data)).Records[0]";
67
+ declare const S3_EVENTBRIDGE_SQS = "Records[*].powertools_json(body).detail";
68
+ export { extractDataFromEnvelope, API_GATEWAY_REST, API_GATEWAY_HTTP, SQS, SNS, EVENTBRIDGE, CLOUDWATCH_EVENTS_SCHEDULED, KINESIS_DATA_STREAM, CLOUDWATCH_LOGS, S3_SNS_SQS, S3_SQS, S3_SNS_KINESIS_FIREHOSE, S3_KINESIS_FIREHOSE, S3_EVENTBRIDGE_SQS, };
69
+ //# sourceMappingURL=envelopes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelopes.d.ts","sourceRoot":"","sources":["../../src/envelopes.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,QAAA,MAAM,uBAAuB,YACrB,UAAU,YACN,MAAM,YACN,sBAAsB,KAC/B,CAMF,CAAC;AAEF,QAAA,MAAM,gBAAgB,0BAA0B,CAAC;AACjD,QAAA,MAAM,gBAAgB,0BAA0B,CAAC;AACjD,QAAA,MAAM,GAAG,qCAAqC,CAAC;AAC/C,QAAA,MAAM,GAAG,gDAAgD,CAAC;AAC1D,QAAA,MAAM,WAAW,WAAW,CAAC;AAC7B,QAAA,MAAM,2BAA2B,WAAW,CAAC;AAC7C,QAAA,MAAM,mBAAmB,gEACsC,CAAC;AAChE,QAAA,MAAM,eAAe,2EACqD,CAAC;AAC3E,QAAA,MAAM,UAAU,yEACwD,CAAC;AACzE,QAAA,MAAM,MAAM,gDAAgD,CAAC;AAC7D,QAAA,MAAM,uBAAuB,4FAC8D,CAAC;AAC5F,QAAA,MAAM,mBAAmB,mEACyC,CAAC;AACnE,QAAA,MAAM,kBAAkB,4CAA4C,CAAC;AAErE,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,gBAAgB,EAChB,GAAG,EACH,GAAG,EACH,WAAW,EACX,2BAA2B,EAC3B,mBAAmB,EACnB,eAAe,EACf,UAAU,EACV,MAAM,EACN,uBAAuB,EACvB,mBAAmB,EACnB,kBAAkB,GACnB,CAAC"}
@@ -0,0 +1,74 @@
1
+ import { search } from './search.js';
2
+ import { PowertoolsFunctions } from './PowertoolsFunctions.js';
3
+ /**
4
+ * Searches and extracts data using JMESPath
5
+ *
6
+ * Envelope being the JMESPath expression to extract the data you're after
7
+ *
8
+ * Built-in JMESPath functions include: `powertools_json`, `powertools_base64`, `powertools_base64_gzip`
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { extractDataFromEnvelope } from '@aws-lambda-powertools/jmespath/envelopes';
13
+ *
14
+ * type CustomEvent = {
15
+ * body: string; // "{\"customerId\":\"dd4649e6-2484-4993-acb8-0f9123103394\"}"
16
+ * };
17
+ *
18
+ * type EventBody = {
19
+ * customerId: string;
20
+ * };
21
+ *
22
+ * export const handler = async (event: CustomEvent): Promise<unknown> => {
23
+ * const payload = extractDataFromEnvelope<EventBody>(event, "powertools_json(body)");
24
+ * const { customerId } = payload; // now deserialized
25
+ * // ...
26
+ * };
27
+ * ```
28
+ *
29
+ * We provide built-in envelopes for popular AWS Lambda event sources to easily decode and/or deserialize JSON objects.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import {
34
+ * extractDataFromEnvelope,
35
+ * SQS,
36
+ * } from '@aws-lambda-powertools/jmespath/envelopes';
37
+ * import type { SQSEvent } from 'aws-lambda';
38
+ *
39
+ * type MessageBody = {
40
+ * customerId: string;
41
+ * };
42
+ *
43
+ * export const handler = async (event: SQSEvent): Promise<unknown> => {
44
+ * const records = extractDataFromEnvelope<Array<MessageBody>>(event, SQS);
45
+ * for (const record in records) { // records is now a list containing the deserialized body of each message
46
+ * const { customerId } = record;
47
+ * }
48
+ * };
49
+ * ```
50
+ *
51
+ * @param data The JSON object to search
52
+ * @param envelope The JMESPath expression to use
53
+ * @param options The parsing options to use
54
+ */
55
+ const extractDataFromEnvelope = (data, envelope, options) => {
56
+ if (!options) {
57
+ options = { customFunctions: new PowertoolsFunctions() };
58
+ }
59
+ return search(envelope, data, options);
60
+ };
61
+ const API_GATEWAY_REST = 'powertools_json(body)';
62
+ const API_GATEWAY_HTTP = 'powertools_json(body)';
63
+ const SQS = 'Records[*].powertools_json(body)';
64
+ const SNS = 'Records[0].Sns.Message | powertools_json(@)';
65
+ const EVENTBRIDGE = 'detail';
66
+ const CLOUDWATCH_EVENTS_SCHEDULED = 'detail';
67
+ const KINESIS_DATA_STREAM = 'Records[*].kinesis.powertools_json(powertools_base64(data))';
68
+ const CLOUDWATCH_LOGS = 'awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]';
69
+ const S3_SNS_SQS = 'Records[*].powertools_json(body).powertools_json(Message).Records[0]';
70
+ const S3_SQS = 'Records[*].powertools_json(body).Records[0]';
71
+ const S3_SNS_KINESIS_FIREHOSE = 'records[*].powertools_json(powertools_base64(data)).powertools_json(Message).Records[0]';
72
+ const S3_KINESIS_FIREHOSE = 'records[*].powertools_json(powertools_base64(data)).Records[0]';
73
+ const S3_EVENTBRIDGE_SQS = 'Records[*].powertools_json(body).detail';
74
+ export { extractDataFromEnvelope, API_GATEWAY_REST, API_GATEWAY_HTTP, SQS, SNS, EVENTBRIDGE, CLOUDWATCH_EVENTS_SCHEDULED, KINESIS_DATA_STREAM, CLOUDWATCH_LOGS, S3_SNS_SQS, S3_SQS, S3_SNS_KINESIS_FIREHOSE, S3_KINESIS_FIREHOSE, S3_EVENTBRIDGE_SQS, };