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
0.7.0 #119
Conversation
@19majkel94 you need to review this one.... 😮 I'll also need to update the docs. Also alternative names for |
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.
Looks great for me 😉 I wasn't able to read every line of diff but the main changes are checked.
README.md
Outdated
|
||
`typings install dt~koa --save --global` | ||
`typings install @types/koa --save --global` | ||
|
||
5. Its important to set these options in `tsconfig.json` file of your project: |
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.
You have skipped 4.
- use 1.
as guthub markdown will transform it to auto enumerated list.
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.
yes I didnt worked on readme yet, will completely update it tomorrow and let you know once again
* Injects a Session object to the controller action parameter. | ||
* Must be applied on a controller action parameter. | ||
*/ | ||
export function Session(objectName?: string): Function { |
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.
Is the return type necessary there? Isn't type inference enough?
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.
just wanted to do it as an convention - to always put return types...
README.md
Outdated
|
||
`typings install dt~koa --save --global` | ||
`typings install @types/koa --save --global` |
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.
npm install 😆
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.
😆
Oh, I haven't check the last commit from an hour ago. And I don't like the idea of current user and authorization checker 😞 All of earlier commits was to simplify things, remove dead code or rarely used functionality, and now you introduce this. Authorization checker works like a regular middleware but with hardcoded support in core (which is simple and unnecessary). Current user decorator also work's like any custom decorator, like the /**
* Extends routing-controllers and socket-controllers functionality
* and allows to inject object stored in JWT token into controller's actions.
*
* @param {string} propertyName name of the request property where express-jwt place JWT token object
*/
export function JWT(propertyName = "user") {
return (object: object, methodName: string, index: number) => {
const reflectedType = (Reflect as any).getMetadata("design:paramtypes", object, methodName)[index];
// add special param handler to routing-controllers library
defaultMetadataArgsStorage().params.push({
target: object.constructor,
method: methodName,
index,
type: ParamTypes.CUSTOM_CONVERTER,
reflectedType,
parseJson: false,
isRequired: false,
format: reflectedType,
transform: (_, req) => req[propertyName]
});
};
} It adds unnecessary overhead to the codebase like the I would consider changes in param decorator handling - no switch-case in core, but all logic should be in |
I would also consider one breaking change - remove |
@pleerock can I help somehow? If you allow me in discussion... About @currentuser and @authorize My opinion: @authorize sounds like action should authorize user and then allow/disallow access. Also authorization is about rights-roles check, authentication on the other hand is about user existence in the application and his/hers authentication status (anonymous, authenticated) @currentuser is a nice decorator! About @jsoncontroller - why do you both hate it? It looks great! If we would have responses in our actions (but we do not, we have res.status, res.send, res.json etc) we would have something like new Response/new JsonResponse (you do get where I got them from, yes?) But Json is a common format to give responses and get requests in. I'm using it and it really does the trick. Giving options to @controller will make it look really uglier :( Who knows, maybe somebody really wants XmlController in the future :D |
correct.
because this: action(@CurrentUser() currentUser: User) is more elegant then: action(@Req() req: Request)
const currentUser = req.user; plus
only if we go back to the future :) |
@pleerock I agree about elegance. But I discovered that my actions are becoming bloated already with @Body, @Req, @res, @Session etc. I think that's everyones personal choice of course. Having @currentuser is a nice touch to existing parameters decorators. { required: true } will throw error "required argument/user" if user does not exist, right? But not UnauthorizedError or smthn? |
action(@Req() req: Request)
const currentUser = req.user; Welcome in ES6 destruction world 😉 action(@Req() { user }: Request) I've used object destruction also on response object: action(@Param("id") id: number, @Res() { sendFile }: express.Response) But it complicates unit testing of controllers - you have to pass whole @Res() { sendFile }: { sendFile: express.Response["sendFile"] }) I would love to see the parametrized version of @Res("sendFile") sendFile: express.Response["sendFile"]) With this developers could do the action(@Req("user") currentUser: User) |
And again I really don't like an idea of export function CurrentUser() {
return registerParamDecorator({
required: true,
value: actionProperties => actionProperties.request.user,
});
}
|
You have good commit messages but yes, you've missed |
@idchlife it will throw UnauthorizedError
goal of routing-controllers is to avoid using req and res as much as possible :) I would like
if problem is in number of loc, don't worry... We'll need to refactor routing-controllers codebase. I already made a small refactoring, but it is not enough. Im not satisfied with routing-controllers codebase. It lacks of good abstraction and slightly complicated. We'll do that in the future. |
I released 0.7.0.alpha in |
Agree, but express has so many properties or getter methods on Also, there are many use-case when returning json is not enough, like |
@19majkel94 you could just write a custom middleware function that merges all the |
I don't like that people store in "req" whatever they want. First of all req is a type of
regarding to |
Wait... what?! From express doc about
So I don't think it's a good idea. Sure, you can do this, but what's the purpose? Why this: action(@Params() params: any) {
const userId = params.session.user.id;
} is better than simply:
Can you tell me @marshall007 ? 😉
I haven't talked about this case. I use in my project the
It should work the same way as the @Get("/files/:id")
@SendFile(options)
getFile(@Param("id") id: number) {
return getFilePathById(id);
} So just calling return new Promise<void>((resolve, reject) => {
res.sendFile(filePath), (err: any) => err ? resolve() : reject(err));
}); BTW, there is a "typo" in readme - copy-paste left 😉 |
action(@Req() { hostname }: Request) @19majkel94 the reason it's better to be explicit with your For example, if the reflection metadata tells you that I'm planning to work on tooling for extracting this metadata from |
Ok but it's not the case - |
but what if range will use I think these decorators deserve separate issue and PR for them. Also better name for
no, you should not. Just use |
Just use export function extractReqProp<K extends keyof Request>(propName: K, req: Request): Request[K] {
const prop = req[propName] as object|Function;
if (typeof prop === "function") {
return prop.bind(req);
} else {
return prop;
}
}
Yeah, it's more clear but as you said:
Also @marshall007 said that the goal is:
So
It's just a name of Response method. More intuitive for express user than a fancy name not matching the method. But the best way would be to introduce custom method decorators which could work just like |
everything is relative, right?) avoid, but not for such a dirty operations. |
@19majkel94 in that case you would want to use the |
It can't be fully parsed AOT, it needs
In term of self-described API - yes, but the goal of routing-controllers and DI is to provide an method API which focus on the problem (return entity by id) not the way to achieve it (get a param, parse it to get value). It's hard to test if you have to create a http header string instead of pass simple { start, end } object`. The same problem goes to JWT or Session with your solution - I should use I have an idea of action(@Range(options) reqRange) {
const maxSize = getFromSomewhere();
const range = reqRange(maxSize);
// do something with array of parsed range from headers
} where |
…lers, fixed issue with koa
Update auto-validation Readme paragraph + typo
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
No description provided.