Using API mocking tools in the browser helps front-end developers to build web application without having to rely on any backend services.
Mirage runs a mock server alongside the rest of your frontend JavaScript code and existing code dose not need to be changed. It also provides many rich tools for data modelling.
8. What’s The Next Step
Wait for the backend side to complete the API development
A
Do the API development yourself
B
Add mock data to the API request functions in the client side
C Add mock data to the API request functions in the client side
C
8
9. API Function Example
// The fake function
function fetchProducts() {
return new Promise((resolve) => {
setTimeout(() => {
resolve([
{ id: 1, name: 'Orange' },
{ id: 2, name: 'Banana' },
]);
}, 400);
});
}
// The actual function
function fetchProducts() {
return fetch('/api/products');
}
Must be changed to look like the right
one after the API is completed
9
12. What’s Mirage
Mirage
− is an API mocking library running in the browser
− runs alongside the rest of your frontend JavaScript code
− uses an in-memory database to maintain the data
12
14. API Request and Response
HTTP Request
GET/POST/PUT/DELETE ...
HTTP Response
JSON Data
Client Server
Web App
API
Controller
Data
fetch
XMLHttpRequest
14
15. With Mirage Server In The Client
Client
Mock API Mock Data
Fake HTTP Response
GET /api/products
Web App
fetch
XMLHttpRequest
15
In-Memory DB
23. Project Folder Structure
scripts
ㄴ app.js
mock
ㄴ routes
ㄴ product
ㄴ list.route.js
ㄴ detail.route.js
ㄴ server.js
ㄴ settings.dev.js
index.html
✓ Include configurations such as the
response time and enabled routes in
/mock/settings.dev.js
✓ Put route files under /mock/routes
✓ Add subfolders and route files for
different features
✓ No need to update /mock/server.js
when new routes added
23