From 1d111280511149319111fbf4e9b444ea7ff9a254 Mon Sep 17 00:00:00 2001 From: ike Date: Fri, 23 Oct 2020 14:44:32 +0800 Subject: [PATCH] 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 --- .changeset/fifty-jars-rest.md | 6 +++++ packages/middleware/src/middleware.ts | 4 +-- packages/web/src/endpoint/package.ts | 38 +++++++++++---------------- 3 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 .changeset/fifty-jars-rest.md diff --git a/.changeset/fifty-jars-rest.md b/.changeset/fifty-jars-rest.md new file mode 100644 index 000000000..a58cfc616 --- /dev/null +++ b/.changeset/fifty-jars-rest.md @@ -0,0 +1,6 @@ +--- +'@verdaccio/middleware': patch +'@verdaccio/web': patch +--- + +Remove @ts-ignore and any in packages/web/src/endpoint/package.ts diff --git a/packages/middleware/src/middleware.ts b/packages/middleware/src/middleware.ts index a2cb9d02b..8461dfd68 100644 --- a/packages/middleware/src/middleware.ts +++ b/packages/middleware/src/middleware.ts @@ -25,7 +25,7 @@ import { IAuth } from '@verdaccio/auth'; import { VerdaccioError } from '@verdaccio/commons-api'; 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 $NextFunctionVer = NextFunction & any; @@ -165,7 +165,7 @@ export function allow(auth: IAuth): Function { const packageVersion = req.params.filename ? getVersionFromTarball(req.params.filename) : undefined; - const remote: RemoteUser = req.remote_user; + const remote = req.remote_user; logger.trace( { action, user: remote?.name }, `[middleware/allow][@{action}] allow for @{user}` diff --git a/packages/web/src/endpoint/package.ts b/packages/web/src/endpoint/package.ts index 8b1d6f0dc..9b7b330ff 100644 --- a/packages/web/src/endpoint/package.ts +++ b/packages/web/src/endpoint/package.ts @@ -9,26 +9,24 @@ import { getLocalRegistryTarballUri, isVersionValid, } 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 { logger } from '@verdaccio/logger'; -import { NextFunction, Request, Response, Router } from 'express'; +import { Router } from 'express'; import { IAuth } from '@verdaccio/auth'; import { IStorageHandler } from '@verdaccio/store'; -import { Config, Logger, Package } from '@verdaccio/types'; -import { addGravatarSupport } from '../web-utils'; +import { Config, Package, RemoteUser, Version } from '@verdaccio/types'; +import { addGravatarSupport, AuthorAvatar } from '../web-utils'; import { generateGravatarUrl } from '../user'; -export type $SidebarPackage = Package & { latest: any }; -export type $RequestExtend = Request & { remote_user?: any; log: Logger }; -export type $ResponseExtend = Response & { cookies?: any }; -export type $NextFunctionVer = NextFunction & any; +export type $SidebarPackage = Package & { latest: Version }; +export { $RequestExtend, $ResponseExtend, $NextFunctionVer }; // Was required by other packages const getOrder = (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( route: Router, @@ -38,7 +36,7 @@ function addPackageWebApi( ): void { const can = allow(auth); - const checkAllow = (name, remoteUser): Promise => + const checkAllow = (name: string, remoteUser: RemoteUser): Promise => new Promise((resolve, reject): void => { try { auth.allow_access({ packageName: name }, remoteUser, (err, allowed): void => { @@ -63,7 +61,7 @@ function addPackageWebApi( throw err; } - async function processPackages(packages: PackcageExt[] = []): Promise { + async function processPackages(packages: PackcageExt[] = []): Promise { const permissions: PackcageExt[] = []; const packgesCopy = packages.slice(); for (const pkg of packgesCopy) { @@ -100,8 +98,7 @@ function addPackageWebApi( } const { web } = config; - // @ts-ignore - const order: boolean = config.web ? getOrder(web.sort_packages) : true; + const order = web ? getOrder(web.sort_packages) : true; next(sortByName(await processPackages(packages), order)); }); @@ -149,14 +146,13 @@ function addPackageWebApi( callback: function (err: Error, info: $SidebarPackage): void { if (_.isNil(err)) { const { v } = req.query; - let sideBarInfo: any = _.clone(info); + let sideBarInfo = _.clone(info); sideBarInfo.versions = convertDistRemoteToLocalTarballUrls( info, req, config.url_prefix ).versions; - if (isVersionValid(info, v)) { - // @ts-ignore + if (typeof v === 'string' && isVersionValid(info, v)) { sideBarInfo.latest = sideBarInfo.versions[v]; sideBarInfo.latest.author = formatAuthor(sideBarInfo.latest.author); } else { @@ -164,12 +160,10 @@ function addPackageWebApi( 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); + const authorAvatar = config.web + ? addGravatarSupport(sideBarInfo, config.web.gravatar) + : addGravatarSupport(sideBarInfo); + next(authorAvatar); } else { res.status(HTTP_STATUS.NOT_FOUND); res.end();