-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
82 changed files
with
1,603 additions
and
636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# anthill-framework Code Of Conduct | ||
# anthill-framework Code Of Conduct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# anthill-framework useful snippets | ||
|
||
## Run one test | ||
```bash | ||
node ./node_modules/jest/bin/jest.js -i ./src/tests/rest-handler-decorator.test.ts -c ./jest.config.ts -t "decorator add handler to anthill" | ||
``` | ||
|
||
## Run a test file | ||
```bash | ||
node ./node_modules/jest/bin/jest.js -i ./src/tests/rest-handler-decorator.test.ts -c ./jest.config.ts | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { AHException } from "../framework/features/anthill-exception"; | ||
import { AHDependencyContainerMap } from "./models/dependency-container-map"; | ||
|
||
export class AHDependencyContainer { | ||
private container: AHDependencyContainerMap = {}; | ||
|
||
/** | ||
* Register a contructor in order to be able to instanciate it on demand later | ||
* @param identifier The constructor (i.e. class) identifier | ||
* @param constructor The constructor function | ||
*/ | ||
register<T>(identifier: string, constructor: new () => T): void { | ||
if (!Object.keys(this.container).includes(identifier)) { | ||
this.container[identifier] = { | ||
constructor: constructor, | ||
instance: null, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Resolve a given constructor (i.e. class) identifier to the matching instance | ||
* @param identifier The constructor (i.e. class) identifier | ||
* @returns The instance matching with constructor identifier | ||
*/ | ||
resolve<T>(identifier: string): T { | ||
if (!Object.keys(this.container).includes(identifier)) { | ||
throw new AHException(`Dependency ${identifier} has never been registered`); | ||
} | ||
|
||
if (!this.container[identifier].instance) { | ||
this.container[identifier].instance = new this.container[identifier].constructor(); | ||
} | ||
|
||
return this.container[identifier].instance as T; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type AHConstructible<T> = { new (...args: any[]): T }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { AHLambdaHandlerOverridableConfig } from "../../../framework/models/handler/lambda-handler-overridable-config"; | ||
import { AHConstructible } from "../constructible"; | ||
|
||
export type AHLambdaControllerClass<T> = AHConstructible<T> & { | ||
_lambdaHandlerConfig: AHLambdaHandlerOverridableConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { AHRestHandlerOverridableConfig } from "../../../framework/models/handler/rest-handler-overridable-config"; | ||
import { AHConstructible } from "../constructible"; | ||
|
||
export type AHRestControllerClass<T> = AHConstructible<T> & { | ||
_restHandlerConfig: AHRestHandlerOverridableConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface AHDependencyContainerMap { | ||
[key: string]: { | ||
constructor: new () => any; | ||
instance: any; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export enum AHTimeTrackerStateEnum { | ||
Started, | ||
Stopped, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { AHCallable } from "../../../framework/models/handler/callable"; | ||
import { AHHandlerOptions } from "../../../framework/models/handler/handler-options"; | ||
|
||
export interface AHAbstractHandlerConfig<T, U> { | ||
controllerName: Promise<string>; | ||
name: string; | ||
callable: AHCallable<T, U>; | ||
options?: AHHandlerOptions; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/core/models/handler/abstract-handler-overridable-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AHHandlerOptions } from "../../../framework/models/handler/handler-options"; | ||
|
||
export interface AHAbstractHandlerOverridableConfig { | ||
options?: AHHandlerOptions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface AHMultiLevelHandlerConfig<T> { | ||
anthill?: T; | ||
controller?: T; | ||
handler?: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AHLambdaHandlerOverridableConfig } from "../models/handler/lambda-handler-overridable-config"; | ||
import { AHLambdaControllerClass } from "../../core/models/controller-class/lambda-controller-class"; | ||
import { Anthill } from "../features/anthill"; | ||
|
||
/** | ||
* Lambda controller decorator used to decorate controller classes | ||
* @param lambdaControllerOptions Lambda handler options that will be applied to all handlers inside the controller class | ||
* @returns The lambda controller decorator | ||
*/ | ||
export function LambdaController(lambdaControllerOptions: AHLambdaHandlerOverridableConfig = {}): any { | ||
return <T extends AHLambdaControllerClass<T>>(target: T, context: ClassDecoratorContext<T>) => { | ||
Anthill.getInstance()._dependencyContainer.register<T>(context.name, target); | ||
Anthill.getInstance()._dependencyContainer.resolve<T>(context.name)._lambdaHandlerConfig = lambdaControllerOptions; | ||
|
||
return target; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Anthill } from "../features/anthill"; | ||
import { AHLambdaHandler } from "../features/handler/lambda-handler"; | ||
import { AHLambdaHandlerConfig } from "../models/handler/lambda-handler-config"; | ||
|
||
/** | ||
* Lambda handler decorator used to decorate handler method inside controller classes | ||
* @param lambdaHandlerOptions Lambda handler options that will be applied to this handler | ||
* @returns The lambda handler decorator | ||
*/ | ||
export function LambdaHandler<T, A extends [any, ...undefined[]], R extends Promise<any>>( | ||
lambdaHandlerOptions: Partial<AHLambdaHandlerConfig<any, any>> = {}, | ||
) { | ||
|
||
return (target: (this: T, ...args: A) => R, context: ClassMethodDecoratorContext<T, (this: T, ...args: A) => R>) => { | ||
if (!lambdaHandlerOptions.name) { | ||
lambdaHandlerOptions.name = String(context.name); | ||
} | ||
|
||
const controllerNamePromise = new Promise<any>((resolve) => { | ||
context.addInitializer(function () { | ||
if (typeof this === "object") { | ||
// Instance method | ||
return resolve(this.constructor.name); | ||
} | ||
|
||
// static method | ||
return resolve((this as any).name); | ||
}); | ||
}); | ||
|
||
const _lambdaHandlerOptions: AHLambdaHandlerConfig<any, any> = { | ||
controllerName: controllerNamePromise, | ||
name: lambdaHandlerOptions.name, | ||
callable: target as any, | ||
...lambdaHandlerOptions, | ||
}; | ||
|
||
const lambdaHandler = new AHLambdaHandler(_lambdaHandlerOptions); | ||
Anthill.getInstance()._registerHandler(lambdaHandler); | ||
return target; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AHRestControllerClass } from "../../core/models/controller-class/rest-controller-class"; | ||
import { Anthill } from "../features/anthill"; | ||
import { AHRestHandlerOverridableConfig } from "../models/handler/rest-handler-overridable-config"; | ||
|
||
/** | ||
* REST controller decorator used to decorate controller classes | ||
* @param restControllerOptions REST handler options that will be applied to all handlers inside the controller class | ||
* @returns The REST controller decorator | ||
*/ | ||
export function RestController(restControllerOptions: AHRestHandlerOverridableConfig = {}): any { | ||
return <T extends AHRestControllerClass<T>>(target: T, context: ClassDecoratorContext<T>) => { | ||
Anthill.getInstance()._dependencyContainer.register<T>(context.name, target); | ||
Anthill.getInstance()._dependencyContainer.resolve<T>(context.name)._restHandlerConfig = restControllerOptions; | ||
|
||
return target; | ||
}; | ||
} |
Oops, something went wrong.