@curl-runner/cli 1.0.3 → 1.2.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/dist/cli.js +24 -7
- package/package.json +1 -1
- package/src/ci-exit.test.ts +215 -0
- package/src/cli.ts +122 -2
- package/src/executor/request-executor.ts +47 -1
- package/src/parser/yaml.test.ts +176 -0
- package/src/parser/yaml.ts +54 -8
- package/src/types/config.ts +60 -0
- package/src/utils/response-store.test.ts +213 -0
- package/src/utils/response-store.ts +108 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { ExecutionResult, ResponseStoreContext, StoreConfig } from '../types/config';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extracts a value from an object using a dot-notation path.
|
|
5
|
+
* Supports paths like: "body.id", "body.data.token", "headers.content-type", "status"
|
|
6
|
+
*
|
|
7
|
+
* @param obj - The object to extract from
|
|
8
|
+
* @param path - Dot-notation path to the value
|
|
9
|
+
* @returns The extracted value or undefined if not found
|
|
10
|
+
*/
|
|
11
|
+
export function getValueByPath(obj: unknown, path: string): unknown {
|
|
12
|
+
const parts = path.split('.');
|
|
13
|
+
let current: unknown = obj;
|
|
14
|
+
|
|
15
|
+
for (const part of parts) {
|
|
16
|
+
if (current === null || current === undefined) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof current !== 'object') {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Handle array index access like "items.0.id" or "items[0].id"
|
|
25
|
+
const arrayMatch = part.match(/^(\w+)\[(\d+)\]$/);
|
|
26
|
+
if (arrayMatch) {
|
|
27
|
+
const [, key, indexStr] = arrayMatch;
|
|
28
|
+
const index = Number.parseInt(indexStr, 10);
|
|
29
|
+
current = (current as Record<string, unknown>)[key];
|
|
30
|
+
if (Array.isArray(current)) {
|
|
31
|
+
current = current[index];
|
|
32
|
+
} else {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
} else if (/^\d+$/.test(part) && Array.isArray(current)) {
|
|
36
|
+
// Direct numeric index for arrays
|
|
37
|
+
current = current[Number.parseInt(part, 10)];
|
|
38
|
+
} else {
|
|
39
|
+
current = (current as Record<string, unknown>)[part];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return current;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Converts a value to a string for storage.
|
|
48
|
+
* Objects and arrays are JSON stringified.
|
|
49
|
+
*/
|
|
50
|
+
export function valueToString(value: unknown): string {
|
|
51
|
+
if (value === undefined || value === null) {
|
|
52
|
+
return '';
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === 'string') {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
58
|
+
return String(value);
|
|
59
|
+
}
|
|
60
|
+
return JSON.stringify(value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Extracts values from an execution result based on the store configuration.
|
|
65
|
+
*
|
|
66
|
+
* @param result - The execution result to extract from
|
|
67
|
+
* @param storeConfig - Configuration mapping variable names to JSON paths
|
|
68
|
+
* @returns Object containing the extracted values as strings
|
|
69
|
+
*/
|
|
70
|
+
export function extractStoreValues(
|
|
71
|
+
result: ExecutionResult,
|
|
72
|
+
storeConfig: StoreConfig,
|
|
73
|
+
): ResponseStoreContext {
|
|
74
|
+
const extracted: ResponseStoreContext = {};
|
|
75
|
+
|
|
76
|
+
// Build an object that represents the full response structure
|
|
77
|
+
const responseObj: Record<string, unknown> = {
|
|
78
|
+
status: result.status,
|
|
79
|
+
headers: result.headers || {},
|
|
80
|
+
body: result.body,
|
|
81
|
+
metrics: result.metrics,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
for (const [varName, path] of Object.entries(storeConfig)) {
|
|
85
|
+
const value = getValueByPath(responseObj, path);
|
|
86
|
+
extracted[varName] = valueToString(value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return extracted;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Creates a new response store context.
|
|
94
|
+
*/
|
|
95
|
+
export function createStoreContext(): ResponseStoreContext {
|
|
96
|
+
return {};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Merges new values into an existing store context.
|
|
101
|
+
* New values override existing ones with the same key.
|
|
102
|
+
*/
|
|
103
|
+
export function mergeStoreContext(
|
|
104
|
+
existing: ResponseStoreContext,
|
|
105
|
+
newValues: ResponseStoreContext,
|
|
106
|
+
): ResponseStoreContext {
|
|
107
|
+
return { ...existing, ...newValues };
|
|
108
|
+
}
|