@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
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { Token } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for errors thrown during expression parsing and evaluation.
|
|
4
|
+
*/
|
|
5
|
+
declare class JMESPathError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* Expression that was being parsed when the error occurred.
|
|
8
|
+
* Can be set by whatever catches the error.
|
|
9
|
+
*/
|
|
10
|
+
expression?: string;
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
/**
|
|
13
|
+
* Set the expression that was being parsed when the error occurred.
|
|
14
|
+
*
|
|
15
|
+
* The separate method allows the expression to be set after the error is
|
|
16
|
+
* thrown. In some instances the expression is not known until after the
|
|
17
|
+
* error is thrown (i.e. the error is thrown down the call stack).
|
|
18
|
+
*
|
|
19
|
+
* @param expression The expression that was being parsed when the error occurred.
|
|
20
|
+
*/
|
|
21
|
+
setExpression(expression: string): void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error thrown when an unknown token is encountered during the AST construction.
|
|
25
|
+
*/
|
|
26
|
+
declare class LexerError extends JMESPathError {
|
|
27
|
+
/**
|
|
28
|
+
* Position in the expression where the error occurred.
|
|
29
|
+
*/
|
|
30
|
+
lexerPosition: number;
|
|
31
|
+
/**
|
|
32
|
+
* Token value where the error occurred.
|
|
33
|
+
*/
|
|
34
|
+
lexerValue: string;
|
|
35
|
+
constructor(lexerPosition: number, lexerValue: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Error thrown when an invalid or unexpected token type or value is encountered during parsing.
|
|
39
|
+
*/
|
|
40
|
+
declare class ParseError extends JMESPathError {
|
|
41
|
+
/**
|
|
42
|
+
* Position in the expression where the error occurred.
|
|
43
|
+
*/
|
|
44
|
+
lexPosition: number;
|
|
45
|
+
/**
|
|
46
|
+
* Additional information about the error.
|
|
47
|
+
*/
|
|
48
|
+
reason?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Token type where the error occurred.
|
|
51
|
+
*/
|
|
52
|
+
tokenType: Token['type'];
|
|
53
|
+
/**
|
|
54
|
+
* Token value where the error occurred.
|
|
55
|
+
*/
|
|
56
|
+
tokenValue: Token['value'];
|
|
57
|
+
constructor(options: {
|
|
58
|
+
lexPosition: number;
|
|
59
|
+
tokenValue: Token['value'];
|
|
60
|
+
tokenType: Token['type'];
|
|
61
|
+
reason?: string;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Error thrown when an incomplete expression is encountered during parsing.
|
|
66
|
+
*/
|
|
67
|
+
declare class IncompleteExpressionError extends ParseError {
|
|
68
|
+
/**
|
|
69
|
+
* Expression that was being parsed when the error occurred.
|
|
70
|
+
*
|
|
71
|
+
* Can be set by whatever catches the error.
|
|
72
|
+
*/
|
|
73
|
+
expression?: string;
|
|
74
|
+
constructor(options: {
|
|
75
|
+
lexPosition: number;
|
|
76
|
+
tokenValue: Token['value'];
|
|
77
|
+
tokenType: Token['type'];
|
|
78
|
+
reason?: string;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Error thrown when an empty expression is encountered during parsing.
|
|
83
|
+
*/
|
|
84
|
+
declare class EmptyExpressionError extends JMESPathError {
|
|
85
|
+
constructor();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Base class for errors thrown during function execution.
|
|
89
|
+
*
|
|
90
|
+
* When writing a JMESPath expression, you can use functions to transform the
|
|
91
|
+
* data. For example, the `abs()` function returns the absolute value of a number.
|
|
92
|
+
*
|
|
93
|
+
* If an error occurs during function execution, the error is thrown as a
|
|
94
|
+
* subclass of `FunctionError`. The subclass is determined by the type of error
|
|
95
|
+
* that occurred.
|
|
96
|
+
*
|
|
97
|
+
* Errors can be thrown while validating the arguments passed to a function, or
|
|
98
|
+
* while executing the function itself.
|
|
99
|
+
*/
|
|
100
|
+
declare class FunctionError extends JMESPathError {
|
|
101
|
+
/**
|
|
102
|
+
* Function that was being executed when the error occurred.
|
|
103
|
+
* Can be set by whatever catches the error.
|
|
104
|
+
*/
|
|
105
|
+
functionName?: string;
|
|
106
|
+
constructor(message: string);
|
|
107
|
+
/**
|
|
108
|
+
* Set the function that was being validated or executed when the error occurred.
|
|
109
|
+
*
|
|
110
|
+
* The separate method allows the name to be set after the error is
|
|
111
|
+
* thrown. In most cases the error is thrown down the call stack, but we want
|
|
112
|
+
* to show the actual function name used in the expression rather than an internal
|
|
113
|
+
* alias. To avoid passing the function name down the call stack, we set it
|
|
114
|
+
* after the error is thrown.
|
|
115
|
+
*
|
|
116
|
+
* @param functionName The function that was being validated or executed when the error occurred.
|
|
117
|
+
*/
|
|
118
|
+
setEvaluatedFunctionName(functionName: string): void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Error thrown when an unexpected argument is passed to a function.
|
|
122
|
+
*
|
|
123
|
+
* Function arguments are validated before the function is executed. If an
|
|
124
|
+
* invalid argument is passed, the error is thrown. For example, the `abs()`
|
|
125
|
+
* function expects exactly one argument. If more than one argument is passed,
|
|
126
|
+
* an `ArityError` is thrown.
|
|
127
|
+
*/
|
|
128
|
+
declare class ArityError extends FunctionError {
|
|
129
|
+
actualArity: number;
|
|
130
|
+
expectedArity: number;
|
|
131
|
+
constructor(options: {
|
|
132
|
+
expectedArity: number;
|
|
133
|
+
actualArity: number;
|
|
134
|
+
});
|
|
135
|
+
protected pluralize(word: string, count: number): string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Error thrown when an unexpected number of arguments is passed to a variadic function.
|
|
139
|
+
*
|
|
140
|
+
* Variadic functions are functions that accept a variable number of arguments.
|
|
141
|
+
* For example, the `max()` function accepts any number of arguments and returns
|
|
142
|
+
* the largest one. If no arguments are passed, it returns `null`.
|
|
143
|
+
*
|
|
144
|
+
* If the number of arguments passed to a variadic function is not within the
|
|
145
|
+
* expected range, this error is thrown.
|
|
146
|
+
*/
|
|
147
|
+
declare class VariadicArityError extends ArityError {
|
|
148
|
+
constructor(options: {
|
|
149
|
+
expectedArity: number;
|
|
150
|
+
actualArity: number;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Error thrown when an invalid argument type is passed to a built-in function.
|
|
155
|
+
*
|
|
156
|
+
* Function arguments are validated before the function is executed. If an
|
|
157
|
+
* invalid argument type is found, this error is thrown. For example, the
|
|
158
|
+
* `abs()` function expects a number as its argument. If a string is passed
|
|
159
|
+
* instead, this error is thrown.
|
|
160
|
+
*/
|
|
161
|
+
declare class JMESPathTypeError extends FunctionError {
|
|
162
|
+
actualType: string;
|
|
163
|
+
currentValue: unknown;
|
|
164
|
+
expectedTypes: string[];
|
|
165
|
+
constructor(options: {
|
|
166
|
+
currentValue: unknown;
|
|
167
|
+
actualType: string;
|
|
168
|
+
expectedTypes: string[];
|
|
169
|
+
});
|
|
170
|
+
protected serializeExpectedTypes(): string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Error thrown when an unknown function is used in an expression.
|
|
174
|
+
*
|
|
175
|
+
* When evaluating a JMESPath expression, the interpreter looks up the function
|
|
176
|
+
* name in a table of built-in functions, as well as any custom functions
|
|
177
|
+
* provided by the user. If the function name is not found, this error is thrown.
|
|
178
|
+
*/
|
|
179
|
+
declare class UnknownFunctionError extends FunctionError {
|
|
180
|
+
constructor(funcName: string);
|
|
181
|
+
}
|
|
182
|
+
export { ArityError, EmptyExpressionError, IncompleteExpressionError, JMESPathError, JMESPathTypeError, LexerError, ParseError, UnknownFunctionError, VariadicArityError, };
|
|
183
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;GAEG;AACH,cAAM,aAAc,SAAQ,KAAK;IAC/B;;;OAGG;IACI,UAAU,CAAC,EAAE,MAAM,CAAC;gBAER,OAAO,EAAE,MAAM;IAMlC;;;;;;;;OAQG;IACI,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;CAM/C;AAED;;GAEG;AACH,cAAM,UAAW,SAAQ,aAAa;IACpC;;OAEG;IACI,aAAa,EAAE,MAAM,CAAC;IAC7B;;OAEG;IACI,UAAU,EAAE,MAAM,CAAC;gBAEP,aAAa,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAS7D;AAED;;GAEG;AACH,cAAM,UAAW,SAAQ,aAAa;IACpC;;OAEG;IACI,WAAW,EAAE,MAAM,CAAC;IAC3B;;OAEG;IACI,MAAM,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACI,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAChC;;OAEG;IACI,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;gBAEf,OAAO,EAAE;QAC1B,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;QAC3B,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;QACzB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;CAmBF;AAED;;GAEG;AACH,cAAM,yBAA0B,SAAQ,UAAU;IAChD;;;;OAIG;IACI,UAAU,CAAC,EAAE,MAAM,CAAC;gBAER,OAAO,EAAE;QAC1B,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU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AAED,OAAO,EACL,UAAU,EACV,oBAAoB,EACpB,yBAAyB,EACzB,aAAa,EACb,iBAAiB,EACjB,UAAU,EACV,UAAU,EACV,oBAAoB,EACpB,kBAAkB,GACnB,CAAC"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for errors thrown during expression parsing and evaluation.
|
|
3
|
+
*/
|
|
4
|
+
class JMESPathError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'JMESPathError';
|
|
8
|
+
this.message = message;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Set the expression that was being parsed when the error occurred.
|
|
12
|
+
*
|
|
13
|
+
* The separate method allows the expression to be set after the error is
|
|
14
|
+
* thrown. In some instances the expression is not known until after the
|
|
15
|
+
* error is thrown (i.e. the error is thrown down the call stack).
|
|
16
|
+
*
|
|
17
|
+
* @param expression The expression that was being parsed when the error occurred.
|
|
18
|
+
*/
|
|
19
|
+
setExpression(expression) {
|
|
20
|
+
this.expression = expression;
|
|
21
|
+
// Set the message to include the expression.
|
|
22
|
+
this.message = `${this.message} in expression: ${this.expression}`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Error thrown when an unknown token is encountered during the AST construction.
|
|
27
|
+
*/
|
|
28
|
+
class LexerError extends JMESPathError {
|
|
29
|
+
constructor(lexerPosition, lexerValue) {
|
|
30
|
+
super('Bad jmespath expression');
|
|
31
|
+
this.name = 'LexerError';
|
|
32
|
+
this.lexerPosition = lexerPosition;
|
|
33
|
+
this.lexerValue = lexerValue;
|
|
34
|
+
// Set the message to include the lexer position and value.
|
|
35
|
+
this.message = `${this.message}: unknown token "${this.lexerValue}" at column ${this.lexerPosition}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Error thrown when an invalid or unexpected token type or value is encountered during parsing.
|
|
40
|
+
*/
|
|
41
|
+
class ParseError extends JMESPathError {
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super('Invalid jmespath expression');
|
|
44
|
+
this.name = 'ParseError';
|
|
45
|
+
this.lexPosition = options.lexPosition;
|
|
46
|
+
this.tokenValue = options.tokenValue;
|
|
47
|
+
this.tokenType = options.tokenType;
|
|
48
|
+
this.reason = options.reason;
|
|
49
|
+
// Set the message to include the lexer position and token info.
|
|
50
|
+
let issue;
|
|
51
|
+
if (this.reason) {
|
|
52
|
+
issue = this.reason;
|
|
53
|
+
}
|
|
54
|
+
else if (this.tokenType === 'eof') {
|
|
55
|
+
issue = 'found unexpected end of expression (EOF)';
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
issue = `found unexpected token "${this.tokenValue}" (${this.tokenType})`;
|
|
59
|
+
}
|
|
60
|
+
this.message = `${this.message}: parse error at column ${this.lexPosition}, ${issue}`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Error thrown when an incomplete expression is encountered during parsing.
|
|
65
|
+
*/
|
|
66
|
+
class IncompleteExpressionError extends ParseError {
|
|
67
|
+
constructor(options) {
|
|
68
|
+
super(options);
|
|
69
|
+
this.name = 'IncompleteExpressionError';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Error thrown when an empty expression is encountered during parsing.
|
|
74
|
+
*/
|
|
75
|
+
class EmptyExpressionError extends JMESPathError {
|
|
76
|
+
constructor() {
|
|
77
|
+
super('Invalid JMESPath expression: cannot be empty.');
|
|
78
|
+
this.name = 'EmptyExpressionError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Base class for errors thrown during function execution.
|
|
83
|
+
*
|
|
84
|
+
* When writing a JMESPath expression, you can use functions to transform the
|
|
85
|
+
* data. For example, the `abs()` function returns the absolute value of a number.
|
|
86
|
+
*
|
|
87
|
+
* If an error occurs during function execution, the error is thrown as a
|
|
88
|
+
* subclass of `FunctionError`. The subclass is determined by the type of error
|
|
89
|
+
* that occurred.
|
|
90
|
+
*
|
|
91
|
+
* Errors can be thrown while validating the arguments passed to a function, or
|
|
92
|
+
* while executing the function itself.
|
|
93
|
+
*/
|
|
94
|
+
class FunctionError extends JMESPathError {
|
|
95
|
+
constructor(message) {
|
|
96
|
+
super(message);
|
|
97
|
+
this.name = 'FunctionError';
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Set the function that was being validated or executed when the error occurred.
|
|
101
|
+
*
|
|
102
|
+
* The separate method allows the name to be set after the error is
|
|
103
|
+
* thrown. In most cases the error is thrown down the call stack, but we want
|
|
104
|
+
* to show the actual function name used in the expression rather than an internal
|
|
105
|
+
* alias. To avoid passing the function name down the call stack, we set it
|
|
106
|
+
* after the error is thrown.
|
|
107
|
+
*
|
|
108
|
+
* @param functionName The function that was being validated or executed when the error occurred.
|
|
109
|
+
*/
|
|
110
|
+
setEvaluatedFunctionName(functionName) {
|
|
111
|
+
this.message = this.message.replace('for function undefined', `for function ${functionName}()`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Error thrown when an unexpected argument is passed to a function.
|
|
116
|
+
*
|
|
117
|
+
* Function arguments are validated before the function is executed. If an
|
|
118
|
+
* invalid argument is passed, the error is thrown. For example, the `abs()`
|
|
119
|
+
* function expects exactly one argument. If more than one argument is passed,
|
|
120
|
+
* an `ArityError` is thrown.
|
|
121
|
+
*/
|
|
122
|
+
class ArityError extends FunctionError {
|
|
123
|
+
constructor(options) {
|
|
124
|
+
super('Invalid arity for JMESPath function');
|
|
125
|
+
this.name = 'ArityError';
|
|
126
|
+
this.actualArity = options.actualArity;
|
|
127
|
+
this.expectedArity = options.expectedArity;
|
|
128
|
+
const arityParticle = this.actualArity > this.expectedArity ? 'at most' : 'at least';
|
|
129
|
+
// Set the message to include the error info.
|
|
130
|
+
this.message = `Expected ${arityParticle} ${this.expectedArity} ${this.pluralize('argument', this.expectedArity)} for function ${this.functionName}, received ${this.actualArity}`;
|
|
131
|
+
}
|
|
132
|
+
pluralize(word, count) {
|
|
133
|
+
return count === 1 ? word : `${word}s`;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Error thrown when an unexpected number of arguments is passed to a variadic function.
|
|
138
|
+
*
|
|
139
|
+
* Variadic functions are functions that accept a variable number of arguments.
|
|
140
|
+
* For example, the `max()` function accepts any number of arguments and returns
|
|
141
|
+
* the largest one. If no arguments are passed, it returns `null`.
|
|
142
|
+
*
|
|
143
|
+
* If the number of arguments passed to a variadic function is not within the
|
|
144
|
+
* expected range, this error is thrown.
|
|
145
|
+
*/
|
|
146
|
+
class VariadicArityError extends ArityError {
|
|
147
|
+
constructor(options) {
|
|
148
|
+
super(options);
|
|
149
|
+
this.name = 'VariadicArityError';
|
|
150
|
+
// Set the message to include the error info.
|
|
151
|
+
this.message = `Expected ${this.expectedArity} ${this.pluralize('argument', this.expectedArity)} for function ${this.functionName}, received ${this.actualArity}`;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Error thrown when an invalid argument type is passed to a built-in function.
|
|
156
|
+
*
|
|
157
|
+
* Function arguments are validated before the function is executed. If an
|
|
158
|
+
* invalid argument type is found, this error is thrown. For example, the
|
|
159
|
+
* `abs()` function expects a number as its argument. If a string is passed
|
|
160
|
+
* instead, this error is thrown.
|
|
161
|
+
*/
|
|
162
|
+
class JMESPathTypeError extends FunctionError {
|
|
163
|
+
constructor(options) {
|
|
164
|
+
super('Invalid type for JMESPath expression');
|
|
165
|
+
this.name = 'JMESPathTypeError';
|
|
166
|
+
this.currentValue = options.currentValue;
|
|
167
|
+
this.actualType = options.actualType;
|
|
168
|
+
this.expectedTypes = options.expectedTypes;
|
|
169
|
+
// Set the message to include the error info.
|
|
170
|
+
this.message = `Invalid argument type for function ${this.functionName}, expected ${this.serializeExpectedTypes()} but found "${this.actualType}"`;
|
|
171
|
+
}
|
|
172
|
+
serializeExpectedTypes() {
|
|
173
|
+
const types = [];
|
|
174
|
+
for (const type of this.expectedTypes) {
|
|
175
|
+
types.push(`"${type}"`);
|
|
176
|
+
}
|
|
177
|
+
return types.length === 1 ? types[0] : `one of ${types.join(', ')}`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Error thrown when an unknown function is used in an expression.
|
|
182
|
+
*
|
|
183
|
+
* When evaluating a JMESPath expression, the interpreter looks up the function
|
|
184
|
+
* name in a table of built-in functions, as well as any custom functions
|
|
185
|
+
* provided by the user. If the function name is not found, this error is thrown.
|
|
186
|
+
*/
|
|
187
|
+
class UnknownFunctionError extends FunctionError {
|
|
188
|
+
constructor(funcName) {
|
|
189
|
+
super('Unknown function');
|
|
190
|
+
this.name = 'UnknownFunctionError';
|
|
191
|
+
// Set the message to include the error info.
|
|
192
|
+
this.message = `Unknown function: ${funcName}()`;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
export { ArityError, EmptyExpressionError, IncompleteExpressionError, JMESPathError, JMESPathTypeError, LexerError, ParseError, UnknownFunctionError, VariadicArityError, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EACL,aAAa,EACb,UAAU,EACV,UAAU,EACV,yBAAyB,EACzB,UAAU,EACV,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,aAAa,CAAC"}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { JMESPathParsingOptions, JSONObject } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Search for data in a JSON object using a JMESPath expression.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
8
|
+
*
|
|
9
|
+
* const data = {
|
|
10
|
+
* foo: {
|
|
11
|
+
* bar: {
|
|
12
|
+
* baz: 1
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const result = search('foo.bar.baz', data);
|
|
18
|
+
* console.log(result); // 1
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* By default the search function will use all the built-in functions
|
|
22
|
+
* present in the [JMESPath specification](https://jmespath.org/specification.html).
|
|
23
|
+
*
|
|
24
|
+
* Powertools for AWS Lambda provides some additional functions that can be used
|
|
25
|
+
* by passing them in the `customFunctions` option.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
30
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
31
|
+
*
|
|
32
|
+
* const data = {
|
|
33
|
+
* body: "{\"foo\": \"bar\"}"
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* const result = search(
|
|
37
|
+
* 'powertools_json(body)',
|
|
38
|
+
* data,
|
|
39
|
+
* { customFunctions: new PowertoolsFunctions() }
|
|
40
|
+
* );
|
|
41
|
+
* console.log(result); // { foo: 'bar' }
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param expression The JMESPath expression to use
|
|
45
|
+
* @param data The JSON object to search
|
|
46
|
+
* @param options The parsing options to use
|
|
47
|
+
*/
|
|
48
|
+
declare const search: (expression: string, data: JSONObject, options?: JMESPathParsingOptions) => unknown;
|
|
49
|
+
export { search };
|
|
50
|
+
//# sourceMappingURL=search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/search.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIrE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,QAAA,MAAM,MAAM,eACE,MAAM,QACZ,UAAU,YACN,sBAAsB,KAC/B,OAEF,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Parser } from './Parser.js';
|
|
2
|
+
const parser = new Parser();
|
|
3
|
+
/**
|
|
4
|
+
* Search for data in a JSON object using a JMESPath expression.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
9
|
+
*
|
|
10
|
+
* const data = {
|
|
11
|
+
* foo: {
|
|
12
|
+
* bar: {
|
|
13
|
+
* baz: 1
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* };
|
|
17
|
+
*
|
|
18
|
+
* const result = search('foo.bar.baz', data);
|
|
19
|
+
* console.log(result); // 1
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* By default the search function will use all the built-in functions
|
|
23
|
+
* present in the [JMESPath specification](https://jmespath.org/specification.html).
|
|
24
|
+
*
|
|
25
|
+
* Powertools for AWS Lambda provides some additional functions that can be used
|
|
26
|
+
* by passing them in the `customFunctions` option.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
31
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
32
|
+
*
|
|
33
|
+
* const data = {
|
|
34
|
+
* body: "{\"foo\": \"bar\"}"
|
|
35
|
+
* };
|
|
36
|
+
*
|
|
37
|
+
* const result = search(
|
|
38
|
+
* 'powertools_json(body)',
|
|
39
|
+
* data,
|
|
40
|
+
* { customFunctions: new PowertoolsFunctions() }
|
|
41
|
+
* );
|
|
42
|
+
* console.log(result); // { foo: 'bar' }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param expression The JMESPath expression to use
|
|
46
|
+
* @param data The JSON object to search
|
|
47
|
+
* @param options The parsing options to use
|
|
48
|
+
*/
|
|
49
|
+
const search = (expression, data, options) => {
|
|
50
|
+
return parser.parse(expression).search(data, options);
|
|
51
|
+
};
|
|
52
|
+
export { search };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { JSONValue, JSONArray } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { Functions } from './Functions.js';
|
|
3
|
+
import { BINDING_POWER } from './constants.js';
|
|
4
|
+
/**
|
|
5
|
+
* A token in the JMESPath AST.
|
|
6
|
+
*/
|
|
7
|
+
type Token = {
|
|
8
|
+
type: keyof typeof BINDING_POWER;
|
|
9
|
+
value: JSONValue;
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A node in the JMESPath AST.
|
|
15
|
+
*/
|
|
16
|
+
type Node = {
|
|
17
|
+
type: string;
|
|
18
|
+
children: Node[];
|
|
19
|
+
value?: JSONValue;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Options for parsing.
|
|
23
|
+
*
|
|
24
|
+
* You can use this type to customize the parsing of JMESPath expressions.
|
|
25
|
+
*
|
|
26
|
+
* For example, you can use this type to provide custom functions to the parser.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { search } from '@aws-lambda-powertools/jmespath';
|
|
31
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
32
|
+
*
|
|
33
|
+
* const expression = 'powertools_json(@)';
|
|
34
|
+
*
|
|
35
|
+
* const result = search(expression, "{\n \"a\": 1\n}", {
|
|
36
|
+
* customFunctions: new PowertoolsFunctions(),
|
|
37
|
+
* });
|
|
38
|
+
* console.log(result); // { a: 1 }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
type JMESPathParsingOptions = {
|
|
42
|
+
/**
|
|
43
|
+
* The custom functions to use.
|
|
44
|
+
*
|
|
45
|
+
* By default, the interpreter uses the standard JMESPath functions
|
|
46
|
+
* available in the [JMESPath specification](https://jmespath.org/specification.html).
|
|
47
|
+
*/
|
|
48
|
+
customFunctions?: Functions;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Decorator for function signatures.
|
|
52
|
+
*/
|
|
53
|
+
type FunctionSignatureDecorator = (target: Functions | typeof Functions, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Options for a function signature.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
|
|
60
|
+
*
|
|
61
|
+
* class MyFunctions extends Functions {
|
|
62
|
+
* @Functions.signature({
|
|
63
|
+
* argumentsSpecs: [['number'], ['number']],
|
|
64
|
+
* variadic: true,
|
|
65
|
+
* })
|
|
66
|
+
* public funcMyMethod(args: Array<number>): unknown {
|
|
67
|
+
* // ...
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @param argumentsSpecs The expected arguments for the function.
|
|
73
|
+
* @param variadic Whether the function is variadic.
|
|
74
|
+
*/
|
|
75
|
+
type FunctionSignatureOptions = {
|
|
76
|
+
argumentsSpecs: Array<Array<string>>;
|
|
77
|
+
variadic?: boolean;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* A JSON parseable object.
|
|
81
|
+
*/
|
|
82
|
+
type JSONObject = JSONArray | JSONValue | object;
|
|
83
|
+
export type { FunctionSignatureDecorator, FunctionSignatureOptions, Node, JMESPathParsingOptions, Token, JSONObject, };
|
|
84
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|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