-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-pipeline.js
More file actions
65 lines (58 loc) · 1.97 KB
/
format-pipeline.js
File metadata and controls
65 lines (58 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Data format pipeline: YAML config → JSON → CSV export
// Demonstrates chaining DocForge API calls
// Usage: node format-pipeline.js
const yamlConfig = `
employees:
- name: Alice Chen
role: Senior Engineer
department: Engineering
salary: 145000
- name: Bob Johnson
role: Product Manager
department: Product
salary: 130000
- name: Carol Williams
role: Designer
department: Design
salary: 120000
- name: Dave Brown
role: Data Scientist
department: Engineering
salary: 140000
`;
const API = 'https://docforge-api.vercel.app/api';
async function pipeline() {
// Step 1: YAML → JSON
console.log('Step 1: Converting YAML to JSON...');
const yamlRes = await fetch(`${API}/yaml-json`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ input: yamlConfig, direction: 'yaml-to-json' })
});
const { output: jsonData } = await yamlRes.json();
console.log(` Parsed ${jsonData.employees.length} employees from YAML`);
// Step 2: JSON → CSV
console.log('Step 2: Converting JSON to CSV...');
const csvRes = await fetch(`${API}/json-to-csv`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ data: jsonData.employees })
});
const { csv, meta } = await csvRes.json();
console.log(` Generated CSV with ${meta.rowCount} rows, ${meta.columnCount} columns`);
console.log();
console.log('=== Final CSV Output ===');
console.log(csv);
// Bonus: verify round-trip by converting CSV back to JSON
console.log();
console.log('=== Round-trip verification (CSV → JSON) ===');
const verifyRes = await fetch(`${API}/csv-to-json`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ csv })
});
const verified = await verifyRes.json();
console.log(` ${verified.meta.rowCount} rows recovered`);
console.log(` Data integrity: ${JSON.stringify(verified.data[0])}`);
}
pipeline();