0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/packages/api/src/index.ts

73 lines
2.2 KiB
TypeScript
Raw Normal View History

import bodyParser from 'body-parser';
import express, { Router } from 'express';
import semver from 'semver';
import { IAuth } from '@verdaccio/auth';
import {
antiLoop,
encodeScopePackage,
match,
validateName,
validatePackage,
} from '@verdaccio/middleware';
import { Storage } from '@verdaccio/store';
2020-03-03 17:59:19 -05:00
import { Config } from '@verdaccio/types';
2018-03-10 15:09:04 -05:00
2020-03-03 17:59:19 -05:00
import distTags from './dist-tags';
import pkg from './package';
import ping from './ping';
2020-03-03 17:59:19 -05:00
import publish from './publish';
import search from './search';
import stars from './stars';
import user from './user';
2020-03-03 17:59:19 -05:00
import profile from './v1/profile';
import v1Search from './v1/search';
import token from './v1/token';
import whoami from './whoami';
2017-06-21 12:02:52 -05:00
if (semver.lte(process.version, 'v15.0.0')) {
global.AbortController = require('abortcontroller-polyfill/dist/cjs-ponyfill').AbortController;
}
export default function (config: Config, auth: IAuth, storage: Storage): Router {
2017-06-21 12:02:52 -05:00
/* eslint new-cap:off */
const app = express.Router();
/* eslint new-cap:off */
// validate all of these params as a package name
// this might be too harsh, so ask if it causes trouble
2018-03-10 17:13:26 -05:00
app.param('package', validatePackage);
app.param('filename', validateName);
app.param('tag', validateName);
app.param('version', validateName);
app.param('revision', validateName);
app.param('token', validateName);
2017-06-21 12:02:52 -05:00
// these can't be safely put into express url for some reason
// TODO: For some reason? what reason?
app.param('_rev', match(/^-rev$/));
app.param('org_couchdb_user', match(/^org\.couchdb\.user:/));
app.param('anything', match(/.*/));
2018-04-30 08:13:54 -05:00
app.use(auth.apiJWTmiddleware());
app.use(bodyParser.json({ strict: false, limit: config.max_body_size || '10mb' }));
2020-03-03 17:59:19 -05:00
// @ts-ignore
app.use(antiLoop(config));
2017-06-21 12:02:52 -05:00
// 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);
profile(app, auth);
// @deprecated endpoint, 404 by default
search(app);
user(app, auth, config);
2017-06-21 12:02:52 -05:00
distTags(app, auth, storage);
publish(app, auth, storage, config);
ping(app);
2019-03-11 10:37:17 -05:00
stars(app, storage);
// @ts-ignore
v1Search(app, auth, storage);
token(app, auth, storage, config);
2017-06-21 12:02:52 -05:00
return app;
2018-03-10 17:13:26 -05:00
}