test structure
This commit is contained in:
parent
3716e80674
commit
95814c5541
@ -2,4 +2,10 @@ import { Router } from 'express';
|
||||
|
||||
export class HttpHandler {
|
||||
public abstract register_handlers(router: Router): void;
|
||||
|
||||
public get_router (): Router {
|
||||
const r = Router ();
|
||||
this.register_handlers (r);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
import { Request, Response, Router } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import Knex from 'knex';
|
||||
import { KnexCrudOptions } from './KnexCrudOptions';
|
||||
import { CrudHandler } from './CrudHandler';
|
||||
import { Knex } from './KnexInterface';
|
||||
import { HttpHandler } from './HttpHandler';
|
||||
import { KnexCrudOptionsReader } from './KnexCrudOptionsReader';
|
||||
|
||||
export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
protected table: string;
|
||||
protected columns: Array<string>;
|
||||
protected options: KnexCrudOptions;
|
||||
protected options: KnexCrudOptionsReader;
|
||||
protected knex: Knex;
|
||||
|
||||
public constructor (
|
||||
@ -21,7 +22,7 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
this.knex = knex;
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.options = options;
|
||||
this.options = new KnexCrudOptionsReader (options);
|
||||
if (this.columns.filter ((val) => val.toLowerCase () === 'id').length > 0) {
|
||||
throw new Error (
|
||||
'the column id cannot be made available to modification'
|
||||
@ -32,7 +33,7 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
protected validate_body (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<object> | object {
|
||||
): Promise<Record<string, unknown>> | Record<string, unknown> {
|
||||
if (typeof req.body === 'undefined') {
|
||||
res.status (http.status_bad_request);
|
||||
res.end ('body was undefined');
|
||||
@ -49,10 +50,10 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
}
|
||||
|
||||
protected ensure_data (
|
||||
data: object,
|
||||
data: Record<string, unknown>,
|
||||
res: Response,
|
||||
fail_on_undef = true
|
||||
): Promise<object>|object {
|
||||
): Promise<Record<string, unknown>> | Record<string, unknown> {
|
||||
const obj = {};
|
||||
const keys = Object.keys (data);
|
||||
for (const col of this.columns) {
|
||||
@ -63,8 +64,10 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
|
||||
}
|
||||
obj[col] = data[col];
|
||||
}
|
||||
for (const col of this.options.optional_columns)
|
||||
obj[col] = data[col];
|
||||
if (typeof this.options.optional_columns !== 'undefined') {
|
||||
for (const col of this.options.optional_columns)
|
||||
obj[col] = data[col];
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -5,84 +5,13 @@ type Authorization = {
|
||||
(req: Request, res: Response, next: Function): unknown;
|
||||
}
|
||||
|
||||
type AuthRunner = {
|
||||
(req: Request, res: Response): Promise<boolean>;
|
||||
interface KnexCrudOptions {
|
||||
general_authorization?: Authorization;
|
||||
create_authorization?: Authorization;
|
||||
read_authorization?: Authorization;
|
||||
update_authorization?: Authorization;
|
||||
delete_authorization?: Authorization;
|
||||
optional_columns?: Array<string>;
|
||||
}
|
||||
|
||||
export class KnexCrudOptions {
|
||||
private _general_authorization?: Authorization;
|
||||
private _create_authorization?: Authorization;
|
||||
private _read_authorization?: Authorization;
|
||||
private _update_authorization?: Authorization;
|
||||
private _delete_authorization?: Authorization;
|
||||
|
||||
public optional_columns?: Array<string>;
|
||||
|
||||
private get_auth_runner (
|
||||
auth?: Authorization
|
||||
): AuthRunner {
|
||||
if (typeof auth === 'undefined')
|
||||
return (): Promise<boolean> => new Promise ((r) => r (true));
|
||||
return (req, res): Promise<boolean> => new Promise ((resolve) => {
|
||||
const result = auth (req, res, resolve);
|
||||
if (typeof result !== 'undefined')
|
||||
resolve (result as boolean);
|
||||
});
|
||||
}
|
||||
|
||||
public set general_authorization (value: Authorization) {
|
||||
this._general_authorization = value;
|
||||
}
|
||||
|
||||
public get create_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._create_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set create_authorization (value: Authorization) {
|
||||
this._create_authorization = value;
|
||||
}
|
||||
|
||||
public get read_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._read_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set read_authorization (value: Authorization) {
|
||||
this._read_authorization = value;
|
||||
}
|
||||
|
||||
public get update_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._update_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set update_authorization (value: Authorization) {
|
||||
this._update_authorization = value;
|
||||
}
|
||||
|
||||
public get delete_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this._general_authorization);
|
||||
const specific = this.get_auth_runner (this._delete_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public set delete_authorization (value: Authorization) {
|
||||
this._delete_authorization = value;
|
||||
}
|
||||
}
|
||||
export { Authorization, KnexCrudOptions };
|
||||
|
66
lib/KnexCrudOptionsReader.ts
Normal file
66
lib/KnexCrudOptionsReader.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { KnexCrudOptions, Authorization } from './KnexCrudOptions';
|
||||
|
||||
type AuthRunner = {
|
||||
(req: Request, res: Response): Promise<boolean>;
|
||||
}
|
||||
|
||||
export class KnexCrudOptionsReader {
|
||||
private options: KnexCrudOptions;
|
||||
|
||||
public constructor (options: KnexCrudOptions) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
private get_auth_runner (
|
||||
auth?: Authorization
|
||||
): AuthRunner {
|
||||
if (typeof auth === 'undefined')
|
||||
return (): Promise<boolean> => new Promise ((r) => r (true));
|
||||
return (req, res): Promise<boolean> => new Promise ((resolve) => {
|
||||
const result = auth (req, res, resolve);
|
||||
if (typeof result !== 'undefined')
|
||||
resolve (result as boolean);
|
||||
});
|
||||
}
|
||||
|
||||
public get optional_columns (): Array<string> | undefined {
|
||||
return this.options.optional_columns;
|
||||
}
|
||||
|
||||
public get create_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this.options.general_authorization);
|
||||
const specific = this.get_auth_runner (this.options.create_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public get read_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this.options.general_authorization);
|
||||
const specific = this.get_auth_runner (this.options.read_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public get update_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this.options.general_authorization);
|
||||
const specific = this.get_auth_runner (this.options.update_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
public get delete_authorization (): Authorization {
|
||||
const general = this.get_auth_runner (this.options.general_authorization);
|
||||
const specific = this.get_auth_runner (this.options.delete_authorization);
|
||||
return async (req: Request, res: Response): Promise<boolean> => {
|
||||
const result = (await general (req, res)) && (await specific (req, res));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
|
||||
interface KnexQuery {
|
||||
insert(data: object): Promise<Array<unknown>>;
|
||||
update(data: object): Promise<unknown>;
|
||||
select(...columns: Array<string>): Promise<Array<object>>;
|
||||
delete(): Promise<unknown>;
|
||||
where(filter: object): KnexQuery;
|
||||
whereNot(filter: object): KnexQuery;
|
||||
whereIn(filter: object): KnexQuery;
|
||||
whereNotIn(filter: object): KnexQuery;
|
||||
whereNull(filter: object): KnexQuery;
|
||||
whereNotNull(filter: object): KnexQuery;
|
||||
whereExists(filter: object): KnexQuery;
|
||||
whereNotExists(filter: object): KnexQuery;
|
||||
whereBetween(filter: object): KnexQuery;
|
||||
whereNotBetween(filter: object): KnexQuery;
|
||||
join(table: string, col: string, on: string): KnexQuery;
|
||||
leftJoin(table: string, col: string, on: string): KnexQuery;
|
||||
rightJoin(table: string, col: string, on: string): KnexQuery;
|
||||
leftOuterJoin(table: string, col: string, on: string): KnexQuery;
|
||||
rightOuterJoin(table: string, col: string, on: string): KnexQuery;
|
||||
fullOuterJoin(table: string, col: string, on: string): KnexQuery;
|
||||
returning(column: string | Array<string>): KnexQuery;
|
||||
}
|
||||
|
||||
type Knex = {
|
||||
(table: string): KnexQuery;
|
||||
}
|
||||
|
||||
export { Knex, KnexQuery };
|
14
package.json
14
package.json
@ -4,7 +4,8 @@
|
||||
"description": "Express handler templates",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"no test\"",
|
||||
"test": "nyc ava",
|
||||
"compile": "tsc",
|
||||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
||||
"ci": "yarn --frozen-lockfile && node jenkins.js"
|
||||
},
|
||||
@ -21,15 +22,20 @@
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@scode/eslint-config-ts": "^1.0.19",
|
||||
"@ava/typescript": "^1.1.1",
|
||||
"@scode/eslint-config-ts": "^1.0.22",
|
||||
"@stryker-mutator/core": "^3.1.0",
|
||||
"@stryker-mutator/javascript-mutator": "^3.1.0",
|
||||
"ava": "^3.7.1",
|
||||
"eslint": "^6.8.0",
|
||||
"nyc": "^15.0.1",
|
||||
"sqlite3": "^4.1.1",
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@scode/consts": "^1.1.4",
|
||||
"@scode/consts": "^1.1.5",
|
||||
"@types/express": "^4.17.6",
|
||||
"express": "^4.17.1"
|
||||
"express": "^4.17.1",
|
||||
"knex": "^0.21.0"
|
||||
}
|
||||
}
|
||||
|
20
test/.eslintrc.js
Normal file
20
test/.eslintrc.js
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||
* This file is part of Requestor which is released under BSD-3-Clause.
|
||||
* See file 'LICENSE' for full license details.
|
||||
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
env: {
|
||||
commonjs: true,
|
||||
es6: true,
|
||||
node: true
|
||||
},
|
||||
extends: [
|
||||
'@scode/eslint-config-ts'
|
||||
],
|
||||
rules: {
|
||||
'node/no-unpublished-import': 'off'
|
||||
}
|
||||
}
|
88
test/KnexCrudHandler.ts
Normal file
88
test/KnexCrudHandler.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import knex from 'knex';
|
||||
import test from 'ava';
|
||||
import express, { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import { KnexCrudHandler } from '../lib/KnexCrudHandler';
|
||||
|
||||
const db = knex ({
|
||||
client: 'sqlite',
|
||||
connection: { filename: './db.sqlite' }
|
||||
});
|
||||
|
||||
/**
|
||||
* general auth
|
||||
*
|
||||
* @param {any} req request
|
||||
* @param {any} res response
|
||||
* @returns {Promise<boolean>} successful response
|
||||
*/
|
||||
function general_auth (req: Request, res: Response): Promise<boolean> {
|
||||
return new Promise ((resolve) => {
|
||||
if (req.headers.auth === 'on') {
|
||||
resolve (true);
|
||||
}
|
||||
else {
|
||||
res.status (http.status_forbidden);
|
||||
res.end ('auth failed');
|
||||
resolve (false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* read auth
|
||||
*
|
||||
* @param {any} req request
|
||||
* @param {any} res response
|
||||
* @returns {Promise<boolean>} successful response
|
||||
*/
|
||||
function read_auth (req: Request, res: Response): Promise<boolean> {
|
||||
return new Promise ((resolve) => {
|
||||
if (req.headers.readauth === 'on') {
|
||||
resolve (true);
|
||||
}
|
||||
else {
|
||||
res.status (http.status_forbidden);
|
||||
res.end ('readauth failed');
|
||||
resolve (false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test.before (async () => {
|
||||
await db.schema.dropTableIfExists ('test');
|
||||
await db.schema.createTable ('test', (t) => {
|
||||
t.increments ('id');
|
||||
t.string ('name');
|
||||
t.integer ('number');
|
||||
});
|
||||
|
||||
const no_auth = new KnexCrudHandler (db, 'test', [
|
||||
'name',
|
||||
'number'
|
||||
], {});
|
||||
|
||||
const auth = new KnexCrudHandler (db, 'test', [ 'name' ], {
|
||||
optional_columns: [ 'number' ],
|
||||
general_authorization: general_auth,
|
||||
read_authorization: read_auth
|
||||
});
|
||||
|
||||
const app = express ();
|
||||
app.use ('/test', no_auth.get_router ());
|
||||
app.use ('/auth', auth.get_router ());
|
||||
// eslint-disable-next-line no-magic-numbers
|
||||
app.listen (3000);
|
||||
});
|
||||
|
||||
test ('insert data', (t) => {});
|
||||
test ('read data', (t) => {});
|
||||
test ('update data', (t) => {});
|
||||
test ('read updated data', (t) => {});
|
||||
|
||||
test ('[a] insert data', (t) => {});
|
||||
test ('[a] insert data without auth', (t) => {});
|
||||
test ('[a] insert data without optional column', (t) => {});
|
||||
test ('[a] read data', (t) => {});
|
||||
test ('[a] read data without auth', (t) => {});
|
||||
test ('[a] read data without readauth', (t) => {});
|
Reference in New Issue
Block a user