aboutsummaryrefslogtreecommitdiff
path: root/routes/api/npm.ts
diff options
context:
space:
mode:
authorIndrajith K L2023-05-13 11:17:27 +0530
committerIndrajith K L2023-05-13 11:17:27 +0530
commitfc0064f6a0ed4c66a726a93786f7e1bb39bcfe80 (patch)
treec5b65cd636abe4f367cc0c36ca20ca21a8ad080a /routes/api/npm.ts
downloadpackage-analyzer-fc0064f6a0ed4c66a726a93786f7e1bb39bcfe80.tar.gz
package-analyzer-fc0064f6a0ed4c66a726a93786f7e1bb39bcfe80.tar.bz2
package-analyzer-fc0064f6a0ed4c66a726a93786f7e1bb39bcfe80.zip
Initial Commit
* 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
Diffstat (limited to 'routes/api/npm.ts')
-rw-r--r--routes/api/npm.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/routes/api/npm.ts b/routes/api/npm.ts
new file mode 100644
index 0000000..4063fc7
--- /dev/null
+++ b/routes/api/npm.ts
@@ -0,0 +1,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()))
+ )
+}