-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (123 loc) · 3.94 KB
/
Copy pathindex.js
File metadata and controls
136 lines (123 loc) · 3.94 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
#!/usr/bin/env node
/**
* Random Joke CLI (no dependencies)
*
* Usage:
* node index.js [--category Any] [--unsafe] [--timeout 5000] [--retries 2] [--repeat 1]
*
* Examples:
* ./index.js
* ./index.js --category Programming
* ./index.js --unsafe --repeat 3
*/
const DEFAULT_TIMEOUT = 5000; // ms
const DEFAULT_RETRIES = 2;
const DEFAULT_REPEAT = 1;
function parseArgs(argv) {
const args = {
category: 'Any',
safe: true,
timeout: DEFAULT_TIMEOUT,
retries: DEFAULT_RETRIES,
repeat: DEFAULT_REPEAT,
};
for (let i = 2; i < argv.length; i++) {
const a = argv[i];
if (a === '--category' || a === '-c') {
args.category = argv[++i] || 'Any';
} else if (a === '--unsafe') {
args.safe = false;
} else if (a === '--timeout') {
args.timeout = Number(argv[++i]) || DEFAULT_TIMEOUT;
} else if (a === '--retries') {
args.retries = Math.max(0, Number(argv[++i]) || DEFAULT_RETRIES);
} else if (a === '--repeat') {
args.repeat = Math.max(1, Number(argv[++i]) || DEFAULT_REPEAT);
} else if (a === '--help' || a === '-h') {
printHelpAndExit();
} else {
console.warn(`Unknown arg: ${a}`);
printHelpAndExit(1);
}
}
return args;
}
function printHelpAndExit(code = 0) {
console.log(`
Random Joke CLI
Usage:
index.js [--category <Category>] [--unsafe] [--timeout <ms>] [--retries <n>] [--repeat <n>]
Options:
--category, -c Joke category (Any, Programming, Misc, Dark, Pun, Spooky, Christmas, etc.)
--unsafe Allow all flags (disables blacklist). By default explicit/racist/etc. are filtered.
--timeout Fetch timeout in milliseconds (default ${DEFAULT_TIMEOUT})
--retries Number of retries on network errors (default ${DEFAULT_RETRIES})
--repeat How many jokes to fetch (default ${DEFAULT_REPEAT})
--help, -h Show this help
`);
process.exit(code);
}
async function fetchWithTimeout(url, timeoutMs, retries) {
let lastErr = null;
for (let attempt = 0; attempt <= retries; attempt++) {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetch(url, { signal: controller.signal });
clearTimeout(id);
if (!res.ok) {
lastErr = new Error(`HTTP ${res.status}`);
// For 4xx/5xx, don't retry further in many cases, but we'll retry anyway a limited number of times.
throw lastErr;
}
const data = await res.json();
return data;
} catch (err) {
clearTimeout(id);
lastErr = err;
const isAbort = err.name === 'AbortError';
const isLast = attempt === retries;
if (isLast) break;
const backoff = 200 * Math.pow(2, attempt); // simple exponential backoff
await new Promise(r => setTimeout(r, backoff));
}
}
throw lastErr;
}
function buildUrl(category, safe) {
const base = 'https://v2.jokeapi.dev/joke';
const cat = encodeURIComponent(category || 'Any');
const params = new URLSearchParams();
if (safe) params.set('blacklistFlags', 'nsfw,religious,political,racist,sexist,explicit');
// You can add other params like lang= or format=
return `${base}/${cat}?${params.toString()}`;
}
function printJoke(data) {
if (!data) return;
if (data.error) {
console.error('API error:', data.message || JSON.stringify(data));
return;
}
if (data.type === 'single') {
console.log(data.joke);
} else {
console.log(data.setup);
console.log(data.delivery);
}
}
async function main() {
const opts = parseArgs(process.argv);
const url = buildUrl(opts.category, opts.safe);
for (let i = 0; i < opts.repeat; i++) {
try {
const data = await fetchWithTimeout(url, opts.timeout, opts.retries);
printJoke(data);
if (i < opts.repeat - 1) console.log('---'); // separator between jokes
} catch (err) {
console.error('Failed to fetch joke:', err.message || err);
process.exitCode = 1;
return;
}
}
}
main();