SugoiJS is a minimal modular framework,
which gives you the ability to use only what you need, fast.
this is a standalone module that can be functional separately (as all of the SugoiJS modules).
The core module of SugoiJS provide common utilities to ease the development process.
The core module, use as utility for all of SugoiJS modules.
npm install --save @sugoi/core
Under your tsconfig - compilerOptions set:
-
"target": "es2015"
-
"emitDecoratorMetadata": true
-
"experimentalDecorators": true
-
"lib": ["es2015","dom"]
You are able to use the config template which was set for the @sugoi/demo application:
{
"compilerOptions": {
"baseUrl": "./src",
"allowJs": true,
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"typeRoots": [
"./node_modules/@types"
],
"types": [
"body-parser",
"express",
"node"
]
}
}
This method allows to cast plain object to class instance with/without applying the constructor. This ability is common use for model based design.
provides the ability to decorate class, method or property in runtime.
SugoiJS re-exports Inversify container class for support singleton injectable (aka. Autowire) services.
By using Containers you can achieve singleton services solutions for request\application lifetime.
SugoiJS provides base abstract exception(error) class which can be extended and used for exceptions handling
SugoiError: {
code:number;
message:string;
data:Array<any>;
}
Feel free to extend this class to identify your own error by:
switch(err.constructor.name){
case "MySugoiError":
//handled error
break;
default:
throw err;
}
Or by:
if( err instanceof MySugoiError){
//handled error
}else{
throw err;
}
Catch decorator gives the ability to catch and handle runtime errors for method or class level based on the exception type.
OnEvent decorator gives the ability to bind method to an event with channel based design.
Log usage of deprecated methods or throw an exception in case of usage.
Gives a class the ability to iterate throw it, decorate a class with this decorator allow to run for loops on the class properties.
SugoiJS provides the ability to apply filter functions on top of methods to reduce invalid payload checking.
For applying policy use the UsePolicy
or UsePolicySync
decorator:
@UsePolicy(policy: TPolicy|string, failedResponseCode: number = 400, ...policyMeta: any[])
@UsePolicySync(policy: TPolicy|string, failedResponseCode: number = 400, ...policyMeta: any[])
policy:TPolicy | string - For set the ref policy, use anonymous function.
failedResponseCode: number - The code which will be under the exception in case the value does not meet the criteria.
policyMeta: any[] - Any further payload data which should pass to the policy.
export type TPolicy = (policyData?: TPolicyPayload) => (Promise<TPolicyResults> | TPolicyResults);
export type TPolicyPayload = { functionArgs?: any[], policyMeta?: any[] };
export type TPolicyResults = true | any; // true means valid, otherwise the value is invalid and the returned value return into the exception as data
@sugoi\core provide pre-defined policy for validating function arguments:
ValidateSchemaPolicy(failedResponseCode: number = 400, ...policyMeta: TValidateSchemaMeta[])
failedResponseCode: number - The code which will be under the exception in case the value does not meet the criterias.
policyMeta: TValidateSchemaMeta - Meta data for validation
{
schema: {[prop:string]:ComparableSchema|ComparableSchema}, - Comperable schema
argIndex?: number, - Function argument index - default is 0
keyInArg?: string - Path to validate under the argument
}
Example: Schema -
{
role:{
text:string // + check the regex - /([A-Z])+/i
}
}
Usage -
@ValidateSchemaPolicy(400, {
schema: {
"role": ComparableSchema.ofType(
{text: ComparableSchema.ofType(SchemaTypes.STRING)
.setRegex("([A-Z])+", "i")}
)
},
argIndex: 0
})
Another way:
@ValidateSchemaPolicy(400, {
schema: {
text: ComparableSchema.ofType(SchemaTypes.STRING)
.setRegex("([A-Z])+", "i")}
},
argIndex: 0,
keyInArg: 'role'
})
You can find further information on Sugoi official website