0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-03-25 02:32:52 -05:00
verdaccio/src/api/web/endpoint/search.js
2019-01-14 17:17:37 +02:00

49 lines
1.3 KiB
JavaScript

/**
* @prettier
* @flow
*/
import Search from '../../../lib/search';
import { DIST_TAGS } from '../../../lib/constants';
import type { Router } from 'express';
import type { IAuth, $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageHandler } from '../../../../types';
function addSearchWebApi(route: Router, storage: IStorageHandler, auth: IAuth) {
// Search package
route.get('/search/:anything', function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
const results: any = Search.query(req.params.anything);
const packages = [];
const getPackageInfo = function(i) {
storage.getPackage({
name: results[i].ref,
uplinksLook: false,
callback: (err, entry) => {
if (!err && entry) {
auth.allow_access({ packageName: entry.name }, req.remote_user, function(err, allowed) {
if (err || !allowed) {
return;
}
packages.push(entry.versions[entry[DIST_TAGS].latest]);
});
}
if (i >= results.length - 1) {
next(packages);
} else {
getPackageInfo(i + 1);
}
},
});
};
if (results.length) {
getPackageInfo(0);
} else {
next([]);
}
});
}
export default addSearchWebApi;