Skip to content

Commit

Permalink
feat: browse web package version (#1457)
Browse files Browse the repository at this point in the history
* feat: allow endpoint to query by version

* chore: update @verdaccio/ui-theme

* test: add unit test for sidebar endpoint by version
  • Loading branch information
juanpicado committed Sep 5, 2019
1 parent 06f659c commit d5303f4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@verdaccio/local-storage": "2.2.1",
"@verdaccio/readme": "8.0.0",
"@verdaccio/streams": "8.0.0",
"@verdaccio/ui-theme": "0.2.3",
"@verdaccio/ui-theme": "0.3.0",
"JSONStream": "1.3.5",
"async": "3.1.0",
"body-parser": "1.19.0",
Expand Down
39 changes: 27 additions & 12 deletions src/api/web/endpoint/package.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import _ from 'lodash';
import { addScope, addGravatarSupport, deleteProperties, sortByName, parseReadme, formatAuthor, convertDistRemoteToLocalTarballUrls } from '../../../lib/utils';
import {
addScope,
addGravatarSupport,
deleteProperties,
sortByName,
parseReadme,
formatAuthor,
convertDistRemoteToLocalTarballUrls,
isVersionValid
} from '../../../lib/utils';
import { allow } from '../../middleware';
import { DIST_TAGS, HEADER_TYPE, HEADERS, HTTP_STATUS } from '../../../lib/constants';
import { generateGravatarUrl } from '../../../utils/user';
Expand Down Expand Up @@ -104,22 +113,28 @@ function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth,
req,
callback: function(err: Error, info: $SidebarPackage): void {
if (_.isNil(err)) {
const {v} = req.query;
let sideBarInfo: any = _.clone(info);
sideBarInfo.versions = convertDistRemoteToLocalTarballUrls(info, req, config.url_prefix).versions;
sideBarInfo.latest = sideBarInfo.versions[info[DIST_TAGS].latest];
sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author);
sideBarInfo = deleteProperties(['readme', '_attachments', '_rev', 'name'], sideBarInfo);
if (config.web) {
sideBarInfo = addGravatarSupport(sideBarInfo, config.web.gravatar);
if (isVersionValid(info, v)) {
sideBarInfo.latest = sideBarInfo.versions[v];
sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author);
} else {
sideBarInfo.latest = sideBarInfo.versions[info[DIST_TAGS].latest];
sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author);
}
sideBarInfo = deleteProperties(['readme', '_attachments', '_rev', 'name'], sideBarInfo);
if (config.web) {
sideBarInfo = addGravatarSupport(sideBarInfo, config.web.gravatar);
} else {
sideBarInfo = addGravatarSupport(sideBarInfo);
}
next(sideBarInfo);
} else {
sideBarInfo = addGravatarSupport(sideBarInfo);
res.status(HTTP_STATUS.NOT_FOUND);
res.end();
}
next(sideBarInfo);
} else {
res.status(HTTP_STATUS.NOT_FOUND);
res.end();
}
},
});
});
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @prettier
*/

import _ from 'lodash';
import fs from 'fs';
import assert from 'assert';
Expand Down Expand Up @@ -599,3 +598,13 @@ export function encodeScopedUri(packageName) {
export function hasDiffOneKey(versions) {
return Object.keys(versions).length !== 1;
}

export function isVersionValid(packageMeta, packageVersion): boolean {
const hasVersion = typeof packageVersion !== 'undefined';
if (!hasVersion) {
return false;
}

const hasMatchVersion = Object.keys(packageMeta.versions).includes(packageVersion);
return hasMatchVersion;
}
29 changes: 28 additions & 1 deletion test/unit/modules/web/api.web.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('endpoint web unit test', () => {
});
});

//FIXME: disable, we need to inspect why fails randomly
//FIXME: disabled, we need to inspect why fails randomly
test.skip('should display scoped readme 404', (done) => {
request(app)
.get('/-/verdaccio/package/readme/@scope/404')
Expand Down Expand Up @@ -115,6 +115,23 @@ describe('endpoint web unit test', () => {
});
});

test('should display sidebar info by version', (done) => {
request(app)
.get('/-/verdaccio/sidebar/@scope/pk1-test?v=1.0.6')
.expect(HTTP_STATUS.OK)
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.end(function(err, res) {
const sideBarInfo = res.body;
const latestVersion = publishMetadata.versions[publishMetadata[DIST_TAGS].latest];

expect(sideBarInfo.latest.author).toBeDefined();
expect(sideBarInfo.latest.author.avatar).toMatch(/www.gravatar.com/);
expect(sideBarInfo.latest.author.name).toBe(latestVersion.author.name);
expect(sideBarInfo.latest.author.email).toBe(latestVersion.author.email);
done();
});
});

test('should display sidebar info 404', (done) => {
request(app)
.get('/-/verdaccio/sidebar/@scope/404')
Expand All @@ -124,6 +141,16 @@ describe('endpoint web unit test', () => {
done();
});
});

test('should display sidebar info 404 with version', (done) => {
request(app)
.get('/-/verdaccio/sidebar/@scope/pk1-test?v=0.0.0-not-found')
.expect(HTTP_STATUS.NOT_FOUND)
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.end(function() {
done();
});
});
});

describe('Search', () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1732,10 +1732,10 @@
resolved "https://registry.verdaccio.org/@verdaccio%2ftypes/-/types-5.2.2.tgz#ecea86c864b905314e29c88e5f93aebcf116c5ff"
integrity sha512-5NbSsCcwCt5ZrA6+vXqUXFM2tAXkS++dju5XSh4TGM+ISon/FS/oUQtE7zmhlaftCt2z2LRhg0/b3wV8QUxSGQ==

"@verdaccio/ui-theme@0.2.3":
version "0.2.3"
resolved "https://registry.verdaccio.org/@verdaccio%2fui-theme/-/ui-theme-0.2.3.tgz#d25335be52bc15ad5c57cbff7607ddc8bf857833"
integrity sha512-rzn336VTjReOyxPdYapMDxgH+5PvxpT49GGdnHOcB7JEdDgl4Qa2mQIzRol/3GLahXMfOX+6uRY/ySBdlRo69A==
"@verdaccio/ui-theme@0.3.0":
version "0.3.0"
resolved "https://registry.verdaccio.org/@verdaccio%2fui-theme/-/ui-theme-0.3.0.tgz#573a04437cc672e3efe6f4db055f775d86df0958"
integrity sha512-CpbiJTxacv/j7yG31x089ZRM4bVTqGRxQEgN4uyqepWtYu6OgI/SvU6rpTuF1BjMkBuvSv8SRBKhpJav5oEs0Q==

"@yarnpkg/lockfile@^1.1.0":
version "1.1.0"
Expand Down

0 comments on commit d5303f4

Please sign in to comment.