forked from Laboratoria/DEV004-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
49 lines (44 loc) · 1.21 KB
/
api.js
File metadata and controls
49 lines (44 loc) · 1.21 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
import { readFile } from "fs";
export const readMD = (path) => {
return new Promise((resolve, reject) => {
// 5. LEEMOS EL ARCHIVO
readFile(path, "utf8", function (err, data) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
export const findLinks = (contenido, ruta) => {
// 6. LEEMOS LINKS
let regex = /\[([^\]!]+)]\((https:\/\/[^\)]+)\)/gi;
const myMatch = [...contenido.matchAll(regex)].map((m) => ({
fyle: ruta,
text: m[1],
href: m[2],
}));
return myMatch;
};
//7. REALIZO PETICIÓN HTTP, MOSTRANDO STATUS Y MESSAGE
export const validate = (arr) => {
const arrPromesas = arr.map((item) => {
return fetch(item.href);
});
return new Promise((resolve, reject) => {
const resultados = [];
Promise.allSettled(arrPromesas)
.then((responses) => {
responses.forEach((response, index) => {
const element = arr[index];
if (response.status === "fulfilled") {
const res = response.value;
element.status = res.status;
element.message = res.statusText;
resultados.push(element);
}
});
resolve(resultados);
})
});
};