feat(core): Added GuardError

This commit is contained in:
Philipp Kunz 2024-08-25 18:03:37 +02:00
parent 425477548f
commit 1d06c878e1
11 changed files with 505 additions and 439 deletions

View File

@ -1,140 +0,0 @@
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: '$CI_BUILD_STAGE'
stages:
- security
- test
- release
- metadata
before_script:
- npm install -g @shipzone/npmci
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
only:
- tags
tags:
- lossless
- docker
- notpriv
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags:
- docker
allow_failure: true
# ====================
# test stage
# ====================
testStable:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- lossless
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g typescript
- npmci npm prepare
- npmci npm install
tags:
- lossless
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- lossless
- docker
- notpriv
pages:
stage: metadata
script:
- npmci node install lts
- npmci command npm install -g @git.zone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- lossless
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

26
changelog.md Normal file
View File

@ -0,0 +1,26 @@
# Changelog
## 2024-08-25 - 3.1.0 - feat(core)
Added Guard Handling and Removed npmci from CI
- Refactored Guard and GuardSet classes for better modularization
- Introduced GuardError for detailed error handling
- Updated dependencies versions in package.json
- Removed `npmci` configuration from GitLab CI
## 2024-05-30 - 3.0.0 to 3.0.2 - Core and API updates
Series of updates and fixes.
- BREAKING CHANGE(api): changed API to be more concise
- fix(core): update
## 2022-03-21 - 1.0.5 to 2.0.1 - Core updates and new org scheme
Multiple updates including breaking changes and new organizational scheme.
- BREAKING CHANGE(core): updated to esm
- switch to new org scheme
## 2019-08-07 - 1.0.3 to 1.0.5 - Core updates
Fixes for core components.
- fix(core): update

View File

@ -15,14 +15,14 @@
},
"devDependencies": {
"@api.global/typedserver": "^3.0.50",
"@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tsbuild": "^2.1.84",
"@git.zone/tsrun": "^1.2.49",
"@git.zone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.12.13"
"@push.rocks/tapbundle": "^5.0.24",
"@types/node": "^22.5.0"
},
"dependencies": {
"@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartpromise": "^4.0.4",
"@push.rocks/smartrequest": "^2.0.15"
},
"files": [

File diff suppressed because it is too large Load Diff

View File

@ -3,10 +3,10 @@ import * as smartguard from '../ts/index.js';
import * as typedserver from '@api.global/typedserver';
import * as smartrequest from '@push.rocks/smartrequest';
let smartexpressInstance: typedserver.servertools.Server;
let typedserverInstance: typedserver.servertools.Server;
tap.test('should create a demo smartexpress instance', async () => {
smartexpressInstance = new typedserver.servertools.Server({
typedserverInstance = new typedserver.servertools.Server({
cors: true,
forceSsl: false,
defaultAnswer: async () => 'hi there',
@ -30,7 +30,7 @@ tap.test('should be able to create smartguards for a request', async () => {
}
});
smartexpressInstance.addRoute(
typedserverInstance.addRoute(
'/testroute',
new typedserver.servertools.Handler('ALL', async (req, res) => {
await smartguard.passGuardsOrReject(
@ -49,7 +49,7 @@ tap.test('should be able to create smartguards for a request', async () => {
});
tap.test('should start server with guards in place', async () => {
await smartexpressInstance.start();
await typedserverInstance.start();
});
tap.test('should execute a request', async () => {
@ -59,7 +59,7 @@ tap.test('should execute a request', async () => {
});
tap.test('should end the demo smartexpress instance', async () => {
await smartexpressInstance.stop();
await typedserverInstance.stop();
});
tap.start();

View File

@ -1,8 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartguard',
version: '3.0.2',
version: '3.1.0',
description: 'A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.'
}

View File

@ -9,10 +9,10 @@ export interface IGuardOptions {
export class Guard<T> {
private guardFunction: TGuardFunction<T>;
public guardoOptions: IGuardOptions;
public options: IGuardOptions;
constructor(guardFunctionArg: TGuardFunction<T>, optionsArg?: IGuardOptions) {
this.guardFunction = guardFunctionArg;
this.guardoOptions = optionsArg;
this.options = optionsArg;
}
/**
@ -27,7 +27,7 @@ export class Guard<T> {
public async getFailedHint(dataArg: T) {
const result = await this.exec(dataArg);
if (!result) {
return this.guardoOptions.failedHint;
return this.options.failedHint;
} else {
return null;
}

8
ts/classes.guarderror.ts Normal file
View File

@ -0,0 +1,8 @@
import * as plugins from './smartguard.plugins.js';
export class GuardError extends Error {
constructor(message: string) {
super(message);
this.name = 'GuardError';
}
}

View File

@ -1,5 +1,5 @@
import * as plugins from './smartguard.plugins.js';
import { Guard, type TGuardFunction } from './smartguard.classes.guard.js';
import { Guard, type TGuardFunction } from './classes.guard.js';
export interface IExecOptions {
mode?: 'parallel' | 'serial';

View File

@ -1,14 +1,19 @@
import * as plugins from './smartguard.plugins.js';
import { Guard } from './smartguard.classes.guard.js';
import { GuardSet } from './smartguard.classes.guardset.js';
export * from './smartguard.classes.guard.js';
export * from './smartguard.classes.guardset.js';
import { Guard } from './classes.guard.js';
export * from './classes.guarderror.js';
export * from './classes.guard.js';
export * from './classes.guardset.js';
import { GuardSet } from './classes.guardset.js';
import { GuardError } from './classes.guarderror.js';
export const passGuardsOrReject = async <T>(dataArg: T, guards: Array<Guard<T>>) => {
const guardSet = new GuardSet<T>(guards);
const result = await guardSet.allGuardsPass(dataArg);
if (!result) {
throw new Error('Guard failed');
const failedHint = await guardSet.getFailedHint(dataArg);
throw new GuardError(`Guards failed:
${failedHint}
`);
}
return ;
};