aboutsummaryrefslogtreecommitdiff
path: root/routes/api/npm.ts
blob: 4063fc78bb016e0ab737a4fa3fea1671ae0efc4a (plain)
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
import { Handlers } from "$fresh/server.ts";

export const handler: Handlers<any, { data: any }> = {
    async POST(_req, ctx) {
        const requestPackages: Object = await _req.json();
        // Loop through items
        console.log(typeof requestPackages)
        let packageUrls: any[] = [];
        for (let packageName in requestPackages) {
            packageUrls = [
                ...packageUrls,
                `https://registry.npmjs.org/${packageName}`
            ];
        }

        const packageDetailsResponse = await fetchPackageDetails(packageUrls);
        const packageDetails = Object.keys(requestPackages).map((packageName, index) => {
            return {
                [packageName]: packageDetailsResponse[index]
            }
        })
        return Response.json({ packageDetails });
    },
};

async function fetchPackageDetails(packageUrls: any[]) {
    return await Promise.all(
        packageUrls.map(url => fetch(url).then(res => res.json()))
    )
}