fix(core): update
This commit is contained in:
parent
9a0f287771
commit
fa4e54a7ee
1146
package-lock.json
generated
1146
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -15,10 +15,14 @@
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/smartexpress": "^3.0.35",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.11.7",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {}
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrequest": "^1.1.16"
|
||||
}
|
||||
}
|
||||
|
61
test/test.ts
61
test/test.ts
@ -1,8 +1,59 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartguard from '../ts/index'
|
||||
import * as smartguard from '../ts/index';
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
console.log(smartguard.standardExport)
|
||||
})
|
||||
let smartexpressInstance: smartexpress.Server;
|
||||
|
||||
tap.start()
|
||||
tap.test('should create a demo smartexpress instance', async () => {
|
||||
smartexpressInstance = new smartexpress.Server({
|
||||
cors: true,
|
||||
forceSsl: false,
|
||||
defaultAnswer: async () => 'hi there',
|
||||
port: 3211
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should be able to create smartguards for a request', async () => {
|
||||
interface IRequestGuardData {
|
||||
req: smartexpress.Request;
|
||||
res: smartexpress.Response;
|
||||
}
|
||||
const ipGuard = new smartguard.Guard<IRequestGuardData>(async (dataArg) => {
|
||||
console.log('executing ip guard');
|
||||
if (dataArg) {
|
||||
console.log('ip guard succeeded');
|
||||
return true;
|
||||
} else {
|
||||
console.log('ip guard failed!');
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
smartexpressInstance.addRoute('/testroute', new smartexpress.Handler('ALL', async (req, res) => {
|
||||
await smartguard.passGuards({
|
||||
req,
|
||||
res
|
||||
}, [ipGuard]);
|
||||
console.log('ip guard said ok');
|
||||
res.status(200);
|
||||
res.send('hi');
|
||||
}));
|
||||
console.log('Got here ok');
|
||||
});
|
||||
|
||||
tap.test('should start server with guards in place', async () => {
|
||||
await smartexpressInstance.start();
|
||||
});
|
||||
|
||||
tap.test('should execute a request', async () => {
|
||||
const response = await smartrequest.request('http://localhost:3211/testroute', {
|
||||
method: 'GET'
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should end the demo smartexpress instance', async () => {
|
||||
await smartexpressInstance.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
17
ts/index.ts
17
ts/index.ts
@ -1,3 +1,18 @@
|
||||
import * as plugins from './smartguard.plugins';
|
||||
import { Guard } from './smartguard.classes.guard';
|
||||
import { GuardSet } from './smartguard.classes.guardset';
|
||||
export * from './smartguard.classes.guard';
|
||||
|
||||
export let standardExport = 'Hi there! :) This is an exported string';
|
||||
export const passGuards = async <T>(dataArg: T, guards: Array<Guard<T>>) => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
const guardSet = new GuardSet<T>(guards);
|
||||
const results = await guardSet.executeGuardsWithData(dataArg);
|
||||
for (const result of results) {
|
||||
if(!result) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
done.resolve();
|
||||
await done.promise;
|
||||
return;
|
||||
};
|
8
ts/smartguard.classes.blockhandler.ts
Normal file
8
ts/smartguard.classes.blockhandler.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import * as plugins from './smartguard.plugins';
|
||||
|
||||
/**
|
||||
* a block handler is used
|
||||
*/
|
||||
export class BlockHandler {
|
||||
|
||||
}
|
19
ts/smartguard.classes.guard.ts
Normal file
19
ts/smartguard.classes.guard.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import * as plugins from './smartguard.plugins';
|
||||
|
||||
export type TGuardFunction<T> = (dataArg: T) => Promise<boolean>;
|
||||
|
||||
export class Guard<T> {
|
||||
private guardFunction: TGuardFunction<T>;
|
||||
constructor(guardFunctionArg: TGuardFunction<T>) {
|
||||
this.guardFunction = guardFunctionArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* executes the guard against a data argument;
|
||||
* @param dataArg
|
||||
*/
|
||||
public async executeGuardWithData(dataArg: T) {
|
||||
const result = await this.guardFunction(dataArg);
|
||||
return result;
|
||||
}
|
||||
}
|
23
ts/smartguard.classes.guardset.ts
Normal file
23
ts/smartguard.classes.guardset.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import * as plugins from './smartguard.plugins';
|
||||
import { Guard } from './smartguard.classes.guard';
|
||||
|
||||
/**
|
||||
* a guardSet is a set of guards that need to be fulfilled
|
||||
*/
|
||||
export class GuardSet<T> {
|
||||
public guards: Array<Guard<T>>;
|
||||
public passed: boolean;
|
||||
constructor(guardsArrayArg: Array<Guard<T>>) {
|
||||
this.guards = guardsArrayArg;
|
||||
}
|
||||
|
||||
public async executeGuardsWithData(dataArg: T) {
|
||||
const resultPromises: Array<Promise<boolean>> = [];
|
||||
for (const guard of this.guards) {
|
||||
const resultPromise = guard.executeGuardWithData(dataArg);
|
||||
resultPromises.push(resultPromise);
|
||||
}
|
||||
const results = Promise.all(resultPromises);
|
||||
return results;
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
const removeme = {};
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
// pushrocks scope
|
||||
export {
|
||||
removeme
|
||||
}
|
||||
smartpromise
|
||||
};
|
Loading…
Reference in New Issue
Block a user