From 5ff91fe2eeb8b3919f9ebd6150aecd25518a3520 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 28 Nov 2015 13:45:30 +0000 Subject: [PATCH] resourceful example --- README.md | 4 ++-- example/mock/.local-web-server.json | 4 ++++ example/mock/mocks/data.json | 3 --- example/mock/mocks/user.js | 41 +++++++++++++++++++++++++++++++++++++ example/mock/mocks/users.js | 9 +++----- example/mock/mocks/users.json | 5 +++++ jsdoc2md/README.hbs | 4 ++-- package.json | 3 ++- 8 files changed, 59 insertions(+), 14 deletions(-) delete mode 100644 example/mock/mocks/data.json create mode 100644 example/mock/mocks/user.js create mode 100644 example/mock/mocks/users.json diff --git a/README.md b/README.md index 9b1db0e..87309b1 100644 --- a/README.md +++ b/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 diff --git a/example/mock/.local-web-server.json b/example/mock/.local-web-server.json index fe66001..736460e 100644 --- a/example/mock/.local-web-server.json +++ b/example/mock/.local-web-server.json @@ -49,6 +49,10 @@ { "route": "/users", "module": "/mocks/users.js" + }, + { + "route": "/users/:id", + "module": "/mocks/user.js" } ] } diff --git a/example/mock/mocks/data.json b/example/mock/mocks/data.json deleted file mode 100644 index fe13696..0000000 --- a/example/mock/mocks/data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "one": "static data" -} diff --git a/example/mock/mocks/user.js b/example/mock/mocks/user.js new file mode 100644 index 0000000..576d7a5 --- /dev/null +++ b/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 diff --git a/example/mock/mocks/users.js b/example/mock/mocks/users.js index 08bc7e9..2be8d18 100644 --- a/example/mock/mocks/users.js +++ b/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 }) diff --git a/example/mock/mocks/users.json b/example/mock/mocks/users.json new file mode 100644 index 0000000..09a166b --- /dev/null +++ b/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" } +] diff --git a/jsdoc2md/README.hbs b/jsdoc2md/README.hbs index 0363c94..71cb4df 100644 --- a/jsdoc2md/README.hbs +++ b/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 diff --git a/package.json b/package.json index c46b567..40a01bc 100644 --- a/package.json +++ b/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" } }