|
| 1 | +let urlResources = [ |
| 2 | + { |
| 3 | + "name": "ReScript Test Framework", |
| 4 | + "description": "The most minimalistic testing library you will find for testing ReScript code", |
| 5 | + "keywords": ["testing", "minimal", "experimental"], |
| 6 | + "urlHref": "https://github.com/rescript-lang/rescript-project-template/blob/test/tests/Tests.res", |
| 7 | + "official": true |
| 8 | + }, |
| 9 | + { |
| 10 | + "name": "genType", |
| 11 | + "description": "Better interop with JS & TS in ReScript", |
| 12 | + "keywords": ["rescript", "typescript"], |
| 13 | + "urlHref": "https://github.com/reason-association/genType", |
| 14 | + "official": true |
| 15 | + } |
| 16 | +] |
| 17 | + |
| 18 | +export async function onRequestGET(context) { |
| 19 | + const packages = await fetchNpmPackages() |
| 20 | + return Response.json({ |
| 21 | + ...packages, |
| 22 | + urlResources, |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +async function fetchNpmPackages() { |
| 27 | + let baseUrl = "https://registry.npmjs.org/-/v1/search?text=keywords:rescript&size=250&maintenance=1.0&popularity=0.5&quality=0.9" |
| 28 | + |
| 29 | + let [data1, data2, data3] = await Promise.all3([ |
| 30 | + fetch(baseUrl) |
| 31 | + .then(res => res.json()) |
| 32 | + .then(data => parsePkgs(data)), |
| 33 | + fetch(baseUrl + "&from=250") |
| 34 | + .then(res => res.json()) |
| 35 | + .then(data => parsePkgs(data)), |
| 36 | + fetch(baseUrl + "&from=500") |
| 37 | + .then(res => res.json()) |
| 38 | + .then(data => parsePkgs(data)), |
| 39 | + ]) |
| 40 | + |
| 41 | + let unmaintained = [] |
| 42 | + |
| 43 | + function shouldAllow(pkg) { |
| 44 | + // These are packages that we do not want to filter out when loading searching from NPM. |
| 45 | + let packageAllowList = [] |
| 46 | + |
| 47 | + if (packageAllowList.includes(pkg)) { |
| 48 | + return true |
| 49 | + } |
| 50 | + |
| 51 | + if (pkg.name.includes("reason")) { |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + if (pkg.maintenanceScore < 0.3) { |
| 56 | + unmaintained.push(pkg) |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + return true |
| 61 | + } |
| 62 | + |
| 63 | + let packages = [] |
| 64 | + for (let pkg of data1) if (shouldAllow(pkg)) packages.push(pkg) |
| 65 | + for (let pkg of data2) if (shouldAllow(pkg)) packages.push(pkg) |
| 66 | + for (let pkg of data3) if (shouldAllow(pkg)) packages.push(pkg) |
| 67 | + |
| 68 | + return { |
| 69 | + packages, |
| 70 | + unmaintained, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function parsePkgs(data) { |
| 75 | + return data["objects"].map(item => { |
| 76 | + let pkg = item["package"] |
| 77 | + return { |
| 78 | + name: pkg["name"], |
| 79 | + version: pkg["version"], |
| 80 | + keywords: uniqueKeywords(filterKeywords(pkg["keywords"])), |
| 81 | + description: pkg["description"] ?? "", |
| 82 | + repositoryHref: pkg["links"]?.["repository"] ?? null, |
| 83 | + npmHref: pkg["links"]["npm"], |
| 84 | + searchScore: item["searchScore"], |
| 85 | + maintenanceScore: item["score"]["detail"]["maintenance"], |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +function filterKeywords(keywords) { |
| 91 | + return keywords.filter(kw => { |
| 92 | + let k = kw.toLowerCase() |
| 93 | + return !( |
| 94 | + k === "reasonml" || |
| 95 | + k === "reason" || |
| 96 | + k === "ocaml" || |
| 97 | + k === "bucklescript" || |
| 98 | + k === "rescript" |
| 99 | + ) |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +function uniqueKeywords(keywords) { |
| 104 | + return [...new Set(keywords)] |
| 105 | +} |
0 commit comments