Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raplaced validator with Deno module validator4oak #24

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@switcherapi/switcher-searchdocs",
"version": "1.0.1",
"version": "1.0.2",
"description": "A remote document search engine that uses Skimming for Deno",
"tasks": {
"run": "deno run --allow-net --allow-env --allow-read src/index.ts",
Expand All @@ -13,5 +13,6 @@
"lcov": "deno coverage coverage --lcov --output=coverage/report.lcov",
"clean": "rm -rf ./npm ./coverage",
"cover": "deno task clean && deno task test && deno task lcov && genhtml -o coverage/html coverage/report.lcov"
}
},
"lock": false
}
251 changes: 101 additions & 150 deletions deno.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.projectKey=switcherapi_switcher-searchdocs
sonar.projectName=switcher-searchdocs
sonar.organization=switcherapi
sonar.projectVersion=1.0.1
sonar.projectVersion=1.0.2

sonar.javascript.lcov.reportPaths=coverage/report.lcov

Expand Down
2 changes: 1 addition & 1 deletion src/api-docs/swagger-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
title: 'Switcher SearchDocs API',
version: 'v1.0.1',
version: 'v1.0.2',
description: 'SearchDocs API powered by Deno, Oak and Skimming Module .',
contact: {
name: 'Roger Floriano (petruki)',
Expand Down
7 changes: 4 additions & 3 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export { Application, Router } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export type { Middleware, Next } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export { Context } from 'https://deno.land/x/oak@v14.1.1/context.ts';
export { Application, Router } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export type { Middleware, Next } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export { Context } from 'https://deno.land/x/oak@14.2.0/context.ts';
export { load } from 'https://deno.land/std@0.218.2/dotenv/mod.ts';
export { bold, cyan, green } from 'https://deno.land/std@0.218.2/fmt/colors.ts';
export { Skimming } from 'https://deno.land/x/skimming@v1.0.12/mod.ts';
export type { Output } from 'https://deno.land/x/skimming@v1.0.12/mod.ts';
export { ValidatorFn, ValidatorMiddleware } from 'https://deno.land/x/validator4oak@v1.0.0/mod.ts';
102 changes: 0 additions & 102 deletions src/middleware/validator.ts

This file was deleted.

30 changes: 17 additions & 13 deletions src/routes/searchdocs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Context, Router } from '../deps.ts';
import { Context, Router, ValidatorFn, ValidatorMiddleware } from '../deps.ts';
import { toSearchDocsRequestDto, toSearchDocsResponseDto } from '../dto/mapper.ts';
import Validator from '../middleware/validator.ts';
import { getEnv, responseError, responseSuccess } from '../utils.ts';
import SearchDocsService from '../services/searchdocs.ts';
import { SearchDocsQueryParams } from '../dto/request.ts';
Expand All @@ -9,18 +8,23 @@ const router = new Router();
let service: SearchDocsService;

const { ignoreCase, previewLength, query, regex, skipCache, trimContent, url } = SearchDocsQueryParams;
const { checkParam, required, hasLenght, isUrl, isBoolean, isNumeric } = Validator;
const { query: checkQuery, useErrorHandler } = ValidatorMiddleware.createMiddleware();
const { hasLenght, isUrl, isBoolean, isNumeric } = ValidatorFn.createValidator();

useErrorHandler((context: Context, error: string) => {
return responseError(context, new Error(error), 422);
});

router.get(
'/',
checkParam([
{ key: query, validators: [required(), hasLenght({ max: 100 })] },
{ key: url, validators: [isUrl()] },
{ key: previewLength, validators: [isNumeric()] },
{ key: ignoreCase, validators: [isBoolean()] },
{ key: trimContent, validators: [isBoolean()] },
{ key: regex, validators: [isBoolean()] },
{ key: skipCache, validators: [isBoolean()] },
checkQuery([
{ key: query, validators: [hasLenght({ max: 100 })] },
{ key: url, validators: [isUrl()], optional: true },
{ key: previewLength, validators: [isNumeric()], optional: true },
{ key: ignoreCase, validators: [isBoolean()], optional: true },
{ key: trimContent, validators: [isBoolean()], optional: true },
{ key: regex, validators: [isBoolean()], optional: true },
{ key: skipCache, validators: [isBoolean()], optional: true },
]),
async (context: Context) => {
try {
Expand All @@ -33,14 +37,14 @@ router.get(
},
);

const getService = () => {
function getService() {
if (!service) {
const expireDuration = parseInt(getEnv('APP_CACHE_EXP_DURATION', '30'));
const size = parseInt(getEnv('APP_CACHE_SIZE', '100'));
service = new SearchDocsService({ expireDuration, size });
}

return service;
};
}

export default router;
4 changes: 2 additions & 2 deletions test/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { testing } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export type { Middleware } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export { testing } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export type { Middleware } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export { assert, assertEquals, assertFalse, assertObjectMatch } from 'https://deno.land/std@0.218.2/assert/mod.ts';
export { superoak } from 'https://deno.land/x/superoak@4.8.1/mod.ts';
export type { IResponse } from 'https://deno.land/x/superoak@4.8.1/mod.ts';
6 changes: 3 additions & 3 deletions test/routes/searchdocs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Deno.test({
const request = await superoak(app);
const response = await request.get(`/`)
.send()
.expect(400);
.expect(422);

assertEquals(response.body.error, 'Invalid query input. Cause: it is empty.');
assertEquals(response.body.error, 'Invalid query input. Cause: it is required.');
},
});

Expand All @@ -77,7 +77,7 @@ Deno.test({
.send()
.expect(422);

assertEquals(response.body.error, 'Invalid query input. Cause: it is greater than 100 characters.');
assertEquals(response.body.error, 'Invalid query input. Cause: it exceeds the maximum length of 100.');
},
});

Expand Down