From 00d6edbc54f08e40e5a94c6886f601a91c647c37 Mon Sep 17 00:00:00 2001 From: nvc8996 Date: Wed, 15 Sep 2021 01:13:26 +0300 Subject: [PATCH] redundant cleared + style fixed --- src/controllers/transport.ts | 5 ++--- src/exceptions/httpException.ts | 4 +++- src/models/alias.ts | 14 +++++--------- src/models/file.ts | 16 ++++++---------- src/models/page.ts | 21 --------------------- src/models/pageOrder.ts | 14 +++----------- src/routes/api/index.ts | 2 +- src/routes/api/pages.ts | 2 +- src/routes/api/transport.ts | 2 +- src/routes/auth.ts | 4 ++-- src/routes/index.ts | 3 +-- src/routes/middlewares/pages.ts | 2 +- src/utils/database/index.ts | 6 ------ src/utils/database/initDb.ts | 1 + test/database.ts | 5 ++--- test/express.ts | 3 ++- test/models/file.ts | 2 -- test/models/page.ts | 28 +++++++++++++++------------- test/models/pageOrder.ts | 7 +++---- test/rcparser.ts | 8 ++------ test/rest/aliases.ts | 6 +++--- test/rest/pages.ts | 15 +++++++-------- test/rest/transport.ts | 8 +++----- 23 files changed, 64 insertions(+), 114 deletions(-) diff --git a/src/controllers/transport.ts b/src/controllers/transport.ts index 4de4247..0b659fa 100644 --- a/src/controllers/transport.ts +++ b/src/controllers/transport.ts @@ -2,11 +2,10 @@ import fileType from 'file-type'; import fetch from 'node-fetch'; import fs from 'fs'; import nodePath from 'path'; - +import config from 'config'; import Model, { FileData } from '../models/file'; import crypto from '../utils/crypto'; import deepMerge from '../utils/objects'; -import config from 'config'; const random16 = crypto.random16; @@ -107,7 +106,7 @@ class Transport { const fields: string[] = path.split(':'); if (fields.length > 1) { - let object = {} as Dict; + let object: Dict = {}; const result = object; fields.forEach((field, i) => { diff --git a/src/exceptions/httpException.ts b/src/exceptions/httpException.ts index af0e6ba..46ff115 100644 --- a/src/exceptions/httpException.ts +++ b/src/exceptions/httpException.ts @@ -1,5 +1,7 @@ /** - * + * HttpException class for middleware + * @property {number} status - exception status code + * @property {string} message - detail about the exception */ class HttpException extends Error { public status: number; diff --git a/src/models/alias.ts b/src/models/alias.ts index 5e9866c..3a36232 100644 --- a/src/models/alias.ts +++ b/src/models/alias.ts @@ -32,11 +32,11 @@ interface AliasData { * @property {string} id - entity title */ class Alias { - public _id: string | undefined; - public hash: string | undefined; - public type: string | undefined; - public deprecated: boolean | undefined; - public id: string | undefined; + public _id?: string; + public hash?: string; + public type?: string; + public deprecated?: boolean; + public id?: string; /** * @class @@ -84,10 +84,6 @@ class Alias { data = await aliasesDb.findOne({ hash: hash }); } - if (data instanceof Error) { - return new Alias(); - } - return new Alias(data); } diff --git a/src/models/file.ts b/src/models/file.ts index dd099ea..50f4622 100644 --- a/src/models/file.ts +++ b/src/models/file.ts @@ -34,12 +34,12 @@ export interface FileData { * @property {number} size - size of the file in */ class File { - public _id: string | undefined; - public name: string | undefined; - public filename: string | undefined; - public path: string | undefined; - public mimetype: string | undefined; - public size: number | undefined; + public _id?: string; + public name?: string; + public filename?: string; + public path?: string; + public mimetype?: string; + public size?: number; /** * @class @@ -90,10 +90,6 @@ class File { public static async getAll(query: object = {}): Promise { const docs = await filesDb.find(query); - if (docs instanceof Error) { - return []; - } - return Promise.all(docs.map(doc => new File(doc))); } diff --git a/src/models/page.ts b/src/models/page.ts index 37f1538..4cfe193 100644 --- a/src/models/page.ts +++ b/src/models/page.ts @@ -17,7 +17,6 @@ export interface PageData { uri?: string; body?: any; parent?: string; - [key: string]: any; } /** @@ -63,10 +62,6 @@ class Page { public static async get(_id: string): Promise { const data = await pagesDb.findOne({ _id }); - if (data instanceof Error) { - return new Page(); - } - return new Page(data); } @@ -79,10 +74,6 @@ class Page { public static async getByUri(uri: string): Promise { const data = await pagesDb.findOne({ uri }); - if (data instanceof Error) { - return new Page(); - } - return new Page(data); } @@ -95,10 +86,6 @@ class Page { public static async getAll(query: object = {}): Promise { const docs = await pagesDb.find(query); - if (docs instanceof Error) { - return []; - } - return Promise.all(docs.map(doc => new Page(doc))); } @@ -148,10 +135,6 @@ class Page { public async getParent(): Promise { const data = await pagesDb.findOne({ _id: this._parent }); - if (data instanceof Error) { - return null; - } - return new Page(data); } @@ -163,10 +146,6 @@ class Page { public get children(): Promise { return pagesDb.find({ parent: this._id }) .then(data => { - if (data instanceof Error) { - return []; - } - return data.map(page => new Page(page)); }); } diff --git a/src/models/pageOrder.ts b/src/models/pageOrder.ts index d4b14f0..ceacce5 100644 --- a/src/models/pageOrder.ts +++ b/src/models/pageOrder.ts @@ -52,9 +52,9 @@ class PageOrder { public static async get(pageId: string): Promise { const order = await db.findOne({ page: pageId }); - let data = {} as PageOrderData; + let data: PageOrderData = {}; - if (order instanceof Error || order === null) { + if (order === null) { data.page = pageId; } else { data = order; @@ -72,10 +72,6 @@ class PageOrder { public static async getAll(query: object = {}): Promise { const docs = await db.find(query); - if (docs === null || docs instanceof Error) { - return []; - } - return Promise.all(docs.map(doc => new PageOrder(doc))); } @@ -228,9 +224,7 @@ class PageOrder { if (!this._id) { const insertedRow = await db.insert(this.data) as { _id: string}; - if (!(insertedRow instanceof Error)) { - this._id = insertedRow._id; - } + this._id = insertedRow._id; } else { await db.update({ _id: this._id }, this.data); } @@ -247,8 +241,6 @@ class PageOrder { await db.remove({ _id: this._id }); delete this._id; - - // return this; } } diff --git a/src/routes/api/index.ts b/src/routes/api/index.ts index 3b49704..be225d8 100644 --- a/src/routes/api/index.ts +++ b/src/routes/api/index.ts @@ -1,8 +1,8 @@ import express from 'express'; - import pagesAPI from './pages'; import transportAPI from './transport'; import linksAPI from './links'; + const router = express.Router(); router.use('/', pagesAPI); diff --git a/src/routes/api/pages.ts b/src/routes/api/pages.ts index 66e929c..e6b3995 100644 --- a/src/routes/api/pages.ts +++ b/src/routes/api/pages.ts @@ -1,7 +1,7 @@ import express, { Request, Response } from 'express'; +import multerFunc from 'multer'; import Pages from '../../controllers/pages'; import PagesOrder from '../../controllers/pagesOrder'; -import multerFunc from 'multer'; const router = express.Router(); const multer = multerFunc(); diff --git a/src/routes/api/transport.ts b/src/routes/api/transport.ts index d23ef0b..15219e0 100644 --- a/src/routes/api/transport.ts +++ b/src/routes/api/transport.ts @@ -2,9 +2,9 @@ import { Request, Response, Router } from 'express'; import multer, { StorageEngine } from 'multer'; import mime from 'mime'; import mkdirp from 'mkdirp'; +import config from 'config'; import Transport from '../../controllers/transport'; import { random16 } from '../../utils/crypto'; -import config from 'config'; const router = Router(); diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 51121d3..89c90b9 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -1,11 +1,11 @@ import express, { Request, Response } from 'express'; import jwt from 'jsonwebtoken'; -import Users from '../controllers/users'; import config from 'config'; import bcrypt from 'bcrypt'; import csrf from 'csurf'; - import * as dotenv from 'dotenv'; +import Users from '../controllers/users'; + dotenv.config(); const router = express.Router(); diff --git a/src/routes/index.ts b/src/routes/index.ts index 9cd12a5..2057c44 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,12 +1,11 @@ import express from 'express'; - import home from './home'; import pages from './pages'; import auth from './auth'; import aliases from './aliases'; import api from './api'; - import pagesMiddleware from './middlewares/pages'; + const router = express.Router(); router.use('/', pagesMiddleware, home); diff --git a/src/routes/middlewares/pages.ts b/src/routes/middlewares/pages.ts index 7f2ef0b..523b7cc 100644 --- a/src/routes/middlewares/pages.ts +++ b/src/routes/middlewares/pages.ts @@ -24,7 +24,7 @@ function createMenuTree(parentPageId: string, pages: Page[], pagesOrder: PageOrd * if we got some children order on parents tree, then we push found pages in order sequence * otherwise just find all pages includes parent tree */ - let ordered: any = []; + let ordered: any[] = []; if (childrenOrder) { ordered = childrenOrder.order.map((pageId: string) => { diff --git a/src/utils/database/index.ts b/src/utils/database/index.ts index 6c01a6d..414415d 100644 --- a/src/utils/database/index.ts +++ b/src/utils/database/index.ts @@ -1,8 +1,3 @@ -// import pages from './pages'; -// import files from './files'; -// import password from './password'; -// import aliases from './aliases'; -// import pagesOrder from './pagesOrder'; import Datastore from 'nedb'; import initDb from './initDb'; @@ -19,7 +14,6 @@ interface Options { returnUpdatedDocs?: boolean; } - /** * @class Database * @classdesc Simple decorator class to work with nedb datastore diff --git a/src/utils/database/initDb.ts b/src/utils/database/initDb.ts index c9ee43a..7475369 100644 --- a/src/utils/database/initDb.ts +++ b/src/utils/database/initDb.ts @@ -3,6 +3,7 @@ import config from 'config'; import path from 'path'; /** + * Init function for nedb instance * * @param {string} name - name of the data file * @returns {Datastore} db - nedb instance diff --git a/test/database.ts b/test/database.ts index 1bde47c..808ad2c 100644 --- a/test/database.ts +++ b/test/database.ts @@ -1,8 +1,8 @@ import fs from "fs"; import config from "config"; import { expect } from "chai"; - import Datastore from "nedb"; + import { Database } from "../src/utils/database"; interface Document { @@ -166,8 +166,7 @@ describe('Database', () => { } try { - // await db.update({field: {$undefinedComparator: 1}}); - throw new Error('Unknown comparison function $undefinedComparator'); + await db.update({field: {$undefinedComparator: 1}}); } catch (err) { expect(err.message).to.equal('Unknown comparison function $undefinedComparator'); } diff --git a/test/express.ts b/test/express.ts index 71ac25a..6851420 100644 --- a/test/express.ts +++ b/test/express.ts @@ -1,7 +1,8 @@ -import server from "../bin/server"; import chaiHTTP from "chai-http"; import chai, { expect } from "chai"; +import server from "../bin/server"; + const app = server.app; chai.use(chaiHTTP); diff --git a/test/models/file.ts b/test/models/file.ts index 4a86767..320f3a6 100644 --- a/test/models/file.ts +++ b/test/models/file.ts @@ -53,8 +53,6 @@ describe('File model', () => { file = new File(initialData); - // const json = file.toJSON(); - data = file.data; expect(data._id).to.equal(initialData._id); diff --git a/test/models/page.ts b/test/models/page.ts index b3e72ea..71cf48d 100644 --- a/test/models/page.ts +++ b/test/models/page.ts @@ -224,17 +224,21 @@ describe('Page model', () => { const savedPage = await page.save(); - if (savedPage._id !== undefined) { - const foundPage = await Page.get(savedPage._id); - - const {data} = foundPage; - - expect(data._id).to.equal(savedPage._id); - expect(data.title).to.equal(initialData.body.blocks[0].data.text); - expect(data.uri).to.equal(transformToUri(initialData.body.blocks[0].data.text)); - expect(data.body).to.deep.equal(initialData.body); + if (savedPage._id == undefined) { + await page.destroy(); + + return; } + const foundPage = await Page.get(savedPage._id); + + const {data} = foundPage; + + expect(data._id).to.equal(savedPage._id); + expect(data.title).to.equal(initialData.body.blocks[0].data.text); + expect(data.uri).to.equal(transformToUri(initialData.body.blocks[0].data.text)); + expect(data.body).to.deep.equal(initialData.body); + await page.destroy(); }); @@ -311,8 +315,6 @@ describe('Page model', () => { } ); - // child.parent = parent; - const {_id: childId} = await child.save(); const testedParent = await child.getParent(); @@ -329,7 +331,8 @@ describe('Page model', () => { expect(children.length).to.equal(1); - const testedChild = children.pop() as Page; + const temp: Page|undefined = children.pop(); + const testedChild: Page = !temp ? new Page({}) : temp; expect(testedChild._id).to.equal(childId); expect(testedChild.title).to.equal(child.body.blocks[0].data.text); @@ -380,7 +383,6 @@ describe('Page model', () => { } order.forEach(id => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars deleteRecursively(id); }); diff --git a/test/models/pageOrder.ts b/test/models/pageOrder.ts index 9e69529..d7e436b 100644 --- a/test/models/pageOrder.ts +++ b/test/models/pageOrder.ts @@ -110,10 +110,9 @@ describe('PageOrder model', () => { pageOrder.remove('2'); expect(pageOrder.data.order).to.deep.equals(['1', '3']); - // Not allowed by TypeScript - // expect(() => { - // pageOrder.push(3); - // }).to.throw('given id is not string'); + expect(() => { + pageOrder.push(3); + }).to.throw('given id is not string'); pageOrder.push('4'); pageOrder.push('5'); diff --git a/test/rcparser.ts b/test/rcparser.ts index ea58355..6686b3b 100644 --- a/test/rcparser.ts +++ b/test/rcparser.ts @@ -1,18 +1,14 @@ import { expect } from "chai"; - import fs from "fs"; import path from "path"; import config from "config"; -import rcParser from "../src/utils/rcparser"; import sinon = require('sinon'); +import rcParser from "../src/utils/rcparser"; + const rcPath = path.resolve(process.cwd(), config.get('rcFile')); describe('RC file parser test', () => { - beforeEach(function () { - // spy = sinon.stub(console, 'log'); - }); - afterEach(() => { if (fs.existsSync(rcPath)) { fs.unlinkSync(rcPath); diff --git a/test/rest/aliases.ts b/test/rest/aliases.ts index 00fb585..fe9ec3e 100644 --- a/test/rest/aliases.ts +++ b/test/rest/aliases.ts @@ -1,12 +1,12 @@ -import server from '../../bin/server'; -const app = server.app; - import fs from 'fs'; import path from 'path'; import config from 'config'; import chai from 'chai'; import chaiHTTP from 'chai-http'; +import server from '../../bin/server'; + const {expect} = chai; +const app = server.app; chai.use(chaiHTTP); diff --git a/test/rest/pages.ts b/test/rest/pages.ts index 336408d..0e7a8f3 100644 --- a/test/rest/pages.ts +++ b/test/rest/pages.ts @@ -1,17 +1,16 @@ -import server from '../../bin/server'; -const app = server.app; - -import model from '../../src/models/page'; -import Page from '../../src/models/page'; -import PageOrder from '../../src/models/pageOrder'; -import translateString from '../../src/utils/translation'; - import fs from 'fs'; import path from 'path'; import config from 'config'; import chai from 'chai'; import chaiHTTP from 'chai-http'; +import server from '../../bin/server'; +import model from '../../src/models/page'; +import Page from '../../src/models/page'; +import PageOrder from '../../src/models/pageOrder'; +import translateString from '../../src/utils/translation'; + const {expect} = chai; +const app = server.app; chai.use(chaiHTTP); diff --git a/test/rest/transport.ts b/test/rest/transport.ts index 0d62f0e..eeef7d5 100644 --- a/test/rest/transport.ts +++ b/test/rest/transport.ts @@ -4,14 +4,12 @@ import fileType from 'file-type'; import chai from 'chai'; import chaiHTTP from 'chai-http'; import rimraf from 'rimraf'; -const {expect} = chai; - +import config from 'config'; import server from '../../bin/server'; -const app = server.app; - import model from '../../src/models/file'; -import config from 'config'; +const {expect} = chai; +const app = server.app; chai.use(chaiHTTP);