This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const withRetry = ( | |
func: (...args: any[]) => Promise<any>, | |
maxAttempts: number = 3, | |
timeout: number = 1_000 | |
) => { | |
const sleep = (ms: number) => { | |
return new Promise((resolve) => { | |
return setTimeout(resolve, ms); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const app = express(); | |
const PORT = 8000; | |
const diContainer = configureDI(); | |
configureRouter(app, diContainer) | |
app.listen(PORT, () => { | |
console.log(`⚡️[server]: Server is running at http://localhost:${PORT}`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function configureRouter(app: core.Express, diContainer: IDIContainer) { | |
const usersController = diContainer.get(UsersController); | |
app.route('/users') | |
.get(usersController.actionIndex.bind(usersController)) | |
.post(usersController.actionCreate.bind(usersController)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DIContainer, { IDIContainer, object } from "rsdi"; | |
import { controllers } from "./controllers"; | |
import { repositories } from "./repositories"; | |
import { AppLogger } from "../../lib/Logger"; | |
export default function configureDI(): IDIContainer { | |
const container = new DIContainer(); | |
container.add({ | |
[AppLogger.name]: object(AppLogger), | |
[UsersRepository.name]: object(UsersRepository) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class UsersRepository { | |
async findAll() { | |
return [ | |
{ | |
id: 1, | |
name: "John Doe" | |
}, | |
{ | |
id: 2, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class UsersController { | |
public constructor( | |
private readonly usersRepository: UsersRepository, | |
private readonly logger: AppLogger | |
) {} | |
public async actionIndex(req: Request, res: Response) { | |
try { | |
const users = await this.usersRepository.findAll(); | |
res.send(users); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import usersRepository from "./path/to/user-repo.ts" | |
import logger from "./path/to/getLogger.ts" | |
app.get('/users', function (req, res) { | |
try { | |
const users = usersRepository.findAll(); | |
res.send(users); | |
} catch (e) { | |
logger.error(e); | |
throw new InternalSergerError("Cannot get users"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# step #1 - Inline code inside a consumer | |
# code is simple enough, consumer is simple enough | |
class OrderService { | |
public processOrder(Order order) { | |
// validate input | |
let totalPrice = orderData.items.reduce( (totalPrice, orderItem) => totalPrice + (orderItem.price * orderItem.quantity)); | |
totalPrice = totalPrice + totalPrice * 0.05; // add service commission | |
// process payment | |
// update order state in DB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderManager { | |
public createOrder(orderData): Order { | |
this.validate(orderData); | |
const order = new Order(orderData); | |
order.setTotalPrice(this.calculateTotal(orderData)); | |
this.pay(orderData) | |
await this.save(orderData); | |
return order; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let totalPrice = orderData.items.reduce( (totalPrice, orderItem) => totalPrice + (orderItem.price * orderItem.quantity)); | |
totalPrice = totalPrice + totalPrice * 0.05; // add service commission | |
// vs | |
const total = (new OrderCalculator).calculateTotal(orderData); |
NewerOlder