0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/packages/api/src/stars.ts
Juan Picado a8e1ffd72f
chore: list stars of an user (#3451)
* chore: list stars of user

* chore: add pnpm test e2e
2022-10-21 23:05:18 +02:00

36 lines
1.2 KiB
TypeScript

import { Response, Router } from 'express';
import _ from 'lodash';
import { HTTP_STATUS, USERS, errorUtils } from '@verdaccio/core';
import { Storage } from '@verdaccio/store';
import { Version } from '@verdaccio/types';
import { $NextFunctionVer, $RequestExtend } from '../types/custom';
export default function (route: Router, storage: Storage): void {
route.get(
'/-/_view/starredByUser',
async (req: $RequestExtend, res: Response, next: $NextFunctionVer): Promise<void> => {
const query: { key: string } = req.query;
if (typeof query?.key !== 'string') {
return next(errorUtils.getBadRequest('missing query key username'));
}
try {
const localPackages: Version[] = await storage.getLocalDatabase();
const filteredPackages: Version[] = localPackages.filter((localPackage: Version) =>
_.keys(localPackage[USERS]).includes(query?.key.toString().replace(/['"]+/g, ''))
);
res.status(HTTP_STATUS.OK);
next({
rows: filteredPackages.map((filteredPackage: Version) => ({
value: filteredPackage.name,
})),
});
} catch (err: any) {
return next(err);
}
}
);
}