Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 32e72dc131 | |||
| 5371bd058a | |||
| eb9151e23e | |||
| fa4e54a7ee |
+8
-6
@@ -1,5 +1,5 @@
|
|||||||
# gitzone ci_default
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
@@ -50,13 +50,13 @@ testLTS:
|
|||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
@@ -65,7 +65,7 @@ testSTABLE:
|
|||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install lts
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
@@ -98,7 +98,9 @@ trigger:
|
|||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-dbase:npmci
|
||||||
|
services:
|
||||||
|
- docker:18-dind
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
|||||||
Generated
+1133
-15
File diff suppressed because it is too large
Load Diff
+17
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartguard",
|
"name": "@pushrocks/smartguard",
|
||||||
"version": "1.0.2",
|
"version": "1.0.4",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "smart guards for validations",
|
"description": "smart guards for validations",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@@ -15,10 +15,25 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.0.22",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.15",
|
||||||
|
"@pushrocks/smartexpress": "^3.0.35",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/tapbundle": "^3.0.7",
|
||||||
"@types/node": "^10.11.7",
|
"@types/node": "^10.11.7",
|
||||||
"tslint": "^5.11.0",
|
"tslint": "^5.11.0",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {}
|
"dependencies": {
|
||||||
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
|
"@pushrocks/smartrequest": "^1.1.16"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/*",
|
||||||
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"dist_ts_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# @pushrocks/smartguard
|
||||||
|
smart guards for validations
|
||||||
|
|
||||||
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartguard)
|
||||||
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartguard)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smartguard)
|
||||||
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartguard/)
|
||||||
|
|
||||||
|
## Status for master
|
||||||
|
[](https://gitlab.com/pushrocks/smartguard/commits/master)
|
||||||
|
[](https://gitlab.com/pushrocks/smartguard/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/@pushrocks/smartguard)
|
||||||
|
[](https://snyk.io/test/npm/@pushrocks/smartguard)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://prettier.io/)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
||||||
+61
-4
@@ -1,8 +1,65 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
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 () => {
|
let smartexpressInstance: smartexpress.Server;
|
||||||
console.log(smartguard.standardExport)
|
|
||||||
|
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.start()
|
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();
|
||||||
|
|||||||
+16
-1
@@ -1,3 +1,18 @@
|
|||||||
import * as plugins from './smartguard.plugins';
|
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;
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import * as plugins from './smartguard.plugins';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a block handler is used
|
||||||
|
*/
|
||||||
|
export class BlockHandler {}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,4 @@
|
|||||||
const removeme = {};
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
export {
|
|
||||||
removeme
|
// pushrocks scope
|
||||||
}
|
export { smartpromise };
|
||||||
|
|||||||
Reference in New Issue
Block a user