-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·190 lines (162 loc) · 5.49 KB
/
app.js
File metadata and controls
executable file
·190 lines (162 loc) · 5.49 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* postmanBackup - Backup Postman collections and environments
* Author: Alexander Kytmanov
*/
const fs = require('fs-extra');
const path = require('node:path');
const config = require('./config.json');
// Config extraction
const APIKEYS = config.api_keys;
const PATHCOL = config.path.collections;
const PATHENV = config.path.environments;
const TIMEOUT = config.time_between_requests;
const USEDATE = config.use_date_subfolder;
const USEID = config.use_id_subfolder;
const APIURL = config.api_url;
// Inline logger
const log = {
info: (...args) => console.log('[INFO]', new Date().toISOString(), ...args),
error: (...args) => console.error('[ERROR]', new Date().toISOString(), ...args),
};
/**
* Validate config.json structure
*/
function validateConfig(cfg) {
const errors = [];
if (!Array.isArray(cfg.api_keys) || cfg.api_keys.length === 0) {
errors.push('api_keys must be a non-empty array');
} else if (!cfg.api_keys.every((k) => typeof k === 'string' && k.length > 0)) {
errors.push('all api_keys must be non-empty strings');
}
if (typeof cfg.api_url !== 'string' || !cfg.api_url.startsWith('https://')) {
errors.push('api_url must be a string starting with https://');
}
if (!cfg.path || typeof cfg.path.collections !== 'string' || cfg.path.collections.length === 0) {
errors.push('path.collections must be a non-empty string');
}
if (!cfg.path || typeof cfg.path.environments !== 'string' || cfg.path.environments.length === 0) {
errors.push('path.environments must be a non-empty string');
}
if (typeof cfg.time_between_requests !== 'number' || cfg.time_between_requests <= 0) {
errors.push('time_between_requests must be a positive number');
}
if (typeof cfg.use_date_subfolder !== 'boolean') {
errors.push('use_date_subfolder must be a boolean');
}
if (typeof cfg.use_id_subfolder !== 'boolean') {
errors.push('use_id_subfolder must be a boolean');
}
if (errors.length > 0) {
throw new Error('Invalid configuration:\n' + errors.map((e) => ` - ${e}`).join('\n'));
}
}
/**
* Fetch data from Postman API
*/
async function getData(url, apiKey) {
const response = await fetch(url, {
headers: { 'X-Api-Key': apiKey },
});
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}`);
}
return response.json();
}
/**
* Sleep helper for rate limiting
*/
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Build output directory path with optional date/ID subfolders
*/
function buildOutputPath(basePath, apiKey) {
let dir = basePath;
if (USEID) dir = path.join(dir, apiKey);
if (USEDATE) {
const date = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
dir = path.join(dir, date);
}
return dir;
}
/**
* Sanitize a string for use as a filename — replaces path-unsafe characters
*/
function sanitizeFilename(name) {
return name.replace(/[/\\:*?"<>|]/g, '-').trim();
}
/**
* Save JSON data to file
*/
async function saveJson(basePath, apiKey, filename, data) {
const dir = buildOutputPath(basePath, apiKey);
await fs.ensureDir(dir);
const fullPath = path.join(dir, filename);
await fs.writeFile(fullPath, JSON.stringify(data, null, 2), 'utf8');
return fullPath;
}
/**
* Backup all collections for a given API key
*/
async function backupCollections(apiKey) {
const list = await getData(`${APIURL}collections/`, apiKey);
if (!Array.isArray(list.collections)) {
throw new Error(`Unexpected collections response: missing 'collections' array`);
}
for (const col of list.collections) {
await sleep(TIMEOUT);
const detail = await getData(`${APIURL}collections/${col.uid}`, apiKey);
const name = sanitizeFilename(detail.collection.info.name);
const filename = `${col.owner}-${name}.json`;
const savedPath = await saveJson(PATHCOL, apiKey, filename, detail);
log.info(`Collection ${col.uid}: saved to ${savedPath}`);
}
}
/**
* Backup all environments for a given API key
*/
async function backupEnvironments(apiKey) {
const list = await getData(`${APIURL}environments/`, apiKey);
if (!Array.isArray(list.environments)) {
throw new Error(`Unexpected environments response: missing 'environments' array`);
}
for (const env of list.environments) {
await sleep(TIMEOUT);
const detail = await getData(`${APIURL}environments/${env.uid}`, apiKey);
const name = sanitizeFilename(detail.environment.name);
const filename = `${env.owner}-${name}.json`;
const savedPath = await saveJson(PATHENV, apiKey, filename, detail);
log.info(`Environment ${env.uid}: saved to ${savedPath}`);
}
}
if (require.main === module) {
/**
* Main entry point — only runs when executed directly (node app.js)
*/
(async () => {
try {
validateConfig(config);
} catch (err) {
console.error(err.message);
process.exit(1);
}
for (const apiKey of APIKEYS) {
log.info(`Starting backup for key: ${apiKey.slice(0, 8)}...`);
try {
await backupCollections(apiKey);
await backupEnvironments(apiKey);
log.info(`Backup completed successfully for key: ${apiKey.slice(0, 8)}...`);
} catch (err) {
log.error(`Backup failed for key ${apiKey.slice(0, 8)}...: ${err.message}`);
process.exitCode = 1;
}
}
if (process.exitCode === 1) {
log.error('Backup completed with errors. Check output above.');
}
})();
} else {
// Export for testing (when require'd)
module.exports = { validateConfig, buildOutputPath, getData };
}