* Implements Basic Routing and API * Implements Package.json file select and json parse * Displays Project Name, Version & Desc from the package.json * Fetch all package details using npm registry API and map it based on package name
31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
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()))
|
|
)
|
|
}
|