Skip to content

Commit

Permalink
RestHandler decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLep committed Sep 11, 2023
1 parent bfdf281 commit 51294f4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/framework/decorators/restHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AHRestHandler } from "../features/handler/rest-handler";
import { Anthill } from "../features/anthill";
import { AHException } from "../features/anthill-exception";
import { AHRestHandlerParams } from "../models/handler/rest-handler-params";


export function RestHandler(restHandlerOptions: Partial<AHRestHandlerParams>): MethodDecorator {
if (!restHandlerOptions.method) {
throw new AHException("@RestHandler Missing rest handler method");
}

return function (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor): PropertyDescriptor {
if (!restHandlerOptions.name) {
restHandlerOptions.name = String(propertyKey);
}

const _restHandlerOptions: AHRestHandlerParams = {
name: restHandlerOptions.name,
method: restHandlerOptions.method,
callable: descriptor.value,
...restHandlerOptions,
};

const restHandler = new AHRestHandler(_restHandlerOptions);
Anthill.getInstance().registerRestHandler(restHandler);

return descriptor;
};
}
4 changes: 2 additions & 2 deletions src/framework/features/anthill.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AHAwsEvent } from "../models/aws/event/aws-event";
import { AHAbstractHandler } from "../../core/abstract-handler";
import { AHAwsContext } from "../models/aws/aws-context";
import { AHException } from "./anthill-exception";
import { AHLambdaHandler } from "./handler/lambda-handler";
import { AHRestHandler } from "./handler/rest-handler";
import { AHCallable } from "../..";

export class Anthill {
private static instance: Anthill;
Expand Down Expand Up @@ -61,7 +61,7 @@ export class Anthill {
* Expose all registred handlers
* @returns All registred handlers mapped inside an object { [handlerName]: handler }
*/
exposeHandlers(): { [handlerName: string]: AHAbstractHandler<any, any> } {
exposeHandlers(): { [handlerName: string]: AHCallable<any, any> } {
const exportObject = {};

// Expose rest handlers
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export { AHJsonBodyParserMiddleware } from "./framework/features/middleware/json
export { AHHeaderFieldMiddleware } from "./framework/features/middleware/header-field-middleware";
export { AHQuerystringFieldMiddleware } from "./framework/features/middleware/querystring-field-middleware";

/**
* DECORATORS
*/

export { RestHandler } from "./framework/decorators/restHandler";

/**
* HELPERS
*/
Expand Down
38 changes: 38 additions & 0 deletions src/tests/rest-handler-decorator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AHAwsContext, AHAwsEvent, AHException, AHHttpResponse, AHPromiseHelper, AHRestMethodEnum, Anthill, RestHandler, anthill } from "..";
import { AHTestResource } from "./resources/test-resource";

describe('RestHandler decorator', () => {
beforeEach(() => {
Anthill["instance"] = null;
});

test('decorator add handler to anthill', () => {
class AHTest {
@RestHandler({
method: AHRestMethodEnum.Get,
})
async listTest(event: AHAwsEvent, context: AHAwsContext): Promise<AHHttpResponse> {
return AHPromiseHelper.promisify(AHHttpResponse.success(null))
}
}

new AHTest();
const app = anthill();
const handlers = app.exposeHandlers();

expect(Object.keys(handlers).includes("listTest")).toBe(true);
expect(handlers.listTest(AHTestResource.getBaseEvent(), AHTestResource.getBaseContext())).resolves.toBeInstanceOf(AHHttpResponse);
});

test('decorator missing mandatory param', () => {
expect(() => {
class AHTest {
@RestHandler({})
async listTest(event: AHAwsEvent, context: AHAwsContext): Promise<AHHttpResponse> {
return AHPromiseHelper.promisify(AHHttpResponse.success(null))
}
}
new AHTest();
}).toThrow(AHException);
});
});
11 changes: 5 additions & 6 deletions todo.todo
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
- Add structural classes
- Handler (that auto register handlers through anthill app)
-
- System for registering handlers with more ease
- register controller (https://spring.io/guides/tutorials/rest/)
-
- helper for extracting info from AHAwsEvent (decorator / validator / DTO in ?)
- query param
- path param
- body param
- ...
- DTO
- spec in (will replace middleware for qs and body check)
- spec out
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"esModuleInterop": true,
"noImplicitAny": false,
"strictNullChecks": false,
"experimentalDecorators": true,
},
"include": [
"src"
Expand Down

0 comments on commit 51294f4

Please sign in to comment.