-
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
1 parent
bfdf281
commit 51294f4
Showing
6 changed files
with
81 additions
and
8 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 |
---|---|---|
@@ -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; | ||
}; | ||
} |
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,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); | ||
}); | ||
}); |
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