web_api_base 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +21 -0
- package/Application.ts +39 -0
- package/ApplicationConfiguration.ts +82 -0
- package/__tests__/classes/Controller.ts +38 -0
- package/__tests__/decorators/controllers/ControllerDecorators.spec.ts +67 -0
- package/controllers/base/ControllerBase.ts +116 -0
- package/decorators/controllers/ControllerDecorators.ts +187 -0
- package/dependencyInjection/DependecyService.ts +36 -0
- package/enums/httpVerbs/HttpVerbs.ts +7 -0
- package/interfaces/IApplication.ts +13 -0
- package/interfaces/IApplicationConfiguration.ts +6 -0
- package/interfaces/IController.ts +8 -0
- package/interfaces/entities/IDirectory.ts +6 -0
- package/jest.config.ts +6 -0
- package/midlewares/IMidleware.ts +6 -0
- package/package.json +32 -0
- package/services/fileService/FileService.ts +125 -0
- package/services/fileService/FileServiceBase.ts +16 -0
- package/tsconfig.json +103 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
|
|
8
|
+
{
|
|
9
|
+
"type": "node",
|
|
10
|
+
"request": "launch",
|
|
11
|
+
"name": "Launch Program",
|
|
12
|
+
"skipFiles": [
|
|
13
|
+
"<node_internals>/**"
|
|
14
|
+
],
|
|
15
|
+
"program": "${file}",
|
|
16
|
+
"outFiles": [
|
|
17
|
+
"${workspaceFolder}/**/*.js"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|
package/Application.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Express } from "express";
|
|
2
|
+
import ExpressModule from "express";
|
|
3
|
+
import ApplicationConfiguration from "./ApplicationConfiguration"
|
|
4
|
+
import IApplication from "./interfaces/IApplication";
|
|
5
|
+
import IApplicationConfiguration from "./interfaces/IApplicationConfiguration";
|
|
6
|
+
|
|
7
|
+
export default abstract class Application implements IApplication
|
|
8
|
+
{
|
|
9
|
+
|
|
10
|
+
private ApplicationConfiguration : IApplicationConfiguration;
|
|
11
|
+
|
|
12
|
+
public Express : Express;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
constructor()
|
|
16
|
+
{
|
|
17
|
+
this.ApplicationConfiguration = new ApplicationConfiguration();
|
|
18
|
+
|
|
19
|
+
this.Express = ExpressModule();
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
public async StartAsync() : Promise<void>
|
|
25
|
+
{
|
|
26
|
+
await this.ApplicationConfiguration.StartAsync();
|
|
27
|
+
|
|
28
|
+
this.Configure(this.ApplicationConfiguration);
|
|
29
|
+
|
|
30
|
+
this.Express.listen(this.ApplicationConfiguration.Port, this.ApplicationConfiguration.Host, ()=>
|
|
31
|
+
{
|
|
32
|
+
console.log(`App running on ${this.ApplicationConfiguration.Host}:${this.ApplicationConfiguration.Port}`);
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public abstract Configure(appConfig : IApplicationConfiguration): void;
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import File from 'fs';
|
|
2
|
+
|
|
3
|
+
import IApplicationConfiguration from './interfaces/IApplicationConfiguration';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export default class Configuration implements IApplicationConfiguration
|
|
7
|
+
{
|
|
8
|
+
|
|
9
|
+
public Host : string = "0.0.0.0";
|
|
10
|
+
public Port : number = 5555;
|
|
11
|
+
|
|
12
|
+
constructor()
|
|
13
|
+
{
|
|
14
|
+
process.env["root_dir"] = __dirname;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public async StartAsync() : Promise<void>
|
|
18
|
+
{
|
|
19
|
+
if(!await this.CheckFileAsync())
|
|
20
|
+
{
|
|
21
|
+
await this.CreateFileAsync();
|
|
22
|
+
|
|
23
|
+
}else{
|
|
24
|
+
|
|
25
|
+
await this.ReadFileAsync();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private async CheckFileAsync() : Promise<boolean>
|
|
30
|
+
{
|
|
31
|
+
return new Promise<boolean>((resolve, _) => resolve(File.existsSync(`${__dirname}\\config.json`)));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private async ReadFileAsync() : Promise<boolean>
|
|
35
|
+
{
|
|
36
|
+
return new Promise<boolean>((resolve, _) =>
|
|
37
|
+
{
|
|
38
|
+
File.readFile(`${__dirname}\\config.json`, 'utf-8', (error, data) =>
|
|
39
|
+
{
|
|
40
|
+
if(error)
|
|
41
|
+
{
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let json : any = JSON.parse(data);
|
|
46
|
+
|
|
47
|
+
for(let key in this)
|
|
48
|
+
{
|
|
49
|
+
if(json[key] != undefined)
|
|
50
|
+
{
|
|
51
|
+
this[key] = json[key];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
resolve(true);
|
|
56
|
+
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private async CreateFileAsync() : Promise<boolean>
|
|
62
|
+
{
|
|
63
|
+
|
|
64
|
+
return new Promise<boolean>((resolve, _)=>{
|
|
65
|
+
|
|
66
|
+
File.writeFile(`${__dirname}\\config.json`, JSON.stringify(this), 'utf-8', error =>
|
|
67
|
+
{
|
|
68
|
+
|
|
69
|
+
if(error)
|
|
70
|
+
{
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
resolve(true);
|
|
75
|
+
|
|
76
|
+
});
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* istanbul ignore next */
|
|
2
|
+
|
|
3
|
+
import { ControllerBase } from "../../controllers/base/ControllerBase";
|
|
4
|
+
import ControllersDecorators from "../../decorators/controllers/ControllerDecorators";
|
|
5
|
+
import { HTTPVerbs } from "../../enums/httpVerbs/HttpVerbs";
|
|
6
|
+
import IApplication from "../../interfaces/IApplication";
|
|
7
|
+
import {Request, Response} from 'express'
|
|
8
|
+
|
|
9
|
+
@ControllersDecorators.Route("/test")
|
|
10
|
+
export class ControllerTest extends ControllerBase
|
|
11
|
+
{
|
|
12
|
+
constructor()
|
|
13
|
+
{
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@ControllersDecorators.Action("Test")
|
|
18
|
+
@ControllersDecorators.Verb(HTTPVerbs.GET)
|
|
19
|
+
@ControllersDecorators.Argument<string>('name')
|
|
20
|
+
public TestAction(name : string)
|
|
21
|
+
{
|
|
22
|
+
console.log(name);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@ControllersDecorators.Action("Test")
|
|
26
|
+
@ControllersDecorators.Verb(HTTPVerbs.GET)
|
|
27
|
+
@ControllersDecorators.Argument<string, number>('name', 'age')
|
|
28
|
+
public TestActionTwo(name : string, age : number)
|
|
29
|
+
{
|
|
30
|
+
console.log(name, age);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
public AppendSync(application: IApplication): void
|
|
35
|
+
{
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ControllerTest } from "../../classes/Controller";
|
|
2
|
+
import ControllersDecorators from "../../../decorators/controllers/ControllerDecorators";
|
|
3
|
+
import { HTTPVerbs } from "../../../enums/httpVerbs/HttpVerbs";
|
|
4
|
+
|
|
5
|
+
describe('testing controllers decorators', ()=>
|
|
6
|
+
{
|
|
7
|
+
|
|
8
|
+
test("action name", ()=>
|
|
9
|
+
{
|
|
10
|
+
var controller = new ControllerTest();
|
|
11
|
+
let action = ControllersDecorators.GetAction(controller, "TestAction");
|
|
12
|
+
expect(action).toBe("Test");
|
|
13
|
+
|
|
14
|
+
},10^5)
|
|
15
|
+
|
|
16
|
+
test("http verb", ()=>
|
|
17
|
+
{
|
|
18
|
+
var controller = new ControllerTest();
|
|
19
|
+
let verb = ControllersDecorators.GetVerb(controller, "TestAction");
|
|
20
|
+
expect(verb).toBe(HTTPVerbs.GET);
|
|
21
|
+
|
|
22
|
+
},10^5)
|
|
23
|
+
|
|
24
|
+
test("action with one arg", ()=>
|
|
25
|
+
{
|
|
26
|
+
var controller = new ControllerTest();
|
|
27
|
+
|
|
28
|
+
var handler = ControllersDecorators.GetArgumentsHandler(controller, 'TestAction');
|
|
29
|
+
|
|
30
|
+
var arr = handler?.CreateArgumentsList({name : "adriano"});
|
|
31
|
+
|
|
32
|
+
Reflect.apply(controller.TestAction, controller, arr ?? []);
|
|
33
|
+
|
|
34
|
+
expect(arr).not.toBeNull();
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
},10^5)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
test("action with two args", ()=>
|
|
41
|
+
{
|
|
42
|
+
var controller = new ControllerTest();
|
|
43
|
+
|
|
44
|
+
var handler = ControllersDecorators.GetArgumentsHandler(controller, 'TestActionTwo');
|
|
45
|
+
|
|
46
|
+
var arr = handler?.CreateArgumentsList({name : "adriano", age : 30});
|
|
47
|
+
|
|
48
|
+
Reflect.apply(controller.TestActionTwo, controller, arr ?? []);
|
|
49
|
+
|
|
50
|
+
expect(arr).not.toBeNull();
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
},10^5)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
test("controller route", ()=>
|
|
57
|
+
{
|
|
58
|
+
var controller = new ControllerTest();
|
|
59
|
+
|
|
60
|
+
var route = ControllersDecorators.GetRoute(controller);
|
|
61
|
+
|
|
62
|
+
expect(route!).toBe("/test");
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
},10^5)
|
|
66
|
+
|
|
67
|
+
})
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import IController from "../../interfaces/IController";
|
|
2
|
+
import {Request, Response} from 'express'
|
|
3
|
+
import ControllersDecorators from '../../decorators/controllers/ControllerDecorators';
|
|
4
|
+
import DependecyService from '../../dependencyInjection/DependecyService';
|
|
5
|
+
import IApplication from "../../interfaces/IApplication";
|
|
6
|
+
import { HTTPVerbs } from "../../enums/httpVerbs/HttpVerbs";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export class ControllerBase implements IController
|
|
10
|
+
{
|
|
11
|
+
Request : Request = {} as Request;
|
|
12
|
+
Response : Response = {} as Response;
|
|
13
|
+
|
|
14
|
+
constructor()
|
|
15
|
+
{
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public OK<T>(result : T)
|
|
20
|
+
{
|
|
21
|
+
this.Response.status(200);
|
|
22
|
+
this.Response.json(result);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public Created()
|
|
26
|
+
{
|
|
27
|
+
this.Response.status(201);
|
|
28
|
+
this.Response.end();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public BadRequest<T>(result : T)
|
|
32
|
+
{
|
|
33
|
+
this.Response.status(400);
|
|
34
|
+
this.Response.json(result);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public Error<T>(result : T)
|
|
38
|
+
{
|
|
39
|
+
this.Response.status(500);
|
|
40
|
+
this.Response.json(result);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public SendResponse<T>(status : number, result : T)
|
|
44
|
+
{
|
|
45
|
+
this.Response.status(status);
|
|
46
|
+
this.Response.json(result);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public static AppendController<T extends IController>(ctor : { new (...args : any[]) : T;}, application : IApplication) : void
|
|
50
|
+
{
|
|
51
|
+
let empty = new ctor() as any;
|
|
52
|
+
|
|
53
|
+
let methods = Reflect.ownKeys(empty.constructor.prototype).filter(m =>
|
|
54
|
+
{
|
|
55
|
+
return typeof empty[m] == "function" ;
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
let route = ControllersDecorators.GetRoute(empty);
|
|
59
|
+
|
|
60
|
+
if(!route)
|
|
61
|
+
return;
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
for(let method of methods)
|
|
65
|
+
{
|
|
66
|
+
let action = ControllersDecorators.GetAction(empty, method.toString());
|
|
67
|
+
|
|
68
|
+
if(!action){
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let verb = ControllersDecorators.GetVerb(empty, method.toString());
|
|
73
|
+
|
|
74
|
+
if(!verb)
|
|
75
|
+
verb = HTTPVerbs.GET;
|
|
76
|
+
|
|
77
|
+
console.debug("appended : " , verb,`${route}${action}`);
|
|
78
|
+
|
|
79
|
+
(application.Express as any)[verb.toString().toLowerCase()](`${route}${action}`, (req : Request, resp : Response) =>
|
|
80
|
+
{
|
|
81
|
+
|
|
82
|
+
let midlewares = ControllersDecorators.GetMidlewares(empty).reverse();
|
|
83
|
+
|
|
84
|
+
midlewares.push(...ControllersDecorators.GetBefores(empty, method.toString()).reverse());
|
|
85
|
+
|
|
86
|
+
if(midlewares)
|
|
87
|
+
{
|
|
88
|
+
for(let method of midlewares)
|
|
89
|
+
{
|
|
90
|
+
method(req);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let args = ControllersDecorators.GetArgumentsHandler(empty, method.toString());
|
|
95
|
+
let params = [];
|
|
96
|
+
|
|
97
|
+
if(args)
|
|
98
|
+
{
|
|
99
|
+
if(args.Arguments.length > 0)
|
|
100
|
+
{
|
|
101
|
+
if(req.body && verb == (HTTPVerbs.POST || verb == HTTPVerbs.PUT))
|
|
102
|
+
params = args.CreateArgumentsList(req.body);
|
|
103
|
+
if(req.query)
|
|
104
|
+
params.push(...args.CreateArgumentsList(req.query))
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let controller = DependecyService.ResolveCtor(empty.constructor) as IController;
|
|
109
|
+
controller.Request = req;
|
|
110
|
+
controller.Response = resp;
|
|
111
|
+
(controller as any)[method](...params);
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { REFUSED } from 'dns';
|
|
2
|
+
import 'reflect-metadata';
|
|
3
|
+
|
|
4
|
+
import { HTTPVerbs } from '../../enums/httpVerbs/HttpVerbs';
|
|
5
|
+
import IController from '../../interfaces/IController';
|
|
6
|
+
import IMidleware from '../../midlewares/IMidleware';
|
|
7
|
+
|
|
8
|
+
export default class ControllersDecorators
|
|
9
|
+
{
|
|
10
|
+
constructor()
|
|
11
|
+
{
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private static RouteKeyMetadata = "meta:controllerRoute";
|
|
16
|
+
private static ActionVerbKeyMetadata = "meta:actionVerb";
|
|
17
|
+
private static ActionNameKeyMetadata = "meta:actionName";
|
|
18
|
+
private static ArgumentsHandlerKeyMetadata = "meta:argHandler";
|
|
19
|
+
private static ControllerMidlewaresKeyMetadata = "meta:controllerMidlewaresKey";
|
|
20
|
+
private static ActionsMidlewaresKeyMetadata = "meta:actionMidlewaresKey";
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
public static Route(route : string)
|
|
24
|
+
{
|
|
25
|
+
return function( target : Function)
|
|
26
|
+
{
|
|
27
|
+
Reflect.defineMetadata(ControllersDecorators.RouteKeyMetadata, route, target);
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public static Use(midleware : IMidleware)
|
|
33
|
+
{
|
|
34
|
+
return function( target : Function)
|
|
35
|
+
{
|
|
36
|
+
let current : IMidleware[] = Reflect.getMetadata(ControllersDecorators.ControllerMidlewaresKeyMetadata, target) ?? [];
|
|
37
|
+
|
|
38
|
+
current.push(midleware);
|
|
39
|
+
|
|
40
|
+
Reflect.defineMetadata(ControllersDecorators.ControllerMidlewaresKeyMetadata, current, target);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static GetMidlewares(controller : IController) : IMidleware[]
|
|
45
|
+
{
|
|
46
|
+
return Reflect.getMetadata(ControllersDecorators.ControllerMidlewaresKeyMetadata, controller.constructor) ?? [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public static Before(midleware : IMidleware)
|
|
50
|
+
{
|
|
51
|
+
return function( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor)
|
|
52
|
+
{
|
|
53
|
+
let current : IMidleware[] = Reflect.getMetadata(ControllersDecorators.ActionsMidlewaresKeyMetadata, target, methodName) ?? [];
|
|
54
|
+
|
|
55
|
+
current.push(midleware);
|
|
56
|
+
|
|
57
|
+
ControllersDecorators.SetMetaData(ControllersDecorators.ActionsMidlewaresKeyMetadata, target, methodName, current);
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public static GetBefores(controller : IController, methodName : string) : IMidleware[]
|
|
63
|
+
{
|
|
64
|
+
return this.GetMetaData<IMidleware[]>(ControllersDecorators.ActionsMidlewaresKeyMetadata, controller, methodName) ?? [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public static GetRoute(controller : IController) : string | undefined
|
|
68
|
+
{
|
|
69
|
+
return Reflect.getMetadata(ControllersDecorators.RouteKeyMetadata, controller.constructor);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
public static Verb(verb : HTTPVerbs)
|
|
74
|
+
{
|
|
75
|
+
return function( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor)
|
|
76
|
+
{
|
|
77
|
+
ControllersDecorators.SetMetaData(ControllersDecorators.ActionVerbKeyMetadata, target, methodName, verb);
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public static GetVerb(target : IController, methodName : string ) : HTTPVerbs | undefined
|
|
83
|
+
{
|
|
84
|
+
let meta = this.GetMetaData<HTTPVerbs>(ControllersDecorators.ActionVerbKeyMetadata, target, methodName);
|
|
85
|
+
|
|
86
|
+
return meta;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static Action(actionName : String)
|
|
90
|
+
{
|
|
91
|
+
return function( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor)
|
|
92
|
+
{
|
|
93
|
+
ControllersDecorators.SetMetaData(ControllersDecorators.ActionNameKeyMetadata, target, methodName, actionName);
|
|
94
|
+
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public static GetAction(target : IController, methodName : string ) : string | undefined
|
|
99
|
+
{
|
|
100
|
+
let meta = this.GetMetaData<string>(ControllersDecorators.ActionNameKeyMetadata, target, methodName);
|
|
101
|
+
|
|
102
|
+
return meta;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
public static Argument<T>(argName1 : string) : ( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor) => void
|
|
107
|
+
public static Argument<T, U>(argName1 : string, argName2?: string) : ( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor) => void
|
|
108
|
+
public static Argument<T, U, K>(argName1 : string, argName2?: string, argName3? : string) : ( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor) => void
|
|
109
|
+
public static Argument<T, U, K, Y>(argName1 : string, argName2?: string, argName3? : string, argName4? : string) : ( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor) => void
|
|
110
|
+
public static Argument<T, U, K, Y, J>(argName1 : string, argName2?: string, argName3? : string, argName4? : string, argName5? : string) : ( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor) => void
|
|
111
|
+
public static Argument<T, U, K, Y, J, V>(argName1 : string, argName2?: string, argName3? : string, argName4? : string, argName5? : string, argName6? : string) : ( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor) => void
|
|
112
|
+
{
|
|
113
|
+
return function( target : Object, methodName : string, propertyDescriptor : PropertyDescriptor)
|
|
114
|
+
{
|
|
115
|
+
ControllersDecorators.SetMetaData(ControllersDecorators.ArgumentsHandlerKeyMetadata, target, methodName,
|
|
116
|
+
{
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
Arguments : [argName1, argName2, argName3, argName4, argName5, argName6],
|
|
120
|
+
|
|
121
|
+
CreateArgumentsList : (args : any) =>
|
|
122
|
+
{
|
|
123
|
+
let results = [] as any[];
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
if (argName1 && (args[argName1] as unknown as T) != undefined)
|
|
127
|
+
results[0] = args[argName1] as T;
|
|
128
|
+
|
|
129
|
+
if (argName2 && (args[argName2] as unknown as U) != undefined)
|
|
130
|
+
results[1] = args[argName2] as U;
|
|
131
|
+
|
|
132
|
+
if (argName3 && (args[argName3] as unknown as K) != undefined)
|
|
133
|
+
results[2] = args[argName3] as K;
|
|
134
|
+
|
|
135
|
+
if (argName4 && (args[argName4] as unknown as Y) != undefined)
|
|
136
|
+
results[3] = args[argName4] as Y;
|
|
137
|
+
|
|
138
|
+
if (argName5 && (args[argName5] as unknown as J) != undefined)
|
|
139
|
+
results[4] = args[argName5] as J;
|
|
140
|
+
|
|
141
|
+
if (argName6 && (args[argName6] as unknown as V) != undefined)
|
|
142
|
+
results[5] = args[argName6] as V;
|
|
143
|
+
|
|
144
|
+
return results;
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
public static GetArgumentsHandler(target : IController, methodName : string ) : IArgumentResolverHandler | undefined
|
|
155
|
+
{
|
|
156
|
+
let handler = this.GetMetaData<IArgumentResolverHandler>(ControllersDecorators.ArgumentsHandlerKeyMetadata, target, methodName);
|
|
157
|
+
|
|
158
|
+
return handler;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private static SetMetaData<T>(key: string, target : Object, methodName : string, value : T)
|
|
162
|
+
{
|
|
163
|
+
var meta = Reflect.getOwnMetadata(key, target as Object, methodName);
|
|
164
|
+
|
|
165
|
+
if(!meta)
|
|
166
|
+
Reflect.defineMetadata(key, value, target as Object, methodName);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
private static GetMetaData<T>(key: string, target : Object, methodName : string) : T | undefined
|
|
171
|
+
{
|
|
172
|
+
var meta = Reflect.getMetadata(key, target, methodName);
|
|
173
|
+
|
|
174
|
+
if(meta != undefined)
|
|
175
|
+
return meta as T;
|
|
176
|
+
else
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
}
|
|
181
|
+
interface IArgumentResolverHandler
|
|
182
|
+
{
|
|
183
|
+
Arguments : string[];
|
|
184
|
+
CreateArgumentsList : (args :any) => any[];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default class DependecyService
|
|
2
|
+
{
|
|
3
|
+
private static _services : IService[] = [];
|
|
4
|
+
|
|
5
|
+
public static RegisterFor(type : Function, ctor : { new (...args : any[]) : any;}, builder? : () => any) : void
|
|
6
|
+
{
|
|
7
|
+
let defaultBuilder = () => Reflect.construct(ctor ?? type, []) as any;
|
|
8
|
+
|
|
9
|
+
this._services.push({ Type : type, Builder : builder ?? defaultBuilder });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public static Register(type : Function, builder? : () => any) : void
|
|
13
|
+
{
|
|
14
|
+
let defaultBuilder = () => Reflect.construct(type, []);
|
|
15
|
+
|
|
16
|
+
this._services.push({ Type : type, Builder : builder ?? defaultBuilder });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static Resolve<T>(type : Function, args? : any[]) : T
|
|
20
|
+
{
|
|
21
|
+
return this._services.find(u => u.Type == type)?.Builder(args) as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static ResolveCtor(ctor : Function, args? : any[]) : any
|
|
25
|
+
{
|
|
26
|
+
return this._services.find(u => u.Type == ctor)?.Builder(args);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
interface IService
|
|
33
|
+
{
|
|
34
|
+
Type : Function;
|
|
35
|
+
Builder : Function;
|
|
36
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Express } from "express";
|
|
2
|
+
import IApplicationConfiguration from './IApplicationConfiguration';
|
|
3
|
+
|
|
4
|
+
import IController from "./IController";
|
|
5
|
+
|
|
6
|
+
export default interface IApplication
|
|
7
|
+
{
|
|
8
|
+
Express : Express;
|
|
9
|
+
|
|
10
|
+
StartAsync() : Promise<void>;
|
|
11
|
+
|
|
12
|
+
Configure(appConfig : IApplicationConfiguration): void;
|
|
13
|
+
}
|
package/jest.config.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web_api_base",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "web api base",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "jest",
|
|
8
|
+
"debug": "node ./dist/Index.js",
|
|
9
|
+
"transpile": "tsc -p tsconfig.json",
|
|
10
|
+
"run:tests": "npx jest"
|
|
11
|
+
},
|
|
12
|
+
"author": "adriano.marino1992@gmail.com",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@types/express": "^4.17.15",
|
|
16
|
+
"@types/jest": "^29.2.4",
|
|
17
|
+
"@types/node": "^18.11.17",
|
|
18
|
+
"@types/reflect-metadata": "^0.1.0",
|
|
19
|
+
"cors": "^2.8.5",
|
|
20
|
+
"express": "^4.18.2",
|
|
21
|
+
"formidable": "^2.1.1",
|
|
22
|
+
"jest": "^29.3.1",
|
|
23
|
+
"platform-folders": "^0.6.0",
|
|
24
|
+
"reflect-metadata": "^0.1.13",
|
|
25
|
+
"ts-jest": "^29.0.3",
|
|
26
|
+
"ts-node": "^10.9.1",
|
|
27
|
+
"typescript": "^4.9.4"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/formidable": "^2.0.5"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
|
|
2
|
+
import File from 'fs';
|
|
3
|
+
import FileAsync from 'fs/promises';
|
|
4
|
+
import Path from 'path';
|
|
5
|
+
import {getDesktopFolder} from 'platform-folders'
|
|
6
|
+
|
|
7
|
+
import FileServiceBase from "./FileServiceBase";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export default class FileService extends FileServiceBase
|
|
11
|
+
{
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
public override GetDefaultDir(): string {
|
|
15
|
+
|
|
16
|
+
return getDesktopFolder();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
public override CreateDirectory(path: string): Promise<void> {
|
|
21
|
+
|
|
22
|
+
return new Promise<void>(async (resolve, reject) =>
|
|
23
|
+
{
|
|
24
|
+
try{
|
|
25
|
+
|
|
26
|
+
return resolve(File.mkdirSync(path));
|
|
27
|
+
|
|
28
|
+
}catch(err)
|
|
29
|
+
{
|
|
30
|
+
return reject(err);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public override FileExists(file: string): Promise<boolean> {
|
|
36
|
+
return new Promise<boolean>(async (resolve, reject) =>
|
|
37
|
+
{
|
|
38
|
+
try{
|
|
39
|
+
|
|
40
|
+
return resolve(File.existsSync(file) && File.lstatSync(file).isFile());
|
|
41
|
+
|
|
42
|
+
}catch(err)
|
|
43
|
+
{
|
|
44
|
+
return reject(err);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public override DirectoryExists(path: string): Promise<boolean> {
|
|
50
|
+
return new Promise<boolean>(async (resolve, reject) =>
|
|
51
|
+
{
|
|
52
|
+
try{
|
|
53
|
+
|
|
54
|
+
return resolve(File.existsSync(path) && File.lstatSync(path).isDirectory());
|
|
55
|
+
|
|
56
|
+
}catch(err)
|
|
57
|
+
{
|
|
58
|
+
return reject(err);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public override GetAllFiles(origin: string): Promise<string[]> {
|
|
64
|
+
|
|
65
|
+
return new Promise<string[]>(async (resolve, reject) =>
|
|
66
|
+
{
|
|
67
|
+
if(!File.existsSync(origin))
|
|
68
|
+
return reject(new Error(`The path ${origin} not exists`));
|
|
69
|
+
|
|
70
|
+
try{
|
|
71
|
+
|
|
72
|
+
let files = await FileAsync.readdir(origin, {withFileTypes : true});
|
|
73
|
+
|
|
74
|
+
return resolve(files.filter(u => u.isFile()).map(u => Path.join(origin, u.name)));
|
|
75
|
+
|
|
76
|
+
}catch(err)
|
|
77
|
+
{
|
|
78
|
+
return reject(err);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public override GetAllForders(origin: string): Promise<string[]> {
|
|
84
|
+
|
|
85
|
+
return new Promise<string[]>(async (resolve, reject) =>
|
|
86
|
+
{
|
|
87
|
+
if(!File.existsSync(origin))
|
|
88
|
+
return reject(new Error(`The path ${origin} not exists`));
|
|
89
|
+
|
|
90
|
+
try{
|
|
91
|
+
|
|
92
|
+
let files = await FileAsync.readdir(origin, {withFileTypes : true});
|
|
93
|
+
|
|
94
|
+
return resolve(files.filter(u => u.isDirectory()).map(u => Path.join(origin, u.name)));
|
|
95
|
+
|
|
96
|
+
}catch(err)
|
|
97
|
+
{
|
|
98
|
+
return reject(err);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public override CopyAsync(origin: string, dest: string): Promise<void> {
|
|
104
|
+
|
|
105
|
+
return new Promise<void>(async (resolve, reject)=>
|
|
106
|
+
{
|
|
107
|
+
try{
|
|
108
|
+
await File.copyFile(origin, dest, (err) =>
|
|
109
|
+
{
|
|
110
|
+
if(err)
|
|
111
|
+
throw err;
|
|
112
|
+
|
|
113
|
+
resolve();
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
}catch(err)
|
|
117
|
+
{
|
|
118
|
+
reject(err);
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IDirectory } from "../../interfaces/entities/IDirectory";
|
|
2
|
+
|
|
3
|
+
export default abstract class FileServiceBase
|
|
4
|
+
{
|
|
5
|
+
constructor(){}
|
|
6
|
+
|
|
7
|
+
public abstract CopyAsync(origin: string, dest: string): Promise<void>;
|
|
8
|
+
public abstract GetAllFiles(origin: string): Promise<string[]>;
|
|
9
|
+
public abstract GetAllForders(origin: string): Promise<string[]>;
|
|
10
|
+
public abstract FileExists(file: string): Promise<boolean>;
|
|
11
|
+
public abstract DirectoryExists(path: string): Promise<boolean>;
|
|
12
|
+
public abstract CreateDirectory(path: string): Promise<void>;
|
|
13
|
+
public abstract GetDefaultDir(): string;
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
"rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
39
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
40
|
+
|
|
41
|
+
/* JavaScript Support */
|
|
42
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
43
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
44
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
45
|
+
|
|
46
|
+
/* Emit */
|
|
47
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
48
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
49
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
50
|
+
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
51
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
52
|
+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
53
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
54
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
55
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
56
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
57
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
58
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
59
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
60
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
61
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
62
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
63
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
64
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
65
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
66
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
67
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
68
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
69
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
70
|
+
|
|
71
|
+
/* Interop Constraints */
|
|
72
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
73
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
74
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
75
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
76
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
77
|
+
|
|
78
|
+
/* Type Checking */
|
|
79
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
80
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
81
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
82
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
83
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
84
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
85
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
86
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
87
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
88
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
89
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
90
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
91
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
92
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
93
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
94
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
95
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
96
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
97
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
98
|
+
|
|
99
|
+
/* Completeness */
|
|
100
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
101
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
102
|
+
}
|
|
103
|
+
}
|