|
| 1 | +/** |
| 2 | + * @fileoverview |
| 3 | + * Implements the OpenAPI Runtime Expression evaluator defined in OAS 3.x. |
| 4 | + * Used for dynamically deriving values for Links and Callbacks from HTTP messages. |
| 5 | + * |
| 6 | + * @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#runtime-expressions |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Context required to evaluate a runtime expression. |
| 11 | + * Represents the state of the HTTP interaction (Request/Response). |
| 12 | + */ |
| 13 | +export interface RuntimeContext { |
| 14 | + url: string; |
| 15 | + method: string; |
| 16 | + statusCode: number; |
| 17 | + request: { |
| 18 | + headers: Record<string, string | string[] | undefined>; |
| 19 | + query: Record<string, string | string[] | undefined>; |
| 20 | + path: Record<string, string | undefined>; |
| 21 | + body?: any; |
| 22 | + }; |
| 23 | + response?: { |
| 24 | + headers: Record<string, string | string[] | undefined>; |
| 25 | + body?: any; |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Resolves a JSON Pointer (RFC 6901) against a target object. |
| 31 | + * Used internally for processing `$request.body#/foo` style expressions. |
| 32 | + * |
| 33 | + * @param data The target object (body). |
| 34 | + * @param pointer The JSON pointer string (e.g., "/user/0/id"). |
| 35 | + * @returns The resolved value or undefined if not found. |
| 36 | + */ |
| 37 | +export function evaluateJsonPointer(data: any, pointer: string): any { |
| 38 | + if (pointer === '' || pointer === '#') return data; |
| 39 | + |
| 40 | + // Remove leading # if present (URI fragment style) |
| 41 | + const cleanPointer = pointer.startsWith('#') ? pointer.substring(1) : pointer; |
| 42 | + |
| 43 | + if (!cleanPointer.startsWith('/')) return undefined; |
| 44 | + |
| 45 | + const tokens = cleanPointer.split('/').slice(1).map(token => |
| 46 | + token.replace(/~1/g, '/').replace(/~0/g, '~') |
| 47 | + ); |
| 48 | + |
| 49 | + let current = data; |
| 50 | + for (const token of tokens) { |
| 51 | + if (current === null || typeof current !== 'object') { |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + // Arrays handling: standard JSON pointer can access array indices |
| 55 | + if (Array.isArray(current)) { |
| 56 | + if (!/^\d+$/.test(token)) return undefined; |
| 57 | + const index = parseInt(token, 10); |
| 58 | + if (index < 0 || index >= current.length) { |
| 59 | + return undefined; |
| 60 | + } |
| 61 | + current = current[index]; |
| 62 | + } else { |
| 63 | + if (!(token in current)) { |
| 64 | + return undefined; |
| 65 | + } |
| 66 | + current = current[token]; |
| 67 | + } |
| 68 | + } |
| 69 | + return current; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Helper to extract a header value case-insensitively (RFC 7230). |
| 74 | + */ |
| 75 | +function getHeader(headers: Record<string, string | string[] | undefined>, key: string): string | undefined { |
| 76 | + const lowerKey = key.toLowerCase(); |
| 77 | + const foundKey = Object.keys(headers).find(k => k.toLowerCase() === lowerKey); |
| 78 | + if (!foundKey) return undefined; |
| 79 | + const val = headers[foundKey]; |
| 80 | + return Array.isArray(val) ? val[0] : val; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Helper to extract a query parameter (Case-sensitive). |
| 85 | + */ |
| 86 | +function getQuery(query: Record<string, string | string[] | undefined>, key: string): string | undefined { |
| 87 | + const val = query[key]; |
| 88 | + return Array.isArray(val) ? val[0] : val; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Resolves a single, bare runtime expression (e.g. "$request.body#/id"). |
| 93 | + * Preserves the type of the referenced value (e.g. boolean, number, object). |
| 94 | + */ |
| 95 | +function resolveSingleExpression(expr: string, context: RuntimeContext): any { |
| 96 | + if (expr === '$url') return context.url; |
| 97 | + if (expr === '$method') return context.method; |
| 98 | + if (expr === '$statusCode') return context.statusCode; |
| 99 | + |
| 100 | + if (expr.startsWith('$request.')) { |
| 101 | + const part = expr.substring(9); // remove "$request." |
| 102 | + if (part.startsWith('header.')) { |
| 103 | + return getHeader(context.request.headers, part.substring(7)); |
| 104 | + } |
| 105 | + if (part.startsWith('query.')) { |
| 106 | + return getQuery(context.request.query, part.substring(6)); |
| 107 | + } |
| 108 | + if (part.startsWith('path.')) { |
| 109 | + return context.request.path[part.substring(5)]; |
| 110 | + } |
| 111 | + if (part.startsWith('body')) { |
| 112 | + if (part === 'body') return context.request.body; |
| 113 | + if (part.startsWith('body#')) { |
| 114 | + return evaluateJsonPointer(context.request.body, part.substring(5)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (expr.startsWith('$response.')) { |
| 120 | + if (!context.response) return undefined; |
| 121 | + const part = expr.substring(10); // remove "$response." |
| 122 | + if (part.startsWith('header.')) { |
| 123 | + return getHeader(context.response.headers, part.substring(7)); |
| 124 | + } |
| 125 | + if (part.startsWith('body')) { |
| 126 | + if (part === 'body') return context.response.body; |
| 127 | + if (part.startsWith('body#')) { |
| 128 | + return evaluateJsonPointer(context.response.body, part.substring(5)); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return undefined; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Evaluates a runtime expression against a given connection context. |
| 138 | + * |
| 139 | + * Supports: |
| 140 | + * 1. Direct expressions: "$request.query.id" -> returns the value (preserving type). |
| 141 | + * 2. Embedded string expressions: "https://example.com/{$request.path.id}" -> returns interpolated string. |
| 142 | + * |
| 143 | + * @param expression The expression string defined in the OpenAPI Link or Callback. |
| 144 | + * @param context The runtime data (request info, response info). |
| 145 | + * @returns The evaluated result. |
| 146 | + */ |
| 147 | +export function evaluateRuntimeExpression(expression: string, context: RuntimeContext): any { |
| 148 | + const hasBraces = expression.includes('{') && expression.includes('}'); |
| 149 | + |
| 150 | + // Case 1: Bare expression (must start with $) |
| 151 | + if (expression.startsWith('$') && !hasBraces) { |
| 152 | + return resolveSingleExpression(expression, context); |
| 153 | + } |
| 154 | + |
| 155 | + // Case 2: Constant string (no braces, no $) |
| 156 | + if (!expression.includes('{')) { |
| 157 | + return expression; |
| 158 | + } |
| 159 | + |
| 160 | + // Case 3: Embedded template string (e.g., "foo/{$url}/bar") |
| 161 | + return expression.replace(/\{([^}]+)\}/g, (_, innerExpr) => { |
| 162 | + const trimmed = innerExpr.trim(); |
| 163 | + // Only interpolate if it looks like a variable we recognize, otherwise leave it? |
| 164 | + // OAS implies logical expressions inside braces. |
| 165 | + const val = resolveSingleExpression(trimmed, context); |
| 166 | + return val !== undefined ? String(val) : ''; |
| 167 | + }); |
| 168 | +} |
0 commit comments