decorators to expose controllers and routes
juliosouzam opened this issue · 1 comments
juliosouzam commented
Possibility to use decorators
import { controller, expose } from './decorators';
type User = {
id: number;
name: string;
password: string;
};
interface IUserRepository {
findByEmail(email: string): User | undefined;
}
interface CredentialRequest {
email: string;
password: string;
}
@controller()
class LoginController {
constructor(private readonly userRepositories: IUserRepository) {}
@expose()
public postLogin(createndials: CredentialRequest) {
const { email, password } = createndials;
const user = this.userRepositories.findByEmail(email);
if (!user) {
throw new Error('User not found');
}
const token = this.generateToken({ email, password });
return { token };
}
private generateToken({
email,
password,
}: {
email: string;
password: string;
}) {
return `${email}.${password}`;
}
}
- Can use
class
to set context. - Better code separation.
- Easier tests to be applied.
- Possibility of separating rules by domain.
lbguilherme commented
I don't think it fits well with the developer experience of sdkgen. You shouldn't have to write TypeScript types or mark functions as exposed. Instead, you write the API description and it is handled for you. Changing this concept transforms it into something else entirely. If that's the goal, then Nest.js is already a good alternative.
I'm closing this issue. If someone else thinks this is good and wants to propose a transition plan, please comment bellow.