Skip to content

Commit 30c87ce

Browse files
committed
Merge branch 'master' of https://github.com/sugoiJS/Core
2 parents f6de25a + ab1a674 commit 30c87ce

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 sugoiJS
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# @Sugoi\core
2+
3+
![Sugoi logo](https://www.sugoijs.com/assets/logo_inverse.png)
4+
5+
6+
## Introduction
7+
SugoiJS is a minimal modular framework,
8+
9+
which gives you the ability to use only what you need, fast.
10+
11+
this is a standalone module that can be functional separately (as all of the SugoiJS modules).
12+
13+
## Installation
14+
15+
> npm install --save @sugoi/core
16+
17+
18+
## Policies (Guards)
19+
20+
SugoiJS provides policy which can be use for guarding any function on the server.
21+
22+
The Policies use by two simple steps:
23+
24+
#### @Policy(policyId?:string)
25+
26+
This decorator register the function as policy validator.
27+
28+
This decorated function will be later can be use for guard our functions.
29+
30+
> policyId?: string - The id which will be use as an alias for calling this function, default is ${class name}.${function name}
31+
32+
#### @UsePolicy(policy: TPolicy|string, failedResponseCode: number = 400, ...policyMeta: any[])
33+
34+
This decorator declare the function guarded by policy.
35+
36+
> policy:TPolicy| string - For set the ref policy, use the policy Id from previous section nor anonymous function reference.
37+
38+
> failedResponseCode: number - The code which will be under the exception in case the value does not meet the criterias.
39+
40+
> policyMeta: any[] - Any further payload data which should pass to the policy.
41+
42+
### Pre-defined policies:
43+
44+
@sugoi\core provide pre-defined policy for validating function arguments:
45+
46+
ValidateSchemaPolicy(failedResponseCode: number = 400, ...policyMeta: TValidateSchemaMeta[])
47+
48+
> failedResponseCode: number - The code which will be under the exception in case the value does not meet the criterias.
49+
50+
> policyMeta: TValidateSchemaMeta - Meta data for validation
51+
52+
{
53+
schema: {[prop:string]:ComparableSchema|ComparableSchema}, - Comperable schema
54+
argIndex?: number, - Function argument index - default is 0
55+
keyInArg?: string - Key in argument
56+
}
57+
58+
Example:
59+
Schema -
60+
61+
{
62+
role:{
63+
text:string//with regex /([A-Z])+/i
64+
}
65+
}
66+
67+
Usage -
68+
69+
@ValidateSchemaPolicy(400, {
70+
schema: {
71+
"role": ComparableSchema.ofType(
72+
{text: ComparableSchema.ofType(SchemaTypes.STRING).setRegex("([A-Z])+", "i")}
73+
)
74+
},
75+
argIndex: 0
76+
})
77+
78+
79+
### Build your own policies:
80+
81+
Policy can be any function of type TPolicy
82+
83+
> TPolicy = (policyData?:{functionArgs: any[], policyMeta: any[]})=>(Promise < (true|any) > | (true|any))
84+
85+
When result is boolean `true` means the data is valid, all the other values will be shown on the exception
86+
87+
### Policy full example:
88+
89+
class Validators{
90+
91+
@Policy() //register this function as policy using the class name and function name, same as use @Policy("Validators.myNumberValidation")
92+
static myNumberValidation(policyData:{functionArgs: any[], policyMeta: {argIndexToValidate:number,maxValue:number}[]}): true|any{
93+
const myMeta = policyMeta[0];
94+
//those are the meta data values which passed to the decorator itself while using @UsePolicy()
95+
const argIndexToValidate = myMeta.argIndexToValidate;
96+
const maxValue = myMeta.maxValue;
97+
98+
if(policyData.functionArgs[argIndexToValidate] < maxValue){
99+
return true; //Is valid, continue to the function/next policy
100+
}else{
101+
return policyData.functionArgs[argToValidate]; //so we will be able to identify the issue on the exception
102+
}
103+
}
104+
}
105+
106+
@UsePolicy("Validators.myNumberValidation",{argIndexToValidate:0,maxValue:5})
107+
lowerThan5NumberLogger(myNumber){
108+
console.log(`number is lower the 5! ${myNumber}`);
109+
}
110+
111+
## Container
112+
113+
SugoiJS re-exports [Inversify container class](https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md)
114+
for support singleton injectable (autowire) services.
115+
116+
By using Containers you can achieve singleton services solutions for request\application liftime.
117+
118+
119+
## Exceptions
120+
121+
SugoiJS provides base abstract exception(error) class which can be extended and used for exceptions handling
122+
123+
SugoiError:{
124+
code:number;
125+
message:string;
126+
data:Array<any>;
127+
}
128+
129+
Feel free to extend this class to identify your own error by:
130+
131+
switch(err.constructor.name){
132+
case "MySugoiError":
133+
//handled error
134+
break;
135+
default:
136+
throw err;
137+
}
138+
139+
Or by:
140+
141+
if( err instanceof MySugoiError){
142+
//handled error
143+
}else{
144+
throw err;
145+
}
146+
147+
## Documentation
148+
149+
You can find further information on [Sugoi official website](http://www.sugoijs.com)

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-minimal

0 commit comments

Comments
 (0)