|
| 1 | +// |
| 2 | +// UserService.js |
| 3 | +// PureMVC JS Demo - EmployeeAdmin Microservice |
| 4 | +// |
| 5 | +// Copyright(c) 2023 Saad Shams <[email protected]> |
| 6 | +// Your reuse is governed by the Creative Commons Attribution 3.0 License |
| 7 | +// |
| 8 | + |
| 9 | +import https from "node:https"; |
| 10 | + |
| 11 | +export class UserService { |
| 12 | + |
| 13 | + constructor() {} |
| 14 | + |
| 15 | + // Simple Use Case: |
| 16 | + // If the data is sourced externally, the Proxy first checks the database (UserData) to see if the data is |
| 17 | + // available. If available, it returns the cached data to the Mediator. Otherwise, it contacts the UserService. |
| 18 | + // Service classes retrieve data from external sources, manage network requests and parsing, and return data to |
| 19 | + // the Proxy, which then caches it in the database for future use before returning it to the Mediator. |
| 20 | + |
| 21 | + // Complex Use Case: |
| 22 | + // A Command will come into play if the data retrieval involves multiple proxies and data sources, |
| 23 | + // typically if the logic spans different domains. |
| 24 | + // The Command encapsulates business logic, manages data retrieval, performs data transformation, and |
| 25 | + // enforces business rules before returning the final data to the Mediator and its component. |
| 26 | + findAllUsers() { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + https.request({ |
| 29 | + method: "GET", hostname: "https://jsonplaceholder.typicode.com", path: "/users" |
| 30 | + }, response => { |
| 31 | + let buffers = []; |
| 32 | + response.on("data", data => buffers.push(data)); |
| 33 | + response.on("end", () => { |
| 34 | + try { |
| 35 | + if(response.statusCode === 200) |
| 36 | + resolve(JSON.parse(Buffer.concat(buffers).toString())); |
| 37 | + else |
| 38 | + reject(JSON.parse(Buffer.concat(buffers).toString())); |
| 39 | + } catch(error) { reject(error); } |
| 40 | + }); |
| 41 | + }).on("error", reject).end(); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | +} |
0 commit comments