resourceful example
This commit is contained in:
@ -286,7 +286,7 @@ Here's an example of a REST collection (users). The config:
|
|||||||
|
|
||||||
### Stored config
|
### Stored config
|
||||||
|
|
||||||
Use the same port and blacklist every time? Persist it to `package.json`:
|
Use the same options every time? Persist then to `package.json`:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"name": "example",
|
"name": "example",
|
||||||
@ -306,7 +306,7 @@ or `.local-web-server.json`
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
local-web-server will merge and use all config found, searching from the current directory upward. In the case both `package.json` and `.local-web-server.json` config is found in the same directory, `.local-web-server.json` will take precedence. Command-line options take precedence over all.
|
local-web-server will merge and use all config found, searching from the current directory upward. In the case both `package.json` and `.local-web-server.json` config is found in the same directory, `.local-web-server.json` will take precedence. Options set on the command line take precedence over all.
|
||||||
|
|
||||||
To inspect stored config, run:
|
To inspect stored config, run:
|
||||||
```sh
|
```sh
|
||||||
|
@ -49,6 +49,10 @@
|
|||||||
{
|
{
|
||||||
"route": "/users",
|
"route": "/users",
|
||||||
"module": "/mocks/users.js"
|
"module": "/mocks/users.js"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"route": "/users/:id",
|
||||||
|
"module": "/mocks/user.js"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"one": "static data"
|
|
||||||
}
|
|
41
example/mock/mocks/user.js
Normal file
41
example/mock/mocks/user.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
const users = require('./users.json')
|
||||||
|
function getUser(id) {
|
||||||
|
return users.find(user => user.id === Number(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
/* responses for /users/:id */
|
||||||
|
const mockResponses = [
|
||||||
|
/* don't support POST */
|
||||||
|
{ request: { method: 'POST' }, response: { status: 400 } },
|
||||||
|
|
||||||
|
/* for GET requests, return a particular user */
|
||||||
|
{
|
||||||
|
request: { method: 'GET' },
|
||||||
|
response: function (ctx, id) {
|
||||||
|
ctx.body = users.find(user => user.id === Number(id))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/* for PUT requests, update the record */
|
||||||
|
{
|
||||||
|
request: { method: 'PUT' },
|
||||||
|
response: function (ctx, id) {
|
||||||
|
const updatedUser = ctx.request.body
|
||||||
|
const existingUserIndex = users.findIndex(user => user.id === Number(id))
|
||||||
|
users.splice(existingUserIndex, 1, updatedUser)
|
||||||
|
ctx.status = 200
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/* DELETE request: remove the record */
|
||||||
|
{
|
||||||
|
request: { method: 'DELETE' },
|
||||||
|
response: function (ctx, id) {
|
||||||
|
const existingUserIndex = users.findIndex(user => user.id === Number(id))
|
||||||
|
users.splice(existingUserIndex, 1)
|
||||||
|
ctx.status = 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
module.exports = mockResponses
|
@ -1,9 +1,6 @@
|
|||||||
const users = [
|
const users = require('./users.json')
|
||||||
{ id: 1, name: 'Lloyd', age: 40, nationality: 'English' },
|
|
||||||
{ id: 2, name: 'Mona', age: 34, nationality: 'Palestinian' },
|
|
||||||
{ id: 3, name: 'Francesco', age: 24, nationality: 'Italian' }
|
|
||||||
]
|
|
||||||
|
|
||||||
|
/* responses for /users */
|
||||||
const mockResponses = [
|
const mockResponses = [
|
||||||
{ request: { method: 'PUT' }, response: { status: 400 } },
|
{ request: { method: 'PUT' }, response: { status: 400 } },
|
||||||
{ request: { method: 'DELETE' }, response: { status: 400 } },
|
{ request: { method: 'DELETE' }, response: { status: 400 } },
|
||||||
@ -12,7 +9,7 @@ const mockResponses = [
|
|||||||
request: { method: 'GET' },
|
request: { method: 'GET' },
|
||||||
response: function (ctx) {
|
response: function (ctx) {
|
||||||
ctx.body = users.filter(user => {
|
ctx.body = users.filter(user => {
|
||||||
const meetsMinAge = user.age >= (Number(ctx.query.minAge) || 0)
|
const meetsMinAge = (user.age || 1000) >= (Number(ctx.query.minAge) || 0)
|
||||||
const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
|
const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
|
||||||
return meetsMinAge && requiredNationality
|
return meetsMinAge && requiredNationality
|
||||||
})
|
})
|
||||||
|
5
example/mock/mocks/users.json
Normal file
5
example/mock/mocks/users.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[
|
||||||
|
{ "id": 1, "name": "Lloyd", "age": 40, "nationality": "English" },
|
||||||
|
{ "id": 2, "name": "Mona", "age": 34, "nationality": "Palestinian" },
|
||||||
|
{ "id": 3, "name": "Francesco", "age": 24, "nationality": "Italian" }
|
||||||
|
]
|
@ -286,7 +286,7 @@ Here's an example of a REST collection (users). The config:
|
|||||||
|
|
||||||
### Stored config
|
### Stored config
|
||||||
|
|
||||||
Use the same port and blacklist every time? Persist it to `package.json`:
|
Use the same options every time? Persist then to `package.json`:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"name": "example",
|
"name": "example",
|
||||||
@ -306,7 +306,7 @@ or `.local-web-server.json`
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
local-web-server will merge and use all config found, searching from the current directory upward. In the case both `package.json` and `.local-web-server.json` config is found in the same directory, `.local-web-server.json` will take precedence. Command-line options take precedence over all.
|
local-web-server will merge and use all config found, searching from the current directory upward. In the case both `package.json` and `.local-web-server.json` config is found in the same directory, `.local-web-server.json` will take precedence. Options set on the command line take precedence over all.
|
||||||
|
|
||||||
To inspect stored config, run:
|
To inspect stored config, run:
|
||||||
```sh
|
```sh
|
||||||
|
@ -56,7 +56,8 @@
|
|||||||
"typical": "^2.4.0"
|
"typical": "^2.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"req-then": "^0.2.2",
|
"jsdoc-to-markdown": "^1.2.1",
|
||||||
|
"req-then": "^0.2.4",
|
||||||
"tape": "^4.2.2"
|
"tape": "^4.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user