Skip to content
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

Add status code decorator #438

Merged
merged 4 commits into from
Feb 24, 2025
Merged

Conversation

Adrianmjim
Copy link
Member

Added

  • Add statusCode decorator

Motivation

Continuing with industry standards, it’s interesting to have a decorator that allows changing the status code of the request without it being handled in the body of the function.

Copy link
Member

@notaphplover notaphplover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I added some unrelated suggestions you can apply in other PR :)

): unknown {
let body: object | string | number | boolean | undefined = undefined;
let statusCode: HttpStatusCode | undefined = undefined;
let httpStatusCode: HttpStatusCode | undefined = statusCode;

if (value instanceof HttpResponse) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad for not seeing this before, never use instanceof on non native types, a user might end up with multiple versions of the core. Instead, consider an is static method the same way AppError does

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if (statusCode !== undefined) {
this._setStatus(request, response, statusCode);
if (httpStatusCode !== undefined) {
this._setStatus(request, response, httpStatusCode);
}

if (typeof body === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rely on the Accept header or delegate in the integration to decide whether or not string (text/plain mime type) or json (application/json mime) are used

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That a good design question. We have 2 ways:

  • Delegate to http drive
  • Get agnostic response

Second option is the current implementation. idk what option is better, for me it's just a approach

@@ -2,8 +2,11 @@ export const METADATA_KEY: {
controller: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rely on multiple constants instead of in a single object. Consider @inversifyjs/core reflect keys as reference

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@notaphplover notaphplover self-requested a review February 24, 2025 14:44
@notaphplover notaphplover merged commit a76c868 into main Feb 24, 2025
5 checks passed
@notaphplover notaphplover deleted the feat/add-status-code-decorator branch February 24, 2025 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants