@usearazzo/parser 1.0.1-alpha.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.
@@ -0,0 +1,73 @@
1
+ import { parse as parseRuntimeExpression, ArazzoRuntimeExpressionParseError } from '@swaggerexpert/arazzo-runtime-expression';
2
+
3
+ /**
4
+ * Options for parsing Arazzo Runtime Expressions.
5
+ * @public
6
+ */
7
+
8
+ /**
9
+ * Result of parsing an Arazzo Runtime Expression: the foundation ParseResult
10
+ * bound to the AST node type this parser produces.
11
+ * @public
12
+ */
13
+
14
+ /**
15
+ * Default options for parsing Arazzo Runtime Expressions. Empty on purpose:
16
+ * the underlying grammar package supplies its own defaults, including a fresh
17
+ * ASTTranslator per call.
18
+ * @public
19
+ */
20
+ export const defaultOptions = {};
21
+
22
+ /**
23
+ * Parses an Arazzo Runtime Expression (e.g. `$inputs.username`,
24
+ * `$steps.myStep.outputs.result`, `$response.body#/pets/0/id`) into its AST.
25
+ *
26
+ * A pure, context-free syntax parser: the expression string in, an AST plus
27
+ * position-carrying diagnostics (`result.matched` / `result.maxMatched`) out.
28
+ * No evaluation is performed and no document is required; evaluating the
29
+ * parsed expression against runtime state is the runner's responsibility.
30
+ *
31
+ * The expression must be the bare form without surrounding braces. Splitting a
32
+ * `{expression}` template into literal and expression spans is left to
33
+ * `@swaggerexpert/arazzo-runtime-expression`'s own `extract`/`interpolate`
34
+ * functions, which remain a foundation dependency rather than being wrapped
35
+ * here.
36
+ *
37
+ * @param expression - The runtime expression source string
38
+ * @param options - Parse options (uses defaultOptions when not provided)
39
+ * @returns The parse result: `{ result, tree, stats, trace }`. On success,
40
+ * `result.success` is `true` and `tree` holds the AST. On failure,
41
+ * `result.success` is `false`, `tree` is `undefined`, and `result.maxMatched`
42
+ * gives the offset into `expression` up to which parsing succeeded.
43
+ * @throws TypeError - When `expression` is not a string.
44
+ * @throws ArazzoRuntimeExpressionParseError - When parsing fails for a reason
45
+ * other than invalid syntax (an unexpected internal error). Invalid syntax is
46
+ * never thrown; it is reported through the returned `result`.
47
+ *
48
+ * @example
49
+ * Valid expression
50
+ * ```typescript
51
+ * import { parseRuntimeExpression } from '@usearazzo/parser';
52
+ *
53
+ * const { result, tree } = parseRuntimeExpression('$steps.myStep.outputs.result');
54
+ * // result.success === true
55
+ * // tree === { type: 'StepsExpression', stepId: 'myStep', field: 'outputs', outputName: 'result' }
56
+ * ```
57
+ *
58
+ * @example
59
+ * Invalid expression
60
+ * ```typescript
61
+ * import { parseRuntimeExpression } from '@usearazzo/parser';
62
+ *
63
+ * const { result, tree } = parseRuntimeExpression('$unknown.thing');
64
+ * // result.success === false
65
+ * // tree === undefined
66
+ * // result.maxMatched === 0 - the offset parsing failed at
67
+ * ```
68
+ * @public
69
+ */
70
+ export function parse(expression, options = defaultOptions) {
71
+ return parseRuntimeExpression(expression, options);
72
+ }
73
+ export { ArazzoRuntimeExpressionParseError };
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _empty = require("@speclynx/apidom-reference/configuration/empty");
8
+ /**
9
+ * @public
10
+ */
11
+
12
+ /**
13
+ * Serves an in-memory document under exactly one URI.
14
+ * @public
15
+ */
16
+ class MemoryResolver extends _empty.Resolver {
17
+ document;
18
+ uri;
19
+ constructor({
20
+ document,
21
+ uri
22
+ }) {
23
+ super({
24
+ name: 'memory'
25
+ });
26
+ this.document = document;
27
+ // must match the File.uri derivation in apidom-reference's parse()
28
+ this.uri = _empty.url.sanitize(_empty.url.stripHash(uri));
29
+ }
30
+ canRead(file) {
31
+ if (file.uri === this.uri) return true;
32
+ // a URI under this resolver's synthetic memory:// base can only be a relative URL
33
+ // resolved against it; apidom records the message of an error thrown here verbatim
34
+ // in the source description annotation, whereas an error thrown from read() would
35
+ // be wrapped and its message lost
36
+ if (this.uri.startsWith('memory://') && file.uri.startsWith(`${this.uri}/`)) {
37
+ throw new _empty.ResolverError('relative URL cannot be resolved because the parent document was parsed from ' + 'inline content. Provide resolve.baseURI or an absolute $self.');
38
+ }
39
+ return false;
40
+ }
41
+ async read(file) {
42
+ try {
43
+ const encoder = new TextEncoder();
44
+ return encoder.encode(this.document);
45
+ } catch (error) {
46
+ throw new _empty.ResolverError(`Error opening file "${file.uri}"`, {
47
+ cause: error
48
+ });
49
+ }
50
+ }
51
+ }
52
+ var _default = exports.default = MemoryResolver;
@@ -0,0 +1,47 @@
1
+ import { url, Resolver, ResolverError } from '@speclynx/apidom-reference/configuration/empty';
2
+
3
+ /**
4
+ * @public
5
+ */
6
+
7
+ /**
8
+ * Serves an in-memory document under exactly one URI.
9
+ * @public
10
+ */
11
+ class MemoryResolver extends Resolver {
12
+ document;
13
+ uri;
14
+ constructor({
15
+ document,
16
+ uri
17
+ }) {
18
+ super({
19
+ name: 'memory'
20
+ });
21
+ this.document = document;
22
+ // must match the File.uri derivation in apidom-reference's parse()
23
+ this.uri = url.sanitize(url.stripHash(uri));
24
+ }
25
+ canRead(file) {
26
+ if (file.uri === this.uri) return true;
27
+ // a URI under this resolver's synthetic memory:// base can only be a relative URL
28
+ // resolved against it; apidom records the message of an error thrown here verbatim
29
+ // in the source description annotation, whereas an error thrown from read() would
30
+ // be wrapped and its message lost
31
+ if (this.uri.startsWith('memory://') && file.uri.startsWith(`${this.uri}/`)) {
32
+ throw new ResolverError('relative URL cannot be resolved because the parent document was parsed from ' + 'inline content. Provide resolve.baseURI or an absolute $self.');
33
+ }
34
+ return false;
35
+ }
36
+ async read(file) {
37
+ try {
38
+ const encoder = new TextEncoder();
39
+ return encoder.encode(this.document);
40
+ } catch (error) {
41
+ throw new ResolverError(`Error opening file "${file.uri}"`, {
42
+ cause: error
43
+ });
44
+ }
45
+ }
46
+ }
47
+ export default MemoryResolver;
@@ -0,0 +1,260 @@
1
+ import { ApiDOMError } from '@speclynx/apidom-error';
2
+ import { ApiDOMErrorOptions } from '@speclynx/apidom-error';
3
+ import type { ApiDOMReferenceOptions } from '@speclynx/apidom-reference/configuration/empty';
4
+ import { ArazzoCriterionParseError } from '@swaggerexpert/arazzo-criterion';
5
+ import { ArazzoRuntimeExpressionParseError } from '@swaggerexpert/arazzo-runtime-expression';
6
+ import { ASTTranslator } from '@swaggerexpert/arazzo-runtime-expression';
7
+ import { ASTTranslator as ASTTranslator_2 } from '@swaggerexpert/arazzo-criterion';
8
+ import { ConditionAST as CriterionConditionAST } from '@swaggerexpert/arazzo-criterion';
9
+ import { ParseOptions } from '@swaggerexpert/arazzo-runtime-expression';
10
+ import { ParseOptions as ParseOptions_2 } from '@swaggerexpert/arazzo-criterion';
11
+ import { ParseResult } from '@swaggerexpert/arazzo-runtime-expression';
12
+ import { ParseResult as ParseResult_2 } from '@swaggerexpert/arazzo-criterion';
13
+ import { ParseResultElement } from '@speclynx/apidom-datamodel';
14
+ import type { PartialDeep } from 'type-fest';
15
+ import { ASTNode as RuntimeExpressionASTNode } from '@swaggerexpert/arazzo-runtime-expression';
16
+
17
+ export { ArazzoCriterionParseError }
18
+
19
+ export { ArazzoRuntimeExpressionParseError }
20
+
21
+ export { CriterionConditionAST }
22
+
23
+ /**
24
+ * Default reference options for parsing Arazzo Documents.
25
+ * @public
26
+ */
27
+ export declare const defaultParseArazzoOptions: ParseArazzoOptions;
28
+
29
+ /**
30
+ * Default options for parsing Arazzo "simple" criterion conditions. Empty on
31
+ * purpose: the underlying grammar package supplies its own defaults, including
32
+ * a fresh ASTTranslator per call.
33
+ * @public
34
+ */
35
+ export declare const defaultParseCriterionConditionOptions: ParseCriterionConditionOptions;
36
+
37
+ /**
38
+ * Default reference options for parsing OpenAPI Documents.
39
+ * @public
40
+ */
41
+ export declare const defaultParseOpenAPIOptions: ParseOpenAPIOptions;
42
+
43
+ /**
44
+ * Default options for parsing Arazzo Runtime Expressions. Empty on purpose:
45
+ * the underlying grammar package supplies its own defaults, including a fresh
46
+ * ASTTranslator per call.
47
+ * @public
48
+ */
49
+ export declare const defaultParseRuntimeExpressionOptions: ParseRuntimeExpressionOptions;
50
+
51
+ /**
52
+ * Parses an Arazzo Document from an object.
53
+ * @param source - The Arazzo Document as a plain object
54
+ * @param options - Reference options (uses defaultOptions when not provided)
55
+ * @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
56
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
57
+ * @public
58
+ */
59
+ export declare function parseArazzo(source: Record<string, unknown>, options?: ParseArazzoOptions): Promise<ParseResultElement>;
60
+
61
+ /**
62
+ * Parses an Arazzo Document from a string or URI.
63
+ * @param source - The Arazzo Document as string content, or a file system path / HTTP(S) URL
64
+ * @param options - Reference options (uses defaultOptions when not provided)
65
+ * @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
66
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
67
+ * @public
68
+ */
69
+ export declare function parseArazzo(source: string, options?: ParseArazzoOptions): Promise<ParseResultElement>;
70
+
71
+ /**
72
+ * Parses an Arazzo Document from a string, object, or URI.
73
+ * @param source - The Arazzo Document as a plain object, string content, or URI
74
+ * @param options - Reference options (uses defaultOptions when not provided)
75
+ * @returns A promise that resolves to the parsed Arazzo Document as ApiDOM data model
76
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
77
+ * @public
78
+ */
79
+ export declare function parseArazzo(source: string | Record<string, unknown>, options?: ParseArazzoOptions): Promise<ParseResultElement>;
80
+
81
+ /**
82
+ * Options for parsing Arazzo Documents.
83
+ * @public
84
+ */
85
+ export declare type ParseArazzoOptions = PartialDeep<ApiDOMReferenceOptions>;
86
+
87
+ /**
88
+ * Parses an Arazzo "simple" criterion condition (e.g. `$statusCode == 200`,
89
+ * `$response.body#/status == 'available' && $response.header.X-Rate-Limit-Remaining > 0`)
90
+ * into its AST.
91
+ *
92
+ * A pure, context-free syntax parser: the condition string in, an AST plus
93
+ * position-carrying diagnostics (`result.matched` / `result.maxMatched`) out.
94
+ * No evaluation is performed and no context is required; evaluating the
95
+ * parsed condition against runtime state is the runner's responsibility.
96
+ * Embedded runtime expression operands are parsed too: each `RuntimeExpression`
97
+ * node in the resulting AST carries its own sub-AST, produced by
98
+ * `@swaggerexpert/arazzo-runtime-expression` (see {@link parseRuntimeExpression}).
99
+ *
100
+ * @param condition - The "simple" criterion condition source string
101
+ * @param options - Parse options (uses defaultOptions when not provided)
102
+ * @returns The parse result: `{ result, tree, stats, trace }`. On success,
103
+ * `result.success` is `true` and `tree` holds the AST. On failure,
104
+ * `result.success` is `false`, `tree` is `undefined`, and `result.maxMatched`
105
+ * gives the offset into `condition` up to which parsing succeeded.
106
+ * @throws TypeError - When `condition` is not a string.
107
+ * @throws ArazzoCriterionParseError - When parsing fails for a reason other
108
+ * than invalid syntax (an unexpected internal error). Invalid syntax is never
109
+ * thrown; it is reported through the returned `result`.
110
+ *
111
+ * @example
112
+ * Valid condition
113
+ * ```typescript
114
+ * import { parseCriterionCondition } from '@usearazzo/parser';
115
+ *
116
+ * const { result, tree } = parseCriterionCondition('$statusCode == 200');
117
+ * // result.success === true
118
+ * // tree.type === 'BinaryExpression'
119
+ * ```
120
+ *
121
+ * @example
122
+ * Invalid condition
123
+ * ```typescript
124
+ * import { parseCriterionCondition } from '@usearazzo/parser';
125
+ *
126
+ * const { result, tree } = parseCriterionCondition('$statusCode ===');
127
+ * // result.success === false
128
+ * // tree === undefined
129
+ * // result.maxMatched - the offset parsing failed at
130
+ * ```
131
+ * @public
132
+ */
133
+ export declare function parseCriterionCondition(condition: string, options?: ParseCriterionConditionOptions): ParseCriterionConditionResult;
134
+
135
+ /**
136
+ * Options for parsing Arazzo "simple" criterion conditions.
137
+ * @public
138
+ */
139
+ export declare type ParseCriterionConditionOptions = ParseOptions_2<ASTTranslator_2>;
140
+
141
+ /**
142
+ * Result of parsing an Arazzo "simple" criterion condition: the foundation
143
+ * ParseResult bound to the AST node type this parser produces.
144
+ * @public
145
+ */
146
+ export declare type ParseCriterionConditionResult = ParseResult_2<CriterionConditionAST>;
147
+
148
+ /**
149
+ * Error thrown by `parseArazzo` and `parseOpenAPI` when parsing fails for any
150
+ * reason. The original error is available via the `cause` property.
151
+ * @public
152
+ */
153
+ export declare class ParseError extends ApiDOMError {
154
+ constructor(message?: string, options?: ApiDOMErrorOptions);
155
+ }
156
+
157
+ /**
158
+ * Parses an OpenAPI Document from an object.
159
+ * @param source - The OpenAPI Document as a plain object
160
+ * @param options - Reference options (uses defaultOptions when not provided)
161
+ * @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
162
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
163
+ * @public
164
+ */
165
+ export declare function parseOpenAPI(source: Record<string, unknown>, options?: ParseOpenAPIOptions): Promise<ParseResultElement>;
166
+
167
+ /**
168
+ * Parses an OpenAPI Document from a string or URI.
169
+ * @param source - The OpenAPI Document as string content, or a file system path / HTTP(S) URL
170
+ * @param options - Reference options (uses defaultOptions when not provided)
171
+ * @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
172
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
173
+ * @public
174
+ */
175
+ export declare function parseOpenAPI(source: string, options?: ParseOpenAPIOptions): Promise<ParseResultElement>;
176
+
177
+ /**
178
+ * Parses an OpenAPI Document from a string, object, or URI.
179
+ * @param source - The OpenAPI Document as a plain object, string content, or URI
180
+ * @param options - Reference options (uses defaultOptions when not provided)
181
+ * @returns A promise that resolves to the parsed OpenAPI Document as ApiDOM data model
182
+ * @throws ParseError - When parsing fails for any reason. The original error is available via the `cause` property.
183
+ * @public
184
+ */
185
+ export declare function parseOpenAPI(source: string | Record<string, unknown>, options?: ParseOpenAPIOptions): Promise<ParseResultElement>;
186
+
187
+ /**
188
+ * Options for parsing OpenAPI Documents.
189
+ * @public
190
+ */
191
+ export declare type ParseOpenAPIOptions = PartialDeep<ApiDOMReferenceOptions>;
192
+
193
+ export { ParseResultElement }
194
+
195
+ /**
196
+ * Parses an Arazzo Runtime Expression (e.g. `$inputs.username`,
197
+ * `$steps.myStep.outputs.result`, `$response.body#/pets/0/id`) into its AST.
198
+ *
199
+ * A pure, context-free syntax parser: the expression string in, an AST plus
200
+ * position-carrying diagnostics (`result.matched` / `result.maxMatched`) out.
201
+ * No evaluation is performed and no document is required; evaluating the
202
+ * parsed expression against runtime state is the runner's responsibility.
203
+ *
204
+ * The expression must be the bare form without surrounding braces. Splitting a
205
+ * `{expression}` template into literal and expression spans is left to
206
+ * `@swaggerexpert/arazzo-runtime-expression`'s own `extract`/`interpolate`
207
+ * functions, which remain a foundation dependency rather than being wrapped
208
+ * here.
209
+ *
210
+ * @param expression - The runtime expression source string
211
+ * @param options - Parse options (uses defaultOptions when not provided)
212
+ * @returns The parse result: `{ result, tree, stats, trace }`. On success,
213
+ * `result.success` is `true` and `tree` holds the AST. On failure,
214
+ * `result.success` is `false`, `tree` is `undefined`, and `result.maxMatched`
215
+ * gives the offset into `expression` up to which parsing succeeded.
216
+ * @throws TypeError - When `expression` is not a string.
217
+ * @throws ArazzoRuntimeExpressionParseError - When parsing fails for a reason
218
+ * other than invalid syntax (an unexpected internal error). Invalid syntax is
219
+ * never thrown; it is reported through the returned `result`.
220
+ *
221
+ * @example
222
+ * Valid expression
223
+ * ```typescript
224
+ * import { parseRuntimeExpression } from '@usearazzo/parser';
225
+ *
226
+ * const { result, tree } = parseRuntimeExpression('$steps.myStep.outputs.result');
227
+ * // result.success === true
228
+ * // tree === { type: 'StepsExpression', stepId: 'myStep', field: 'outputs', outputName: 'result' }
229
+ * ```
230
+ *
231
+ * @example
232
+ * Invalid expression
233
+ * ```typescript
234
+ * import { parseRuntimeExpression } from '@usearazzo/parser';
235
+ *
236
+ * const { result, tree } = parseRuntimeExpression('$unknown.thing');
237
+ * // result.success === false
238
+ * // tree === undefined
239
+ * // result.maxMatched === 0 - the offset parsing failed at
240
+ * ```
241
+ * @public
242
+ */
243
+ export declare function parseRuntimeExpression(expression: string, options?: ParseRuntimeExpressionOptions): ParseRuntimeExpressionResult;
244
+
245
+ /**
246
+ * Options for parsing Arazzo Runtime Expressions.
247
+ * @public
248
+ */
249
+ export declare type ParseRuntimeExpressionOptions = ParseOptions<ASTTranslator>;
250
+
251
+ /**
252
+ * Result of parsing an Arazzo Runtime Expression: the foundation ParseResult
253
+ * bound to the AST node type this parser produces.
254
+ * @public
255
+ */
256
+ export declare type ParseRuntimeExpressionResult = ParseResult<RuntimeExpressionASTNode>;
257
+
258
+ export { RuntimeExpressionASTNode }
259
+
260
+ export { }