mardi 23 avril 2019

Design Pattern to easily switch between local JSON and http call - Angular

I am working on implementing a design where a developer can easily switch between a local JSON file or its corresponding API.

Scenario :

  1. We have https://jsonplaceholder.typicode.com/todos/1 with corresponding json as user1.json.

  2. We have https://reqres.in/api/users/2 with user2.json

I have created a config.json which has a mapping :

[
  {
    "url":"https://jsonplaceholder.typicode.com/todos/1",
    "json" : "/assets/user1.json",
    "local": true
  },
  {
    "url": "https://reqres.in/api/users/2",
    "json" : "assets/user2.json",
    "local": false
  }
]

Now, if you toggle local property of each object, you can see that it toggles between local JSON and REST API calls.

I would really like to get some feedback on my implementation and get to know about potential problems which I might face(if any) in future with this design

Here is the implementation which I have thought about

Aucun commentaire:

Enregistrer un commentaire