From 76fe6cb5d74859501f160c125166beec44432740 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Sat, 10 Mar 2018 21:09:04 +0100 Subject: [PATCH] refactor: apply flow and imports es6 --- src/api/endpoint/api/dist-tags.js | 6 ++---- src/api/endpoint/api/package.js | 4 ++-- src/api/endpoint/api/ping.js | 11 ++++++---- src/api/endpoint/api/publish.js | 6 ++---- src/api/endpoint/api/search.js | 6 ++---- src/api/endpoint/api/user.js | 4 ++-- src/api/endpoint/api/whoami.js | 4 ++-- src/api/endpoint/index.js | 35 ++++++++++++++----------------- test/unit/local-storage.spec.js | 1 + types/index.js | 1 + 10 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/api/endpoint/api/dist-tags.js b/src/api/endpoint/api/dist-tags.js index 363cbeba6..02b8c6ee3 100644 --- a/src/api/endpoint/api/dist-tags.js +++ b/src/api/endpoint/api/dist-tags.js @@ -1,12 +1,10 @@ -'use strict'; - const Middleware = require('../../web/middleware'); const mime = require('mime'); const _ = require('lodash'); const media = Middleware.media; -module.exports = function(route, auth, storage) { +export default function(route, auth, storage) { const can = Middleware.allow(auth); const tag_package_version = function(req, res, next) { if (_.isString(req.body) === false) { @@ -71,4 +69,4 @@ module.exports = function(route, auth, storage) { return next({ok: 'tags updated'}); }); }); -}; +} diff --git a/src/api/endpoint/api/package.js b/src/api/endpoint/api/package.js index 7a2cb3a95..d8e7e0bbb 100644 --- a/src/api/endpoint/api/package.js +++ b/src/api/endpoint/api/package.js @@ -4,7 +4,7 @@ const createError = require('http-errors'); const Middleware = require('../../web/middleware'); const Utils = require('../../../lib/utils'); -module.exports = function(route, auth, storage, config) { +export default function(route, auth, storage, config) { const can = Middleware.allow(auth); // TODO: anonymous user? route.get('/:package/:version?', can('access'), function(req, res, next) { @@ -56,4 +56,4 @@ module.exports = function(route, auth, storage, config) { res.header('Content-Type', 'application/octet-stream'); stream.pipe(res); }); -}; +} diff --git a/src/api/endpoint/api/ping.js b/src/api/endpoint/api/ping.js index b7f8efcbd..9fdee5d24 100644 --- a/src/api/endpoint/api/ping.js +++ b/src/api/endpoint/api/ping.js @@ -1,7 +1,10 @@ -'use strict'; +// @flow -module.exports = function(route) { - route.get('/-/ping', function(req, res, next) { +import type {Router} from 'express'; +import type {$RequestExtend, $ResponseExtend, $NextFunctionVer} from '../../../../types'; + +export default function(route: Router) { + route.get('/-/ping', function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) { next({}); }); -}; +} diff --git a/src/api/endpoint/api/publish.js b/src/api/endpoint/api/publish.js index 894e1adb6..84b6c7efb 100644 --- a/src/api/endpoint/api/publish.js +++ b/src/api/endpoint/api/publish.js @@ -1,5 +1,3 @@ -'use strict'; - const _ = require('lodash'); const Path = require('path'); const createError = require('http-errors'); @@ -13,7 +11,7 @@ const media = Middleware.media; const expect_json = Middleware.expect_json; const notify = Notify.notify; -module.exports = function(router, auth, storage, config) { +export default function(router, auth, storage, config) { const can = Middleware.allow(auth); // publishing a package @@ -185,4 +183,4 @@ module.exports = function(router, auth, storage, config) { }); }); }); -}; +} diff --git a/src/api/endpoint/api/search.js b/src/api/endpoint/api/search.js index eb6c18593..bf0030b47 100644 --- a/src/api/endpoint/api/search.js +++ b/src/api/endpoint/api/search.js @@ -1,6 +1,4 @@ -'use strict'; - -module.exports = function(route, auth, storage) { +export default function(route, auth, storage) { // searching packages route.get('/-/all(\/since)?', function(req, res) { let received_end = false; @@ -96,4 +94,4 @@ module.exports = function(route, auth, storage) { check_finish(); }); }); -}; +} diff --git a/src/api/endpoint/api/user.js b/src/api/endpoint/api/user.js index 25dfd917b..9859c60ce 100644 --- a/src/api/endpoint/api/user.js +++ b/src/api/endpoint/api/user.js @@ -7,7 +7,7 @@ import {ErrorCode} from '../../../lib/utils'; import _ from 'lodash'; import Cookies from 'cookies'; -module.exports = function(route: Router, auth: IAuth) { +export default function(route: Router, auth: IAuth) { route.get('/-/user/:org_couchdb_user', function(req: $RequestExtend, res: $Response, next: $NextFunctionVer) { res.status(200); next({ @@ -70,4 +70,4 @@ module.exports = function(route: Router, auth: IAuth) { roles: [], }); }); -}; +} diff --git a/src/api/endpoint/api/whoami.js b/src/api/endpoint/api/whoami.js index 5225a3074..1f1ba1efc 100644 --- a/src/api/endpoint/api/whoami.js +++ b/src/api/endpoint/api/whoami.js @@ -3,7 +3,7 @@ import type {$Response, Router} from 'express'; import type {$RequestExtend, $NextFunctionVer} from '../../../../types'; -module.exports = function(route: Router) { +export default function(route: Router) { route.get('/whoami', (req: $RequestExtend, res: $Response, next: $NextFunctionVer): void => { if (req.headers.referer === 'whoami') { next({username: req.remote_user.name}); @@ -15,4 +15,4 @@ module.exports = function(route: Router) { route.get('/-/whoami', (req: $RequestExtend, res: $Response, next: $NextFunctionVer): mixed => { next({username: req.remote_user.name}); }); -}; +} diff --git a/src/api/endpoint/index.js b/src/api/endpoint/index.js index 044e113cd..8d49f1329 100644 --- a/src/api/endpoint/index.js +++ b/src/api/endpoint/index.js @@ -1,20 +1,25 @@ -const express = require('express'); -const bodyParser = require('body-parser'); +// @flow + +import type {IAuth, IStorage} from '../../../types'; +import type {Config} from '@verdaccio/types'; + +import express from 'express'; +import bodyParser from 'body-parser'; +import whoami from './api/whoami'; +import ping from './api/ping'; +import user from './api/user'; +import distTags from './api/dist-tags'; +import publish from './api/publish'; +import search from './api/search'; +import pkg from './api/package'; + const Middleware = require('../web/middleware'); const match = Middleware.match; const validateName = Middleware.validate_name; const validatePkg = Middleware.validate_package; const encodeScopePackage = Middleware.encodeScopePackage; -const whoami = require('./api/whoami'); -const ping = require('./api/ping'); -const user = require('./api/user'); -const distTags = require('./api/dist-tags'); -const publish = require('./api/publish'); -const search = require('./api/search'); -const pkg = require('./api/package'); - -module.exports = function(config, auth, storage) { +module.exports = function(config: Config, auth: IAuth, storage: IStorage) { /* eslint new-cap:off */ const app = express.Router(); /* eslint new-cap:off */ @@ -38,23 +43,15 @@ module.exports = function(config, auth, storage) { // app.use(auth.bearer_middleware()) app.use(bodyParser.json({strict: false, limit: config.max_body_size || '10mb'})); app.use(Middleware.anti_loop(config)); - // encode / in a scoped package name to be matched as a single parameter in routes app.use(encodeScopePackage); - // for "npm whoami" whoami(app); - pkg(app, auth, storage, config); - search(app, auth, storage); - user(app, auth); - distTags(app, auth, storage); - publish(app, auth, storage, config); - ping(app); return app; diff --git a/test/unit/local-storage.spec.js b/test/unit/local-storage.spec.js index 8aea6d0ac..1b22ca5c7 100644 --- a/test/unit/local-storage.spec.js +++ b/test/unit/local-storage.spec.js @@ -1,4 +1,5 @@ // @flow + import rimRaf from 'rimraf'; import path from 'path'; import LocalStorage from '../../src/lib/local-storage'; diff --git a/types/index.js b/types/index.js index 06cdd750e..5769c8ddd 100644 --- a/types/index.js +++ b/types/index.js @@ -22,6 +22,7 @@ export interface IAuth { secret: string; plugins: Array; aes_encrypt(buf: Buffer): Buffer; + basic_middleware(): $NextFunctionVer; add_user(user: string, password: string, cb: Callback): any; }