Compare commits
No commits in common. "431e21806b95918fa5558e3a0a7df765408776ee" and "00546001eb3bbdeaa4f05a3825bd1898597b689e" have entirely different histories.
431e21806b
...
00546001eb
@ -1 +0,0 @@
|
|||||||
/dist/
|
|
@ -13,5 +13,12 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
extends: [
|
extends: [
|
||||||
'@scode'
|
'@scode'
|
||||||
]
|
],
|
||||||
|
globals: {
|
||||||
|
Atomics: 'readonly',
|
||||||
|
SharedArrayBuffer: 'readonly'
|
||||||
|
},
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 2018
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,7 +1,6 @@
|
|||||||
/node_modules/
|
/node_modules/
|
||||||
/coverage/
|
/coverage/
|
||||||
/.nyc_output/
|
/.nyc_output/
|
||||||
/dist/
|
|
||||||
|
|
||||||
# stryker temp files
|
# stryker temp files
|
||||||
.stryker-tmp
|
.stryker-tmp
|
||||||
|
1
.npmignore
Normal file
1
.npmignore
Normal file
@ -0,0 +1 @@
|
|||||||
|
/test/
|
144
index.js
Normal file
144
index.js
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* 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-console */
|
||||||
|
/* eslint-disable no-sync */
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const fs = require ('fs');
|
||||||
|
const path = require ('path');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {object} options
|
||||||
|
* @property {any} [opts] object to pass to the handlers
|
||||||
|
* @property {string} [subdir] subdirectory for all requests
|
||||||
|
* @property {boolean} [verbose] enable verbose logging
|
||||||
|
* @property {boolean} [rethrow] rethrow errors (default: true)
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {object} handler_description
|
||||||
|
* @property {string} module_folder folder the module file is in
|
||||||
|
* @property {string} file name of the module
|
||||||
|
* @property {any} opts optional arguments
|
||||||
|
* @property {boolean} rethrow should errors be rethrown
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* wrap a requestor handler to be compatible with express
|
||||||
|
*
|
||||||
|
* @param {handler_description} data handler data
|
||||||
|
* @returns {Function} requestor handler
|
||||||
|
*/
|
||||||
|
function get_handler ({ module_folder, file, opts, rethrow }) {
|
||||||
|
// eslint-disable-next-line global-require
|
||||||
|
const handler = require (path.join (process.cwd (), module_folder, file));
|
||||||
|
|
||||||
|
return (req, res, next) => new Promise (
|
||||||
|
(resolve) => resolve (handler (req, res, next, opts))
|
||||||
|
)
|
||||||
|
.catch ((e) => {
|
||||||
|
if (rethrow)
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* register a handler to the given app
|
||||||
|
*
|
||||||
|
* @param {any} app express app
|
||||||
|
* @param {handler_description} handler_description data for the used handler
|
||||||
|
* @param {string} method method to respond to
|
||||||
|
* @param {string} url url to respond to
|
||||||
|
* @param {boolean} verbose should verbose logging be enabled
|
||||||
|
*/
|
||||||
|
function register_handler (
|
||||||
|
app,
|
||||||
|
handler_description,
|
||||||
|
method,
|
||||||
|
url,
|
||||||
|
verbose
|
||||||
|
) {
|
||||||
|
const handler = get_handler (handler_description);
|
||||||
|
|
||||||
|
if (verbose) {
|
||||||
|
console.log (
|
||||||
|
`[requestor info] redirecting ${url} to ${handler_description.file}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case 'post':
|
||||||
|
app.post (url, handler);
|
||||||
|
break;
|
||||||
|
case 'get':
|
||||||
|
app.get (url, handler);
|
||||||
|
break;
|
||||||
|
case 'put':
|
||||||
|
app.put (url, handler);
|
||||||
|
break;
|
||||||
|
case 'delete':
|
||||||
|
app.delete (url, handler);
|
||||||
|
break;
|
||||||
|
case 'all':
|
||||||
|
app.all (url, handler);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (verbose) {
|
||||||
|
console.warn (
|
||||||
|
`'${method}' did not match any request method, ignoring`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load all request handlers in the given folder
|
||||||
|
*
|
||||||
|
* @param {any} app express app
|
||||||
|
* @param {string} module_folder folder that contains all modules
|
||||||
|
* @param {options} options additional options
|
||||||
|
*/
|
||||||
|
module.exports = function main (
|
||||||
|
app,
|
||||||
|
module_folder,
|
||||||
|
options = { opts: null, subdir: '', verbose: false, rethrow: true }
|
||||||
|
) {
|
||||||
|
const { opts, subdir, verbose, rethrow } = options;
|
||||||
|
|
||||||
|
for (const file of fs.readdirSync (module_folder)) {
|
||||||
|
const regex = /(?<method>.*?)-(?<url>.*?)\.js/u;
|
||||||
|
const { groups } = regex.exec (file);
|
||||||
|
|
||||||
|
if (typeof subdir === 'undefined')
|
||||||
|
groups.url = `/${groups.url}/`;
|
||||||
|
else
|
||||||
|
groups.url = `/${subdir}/${groups.url}/`;
|
||||||
|
|
||||||
|
groups.url = groups.url
|
||||||
|
.replace (/^\/[^/]*\/root/iu, '/')
|
||||||
|
.replace (/\./gu, '/')
|
||||||
|
.replace (/\/+/gu, '/');
|
||||||
|
|
||||||
|
register_handler (
|
||||||
|
app,
|
||||||
|
{
|
||||||
|
file,
|
||||||
|
module_folder,
|
||||||
|
opts,
|
||||||
|
rethrow
|
||||||
|
},
|
||||||
|
groups.method,
|
||||||
|
groups.url,
|
||||||
|
verbose
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
@ -1,8 +0,0 @@
|
|||||||
import { Request, Response } from 'express';
|
|
||||||
|
|
||||||
export interface CrudHandler {
|
|
||||||
create(req: Request, res: Response): Promise<void>;
|
|
||||||
read(req: Request, res: Response): Promise<void>;
|
|
||||||
update(req: Request, res: Response): Promise<void>;
|
|
||||||
delete(req: Request, res: Response): Promise<void>;
|
|
||||||
}
|
|
@ -1,163 +0,0 @@
|
|||||||
import { Request, Response, Router } from 'express';
|
|
||||||
import { http } from '@scode/consts';
|
|
||||||
import { ControlModel, DatabaseModel } from '@scode/modelling';
|
|
||||||
import { CrudHandler } from './CrudHandler';
|
|
||||||
import { HttpHandler } from './HttpHandler';
|
|
||||||
import { DatabaseCrudOptionsReader } from './DatabaseCrudOptionsReader';
|
|
||||||
import { DatabaseCrudOptions } from './DatabaseCrudOptions';
|
|
||||||
|
|
||||||
export class DatabaseCrudHandler extends HttpHandler implements CrudHandler {
|
|
||||||
protected cm:
|
|
||||||
new (object: Record<string, string|number|boolean>) => ControlModel;
|
|
||||||
|
|
||||||
protected dm: new (id?: number) => DatabaseModel;
|
|
||||||
protected options: DatabaseCrudOptionsReader;
|
|
||||||
|
|
||||||
public constructor (
|
|
||||||
cm: new (object: Record<string, string|number|boolean>) => ControlModel,
|
|
||||||
dm: new (id?: number) => DatabaseModel,
|
|
||||||
options: DatabaseCrudOptions = {}
|
|
||||||
) {
|
|
||||||
super ();
|
|
||||||
this.cm = cm;
|
|
||||||
this.dm = dm;
|
|
||||||
this.options = new DatabaseCrudOptionsReader (options);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected validate_body (
|
|
||||||
req: Request,
|
|
||||||
res: Response
|
|
||||||
): Promise<Record<string, unknown> | null> | Record<string, unknown> | null {
|
|
||||||
if (typeof req.body === 'undefined') {
|
|
||||||
res.status (http.status_bad_request);
|
|
||||||
res.end ('body was undefined');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return JSON.parse (req.body);
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
res.status (http.status_bad_request);
|
|
||||||
res.end ('invalid json input');
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async create (req: Request, res: Response): Promise<void> {
|
|
||||||
if (!await this.options.create_authorization (req, res))
|
|
||||||
return;
|
|
||||||
|
|
||||||
const body_data = await this.validate_body (req, res);
|
|
||||||
if (body_data === null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const cm = new this.cm (body_data as Record<string|string, number|boolean>);
|
|
||||||
cm.update ();
|
|
||||||
|
|
||||||
const dm = new this.dm;
|
|
||||||
for (const key of Object.keys (body_data))
|
|
||||||
dm.set (key, cm.get (key));
|
|
||||||
await dm.write ();
|
|
||||||
|
|
||||||
res.status (http.status_created)
|
|
||||||
.end (dm.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async read (req: Request, res: Response): Promise<void> {
|
|
||||||
if (!await this.options.read_authorization (req, res))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (typeof req.headers.id === 'undefined') {
|
|
||||||
res.status (http.status_bad_request)
|
|
||||||
.end ('id undefined');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const dm = new this.dm (parseInt (req.headers.id as string));
|
|
||||||
const found = await dm.read ();
|
|
||||||
|
|
||||||
const cm = new this.cm (dm.object);
|
|
||||||
cm.update ();
|
|
||||||
|
|
||||||
res.status (found ? http.status_ok : http.status_not_found)
|
|
||||||
.json (cm.object);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async update (req: Request, res: Response): Promise<void> {
|
|
||||||
if (!await this.options.update_authorization (req, res))
|
|
||||||
return;
|
|
||||||
|
|
||||||
const body_data = await this.validate_body (req, res);
|
|
||||||
if (body_data === null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (typeof req.headers.id === 'undefined') {
|
|
||||||
res.status (http.status_bad_request)
|
|
||||||
.end ('id undefined');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const dm = new this.dm (parseInt (req.headers.id as string));
|
|
||||||
const found = await dm.read ();
|
|
||||||
if (!found) {
|
|
||||||
res.status (http.status_not_found)
|
|
||||||
.end ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cm = new this.cm (dm.object);
|
|
||||||
cm.update ();
|
|
||||||
|
|
||||||
for (const key of Object.keys (body_data))
|
|
||||||
cm.set (key, body_data[key] as string|number|boolean);
|
|
||||||
cm.update ();
|
|
||||||
|
|
||||||
for (const key of Object.keys (cm.object))
|
|
||||||
dm.set (key, cm.get (key));
|
|
||||||
|
|
||||||
const written = await dm.write ();
|
|
||||||
|
|
||||||
res.status (written ? http.status_ok : http.status_internal_server_error)
|
|
||||||
.end ();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async delete (req: Request, res: Response): Promise<void> {
|
|
||||||
if (!await this.options.delete_authorization (req, res))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (typeof req.headers.id === 'undefined') {
|
|
||||||
res.status (http.status_bad_request)
|
|
||||||
.end ('id undefined');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const dm = new this.dm (parseInt (req.headers.id as string));
|
|
||||||
const found = await dm.read ();
|
|
||||||
if (!found) {
|
|
||||||
res.status (http.status_not_found)
|
|
||||||
.end ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const deleted = await dm.delete ();
|
|
||||||
|
|
||||||
res.status (deleted ? http.status_ok : http.status_internal_server_error)
|
|
||||||
.end ();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async create_or_update (
|
|
||||||
req: Request,
|
|
||||||
res: Response
|
|
||||||
): Promise<void> {
|
|
||||||
if (typeof req.headers.id === 'undefined')
|
|
||||||
await this.create (req, res);
|
|
||||||
else
|
|
||||||
await this.update (req, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
public register_handlers (router: Router): void {
|
|
||||||
router.post ('/', this.create_or_update);
|
|
||||||
router.get ('/', this.read);
|
|
||||||
router.delete ('/', this.delete);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
import { Request, Response } from 'express';
|
|
||||||
|
|
||||||
type Authorization = {
|
|
||||||
(req: Request, res: Response): Promise<boolean>;
|
|
||||||
(req: Request, res: Response, next: Function): unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DatabaseCrudOptions {
|
|
||||||
general_authorization?: Authorization;
|
|
||||||
create_authorization?: Authorization;
|
|
||||||
read_authorization?: Authorization;
|
|
||||||
update_authorization?: Authorization;
|
|
||||||
delete_authorization?: Authorization;
|
|
||||||
optional_columns?: Array<string>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export { Authorization, DatabaseCrudOptions };
|
|
@ -1,74 +0,0 @@
|
|||||||
import { Request, Response } from 'express';
|
|
||||||
import { DatabaseCrudOptions, Authorization } from './DatabaseCrudOptions';
|
|
||||||
|
|
||||||
type AuthRunner = {
|
|
||||||
(req: Request, res: Response): Promise<boolean>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DatabaseCrudOptionsReader {
|
|
||||||
private _options: DatabaseCrudOptions;
|
|
||||||
|
|
||||||
public constructor (options: DatabaseCrudOptions) {
|
|
||||||
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: (value: boolean) => void) => {
|
|
||||||
(async (): Promise<void> => {
|
|
||||||
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 {
|
|
||||||
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,11 +0,0 @@
|
|||||||
import { Router } from 'express';
|
|
||||||
|
|
||||||
export abstract class HttpHandler {
|
|
||||||
public abstract register_handlers(router: Router): void;
|
|
||||||
|
|
||||||
public get_router (): Router {
|
|
||||||
const r = Router ();
|
|
||||||
this.register_handlers (r);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
export { HttpHandler } from './HttpHandler';
|
|
||||||
export { CrudHandler } from './CrudHandler';
|
|
||||||
export { DatabaseCrudHandler } from './DatabaseCrudHandler';
|
|
28
package.json
28
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@scode/requestor",
|
"name": "@scode/requestor",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Express handler templates",
|
"description": "Split express paths into individual files to make api programming more structured",
|
||||||
"main": "dist/index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"no test\"",
|
"test": "nyc ava",
|
||||||
"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",
|
||||||
|
"mutate": "stryker run"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -22,19 +22,11 @@
|
|||||||
"node": ">=10.0.0"
|
"node": ">=10.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@ava/typescript": "^1.1.1",
|
"@scode/eslint-config": "^2.0.2",
|
||||||
"@scode/eslint-config-ts": "^1.0.27",
|
"@stryker-mutator/core": "^3.0.2",
|
||||||
"@stryker-mutator/core": "^3.1.0",
|
"@stryker-mutator/javascript-mutator": "^3.0.2",
|
||||||
"@stryker-mutator/javascript-mutator": "^3.1.0",
|
"ava": "^3.4.0",
|
||||||
"ava": "^3.8.1",
|
|
||||||
"eslint": "^6.8.0",
|
"eslint": "^6.8.0",
|
||||||
"nyc": "^15.0.1",
|
"nyc": "^15.0.0"
|
||||||
"typescript": "^3.8.3"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@scode/consts": "^1.1.7",
|
|
||||||
"@scode/modelling": "^1.0.16",
|
|
||||||
"@types/express": "^4.17.6",
|
|
||||||
"express": "^4.17.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
stryker.conf.js
Normal file
24
stryker.conf.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('@stryker-mutator/api/core').StrykerOptions}
|
||||||
|
*/
|
||||||
|
module.exports = {
|
||||||
|
mutator: 'javascript',
|
||||||
|
packageManager: 'yarn',
|
||||||
|
reporters: [
|
||||||
|
'clear-text',
|
||||||
|
'progress'
|
||||||
|
],
|
||||||
|
testRunner: 'command',
|
||||||
|
transpilers: [],
|
||||||
|
coverageAnalysis: 'all',
|
||||||
|
mutate: [ 'index.js' ]
|
||||||
|
};
|
127
test/main.js
Normal file
127
test/main.js
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const test = require ('ava');
|
||||||
|
|
||||||
|
const requestor = require ('../index');
|
||||||
|
|
||||||
|
const mock = {
|
||||||
|
registered: {},
|
||||||
|
post (path, handler) {
|
||||||
|
this.registered[`post-${path}`] = handler;
|
||||||
|
},
|
||||||
|
get (path, handler) {
|
||||||
|
this.registered[`get-${path}`] = handler;
|
||||||
|
},
|
||||||
|
put (path, handler) {
|
||||||
|
this.registered[`put-${path}`] = handler;
|
||||||
|
},
|
||||||
|
delete (path, handler) {
|
||||||
|
this.registered[`delete-${path}`] = handler;
|
||||||
|
},
|
||||||
|
all (path, handler) {
|
||||||
|
this.registered[`all-${path}`] = handler;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test ('detect requests on root', async (t) => {
|
||||||
|
mock.registered = {};
|
||||||
|
requestor (mock, './test_files/root');
|
||||||
|
const keys = [
|
||||||
|
'all-/',
|
||||||
|
'delete-/',
|
||||||
|
'get-/',
|
||||||
|
'post-/',
|
||||||
|
'put-/'
|
||||||
|
];
|
||||||
|
|
||||||
|
t.deepEqual (Object.keys (mock.registered), keys);
|
||||||
|
const res = await Promise.all (
|
||||||
|
Object.values (mock.registered)
|
||||||
|
.map ((val) => val ())
|
||||||
|
);
|
||||||
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test ('detect requests on root.subfolder', async (t) => {
|
||||||
|
mock.registered = {};
|
||||||
|
requestor (mock, './test_files/root.sub');
|
||||||
|
const keys = [
|
||||||
|
'all-/sub/',
|
||||||
|
'delete-/sub/',
|
||||||
|
'get-/sub/',
|
||||||
|
'post-/sub/',
|
||||||
|
'put-/sub/'
|
||||||
|
];
|
||||||
|
|
||||||
|
t.deepEqual (Object.keys (mock.registered), keys);
|
||||||
|
const res = await Promise.all (
|
||||||
|
Object.values (mock.registered)
|
||||||
|
.map ((val) => val ())
|
||||||
|
);
|
||||||
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test ('detect requests on subfolder', async (t) => {
|
||||||
|
mock.registered = {};
|
||||||
|
requestor (mock, './test_files/sub');
|
||||||
|
const keys = [
|
||||||
|
'all-/sub/',
|
||||||
|
'all-/sub/root/',
|
||||||
|
'delete-/sub/',
|
||||||
|
'get-/sub/',
|
||||||
|
'get-/sub/lv1/lv2/lv3/',
|
||||||
|
'post-/sub/',
|
||||||
|
'put-/sub/'
|
||||||
|
];
|
||||||
|
|
||||||
|
t.deepEqual (Object.keys (mock.registered), keys);
|
||||||
|
const res = await Promise.all (
|
||||||
|
Object.values (mock.registered)
|
||||||
|
.map ((val) => val ())
|
||||||
|
);
|
||||||
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test ('build requests with subdirectory', async (t) => {
|
||||||
|
mock.registered = {};
|
||||||
|
requestor (mock, './test_files/sub', { subdir: 'test' });
|
||||||
|
const keys = [
|
||||||
|
'all-/test/sub/',
|
||||||
|
'all-/test/sub/root/',
|
||||||
|
'delete-/test/sub/',
|
||||||
|
'get-/test/sub/',
|
||||||
|
'get-/test/sub/lv1/lv2/lv3/',
|
||||||
|
'post-/test/sub/',
|
||||||
|
'put-/test/sub/'
|
||||||
|
];
|
||||||
|
|
||||||
|
t.deepEqual (Object.keys (mock.registered), keys);
|
||||||
|
const res = await Promise.all (
|
||||||
|
Object.values (mock.registered)
|
||||||
|
.map ((val) => val ())
|
||||||
|
);
|
||||||
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test ('should rethrow', async (t) => {
|
||||||
|
mock.registered = {};
|
||||||
|
requestor (mock, './test_files/throw');
|
||||||
|
|
||||||
|
const [ func ] = Object.values (mock.registered);
|
||||||
|
await t.throwsAsync (func);
|
||||||
|
});
|
||||||
|
|
||||||
|
test ('should not rethrow', async (t) => {
|
||||||
|
mock.registered = {};
|
||||||
|
requestor (mock, './test_files/throw', { rethrow: false });
|
||||||
|
|
||||||
|
const [ func ] = Object.values (mock.registered);
|
||||||
|
await t.notThrowsAsync (func);
|
||||||
|
});
|
@ -5,13 +5,6 @@
|
|||||||
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
'use strict';
|
||||||
env: {
|
|
||||||
commonjs: true,
|
module.exports = () => 'dummy';
|
||||||
es6: true,
|
|
||||||
node: true
|
|
||||||
},
|
|
||||||
extends: [
|
|
||||||
'@scode/eslint-config-ts'
|
|
||||||
]
|
|
||||||
}
|
|
10
test_files/root.sub/delete-root.sub.js
Normal file
10
test_files/root.sub/delete-root.sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root.sub/get-root.sub.js
Normal file
10
test_files/root.sub/get-root.sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root.sub/not-root.sub.js
Normal file
10
test_files/root.sub/not-root.sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root.sub/post-root.sub.js
Normal file
10
test_files/root.sub/post-root.sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root.sub/put-root.sub.js
Normal file
10
test_files/root.sub/put-root.sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root/all-root.js
Normal file
10
test_files/root/all-root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root/delete-root.js
Normal file
10
test_files/root/delete-root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root/get-root.js
Normal file
10
test_files/root/get-root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root/not-root.js
Normal file
10
test_files/root/not-root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root/post-root.js
Normal file
10
test_files/root/post-root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/root/put-root.js
Normal file
10
test_files/root/put-root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/all-sub.js
Normal file
10
test_files/sub/all-sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/all-sub.root.js
Normal file
10
test_files/sub/all-sub.root.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/delete-sub.js
Normal file
10
test_files/sub/delete-sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/get-sub.js
Normal file
10
test_files/sub/get-sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/get-sub.lv1.lv2.lv3.js
Normal file
10
test_files/sub/get-sub.lv1.lv2.lv3.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/not-sub.js
Normal file
10
test_files/sub/not-sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/post-sub.js
Normal file
10
test_files/sub/post-sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
10
test_files/sub/put-sub.js
Normal file
10
test_files/sub/put-sub.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => 'dummy';
|
12
test_files/throw/all-root.js
Normal file
12
test_files/throw/all-root.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = () => {
|
||||||
|
throw new Error ('foo');
|
||||||
|
};
|
@ -1,66 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
/* Basic Options */
|
|
||||||
// "incremental": true, /* Enable incremental compilation */
|
|
||||||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
|
||||||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
|
||||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
|
||||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
|
||||||
// "checkJs": true, /* Report errors in .js files. */
|
|
||||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
|
||||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
|
||||||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
|
||||||
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
|
||||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
|
||||||
"outDir": "./dist", /* Redirect output structure to the directory. */
|
|
||||||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
|
||||||
// "composite": true, /* Enable project compilation */
|
|
||||||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
|
||||||
// "removeComments": true, /* Do not emit comments to output. */
|
|
||||||
// "noEmit": true, /* Do not emit outputs. */
|
|
||||||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
|
||||||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
|
||||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
|
||||||
|
|
||||||
/* Strict Type-Checking Options */
|
|
||||||
"strict": true, /* Enable all strict type-checking options. */
|
|
||||||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
|
||||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
|
||||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
|
||||||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
|
||||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
|
||||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
|
||||||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
|
||||||
|
|
||||||
/* Additional Checks */
|
|
||||||
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
|
||||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
|
||||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
|
||||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
|
||||||
|
|
||||||
/* Module Resolution Options */
|
|
||||||
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
|
||||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
|
||||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
|
||||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
|
||||||
// "typeRoots": [], /* List of folders to include type definitions from. */
|
|
||||||
// "types": [], /* Type declaration files to be included in compilation. */
|
|
||||||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
|
||||||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
|
||||||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
|
||||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
||||||
|
|
||||||
/* Source Map Options */
|
|
||||||
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
|
||||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
||||||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
|
||||||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
|
||||||
|
|
||||||
/* Experimental Options */
|
|
||||||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
|
||||||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
|
||||||
|
|
||||||
/* Advanced Options */
|
|
||||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user