Browse Source

docs.. mock responses API example

master
Lloyd Brookes 9 years ago
parent
commit
383d48473c
  1. 4
      README.md
  2. 4
      example/mock/.local-web-server.json
  3. 34
      example/mock/mocks/users.js
  4. 4
      jsdoc2md/README.hbs
  5. 6
      lib/middleware.js

4
README.md

@ -21,7 +21,7 @@ A simple web-server for productive front-end development. Typical use cases:
* Mocks are defined with config (static), or code (dynamic).
* CORS-friendly, all origins allowed by default.
* Proxy server
* Map local routes to remote services. Removes CORS issues with remote servers.
* Map local routes to remote servers. Removes CORS pain when consuming remote services.
* File sharing
**Requires node v4.0.0 or higher**.
@ -97,7 +97,7 @@ You're building a web app with client-side routing, so mark `index.html` as the
$ ws --spa index.html
```
By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
By default, typical SPA paths (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
*If a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*

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

@ -45,6 +45,10 @@
{
"route": "/five/:id\\?name=:name",
"module": "/mocks/five.js"
},
{
"route": "/users*",
"module": "/mocks/users.js"
}
]
}

34
example/mock/mocks/users.js

@ -0,0 +1,34 @@
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 mockResponses = [
{ request: { method: 'PUT' }, response: { status: 400 } },
{ request: { method: 'DELETE' }, response: { status: 400 } },
{
/* for GET requests, return a subset of data filtered on 'minAge' and 'nationality' */
request: { method: 'GET' },
response: function (ctx) {
ctx.body = users.filter(user => {
const meetsMinAge = user.age >= (Number(ctx.query.minAge) || 0)
const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
return meetsMinAge && requiredNationality
})
}
},
{
/* for POST requests, create a new user with the request data (a JSON user) */
request: { method: 'POST' },
response: function (ctx) {
const newUser = ctx.request.body
users.push(newUser)
newUser.id = users.length
ctx.status = 201
ctx.response.set('Location', `/users/${newUser.id}`)
}
}
]
module.exports = mockResponses

4
jsdoc2md/README.hbs

@ -21,7 +21,7 @@ A simple web-server for productive front-end development. Typical use cases:
* Mocks are defined with config (static), or code (dynamic).
* CORS-friendly, all origins allowed by default.
* Proxy server
* Map local routes to remote services. Removes CORS issues with remote servers.
* Map local routes to remote servers. Removes CORS pain when consuming remote services.
* File sharing
**Requires node v4.0.0 or higher**.
@ -97,7 +97,7 @@ You're building a web app with client-side routing, so mark `index.html` as the
$ ws --spa index.html
```
By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
By default, typical SPA paths (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
*If a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*

6
lib/middleware.js

@ -101,12 +101,14 @@ function mockResponses (route, targets) {
}
if (target) {
debug('target response: %j', target.response)
debug('target response: %s', target.response)
if (t.isFunction(target.response)) {
const pathMatches = ctx.url.match(pathRe).slice(1)
target.response.apply(null, [ctx].concat(pathMatches))
} else {
} else if (t.isPlainObject(target.response)) {
Object.assign(ctx.response, target.response)
} else {
throw new Error(`Invalid response: ${JSON.stringify(target.response)}`)
}
}

Loading…
Cancel
Save