1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-24 21:15:51 +01:00

add params validation for sidebar fastify web endpoints (#2655)

* sidebar fastify validation

Add params schema validation for sidebar endpoints

* reuse types

Co-authored-by: Juan Picado <juanpicado19@gmail.com>
This commit is contained in:
Diana Morales 2021-12-09 22:00:41 +01:00 committed by GitHub
parent 8e17d19c92
commit 2a86a46b85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,9 +14,22 @@ import {
const debug = buildDebug('verdaccio:web:api:sidebar');
export type $SidebarPackage = Package & { latest: Version };
const stringType = { type: 'string' };
const packageNameSchema = { packageName: stringType };
const paramsSchema = {
scope: stringType,
packageName: stringType,
};
async function sidebarRoute(fastify: FastifyInstance) {
fastify.get('/sidebar/(@:scope/)?:packageName', async (request, reply) => {
fastify.get(
'/sidebar/:scope/:packageName',
{
schema: {
params: paramsSchema,
},
},
async (request, reply) => {
// @ts-ignore
const { packageName, scope } = request.params;
debug('pkg name %s, scope %s ', packageName, scope);
@ -27,7 +40,29 @@ async function sidebarRoute(fastify: FastifyInstance) {
reply.code(fastify.statusCode.OK).send(sidebar);
}
});
}
);
fastify.get(
'/sidebar/:packageName',
{
schema: {
params: packageNameSchema,
},
},
async (request, reply) => {
// @ts-ignore
const { packageName, scope } = request.params;
debug('pkg name %s, scope %s ', packageName, scope);
getSidebar(fastify, request, packageName, (err, sidebar) => {
if (err) {
reply.send(err);
} else {
reply.code(fastify.statusCode.OK).send(sidebar);
}
});
}
);
}
function getSidebar(fastify: FastifyInstance, request: any, packageName, callback) {