Compare commits
46 Commits
fccd69db74
...
master
Author | SHA1 | Date | |
---|---|---|---|
5d9f4754a5 | |||
2cac260954 | |||
d298b8e842 | |||
bd63607477 | |||
55d461fb8e | |||
895893e76a | |||
39fbc73c74 | |||
e1c26e2c5e | |||
d3a93108e2 | |||
7a79aca59b | |||
ffb086a774 | |||
d1499bb6d4 | |||
a6d4a35588 | |||
2e4bb21354 | |||
3ed5253fc4 | |||
274f2b4b7a | |||
6eae1742d6 | |||
cc59df51d0 | |||
2a950310be | |||
9b4ad340a2 | |||
dfc0bc3b7e | |||
c2ea65a992 | |||
3de5485650 | |||
dfd3df544c | |||
7fe6dee81c | |||
1d7791c2b0 | |||
3c8dcf3ad2 | |||
ab70c46501 | |||
3af8b97258 | |||
431e21806b | |||
1541018701 | |||
0eea261e56 | |||
00546001eb | |||
db3ae308e6 | |||
20394e65f8 | |||
7b9ce83c71 | |||
27cec2cb0b | |||
a208a2fb50 | |||
52597ca46f | |||
efa44f0405 | |||
1a98e86954 | |||
ce4e78e0d5 | |||
bda39f279f | |||
111df45892 | |||
bff13c3364 | |||
6f242fb79f |
@ -2,7 +2,7 @@
|
|||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
* This file is part of Requestor which is released under BSD-3-Clause.
|
* This file is part of Requestor which is released under BSD-3-Clause.
|
||||||
* See file 'LICENSE' for full license details.
|
* See file 'LICENSE' for full license details.
|
||||||
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
* Created by Timo Hocker <timo@sapphirecode.ovh>, March 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -12,6 +12,6 @@ module.exports = {
|
|||||||
node: true
|
node: true
|
||||||
},
|
},
|
||||||
extends: [
|
extends: [
|
||||||
'@scode'
|
'@sapphirecode'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
2
Jenkinsfile
vendored
2
Jenkinsfile
vendored
@ -5,7 +5,7 @@ pipeline {
|
|||||||
VERSION = VersionNumber([
|
VERSION = VersionNumber([
|
||||||
versionNumberString:
|
versionNumberString:
|
||||||
'${BUILDS_ALL_TIME}',
|
'${BUILDS_ALL_TIME}',
|
||||||
versionPrefix: '1.1.',
|
versionPrefix: '2.0.',
|
||||||
worstResultForIncrement: 'SUCCESS'
|
worstResultForIncrement: 'SUCCESS'
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
41
jenkins.js
41
jenkins.js
@ -1,33 +1,22 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* eslint-disable no-process-exit */
|
|
||||||
/* eslint-disable no-console */
|
|
||||||
/* eslint-disable no-sync */
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const https = require ('https');
|
||||||
const fs = require ('fs');
|
const fs = require ('fs');
|
||||||
const child_process = require ('child_process');
|
const { execSync: exec_sync } = require ('child_process');
|
||||||
|
|
||||||
const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8'));
|
const run_file = fs.createWriteStream ('.jenkins.run.js');
|
||||||
[
|
|
||||||
,, pkg.version
|
const [
|
||||||
|
,, ...args
|
||||||
] = process.argv;
|
] = process.argv;
|
||||||
fs.writeFileSync ('package.json', JSON.stringify (pkg, null, 2));
|
|
||||||
|
|
||||||
child_process.execSync ('yarn lint', { stdio: 'inherit' });
|
run_file.on ('close', () => {
|
||||||
if (typeof pkg.scripts !== 'undefined' && typeof pkg.scripts.test === 'string')
|
exec_sync (`node .jenkins.run.js ${args.join (' ')}`, { stdio: 'inherit' });
|
||||||
child_process.execSync ('yarn test', { stdio: 'inherit' });
|
});
|
||||||
|
|
||||||
child_process.exec ('git log -1 | grep \'\\[no publish\\]\'')
|
https.get (
|
||||||
.addListener ('exit', (code) => {
|
'https://git.scode.ovh/Timo/standard/raw/branch/master/jenkins.run.js',
|
||||||
if (code === 0) {
|
(msg) => {
|
||||||
console.log ('build not marked for deployment');
|
msg.pipe (run_file);
|
||||||
process.exit (1);
|
}
|
||||||
}
|
);
|
||||||
else { child_process.execSync ('yarn publish'); }
|
|
||||||
});
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* Copyright (C) Sapphirecode - All Rights Reserved
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
||||||
* This file is part of Requestor which is released under BSD-3-Clause.
|
* This file is part of Requestor which is released under BSD-3-Clause.
|
||||||
* See file 'LICENSE' for full license details.
|
* See file 'LICENSE' for full license details.
|
||||||
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
* Created by Timo Hocker <timo@sapphirecode.ovh>, March 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -12,6 +12,6 @@ module.exports = {
|
|||||||
node: true
|
node: true
|
||||||
},
|
},
|
||||||
extends: [
|
extends: [
|
||||||
'@scode/eslint-config-ts'
|
'@sapphirecode/eslint-config-ts'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
|
|
||||||
export interface CrudHandler {
|
export interface CrudHandler {
|
||||||
public create(req: Request, res: Response): Promise<void>;
|
create(req: Request, res: Response): Promise<void>;
|
||||||
public read(req: Request, res: Response): Promise<void>;
|
read(req: Request, res: Response): Promise<void>;
|
||||||
public update(req: Request, res: Response): Promise<void>;
|
update(req: Request, res: Response): Promise<void>;
|
||||||
public delete(req: Request, res: Response): Promise<void>;
|
delete(req: Request, res: Response): Promise<void>;
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,33 @@
|
|||||||
import { Request, Response, Router } from 'express';
|
import { Request, Response, Router } from 'express';
|
||||||
import { http } from '@scode/consts';
|
import { http } from '@sapphirecode/consts';
|
||||||
import { DatabaseCrudOptions } from './DatabaseCrudOptions';
|
import { ControlModel, DatabaseModel } from '@sapphirecode/modelling';
|
||||||
import { CrudHandler } from './CrudHandler';
|
import { CrudHandler } from './CrudHandler';
|
||||||
import { HttpHandler } from './HttpHandler';
|
import { HttpHandler } from './HttpHandler';
|
||||||
import { DatabaseCrudOptionsReader } from './DatabaseCrudOptionsReader';
|
import { DatabaseCrudOptionsReader } from './DatabaseCrudOptionsReader';
|
||||||
|
import { DatabaseCrudOptions } from './DatabaseCrudOptions';
|
||||||
|
|
||||||
export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
||||||
protected table: string;
|
protected cm:
|
||||||
protected columns: Array<string>;
|
new (object: Record<string, string|number|boolean>) => ControlModel;
|
||||||
|
|
||||||
|
protected dm: new (id?: number) => DatabaseModel;
|
||||||
protected options: DatabaseCrudOptionsReader;
|
protected options: DatabaseCrudOptionsReader;
|
||||||
|
|
||||||
public constructor (
|
public constructor (
|
||||||
table: string,
|
cm: new (object: Record<string, string|number|boolean>) => ControlModel,
|
||||||
columns: Array<string>,
|
dm: new (id?: number) => DatabaseModel,
|
||||||
options: DatabaseCrudOptions = {}
|
options: DatabaseCrudOptions = {}
|
||||||
) {
|
) {
|
||||||
super ();
|
super ();
|
||||||
this.table = table;
|
this.cm = cm;
|
||||||
this.columns = columns;
|
this.dm = dm;
|
||||||
this.options = new DatabaseCrudOptionsReader (options);
|
this.options = new DatabaseCrudOptionsReader (options);
|
||||||
if (this.columns.filter ((val) => val.toLowerCase () === 'id').length > 0) {
|
|
||||||
throw new Error (
|
|
||||||
'the column id cannot be made available to modification'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected validate_body (
|
protected validate_body (
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<Record<string, unknown>> | Record<string, unknown> {
|
): Promise<Record<string, unknown> | null> | Record<string, unknown> | null {
|
||||||
if (typeof req.body === 'undefined') {
|
if (typeof req.body === 'undefined') {
|
||||||
res.status (http.status_bad_request);
|
res.status (http.status_bad_request);
|
||||||
res.end ('body was undefined');
|
res.end ('body was undefined');
|
||||||
@ -45,29 +43,6 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ensure_data (
|
|
||||||
data: Record<string, unknown>,
|
|
||||||
res: Response,
|
|
||||||
fail_on_undef = true
|
|
||||||
): Promise<Record<string, unknown>> | Record<string, unknown> {
|
|
||||||
const obj = {};
|
|
||||||
const keys = Object.keys (data);
|
|
||||||
for (const col of this.columns) {
|
|
||||||
if (!keys.includes (col) && fail_on_undef) {
|
|
||||||
res.status (http.status_bad_request)
|
|
||||||
.end (`missing field: ${col}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async create (req: Request, res: Response): Promise<void> {
|
public async create (req: Request, res: Response): Promise<void> {
|
||||||
if (!await this.options.create_authorization (req, res))
|
if (!await this.options.create_authorization (req, res))
|
||||||
return;
|
return;
|
||||||
@ -76,16 +51,15 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
|||||||
if (body_data === null)
|
if (body_data === null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const db_data = await this.ensure_data (body_data, res);
|
const cm = new this.cm (body_data as Record<string|string, number|boolean>);
|
||||||
if (db_data === null)
|
cm.update ();
|
||||||
return;
|
|
||||||
|
|
||||||
const inserted = await this.knex (this.table)
|
const dm = new this.dm;
|
||||||
.returning ('id')
|
dm.assign (cm);
|
||||||
.insert (db_data);
|
await dm.write ();
|
||||||
|
|
||||||
res.status (http.status_created)
|
res.status (http.status_created)
|
||||||
.end (inserted[0]);
|
.end (dm.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async read (req: Request, res: Response): Promise<void> {
|
public async read (req: Request, res: Response): Promise<void> {
|
||||||
@ -98,14 +72,14 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = await this.knex (this.table)
|
const dm = new this.dm (parseInt (req.headers.id as string));
|
||||||
.where ({ id: req.headers.id })
|
const found = await dm.read ();
|
||||||
.select (
|
|
||||||
'id',
|
const cm = new this.cm (dm.get_data ());
|
||||||
...this.columns
|
cm.update ();
|
||||||
);
|
|
||||||
res.status (json.length > 0 ? http.status_ok : http.status_not_found)
|
res.status (found ? http.status_ok : http.status_not_found)
|
||||||
.json (json[0]);
|
.json (cm.get_data ());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async update (req: Request, res: Response): Promise<void> {
|
public async update (req: Request, res: Response): Promise<void> {
|
||||||
@ -116,21 +90,31 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
|||||||
if (body_data === null)
|
if (body_data === null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const db_data = await this.ensure_data (body_data, res, false);
|
|
||||||
if (db_data === null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (typeof req.headers.id === 'undefined') {
|
if (typeof req.headers.id === 'undefined') {
|
||||||
res.status (http.status_bad_request)
|
res.status (http.status_bad_request)
|
||||||
.end ('id undefined');
|
.end ('id undefined');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.knex (this.table)
|
const dm = new this.dm (parseInt (req.headers.id as string));
|
||||||
.where ({ id: req.headers.id })
|
const found = await dm.read ();
|
||||||
.update (db_data);
|
if (!found) {
|
||||||
|
res.status (http.status_not_found)
|
||||||
|
.end ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.status (http.status_ok)
|
const cm = new this.cm (dm.get_data ());
|
||||||
|
cm.update ();
|
||||||
|
|
||||||
|
cm.assign_object (body_data);
|
||||||
|
cm.update ();
|
||||||
|
|
||||||
|
dm.assign (cm);
|
||||||
|
|
||||||
|
const written = await dm.write ();
|
||||||
|
|
||||||
|
res.status (written ? http.status_ok : http.status_internal_server_error)
|
||||||
.end ();
|
.end ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,11 +128,17 @@ export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.knex (this.table)
|
const dm = new this.dm (parseInt (req.headers.id as string));
|
||||||
.where ({ id: req.headers.id })
|
const found = await dm.read ();
|
||||||
.delete ();
|
if (!found) {
|
||||||
|
res.status (http.status_not_found)
|
||||||
|
.end ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.status (http.status_ok)
|
const deleted = await dm.delete ();
|
||||||
|
|
||||||
|
res.status (deleted ? http.status_ok : http.status_internal_server_error)
|
||||||
.end ();
|
.end ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import ControlModel from '@scode/modelling';
|
|
||||||
|
|
||||||
type Authorization = {
|
type Authorization = {
|
||||||
(req: Request, res: Response): Promise<boolean>;
|
(req: Request, res: Response): Promise<boolean>;
|
||||||
@ -13,7 +12,6 @@ interface DatabaseCrudOptions {
|
|||||||
update_authorization?: Authorization;
|
update_authorization?: Authorization;
|
||||||
delete_authorization?: Authorization;
|
delete_authorization?: Authorization;
|
||||||
optional_columns?: Array<string>;
|
optional_columns?: Array<string>;
|
||||||
control_model?: Type<ControlModel>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Authorization, DatabaseCrudOptions };
|
export { Authorization, DatabaseCrudOptions };
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { KnexCrudOptions, Authorization } from './DatabaseCrudOptions';
|
import { DatabaseCrudOptions, Authorization } from './DatabaseCrudOptions';
|
||||||
|
|
||||||
type AuthRunner = {
|
type AuthRunner = {
|
||||||
(req: Request, res: Response): Promise<boolean>;
|
(req: Request, res: Response): Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class KnexCrudOptionsReader {
|
export class DatabaseCrudOptionsReader {
|
||||||
private options: KnexCrudOptions;
|
private _options: DatabaseCrudOptions;
|
||||||
|
|
||||||
public constructor (options: KnexCrudOptions) {
|
public constructor (options: DatabaseCrudOptions) {
|
||||||
this.options = options;
|
this._options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
private get_auth_runner (
|
private get_auth_runner (
|
||||||
@ -17,20 +17,28 @@ export class KnexCrudOptionsReader {
|
|||||||
): AuthRunner {
|
): AuthRunner {
|
||||||
if (typeof auth === 'undefined')
|
if (typeof auth === 'undefined')
|
||||||
return (): Promise<boolean> => new Promise ((r) => r (true));
|
return (): Promise<boolean> => new Promise ((r) => r (true));
|
||||||
return (req, res): Promise<boolean> => new Promise ((resolve) => {
|
return (req, res): Promise<boolean> => new Promise (
|
||||||
const result = auth (req, res, resolve);
|
(resolve: (value: boolean) => void) => {
|
||||||
if (typeof result !== 'undefined')
|
(async (): Promise<void> => {
|
||||||
resolve (result as boolean);
|
let resolved = false;
|
||||||
});
|
const result = await auth (req, res, (cb: unknown) => {
|
||||||
|
resolved = true;
|
||||||
|
resolve (typeof cb === 'undefined' || cb === true);
|
||||||
|
});
|
||||||
|
if (!resolved)
|
||||||
|
resolve (result === true);
|
||||||
|
}) ();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get optional_columns (): Array<string> | undefined {
|
public get optional_columns (): Array<string> | undefined {
|
||||||
return this.options.optional_columns;
|
return this._options.optional_columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get create_authorization (): Authorization {
|
public get create_authorization (): Authorization {
|
||||||
const general = this.get_auth_runner (this.options.general_authorization);
|
const general = this.get_auth_runner (this._options.general_authorization);
|
||||||
const specific = this.get_auth_runner (this.options.create_authorization);
|
const specific = this.get_auth_runner (this._options.create_authorization);
|
||||||
return async (req: Request, res: Response): Promise<boolean> => {
|
return async (req: Request, res: Response): Promise<boolean> => {
|
||||||
const result = (await general (req, res)) && (await specific (req, res));
|
const result = (await general (req, res)) && (await specific (req, res));
|
||||||
return result;
|
return result;
|
||||||
@ -38,8 +46,8 @@ export class KnexCrudOptionsReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get read_authorization (): Authorization {
|
public get read_authorization (): Authorization {
|
||||||
const general = this.get_auth_runner (this.options.general_authorization);
|
const general = this.get_auth_runner (this._options.general_authorization);
|
||||||
const specific = this.get_auth_runner (this.options.read_authorization);
|
const specific = this.get_auth_runner (this._options.read_authorization);
|
||||||
return async (req: Request, res: Response): Promise<boolean> => {
|
return async (req: Request, res: Response): Promise<boolean> => {
|
||||||
const result = (await general (req, res)) && (await specific (req, res));
|
const result = (await general (req, res)) && (await specific (req, res));
|
||||||
return result;
|
return result;
|
||||||
@ -47,8 +55,8 @@ export class KnexCrudOptionsReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get update_authorization (): Authorization {
|
public get update_authorization (): Authorization {
|
||||||
const general = this.get_auth_runner (this.options.general_authorization);
|
const general = this.get_auth_runner (this._options.general_authorization);
|
||||||
const specific = this.get_auth_runner (this.options.update_authorization);
|
const specific = this.get_auth_runner (this._options.update_authorization);
|
||||||
return async (req: Request, res: Response): Promise<boolean> => {
|
return async (req: Request, res: Response): Promise<boolean> => {
|
||||||
const result = (await general (req, res)) && (await specific (req, res));
|
const result = (await general (req, res)) && (await specific (req, res));
|
||||||
return result;
|
return result;
|
||||||
@ -56,8 +64,8 @@ export class KnexCrudOptionsReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get delete_authorization (): Authorization {
|
public get delete_authorization (): Authorization {
|
||||||
const general = this.get_auth_runner (this.options.general_authorization);
|
const general = this.get_auth_runner (this._options.general_authorization);
|
||||||
const specific = this.get_auth_runner (this.options.delete_authorization);
|
const specific = this.get_auth_runner (this._options.delete_authorization);
|
||||||
return async (req: Request, res: Response): Promise<boolean> => {
|
return async (req: Request, res: Response): Promise<boolean> => {
|
||||||
const result = (await general (req, res)) && (await specific (req, res));
|
const result = (await general (req, res)) && (await specific (req, res));
|
||||||
return result;
|
return result;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
|
|
||||||
export class HttpHandler {
|
export abstract class HttpHandler {
|
||||||
public abstract register_handlers(router: Router): void;
|
public abstract register_handlers(router: Router): void;
|
||||||
|
|
||||||
public get_router (): Router {
|
public get_router (): Router {
|
||||||
|
20
package.json
20
package.json
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@scode/requestor",
|
"name": "@sapphirecode/requestor",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Express handler templates",
|
"description": "Express handler templates",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "nyc ava",
|
"test": "echo \"no test\"",
|
||||||
"compile": "tsc",
|
"compile": "tsc",
|
||||||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
|
||||||
"ci": "yarn --frozen-lockfile && node jenkins.js"
|
"ci": "yarn --frozen-lockfile && node jenkins.js"
|
||||||
@ -23,18 +23,18 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@ava/typescript": "^1.1.1",
|
"@ava/typescript": "^1.1.1",
|
||||||
"@scode/eslint-config-ts": "^1.0.22",
|
"@sapphirecode/eslint-config-ts": "^1.0.29",
|
||||||
"@stryker-mutator/core": "^3.1.0",
|
|
||||||
"@stryker-mutator/javascript-mutator": "^3.1.0",
|
|
||||||
"ava": "^3.7.1",
|
|
||||||
"eslint": "^6.8.0",
|
"eslint": "^6.8.0",
|
||||||
"nyc": "^15.0.1",
|
|
||||||
"typescript": "^3.8.3"
|
"typescript": "^3.8.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@scode/consts": "^1.1.5",
|
"@sapphirecode/consts": "^1.1.9",
|
||||||
"@scode/modelling": "^1.0.3",
|
"@sapphirecode/modelling": "^1.0.19",
|
||||||
"@types/express": "^4.17.6",
|
"@types/express": "^4.17.6",
|
||||||
"express": "^4.17.1"
|
"express": "^4.17.1"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"LICENSE",
|
||||||
|
"/dist/"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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'
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,88 +0,0 @@
|
|||||||
import knex from 'knex';
|
|
||||||
import test from 'ava';
|
|
||||||
import express, { Request, Response } from 'express';
|
|
||||||
import { http } from '@scode/consts';
|
|
||||||
import { KnexCrudHandler } from '../lib/DatabaseCrudHandler';
|
|
||||||
|
|
||||||
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) => {});
|
|
1922
yarn-error.log
Normal file
1922
yarn-error.log
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user