0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

fix: improving typescript practices in packages/web/src/endpoint/package.ts (#1972)

* chore: 1. Remove @ts-ignore, 2. Remove :any and infer from existed lib, 3. Prevent variable re-assign

* chore: add changeset
This commit is contained in:
ike 2020-10-23 14:44:32 +08:00 committed by Juan Picado
parent e367c3f1e0
commit 1d11128051
3 changed files with 24 additions and 24 deletions

View file

@ -0,0 +1,6 @@
---
'@verdaccio/middleware': patch
'@verdaccio/web': patch
---
Remove @ts-ignore and any in packages/web/src/endpoint/package.ts

View file

@ -25,7 +25,7 @@ import { IAuth } from '@verdaccio/auth';
import { VerdaccioError } from '@verdaccio/commons-api'; import { VerdaccioError } from '@verdaccio/commons-api';
import { HttpError } from 'http-errors'; import { HttpError } from 'http-errors';
export type $RequestExtend = Request & { remote_user?: any; log: Logger }; export type $RequestExtend = Request & { remote_user?: RemoteUser; log: Logger };
export type $ResponseExtend = Response & { cookies?: any }; export type $ResponseExtend = Response & { cookies?: any };
export type $NextFunctionVer = NextFunction & any; export type $NextFunctionVer = NextFunction & any;
@ -165,7 +165,7 @@ export function allow(auth: IAuth): Function {
const packageVersion = req.params.filename const packageVersion = req.params.filename
? getVersionFromTarball(req.params.filename) ? getVersionFromTarball(req.params.filename)
: undefined; : undefined;
const remote: RemoteUser = req.remote_user; const remote = req.remote_user;
logger.trace( logger.trace(
{ action, user: remote?.name }, { action, user: remote?.name },
`[middleware/allow][@{action}] allow for @{user}` `[middleware/allow][@{action}] allow for @{user}`

View file

@ -9,26 +9,24 @@ import {
getLocalRegistryTarballUri, getLocalRegistryTarballUri,
isVersionValid, isVersionValid,
} from '@verdaccio/utils'; } from '@verdaccio/utils';
import { allow } from '@verdaccio/middleware'; import { allow, $RequestExtend, $ResponseExtend, $NextFunctionVer } from '@verdaccio/middleware';
import { DIST_TAGS, HEADER_TYPE, HEADERS, HTTP_STATUS } from '@verdaccio/dev-commons'; import { DIST_TAGS, HEADER_TYPE, HEADERS, HTTP_STATUS } from '@verdaccio/dev-commons';
import { logger } from '@verdaccio/logger'; import { logger } from '@verdaccio/logger';
import { NextFunction, Request, Response, Router } from 'express'; import { Router } from 'express';
import { IAuth } from '@verdaccio/auth'; import { IAuth } from '@verdaccio/auth';
import { IStorageHandler } from '@verdaccio/store'; import { IStorageHandler } from '@verdaccio/store';
import { Config, Logger, Package } from '@verdaccio/types'; import { Config, Package, RemoteUser, Version } from '@verdaccio/types';
import { addGravatarSupport } from '../web-utils'; import { addGravatarSupport, AuthorAvatar } from '../web-utils';
import { generateGravatarUrl } from '../user'; import { generateGravatarUrl } from '../user';
export type $SidebarPackage = Package & { latest: any }; export type $SidebarPackage = Package & { latest: Version };
export type $RequestExtend = Request & { remote_user?: any; log: Logger }; export { $RequestExtend, $ResponseExtend, $NextFunctionVer }; // Was required by other packages
export type $ResponseExtend = Response & { cookies?: any };
export type $NextFunctionVer = NextFunction & any;
const getOrder = (order = 'asc') => { const getOrder = (order = 'asc') => {
return order === 'asc'; return order === 'asc';
}; };
export type PackcageExt = Package & { author: any; dist?: { tarball: string } }; export type PackcageExt = Package & { author: AuthorAvatar; dist?: { tarball: string } };
function addPackageWebApi( function addPackageWebApi(
route: Router, route: Router,
@ -38,7 +36,7 @@ function addPackageWebApi(
): void { ): void {
const can = allow(auth); const can = allow(auth);
const checkAllow = (name, remoteUser): Promise<boolean> => const checkAllow = (name: string, remoteUser: RemoteUser): Promise<boolean> =>
new Promise((resolve, reject): void => { new Promise((resolve, reject): void => {
try { try {
auth.allow_access({ packageName: name }, remoteUser, (err, allowed): void => { auth.allow_access({ packageName: name }, remoteUser, (err, allowed): void => {
@ -63,7 +61,7 @@ function addPackageWebApi(
throw err; throw err;
} }
async function processPackages(packages: PackcageExt[] = []): Promise<any> { async function processPackages(packages: PackcageExt[] = []): Promise<PackcageExt[]> {
const permissions: PackcageExt[] = []; const permissions: PackcageExt[] = [];
const packgesCopy = packages.slice(); const packgesCopy = packages.slice();
for (const pkg of packgesCopy) { for (const pkg of packgesCopy) {
@ -100,8 +98,7 @@ function addPackageWebApi(
} }
const { web } = config; const { web } = config;
// @ts-ignore const order = web ? getOrder(web.sort_packages) : true;
const order: boolean = config.web ? getOrder(web.sort_packages) : true;
next(sortByName(await processPackages(packages), order)); next(sortByName(await processPackages(packages), order));
}); });
@ -149,14 +146,13 @@ function addPackageWebApi(
callback: function (err: Error, info: $SidebarPackage): void { callback: function (err: Error, info: $SidebarPackage): void {
if (_.isNil(err)) { if (_.isNil(err)) {
const { v } = req.query; const { v } = req.query;
let sideBarInfo: any = _.clone(info); let sideBarInfo = _.clone(info);
sideBarInfo.versions = convertDistRemoteToLocalTarballUrls( sideBarInfo.versions = convertDistRemoteToLocalTarballUrls(
info, info,
req, req,
config.url_prefix config.url_prefix
).versions; ).versions;
if (isVersionValid(info, v)) { if (typeof v === 'string' && isVersionValid(info, v)) {
// @ts-ignore
sideBarInfo.latest = sideBarInfo.versions[v]; sideBarInfo.latest = sideBarInfo.versions[v];
sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author); sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author);
} else { } else {
@ -164,12 +160,10 @@ function addPackageWebApi(
sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author); sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author);
} }
sideBarInfo = deleteProperties(['readme', '_attachments', '_rev', 'name'], sideBarInfo); sideBarInfo = deleteProperties(['readme', '_attachments', '_rev', 'name'], sideBarInfo);
if (config.web) { const authorAvatar = config.web
sideBarInfo = addGravatarSupport(sideBarInfo, config.web.gravatar); ? addGravatarSupport(sideBarInfo, config.web.gravatar)
} else { : addGravatarSupport(sideBarInfo);
sideBarInfo = addGravatarSupport(sideBarInfo); next(authorAvatar);
}
next(sideBarInfo);
} else { } else {
res.status(HTTP_STATUS.NOT_FOUND); res.status(HTTP_STATUS.NOT_FOUND);
res.end(); res.end();