Browse Source

resourceful example

master
Lloyd Brookes 9 years ago
parent
commit
5ff91fe2ee
  1. 4
      README.md
  2. 4
      example/mock/.local-web-server.json
  3. 3
      example/mock/mocks/data.json
  4. 41
      example/mock/mocks/user.js
  5. 9
      example/mock/mocks/users.js
  6. 5
      example/mock/mocks/users.json
  7. 4
      jsdoc2md/README.hbs
  8. 3
      package.json

4
README.md

@ -286,7 +286,7 @@ Here's an example of a REST collection (users). The 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
{
"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:
```sh

4
example/mock/.local-web-server.json

@ -49,6 +49,10 @@
{
"route": "/users",
"module": "/mocks/users.js"
},
{
"route": "/users/:id",
"module": "/mocks/user.js"
}
]
}

3
example/mock/mocks/data.json

@ -1,3 +0,0 @@
{
"one": "static data"
}

41
example/mock/mocks/user.js

@ -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

9
example/mock/mocks/users.js

@ -1,9 +1,6 @@
const users = [
{ id: 1, name: 'Lloyd', age: 40, nationality: 'English' },
{ id: 2, name: 'Mona', age: 34, nationality: 'Palestinian' },
{ id: 3, name: 'Francesco', age: 24, nationality: 'Italian' }
]
const users = require('./users.json')
/* responses for /users */
const mockResponses = [
{ request: { method: 'PUT' }, response: { status: 400 } },
{ request: { method: 'DELETE' }, response: { status: 400 } },
@ -12,7 +9,7 @@ const mockResponses = [
request: { method: 'GET' },
response: function (ctx) {
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)
return meetsMinAge && requiredNationality
})

5
example/mock/mocks/users.json

@ -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" }
]

4
jsdoc2md/README.hbs

@ -286,7 +286,7 @@ Here's an example of a REST collection (users). The 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
{
"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:
```sh

3
package.json

@ -56,7 +56,8 @@
"typical": "^2.4.0"
},
"devDependencies": {
"req-then": "^0.2.2",
"jsdoc-to-markdown": "^1.2.1",
"req-then": "^0.2.4",
"tape": "^4.2.2"
}
}
Loading…
Cancel
Save