Issue
I have monorepo with multiple clients and I try figure out how to manage axios in this project.
Assumptions:
- Multiple clients may or may not have the same interceptors,
- clients have shared API endpoints (shared composables i.e. user.composable -> getUser(): IUser), but also have own ( user.composable -> getClientXData(): unknown) which cannot be compiled and visible at others builds.
monorepo/
├─ apps/
│ ├─ client1/
│ │ ├─ composables/
│ │ │ ├─ user.composable.ts
│ ├─ client2/
│ │ ├─ composables/
│ │ │ ├─ user.composable.ts
│ ├─ client3/
│ │ ├─ composables/
│ │ │ ├─ user.composable.ts
├─ packages/
│ ├─ composables/
│ │ ├─ user.composable.ts
│ ├─ models/
│ │ ├─ IUser.ts
Do you have any ideas how to manage this?
Solution
I would have create an axios service package which any app can consume, but not exactly as it sounds.
First, avoid adding interceptors on the default axios instance. Instead, create new instances which any app will consume by it own needs. (you can also defined shared interceptors for them on the default axios instance)
In addition, I might follow OOP principals with Factory Design Pattern. I would have create a class with all shared API endpoints, and some other classes for each app or behavior which extends the base class with it own API endpoints. now any app can consume the axios-service and get it relevant endpoints the property it pass to the factory.
Answered By - Yarin Barnes Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.