Skip to content

Commit

Permalink
Centralize isURLValidAndAllowed and isSlugValidAndAllowed
Browse files Browse the repository at this point in the history
  • Loading branch information
ramesaliyev committed Jul 23, 2024
1 parent 418f617 commit cfe52aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
16 changes: 16 additions & 0 deletions src/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {isURLValidAndAllowed, isSlugValidAndAllowed} from '../lib/verify';

import AppRequest from './request';
import AppResponse from './response';
import AppLogger from './logger';
Expand Down Expand Up @@ -60,4 +62,18 @@ export default class App {
isDebugMode() {
return this.env.DEBUG_MODE;
}

isURLValidAndAllowed(url:string): boolean {
return isURLValidAndAllowed(url,
this.env.CFG_MIN_URL_LENGTH,
this.env.CFG_MAX_URL_LENGTH
);
}

isSlugValidAndAllowed(slug:string): boolean {
return isSlugValidAndAllowed(slug,
this.env.CFG_MIN_SLUG_LENGTH,
this.env.CFG_MAX_SLUG_LENGTH
);
}
}
20 changes: 5 additions & 15 deletions src/app/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {isSlugValidAndAllowed, isURLValidAndAllowed} from '../lib/verify';
import {generateRandomBase62} from '../lib/base62';

import App from './app';
Expand All @@ -17,23 +16,17 @@ export default class AppModel {
* Insert link if slug and URL are valid.
*/
async createLinkEntry(url:string, slug:string|null = null): Promise<string> {
const {err, env} = this.app;
const errCodes = err.code;
const errCodes = this.app.err.code;

// Check if URL is valid.
const minURLLen = env.CFG_MIN_URL_LENGTH;
const maxURLLen = env.CFG_MAX_URL_LENGTH;
if (!isURLValidAndAllowed(url, minURLLen, maxURLLen)) {
if (!this.app.isURLValidAndAllowed(url)) {
throw errCodes.BAD_URL;
}

// If slug is provided.
if (slug) {
const minSlugLen = env.CFG_MIN_SLUG_LENGTH;
const maxSlugLen = env.CFG_MAX_SLUG_LENGTH;

// Check if slug is valid.
if (!isSlugValidAndAllowed(slug, minSlugLen, maxSlugLen)) {
if (!this.app.isSlugValidAndAllowed(slug)) {
throw errCodes.BAD_SLUG;
}

Expand Down Expand Up @@ -63,16 +56,13 @@ export default class AppModel {
* Helper function to generate a random available slug.
*/
async generateAvailableRandomSlug(): Promise<string> {
const {env} = this.app;
const randomSlugLen = env.CFG_RANDOM_SLUG_LENGTH;
const minSlugLen = env.CFG_MIN_SLUG_LENGTH;
const maxSlugLen = env.CFG_MAX_SLUG_LENGTH;
const randomSlugLen = this.app.env.CFG_RANDOM_SLUG_LENGTH;

// Generate random slug.
const slug = generateRandomBase62(randomSlugLen);

// Verify slug.
if (!isSlugValidAndAllowed(slug, minSlugLen, maxSlugLen)) {
if (!this.app.isSlugValidAndAllowed(slug)) {
return this.generateAvailableRandomSlug();
}

Expand Down
8 changes: 1 addition & 7 deletions src/app/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {URL2} from '../lib/url';
import {isSlugValidAndAllowed} from '../lib/verify';

import App from './app';

interface Parsed {
Expand Down Expand Up @@ -79,10 +77,6 @@ export default class AppRequest {

// Parse if not parsed yet.
if (parsedSlug === false) {
const {env} = this.app;
const minSlugLen = env.CFG_MIN_SLUG_LENGTH;
const maxSlugLen = env.CFG_MAX_SLUG_LENGTH;

parsedSlug = null;

try {
Expand All @@ -91,7 +85,7 @@ export default class AppRequest {
if (pathSegments.length === 1) {
const slug = pathSegments[0];

if (isSlugValidAndAllowed(slug, minSlugLen, maxSlugLen)) {
if (this.app.isSlugValidAndAllowed(slug)) {
parsedSlug = slug;
}
}
Expand Down

0 comments on commit cfe52aa

Please sign in to comment.