interface Logger { info: (msg: string) => void }
class ConsoleLogger implements Logger { info(msg: string): void { console.log("[Console]:", msg) } }
class PinoLogger implements Logger { info(msg: string): void { console.log("[Pino]:" , msg) } }
interface UserData {
name: string
}
class AuthService {
async getUserData(): Promise<UserData> {
return { name: "Big Lebowski" }
}
}
class User {
constructor(private data: UserData) {}
name = () => this.data.name
}
class PaymentService {
constructor(private readonly logger: Logger, private readonly user: User) {}
sendMoney() {
this.logger.info(`Sending monery to the: ${this.user.name()} `)
return true
}
}
export async function runMyApp() {
const logger =
process.env.NODE_ENV === "production"
? new PinoLogger()
: new ConsoleLogger()
const auth = new AuthService()
const user = new User(await auth.getUserData())
const paymentService = new PaymentService(logger, user)
paymentService.sendMoney()
}
console.log(" ---- My App START \n\n")
runMyApp().then(() => {
console.log("\n\n ---- My App END")
})