-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api): domain functions handler core #485
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
|
Deploy preview for codeimage-ui-dev ready! ✅ Preview Built with commit d6b32e4. |
Deploy preview for codeimage-highlight-dev ready! ✅ Preview Built with commit d6b32e4. |
Deploy preview for codeimage ready! ✅ Preview Built with commit d6b32e4. |
Deploy preview for codeimage-website-dev ready! ✅ Preview Built with commit d6b32e4. |
2e03849
to
0bf2658
Compare
0206acc
to
3ac27fa
Compare
…codeimage into feat/api-domain-functions
get handlers(): ResolvedDomainHandlerMap<T> { | ||
return Object.fromEntries( | ||
this.#events.entries(), | ||
) as unknown as ResolvedDomainHandlerMap<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@riccardoperra what do you think instead cast to unknown if we create a function to get typed entries?
something like this:
declare type Entries<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T][];
export const entries = <
T extends Record<string, unknown> | Map<string, unknown>,
>(
obj: T,
): Entries<T> => Object.entries(obj) as any;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this works because the #entries type is Map<string, GenericHandler>, we need to force the cast because it should resolve the type based on "T", assuming that you are registering ALL handlers you are defining.
Thinks of that Registry
contains handlers from multiple modules, and you can call the registryHandlers
from difference files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hackpirodev I also noticed that the current implementation doesn't need as unknown
, so it's basically the same. we can remove it using the same fromEntries() fn
* feat(api): handler core * feat(api): finish impl * feat(api): handler builder * feat(api): simplify code * feat(api): improve type performance * feat(api): handler core * feat(app): cleanup code
Handlers
Handlers are domain scoped functions that can be used to extract business logic and make it independent by the rest of the application.
They are registered into a global registry map
HandlerRegistry
which exposes anadd
method to register a new handler, and aevent
property to retrieve the registered handlersCreating new handlers
Handlers can be created through the
HandlerBuilder
which use the builder pattern under the hood. In order to create the handler correctly you must define the dependencies, the handler name and it's implementation.Registering handlers
Once you have created your handlers, you can register all of them through the
registerHandlers
helper, which accept a list of handlers, the requested dependencies and the registryHandlers can be used from both
handlers
orregistry
mapUsing in Fastify