{"_id":"koa-mock-response","_rev":"8-5849de215d4cb68177a0d1d80b682277","name":"koa-mock-response","description":"Mock a Rest API or any backend service.","dist-tags":{"latest":"1.0.0"},"versions":{"0.0.1":{"name":"koa-mock-response","author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"version":"0.0.1","description":"koa-mock-response","repository":{"type":"git","url":"git+https://github.com/75lb/koa-mock-response.git"},"license":"MIT","main":"./lib/koa-mock-response.js","bin":{"koa-mock-response":"bin/cli.js"},"keywords":[],"engines":{"node":">=0.10.0"},"scripts":{"test":"tape test/*.js","docs":"jsdoc2md -t jsdoc2md/README.md lib/*.js > README.md; echo"},"dependencies":{"array-back":"^1.0.3","path-to-regexp":"^1.5.3","typical":"^2.4.2"},"devDependencies":{"koa":"^2.0.0","req-then":"^0.2.4","tape":"^4.6.0"},"gitHead":"ace03bc0ab1903cfe334331ff996065db6014b45","bugs":{"url":"https://github.com/75lb/koa-mock-response/issues"},"homepage":"https://github.com/75lb/koa-mock-response#readme","_id":"koa-mock-response@0.0.1","_shasum":"4ee31c62af5eea17424552ac63631ba6785f3ae6","_from":".","_npmVersion":"3.9.5","_nodeVersion":"6.2.2","_npmUser":{"name":"75lb","email":"75pound@gmail.com"},"dist":{"shasum":"4ee31c62af5eea17424552ac63631ba6785f3ae6","tarball":"https://registry.npmjs.org/koa-mock-response/-/koa-mock-response-0.0.1.tgz","integrity":"sha512-q4TrGPlYCBLTohCvdMildLRllRxW7LsaRRWpPItnb6KqOQgQTv8i9RQZVvgS5emqsa7Ea1P933uBequKKu8n7w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHUT2LVajXl6Vz8CosqI+aJ0JAjjDLyqNsTzki5yWpBlAiEA1X3l/as7F8IUaqt5UFfC0cOHSzuilIMcqC6bMThmVqg="}]},"maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/koa-mock-response-0.0.1.tgz_1466551187809_0.1491351763252169"},"directories":{}},"0.0.2":{"name":"koa-mock-response","author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"version":"0.0.2","description":"koa-mock-response","repository":{"type":"git","url":"git+https://github.com/75lb/koa-mock-response.git"},"license":"MIT","main":"./lib/koa-mock-response.js","keywords":[],"engines":{"node":">=0.10.0"},"scripts":{"test":"tape test/*.js","docs":"jsdoc2md -t jsdoc2md/README.md lib/*.js > README.md; echo"},"dependencies":{"array-back":"^1.0.3","path-to-regexp":"^1.5.3","test-value":"^2.0.0","typical":"^2.4.2"},"devDependencies":{"koa":"^2.0.0","req-then":"^0.2.4","tape":"^4.6.0"},"gitHead":"ce582fce7285223affcab21b3611505daa791c88","bugs":{"url":"https://github.com/75lb/koa-mock-response/issues"},"homepage":"https://github.com/75lb/koa-mock-response#readme","_id":"koa-mock-response@0.0.2","_shasum":"fc29e9a07bd0bdaeb5a79a1007f371423460b10d","_from":".","_npmVersion":"3.9.5","_nodeVersion":"6.2.2","_npmUser":{"name":"75lb","email":"75pound@gmail.com"},"dist":{"shasum":"fc29e9a07bd0bdaeb5a79a1007f371423460b10d","tarball":"https://registry.npmjs.org/koa-mock-response/-/koa-mock-response-0.0.2.tgz","integrity":"sha512-H/ztayW3Hs2TxJmMmBQ8fscmJ1/MAJviZuzgDcpWAgQ690T3X/lkt8qUyJMmpjukaIdFF2iJtgwCgEzGp/TCUQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAkHeT4AJquHu8o3QBToPdgIHbR6WDEiTInoXvpeKzk3AiAuxmHkj8oR5dQbbikBs7nYQ+XETsNlQ85uB9Di+CeZ2A=="}]},"maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/koa-mock-response-0.0.2.tgz_1466625751193_0.9430874120444059"},"directories":{}},"0.1.0":{"name":"koa-mock-response","author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"version":"0.1.0","description":"Mock a Rest API or any backend service.","repository":{"type":"git","url":"git+https://github.com/lwsjs/koa-mock-response.git"},"license":"MIT","keywords":["lws","koa","mock"],"engines":{"node":">=7.6"},"scripts":{"test":"test-runner test.js"},"dependencies":{"array-back":"^2.0.0","path-to-regexp":"^1.7.0","test-value":"^2.1.0","typical":"^2.6.1"},"devDependencies":{"koa":"^2.3.0","req-then":"^0.6.2","test-runner":"^0.4.0"},"gitHead":"0e09ffc56f2b39841b9f85b587d86d96a43ff414","bugs":{"url":"https://github.com/lwsjs/koa-mock-response/issues"},"homepage":"https://github.com/lwsjs/koa-mock-response#readme","_id":"koa-mock-response@0.1.0","_npmVersion":"5.0.3","_nodeVersion":"8.1.2","_npmUser":{"name":"75lb","email":"75pound@gmail.com"},"dist":{"integrity":"sha512-0e8rpYMpZ5zHk3pFmboAONCRteFHggTdTAsEKfpiiShCeSI5peg5vonwekXTTecbiEfaVxCgrX65NIdjgwh3hg==","shasum":"88fc4c909076882b48e70edd3229d642a41ee9f8","tarball":"https://registry.npmjs.org/koa-mock-response/-/koa-mock-response-0.1.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHoq7bw8EmeQfuFHZoCliHiHPZcs7DDbyKZuhUti48AEAiEAuFfapike4WPkaYHgmlur+iahf6SxGS6upQYaA1T+uZk="}]},"maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/koa-mock-response-0.1.0.tgz_1498423732203_0.24095857399515808"},"directories":{}},"0.2.0":{"name":"koa-mock-response","author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"version":"0.2.0","description":"Mock a Rest API or any backend service.","repository":{"type":"git","url":"git+https://github.com/lwsjs/koa-mock-response.git"},"license":"MIT","keywords":["lws","koa","mock"],"engines":{"node":">=7.6"},"scripts":{"test":"test-runner test.js"},"dependencies":{"array-back":"^2.0.0","path-to-regexp":"^1.7.0","typical":"^2.6.1"},"devDependencies":{"koa":"^2.3.0","req-then":"^0.6.4","test-runner":"^0.4.0"},"gitHead":"2ffdfc36cf60ec83d40c31cc9404d187b27477e9","bugs":{"url":"https://github.com/lwsjs/koa-mock-response/issues"},"homepage":"https://github.com/lwsjs/koa-mock-response#readme","_id":"koa-mock-response@0.2.0","_npmVersion":"5.1.0","_nodeVersion":"8.1.3","_npmUser":{"name":"75lb","email":"75pound@gmail.com"},"dist":{"integrity":"sha512-HmybRN1a3WqcSFvf7tycu2YhBIEHeqzm8bwcsShNWGsTgP86coZOpdI8aqYm/1DFsAQMctnpdWrva4rDr1Pibg==","shasum":"a216490ce75f227fcef3a4b03374e5915e216029","tarball":"https://registry.npmjs.org/koa-mock-response/-/koa-mock-response-0.2.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDAi7wehbYGkQUZH797salBNnU6kvW7O0yBK5w/wCrtjgIhANiY3H8kEA4dKmc1A7uTs4Rlp5+qxSZgCovHR8F65CID"}]},"maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/koa-mock-response-0.2.0.tgz_1499598464086_0.7165012331679463"},"directories":{}},"1.0.0":{"name":"koa-mock-response","author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"version":"1.0.0","description":"Mock a Rest API or any backend service.","repository":{"type":"git","url":"git+https://github.com/lwsjs/koa-mock-response.git"},"license":"MIT","keywords":["lws","koa","mock"],"engines":{"node":">=10"},"scripts":{"test":"test-runner test.js"},"dependencies":{"array-back":"^4.0.1","path-to-regexp":"^6.1.0","typical":"^6.0.0"},"devDependencies":{"koa":"^2.11.0","req-then":"^0.6.4","test-runner":"^0.8.13"},"gitHead":"fd0a830d1a939fc8ab76b69a6862ad65843b0d69","bugs":{"url":"https://github.com/lwsjs/koa-mock-response/issues"},"homepage":"https://github.com/lwsjs/koa-mock-response#readme","_id":"koa-mock-response@1.0.0","_nodeVersion":"13.8.0","_npmVersion":"6.13.7","dist":{"integrity":"sha512-xJjPeeoNopgajdGbNia7CIKMLFDslAAmgBhBaWwTrpFqZa8rfMqiWYM411gSNmoO+Csz6bNm5Tw/OHxp/ASJSA==","shasum":"34a5f021c187f4d2e53e97ff2907ae908e7bc34a","tarball":"https://registry.npmjs.org/koa-mock-response/-/koa-mock-response-1.0.0.tgz","fileCount":6,"unpackedSize":15873,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeQDI2CRA9TVsSAnZWagAA64gP/2jr2WMcCn+TizG/z3j+\nNehGD5t1OTx5/5MmNFgS/x1AF3lg62tykZi/YXCkR04HoNdgh0P24K1Nmndo\n7mAyEYwRwvkrHX/N90+HOBcu3xMo9M4dKO2K8KkwUMUE3bWuJx0MhWJSDrdz\nK4tv+OGRqZDvhSYjXk9onJief4tWxO+QdGgZcs7yFeUiTr+/2Om7oCJWOar4\nnzFYTnOmzevc2a//VkjU2YUIZrFEx0clW1G/S6Zt+z3uZlZDKvsNJni8M0sf\nHmu0Vp5rFCWGdQP0e+HwEl1Q/ugV0e7oWQ8gR4QWtnMBiH0z6rUjvOWcQcqs\nER09b2kUvSb7Kb0VBimHSfnUTwKOnc+tuZTxUCTRUNi0hnWBVcoSQOSKUC+6\nC5gmUy0zGKYmetBTa9Cjyh7r5KbQp4xTYTXf/JzJNVQkB9Qb+DkbJcg9EpgG\nDTUOUGSdkCIzGEYnQIdLPoUap7gGbA+8wBnbM+Q449u4iuCCq8NVpoC7CbWn\naMRTbVHbhmJI4Zi0GA60Ohg684C8rPkd1gAuo31CgJDJacGQuJ6oBme1WClT\nFo+L7YljPOCX1lydY82ys1Cs3sCarftDZ+mY/uTThqbn4BZjwVXspKkJkFsX\ne4EwLm4EC5FkTTVSerhm9aNa/dbSCpf58VJT19Okm1xINMV+1ChfBoMz0h5f\ntZ5D\r\n=aibT\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICIjBQbJZeShMwKNvWkf2TIvYCIfN1/PqSbYqFnGT8aVAiEAvN402LARKLgCxxo7dtRAtg+tcjzms9RBoNVdn3sah0E="}]},"maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"_npmUser":{"name":"75lb","email":"75pound@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/koa-mock-response_1.0.0_1581265462003_0.304858875080148"},"_hasShrinkwrap":false}},"readme":"[![view on npm](https://img.shields.io/npm/v/koa-mock-response.svg)](https://www.npmjs.org/package/koa-mock-response)\n[![npm module downloads](https://img.shields.io/npm/dt/koa-mock-response.svg)](https://www.npmjs.org/package/koa-mock-response)\n[![Build Status](https://travis-ci.org/lwsjs/koa-mock-response.svg?branch=master)](https://travis-ci.org/lwsjs/koa-mock-response)\n[![Dependency Status](https://badgen.net/david/dep/lwsjs/koa-mock-response)](https://david-dm.org/lwsjs/koa-mock-response)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)\n\n# koa-mock-response\n\nMocks give you full control over the response headers and body returned to the client. They can be used to return anything from a simple html string to a resourceful REST API. Typically, they're used to mock services but can be used for anything.\n\nIn the config, define an array called `mocks`. Each mock definition maps a <code>[route](http://expressjs.com/guide/routing.html#route-paths)</code> to a `response`. A simple home page:\n```json\n{\n  \"mocks\": [\n    {\n      \"route\": \"/\",\n      \"response\": {\n        \"body\": \"<h1>Welcome to the Mock Responses example</h1>\"\n      }\n    }\n  ]\n}\n```\n\nUnder the hood, the property values from the `response` object are written onto the underlying [koa response object](https://github.com/koajs/koa/blob/master/docs/api/response.md). You can set any valid koa response properies, for example [type](https://github.com/koajs/koa/blob/master/docs/api/response.md#responsetype-1):\n```json\n{\n  \"mocks\": [\n    {\n      \"route\": \"/\",\n      \"response\": {\n        \"type\": \"text/plain\",\n        \"body\": \"<h1>Welcome to the Mock Responses example</h1>\"\n      }\n    }\n  ]\n}\n```\n\n### Conditional Response\n\nTo define a conditional response, set a `request` object on the mock definition. The `request` value acts as a query - the response defined will only be returned if each property of the `request` query matches. For example, return an XML response *only* if the request headers include `accept: application/xml`, else return 404 Not Found.\n\n```json\n{\n  \"mocks\": [\n    {\n      \"route\": \"/two\",\n      \"request\": { \"accepts\": \"xml\" },\n      \"response\": {\n        \"body\": \"<result id='2' name='whatever' />\"\n      }\n    }\n  ]\n}\n```\n\n### Multiple Potential Responses\n\nTo specify multiple potential responses, set an array of mock definitions to the `responses` property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:\n\n```json\n{\n  \"mocks\": [\n    {\n      \"route\": \"/three\",\n      \"responses\": [\n        {\n          \"request\": { \"method\": \"GET\" },\n          \"response\": {\n            \"body\": \"<h1>Mock response for 'GET' request on /three</h1>\"\n          }\n        },\n        {\n          \"request\": { \"method\": \"POST\" },\n          \"response\": {\n            \"status\": 400,\n            \"body\": { \"message\": \"That method is not allowed.\" }\n          }\n        }\n      ]\n    }\n  ]\n}\n```\n\n### Dynamic Response\n\nThe examples above all returned static data. To define a dynamic response, create a mock module. Specify its path in the `module` property:\n```json\n{\n  \"mocks\": [\n    {\n      \"route\": \"/four\",\n      \"module\": \"/mocks/stream-self.js\"\n    }\n  ]\n}\n```\n\nHere's what the `stream-self` module looks like. The module should export a mock definition (an object, or array of objects, each with a `response` and optional `request`). In this example, the module simply streams itself to the response but you could set `body` to *any* [valid value](https://github.com/koajs/koa/blob/master/docs/api/response.md#responsebody-1).\n```js\nconst fs = require('fs')\n\nmodule.exports = {\n  response: {\n    body: fs.createReadStream(__filename)\n  }\n}\n```\n\n### Response function\n\nFor more power, define the response as a function. It will receive the [koa context](https://github.com/koajs/koa/blob/master/docs/api/context.md) as its first argument. Now you have full programmatic control over the response returned.\n```js\nmodule.exports = {\n  response: function (ctx) {\n    ctx.body = '<h1>I can do anything i want.</h1>'\n  }\n}\n```\n\nIf the route contains tokens, their values are passed to the response. For example, with this mock...\n```json\n{\n  \"mocks\": [\n    {\n      \"route\": \"/players/:id\",\n      \"module\": \"/mocks/players.js\"\n    }\n  ]\n}\n```\n\n...the `id` value is passed to the `response` function. For example, a path of `/players/10?name=Lionel` would pass `10` to the response function. Additional, the value `Lionel` would be available on `ctx.query.name`:\n```js\nmodule.exports = {\n  response: function (ctx, id) {\n    ctx.body = `<h1>id: ${id}, name: ${ctx.query.name}</h1>`\n  }\n}\n```\n\n### RESTful Resource example\n\nHere's an example of a REST collection (users). We'll create two routes, one for actions on the resource collection, one for individual resource actions.\n\n```json\n{\n  \"mocks\": [\n    { \"route\": \"/users\", \"module\": \"/mocks/users.js\" },\n    { \"route\": \"/users/:id\", \"module\": \"/mocks/user.js\" }\n  ]\n}\n```\n\nDefine a module (`users.json`) defining seed data:\n\n```json\n[\n  { \"id\": 1, \"name\": \"Lloyd\", \"age\": 40, \"nationality\": \"English\" },\n  { \"id\": 2, \"name\": \"Mona\", \"age\": 34, \"nationality\": \"Palestinian\" },\n  { \"id\": 3, \"name\": \"Francesco\", \"age\": 24, \"nationality\": \"Italian\" }\n]\n```\n\nThe collection module:\n\n```js\nconst users = require('./users.json')\n\n/* responses for /users */\nconst mockResponses = [\n  /* Respond with 400 Bad Request for PUT and DELETE - inappropriate on a collection */\n  { request: { method: 'PUT' }, response: { status: 400 } },\n  { request: { method: 'DELETE' }, response: { status: 400 } },\n  {\n    /* for GET requests return a subset of data, optionally filtered on 'minAge' and 'nationality' */\n    request: { method: 'GET' },\n    response: function (ctx) {\n      ctx.body = users.filter(user => {\n        const meetsMinAge = (user.age || 1000) >= (Number(ctx.query.minAge) || 0)\n        const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)\n        return meetsMinAge && requiredNationality\n      })\n    }\n  },\n  {\n    /* for POST requests, create a new user and return the path to the new resource */\n    request: { method: 'POST' },\n    response: function (ctx) {\n      const newUser = ctx.request.body\n      users.push(newUser)\n      newUser.id = users.length\n      ctx.status = 201\n      ctx.response.set('Location', `/users/${newUser.id}`)\n    }\n  }\n]\n\nmodule.exports = mockResponses\n```\n\nThe individual resource module:\n\n```js\nconst users = require('./users.json')\n\n/* responses for /users/:id */\nconst mockResponses = [\n  /* don't support POST here */\n  { request: { method: 'POST' }, response: { status: 400 } },\n\n  /* for GET requests, return a particular user */\n  {\n    request: { method: 'GET' },\n    response: function (ctx, id) {\n      ctx.body = users.find(user => user.id === Number(id))\n    }\n  },\n\n  /* for PUT requests, update the record */\n  {\n    request: { method: 'PUT' },\n    response: function (ctx, id) {\n      const updatedUser = ctx.request.body\n      const existingUserIndex = users.findIndex(user => user.id === Number(id))\n      users.splice(existingUserIndex, 1, updatedUser)\n      ctx.status = 200\n    }\n  },\n\n  /* DELETE request: remove the record */\n  {\n    request: { method: 'DELETE' },\n    response: function (ctx, id) {\n      const existingUserIndex = users.findIndex(user => user.id === Number(id))\n      users.splice(existingUserIndex, 1)\n      ctx.status = 200\n    }\n  }\n]\n\nmodule.exports = mockResponses\n```\n\n* * *\n\n&copy; 2017-20 Lloyd Brookes <75pound@gmail.com>.\n","maintainers":[{"name":"75lb","email":"75pound@gmail.com"}],"time":{"modified":"2022-06-19T09:21:04.513Z","created":"2016-06-21T23:19:49.468Z","0.0.1":"2016-06-21T23:19:49.468Z","0.0.2":"2016-06-22T20:02:33.104Z","0.1.0":"2017-06-25T20:48:53.251Z","0.2.0":"2017-07-09T11:07:46.024Z","1.0.0":"2020-02-09T16:24:22.154Z"},"homepage":"https://github.com/lwsjs/koa-mock-response#readme","keywords":["lws","koa","mock"],"repository":{"type":"git","url":"git+https://github.com/lwsjs/koa-mock-response.git"},"author":{"name":"Lloyd Brookes","email":"75pound@gmail.com"},"bugs":{"url":"https://github.com/lwsjs/koa-mock-response/issues"},"license":"MIT","readmeFilename":"README.md"}