1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-21 07:29:37 +01:00

fix: scoped query support (#2208)

This commit is contained in:
Claude 2021-04-28 13:18:30 +08:00 committed by GitHub
parent 6fb388e366
commit 01c5d2ca27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 3 deletions

@ -43,13 +43,23 @@ class Search implements IWebSearch {
public query(query: string): any[] {
const localStorage = this.storage.localStorage as IStorage;
return query === '*'
const hasScope = query.startsWith('@');
// FIXME: lunr-mutable-indexes ignored '@' during indexing
if (hasScope) {
query = query.replace('@', '');
}
const results = query === '*'
? localStorage.storagePlugin.get((items): any => {
items.map(function (pkg): any {
return { ref: pkg, score: 1 };
});
})
: this.index.search(`*${query}*`);
return hasScope
? results.filter(({ ref }) => ref.startsWith('@'))
: results;
}
/**

@ -27,6 +27,20 @@ let packages = [
_npmUser: {
name: 'test_user'
}
},
{
name: '@verdaccio/scope',
description: 'scope',
_npmUser: {
name: 'scope_user'
}
},
{
name: '@any/scope',
description: 'scope',
_npmUser: {
name: 'scope_user'
}
}
];
@ -47,6 +61,13 @@ describe('search', () => {
expect(result).toHaveLength(3);
});
test('search query with @scope', () => {
let result = Search.query('@');
expect(result).toHaveLength(2);
result = Search.query('@verdaccio');
expect(result).toHaveLength(1);
})
test('search remove item', () => {
let item = {
name: 'test6',

@ -180,8 +180,7 @@ describe('endpoint web unit test', () => {
.end(function (err, res) {
// in a normal world, the output would be 1
// https://github.com/verdaccio/verdaccio/issues/345
// should fix this
expect(res.body).toHaveLength(0);
expect(res.body).toHaveLength(1);
done();
});
});