• Stars
    star
    2,276
  • Rank 20,237 (Top 0.4 %)
  • Language
    TypeScript
  • Created over 3 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React + TypeScript app built using the clean architecture principles in a more functional way.

Other languages: Russian.

Frontend Clean Architecture

A React + TypeScript example app built using the clean architecture in a functional(-ish) way.

Things to Consider

There are a few compromises and simplifications in the code that are worth to be mentioned.

Shared Kernel

Shared Kernel is the code and data on which any modules can depend, but only if this dependency would not increase coupling. More details about the limitations and application are well described in the article "DDD, Hexagonal, Onion, Clean, CQRS, ... How I put it all together".

In this application, the shared kernel includes global type annotations that can be accessed anywhere in the app and by any module. Such types are collected in shared-kernel.d.ts.

Dependency in the Domain

The createOrder function uses the library-like function currentDatetime to specify the order creation date. This is not quite correct, because the domain should not depend on anything.

Ideally, the implementation of the Order type should accept all the necessary data, including the date, from outside. The creation of this entity would be in the application layer in orderProducts:

async function orderProducts(user: User, { products }: Cart) {
  const datetime = currentDatetime();
  const order = new Order(user, products, datetime);

  // ...
}

Use Case Testability

The order creation function orderProduct itself is framework-independent right now and can't be used and tested in isolation from React. The hook wrapper though is only used to provide the use case to components and to inject services into the use case itself.

In a canonical implementation, the function of the use case would be extracted outside the hook, and the services would be passed to the use case via a last argument or a DI:

type Dependencies = {
  notifier?: NotificationService;
  payment?: PaymentService;
  orderStorage?: OrderStorageService;
};

async function orderProducts(
  user: User,
  cart: Cart,
  dependencies: Dependencies = defaultDependencies
) {
  const { notifier, payment, orderStorage } = dependencies;

  // ...
}

Hook would then become an adapter:

function useOrderProducts() {
  const notifier = useNotifier();
  const payment = usePayment();
  const orderStorage = useOrdersStorage();

  return (user: User, cart: Cart) =>
    orderProducts(user, cart, {
      notifier,
      payment,
      orderStorage,
    });
}

In the sources, I thought it was unnecessary, as it would distract from the essence.

Crooked DI

In the application layer we inject services by hand:

export function useAuthenticate() {
  const storage: UserStorageService = useUserStorage();
  const auth: AuthenticationService = useAuth();

  // ...
}

In a good way, this should be automated and done through the dependency injection. But in the case of React and hooks, we can use them as a “container” that returns an implementation of the specified interface.

In this particular application, it didn't make much sense to set up the DI because it would distract from the main topic.

More Repositories

1

refactor-like-a-superhero

How to refactor code efficiently and without pain.
945
star
2

solidbook

Book about the SOLID principles and object-oriented software design.
MDX
599
star
3

tools

Список полезных сервисов для разработчиков.
131
star
4

front-not-pain

How to work with ease and joy. Uplifting guide for frontend developers.
HTML
118
star
5

scroller

Fast, light-weight (4KB gzip), and dependency-free content scroller.
JavaScript
117
star
6

explicit-design

Source code for the “Explicit Design” post series.
TypeScript
91
star
7

www

Source code for my blog.
Svelte
81
star
8

treees

Trees images generator based on L-Systems made with TypeScript using OOP principles.
TypeScript
52
star
9

ttt-tdd

Book about test-driven development with an example of making “Tic-Tac-Toe” by TDD.
HTML
31
star
10

refactor-like-a-superhero-talk

Source code and examples for “Refactor Like a Superhero” talk.
JavaScript
29
star
11

text-generator

A naive text generator built in JavaScript using Markov chains.
JavaScript
26
star
12

testing-workshop

Source code samples for the workshop about testing React applications.
JavaScript
23
star
13

you-really-dont-need-redux-now

Redux vs Context API + Hooks comparison article.
JavaScript
20
star
14

react-scroller

React component wrapper for Scroller.
JavaScript
20
star
15

di-ts-in-practice

A sample app for a post about dependency injection with TypeScript.
TypeScript
18
star
16

rule-based-data-validation

Declarative data validation made with rule-based approach and functional programming.
TypeScript
17
star
17

fsm-example

Finite state machine example written in vanilla Javascript.
JavaScript
15
star
18

tmstmp

Converts timestamps to dates and backwards.
TypeScript
13
star
19

vue-scroller

Vue component wrapper for Scroller.
Vue
7
star
20

web-worker-example

Using Web Workers to Boost Performance.
CSS
7
star
21

binary-full-adder-in-the-game-of-life

Binary adder implementation in the Game of Life written in JavaScript using canvas.
JavaScript
6
star
22

bespoyasov

Hello · Hallå · Привет!
5
star
23

utils

Javascript helper-functions
JavaScript
5
star
24

kursovik-redux-electron

App showing $/₽ exchange
JavaScript
3
star
25

traktor-html-css-workshop

Source code samples for the HTML & CSS workshop at Traktor School.
HTML
2
star
26

clickme

“Click me!“ implementation made with RxJS.
TypeScript
2
star
27

react-piano-next

A virtual piano keyboard built with React and deployed with Vercel (former Next).
TypeScript
2
star
28

next-app-deployment

The example Next app, that we're going to deploy on Vercel, Heroku and a custom static server.
TypeScript
2
star
29

shitty-advices

HTML
1
star
30

kursovik-rn-expo

Rebuilt the Kursovik app using React Native and Expo because why not.
JavaScript
1
star
31

loose-equals

JavaScript explicit non-strict equality comparator.
JavaScript
1
star
32

wesbos-advanced-react-rerecorded

Let's relearn this stuff.
JavaScript
1
star
33

wesbos-node-course

Node course
JavaScript
1
star
34

bookmark

Crossbrowser aside anchor-navigation
JavaScript
1
star
35

unv-proj

University graduation project
Python
1
star
36

teaching-as-a-negotiation-skill

Слайды к докладу
HTML
1
star
37

figma-tags-finder

A missing tags manager for Figma
TypeScript
1
star
38

morse

Text to Morse code translator.
JavaScript
1
star