-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.ts
37 lines (30 loc) · 1.16 KB
/
package.ts
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
import { RequestHandler } from 'express';
import got from 'got';
import { NPMPackage, NPMPackageFull } from './types';
var util = require('util');
/**
* Attempts to retrieve package data from the npm registry and return it
*/
export const getPackage: RequestHandler = async function (req, res, next) {
const { name, version } = req.params;
try {
const dependencies = await getPackagesRecursive(name, version);
return res.status(200).json({ name, version, dependencies });
} catch (error) {
return next(error);
}
};
async function getPackagesRecursive( name: string, version: string): Promise<NPMPackageFull[]> {
const npmPackage: NPMPackage = await got(
`https://registry.npmjs.org/${name}`,
).json();
const dependencies = npmPackage.versions[version]?.dependencies;
if ( dependencies) {
return await Promise.all(Object.keys(dependencies)
.map( async key => {
const npmPackaFull: NPMPackageFull = { name: key, version:dependencies[key], dependencies: await getPackagesRecursive(key, dependencies[key].replace('^', '').replace('~', '').replace('>','').replace('<','').replace('=',''))};
return npmPackaFull;
} ));
}
return [];
}