[MGNLRESTUI-75] Broken REST Client and REST Client UI demo projects Created: 11/May/23 Updated: 08/Sep/23 |
|
| Status: | Accepted |
| Project: | Magnolia REST Client UI |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major |
| Reporter: | Christopher Zimmermann | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Template: |
|
||||||||||||
| Acceptance criteria: |
Empty
|
||||||||||||
| Task DoR: |
Empty
|
||||||||||||
| Date of First Response: | |||||||||||||
| Epic Link: | RESTClient 2024 | ||||||||||||
| Team: | |||||||||||||
| Description |
|
Several of the REST Client and REST Client UI documented demo projects are no longer working - because the API's have changed. They should be updated. Where possible consider changing to a mock service (or a simple mock file) so that they will not go out of date. Some context: |
| Comments |
| Comment by Scot Rhodes [ 11/May/23 ] |
|
You can base some mock services on something like this policy provider. It's very straight-forward: const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); // Middleware to parse JSON body of incoming requests // Example policy data let policies = [ { policyNumber: 'POL-12345', policyHolder: 'John Doe', effectiveDate: '2023-01-01', expirationDate: '2024-01-01', }, { policyNumber: 'POL-67890', policyHolder: 'Jane Doe', effectiveDate: '2023-02-01', expirationDate: '2024-02-01', }, ]; // Basic Authentication Middleware function basicAuth(req, res, next) { const authHeader = req.headers.authorization; if (!authHeader) { res.setHeader('WWW-Authenticate', 'Basic'); return res.status(401).json({ error: 'Unauthorized' }); } const [username, password] = Buffer.from(authHeader.split(' ')[1], 'base64').toString().split(':'); if (username !== 'superuser' || password !== 'superuser') { res.setHeader('WWW-Authenticate', 'Basic'); return res.status(401).json({ error: 'Invalid credentials' }); } next(); } // Add basic authentication middleware to protect the following routes app.use(basicAuth); // Get a list of policies app.get('/policies', (req, res) => { res.json(policies); }); // Get a policy based on the policy number app.get('/policies/:policyNumber', (req, res) => { const policyNumber = req.params.policyNumber; const policy = policies.find((p) => p.policyNumber === policyNumber); if (!policy) { return res.status(404).json({ error: 'Policy not found' }); } res.json(policy); }); // Add a new policy app.post('/policies', (req, res) => { const newPolicy = req.body; // Validate new policy data if (!newPolicy.policyNumber || !newPolicy.policyHolder || !newPolicy.effectiveDate || !newPolicy.expirationDate) { return res.status(400).json({ error: 'Missing required policy fields' }); } // Check if a policy with the same policy number already exists if (policies.some((p) => p.policyNumber === newPolicy.policyNumber)) { return res.status(400).json({ error: 'Policy with this number already exists' }); } // Add new policy to the array policies.push(newPolicy); // Send the new policy as the response res.status(201).json(newPolicy); }); app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); }); Save that to index.js with a package.json like this: "name": "mock-pc-server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.18.2" } } Then run node index.js |
| Comment by Scot Rhodes [ 11/May/23 ] |
|
Or even better, use Topher's suggestion and use[ JSON Server |https://www.npmjs.com/package/json-server]
|