{"_id":"@short.io/opensearch-mock","_rev":"3-f79cfec9857fcb3e58baddc7a4133f4f","name":"@short.io/opensearch-mock","dist-tags":{"latest":"0.4.0"},"versions":{"0.3.1":{"name":"@short.io/opensearch-mock","version":"0.3.1","keywords":["elasticsearch","elastic","kibana","mapping","REST","search","client","index","mock","test","stub","sinon"],"author":{"name":"Tomas Della Vedova"},"license":"Apache-2.0","_id":"@short.io/opensearch-mock@0.3.1","maintainers":[{"name":"igor.r","email":"igor@short.io"},{"name":"kostenko","email":"andrey@kostenko.name"}],"homepage":"https://github.com/elastic/elasticsearch-js-mock#readme","bugs":{"url":"https://github.com/elastic/elasticsearch-js-mock/issues"},"dist":{"shasum":"41678404b94342ac60263a8df590f9b1efa80622","tarball":"https://registry.npmjs.org/@short.io/opensearch-mock/-/opensearch-mock-0.3.1.tgz","fileCount":10,"integrity":"sha512-gwLTzxJrZNGVQNn+FHLhpw7at3jHb6tNczq2hvFPJ3C3nLaCBRkbP2IQHrIDzklLBVFV83Ug0Xgs73A5o9eLtg==","signatures":[{"sig":"MEUCIEYBihOpuK4UkSMP0whvIIygYUebItE4cOAuYyp4w278AiEA4+4LvalhwgyM5UC1NJtFx7ziOyVswKYCHNSLOwJ5mL8=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":49057,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiLJNOACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqMFBAAkdQ/BflsE0+IPK5w2Orss+OUfWq0wEyesmUkgvQru0hHzNJD\r\ngbFUSrHiEatZVwbdiTZsmQdPR/EMx4LkoQGhwqoK6FLNgdAXL6sKCPr4+s23\r\n15KKV/sb0NBZnzFOokWpYWswewfe7DasSWU8KJmJ7XW1diFV+yc51w9yg4NY\r\nrrjrzNEurl/KRbf6Cfcp5unogtQDkzvaYgxpthlntGTD5iraVXDwWwuB3O2n\r\nuxSQvh9k2PU0PqI9JNHBLm4RL/C0uz7fvPod8ORlJHbLzzbTuXQDJHVxC2zR\r\nWeDm33aQhjIVRGhpUCfLrkL4EQO1acV2LYL5AtPGrvnwb8aXigpFiZ52cid6\r\nupdW9XUBWE9bZkIutFkUBucwunuJE4IqAOWrD96VUnMTrBZhlxFM5ZhdGgAk\r\nwqvN8DfKq6//l/omUra0oRfl2if1bIN8DuZj/HOxFBAaplQUomySErlPCgVu\r\nWfSONlSFw9QUoMJb0Md3bDkAsoAFKI0OFFwk8cXXNBN1k6nSCoZC56Xu1v2e\r\n7Y1leaCaU040Cxi4aHlZEhs8xOrnseFMvfvL8lwZPv0QM2PviiIMm4+SBjlR\r\nS+q33FfQF8r3AWZyEoEmutkeSvFGOF+GCCP4Qo3oVoUDucgQnwuznTucqMBp\r\nuza6vNOmWf8XiA2+hNVeTqz9I47FT8XRqdw=\r\n=+you\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","types":"index.d.ts","gitHead":"b3b7fcf14367c13cc4a725a35423dfde562b94ba","scripts":{"test":"standard && ava -v && tsd","coverage:check":"nyc ava && nyc check-coverage --branches=100 --lines=100 --functions=100 --statements=100","coverage:report":"nyc ava && nyc report --reporter=lcov && echo \"\n==> open coverage/lcov-report/index.html\""},"_npmUser":{"name":"kostenko","email":"andrey@kostenko.name"},"repository":{"url":"git+https://github.com/elastic/elasticsearch-js-mock.git","type":"git"},"_npmVersion":"8.1.0","description":"Mock utility for the OpenSearch's Node.js client (based on Apache-licensed elasticsearch client)","directories":{},"_nodeVersion":"16.13.0","dependencies":{"find-my-way":"^4.3.3","into-stream":"^6.0.0","fast-deep-equal":"^3.1.3"},"_hasShrinkwrap":false,"devDependencies":{"ava":"^3.15.0","nyc":"^15.1.0","tsd":"^0.17.0","standard":"^16.0.3","@opensearch-project/opensearch":"^1.0.2"},"_npmOperationalInternal":{"tmp":"tmp/opensearch-mock_0.3.1_1647088462506_0.20668728560696747","host":"s3://npm-registry-packages"}},"0.4.0":{"name":"@short.io/opensearch-mock","version":"0.4.0","description":"Mock utility for the Elasticsearch's Node.js client","main":"index.js","types":"index.d.ts","scripts":{"test":"standard && ava -v && tsd","coverage:report":"nyc ava && nyc report --reporter=lcov && echo \"\n==> open coverage/lcov-report/index.html\"","coverage:check":"nyc ava && nyc check-coverage --branches=100 --lines=100 --functions=100 --statements=100"},"repository":{"type":"git","url":"git+https://github.com/elastic/elasticsearch-js-mock.git"},"keywords":["elasticsearch","elastic","kibana","mapping","REST","search","client","index","mock","test","stub","sinon"],"author":{"name":"Tomas Della Vedova"},"license":"Apache-2.0","bugs":{"url":"https://github.com/elastic/elasticsearch-js-mock/issues"},"homepage":"https://github.com/elastic/elasticsearch-js-mock#readme","devDependencies":{"@opensearch-project/opensearch":"^2.12.0","ava":"^6.1.3","nyc":"^17.1.0","standard":"^17.1.2","tsd":"^0.31.2"},"dependencies":{"fast-deep-equal":"^3.1.3","find-my-way":"^9.0.1","into-stream":"^6.0.0"},"_id":"@short.io/opensearch-mock@0.4.0","gitHead":"e0b39381db94677d5f165bb710bbb6119d89b90a","_nodeVersion":"20.16.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-f69ne7aINVKJOfpwb2wZg9Mdq/Lv/1S6TtUi1axWydWHdzcUaGSlbP/sJKXlJYNj3otayCDd5F/FAvTSKhu9IQ==","shasum":"bf5a23ff026a527e5c610aceb36c12bd137cdc5a","tarball":"https://registry.npmjs.org/@short.io/opensearch-mock/-/opensearch-mock-0.4.0.tgz","fileCount":10,"unpackedSize":48551,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBsIXcgL4i5vwWiyTR+H9tolCsEM5YDinhHDU/oJ/wVmAiEAmEe1/YHSP0g0ZK712q7EoeH/avkiUUUOUwcB2A2snF8="}]},"_npmUser":{"name":"kostenko","email":"andrey@kostenko.name"},"directories":{},"maintainers":[{"name":"igor.r","email":"igor@short.io"},{"name":"kostenko","email":"andrey@kostenko.name"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/opensearch-mock_0.4.0_1726841930373_0.10535028617987074"},"_hasShrinkwrap":false}},"time":{"created":"2022-03-12T12:34:22.506Z","modified":"2024-09-20T14:18:50.838Z","0.3.1":"2022-03-12T12:34:22.636Z","0.4.0":"2024-09-20T14:18:50.619Z"},"bugs":{"url":"https://github.com/elastic/elasticsearch-js-mock/issues"},"author":{"name":"Tomas Della Vedova"},"license":"Apache-2.0","homepage":"https://github.com/elastic/elasticsearch-js-mock#readme","keywords":["elasticsearch","elastic","kibana","mapping","REST","search","client","index","mock","test","stub","sinon"],"repository":{"type":"git","url":"git+https://github.com/elastic/elasticsearch-js-mock.git"},"description":"Mock utility for the Elasticsearch's Node.js client","maintainers":[{"name":"igor.r","email":"igor@short.io"},{"name":"kostenko","email":"andrey@kostenko.name"}],"readme":"# Opensearch Node.js client mock utility\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)  ![build](https://github.com/short-io/opensearch-js-mock/workflows/build/badge.svg)\n\nWhen testing your application you don't always need to have an Elasticsearch instance up and running, but you might still need to use the client for fetching some data. If you are facing this situation, this library is what you need.\n\n### Features\n\n- Simple and intuitive API\n- Mocks only the http layer, leaving the rest of the client working as usual\n- Maximum flexibility thanks to \"strict\" or \"loose\" mocks\n\n## Install\n```\nnpm install @short.io/opensearch-js-mock --save-dev\n```\n\n## Usage\n\n```js\nconst { Client } = require('@opensearch-project/opensearch')\nconst Mock = require('@opensearch/opensearch-mock')\n\nconst mock = new Mock()\nconst client = new Client({\n  node: 'http://localhost:9200',\n  Connection: mock.getConnection()\n})\n\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n}, () => {\n  return { status: 'ok' }\n})\n\nclient.cat.health(console.log)\n```\n\n## API\n\n#### `Constructor`\n\nBefore start using the library you need to create a new instance:\n```js\nconst Mock = require('@elastic/elasticsearch-mock')\nconst mock = new Mock()\n```\n\n#### `add`\n\nAdds a new mock for a given pattern and assigns it to a resolver function.\n\n```js\n// every GET request to the `/_cat/health` path\n// will return `{ status: 'ok' }`\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n}, () => {\n  return { status: 'ok' }\n})\n```\n\nYou can also specify multiple methods and/or paths at the same time:\n```js\n// This mock will catch every search request against any index\nmock.add({\n  method: ['GET', 'POST'],\n  path: ['/_search', '/:index/_search']\n}, () => {\n  return { status: 'ok' }\n})\n```\n\n#### `get`\n\nReturns the matching resolver function for the given pattern, it returns `null` if there is not a matching pattern.\n\n```js\nconst fn = mock.get({\n  method: 'GET',\n  path: '/_cat/health'\n})\n```\n\n#### `clear`\n\nClears/removes mocks for specific route(s).\n\n```js\nmock.clear({\n  method: ['GET'],\n  path: ['/_search', '/:index/_search']\n})\n```\n\n#### `clearAll`\n\nClears all mocks.\n\n```js\nmock.clearAll()\n```\n\n#### `getConnection`\n\nReturns a custom `Connection` class that you **must** pass to the Elasticsearch client instance.\n\n```js\nconst { Client } = require('@elastic/elasticsearch')\nconst Mock = require('@elastic/elasticsearch-mock')\n\nconst mock = new Mock()\nconst client = new Client({\n  node: 'http://localhost:9200',\n  Connection: mock.getConnection()\n})\n```\n\n### Mock patterns\n\nA pattern is an object that describes an http query to Elasticsearch, and it looks like this:\n```ts\ninterface MockPattern {\n  method: string\n  path: string\n  querystring?: Record<string, string>\n  body?: Record<string, any>\n}\n```\n\nThe more field you specify, the more the mock will be strict, for example:\n```js\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n  querystring: { pretty: 'true' }\n}, () => {\n  return { status: 'ok' }\n})\n\nclient.cat.health(console.log) // => 404 error\nclient.cat.health({ pretty: true }, console.log) // => { status: 'ok' }\n```\n\nYou can craft custom responses for different queries:\n\n```js\nmock.add({\n  method: 'POST',\n  path: '/indexName/_search'\n  body: { query: { match_all: {} } }\n}, () => {\n  return {\n    hits: {\n      total: { value: 1, relation: 'eq' },\n      hits: [{ _source: { baz: 'faz' } }]\n    }\n  }\n})\n\nmock.add({\n  method: 'POST',\n  path: '/indexName/_search',\n  body: { query: { match: { foo: 'bar' } } }\n}, () => {\n  return {\n    hits: {\n      total: { value: 0, relation: 'eq' },\n      hits: []\n    }\n  }\n})\n```\n\nYou can also specify dynamic urls:\n```js\nmock.add({\n  method: 'GET',\n  path: '/:index/_count'\n}, () => {\n  return { count: 42 }\n})\n\nclient.count({ index: 'foo' }, console.log) // => { count: 42 }\nclient.count({ index: 'bar' }, console.log) // => { count: 42 }\n```\n\nWildcards are supported as well.\n```js\nmock.add({\n  method: 'HEAD',\n  path: '*'\n}, () => {\n  return { status: 'ok' }\n})\n\nclient.indices.exists({ index: 'foo' }, console.log) // => { status: 'ok' }\nclient.ping(console.log) // => { status: 'ok' }\n```\n\n### Dynamic responses\n\nThe resolver function takes a single parameter which represent the API call that has been made by the client.\nYou can use it to craft dynamic responses.\n\n```js\nmock.add({\n  method: 'POST',\n  path: '/indexName/_search',\n}, params => {\n  return { query: params.body.query }\n})\n```\n\n### Errors\n\nThis utility uses the same error classes of the Elasticsearch client, if you want to return an error for a specific API call, you should use the `ResponseError` class:\n\n```js\nconst { errors } = require('@elastic/elasticsearch')\nconst Mock = require('@elastic/elasticsearch-mock')\n\nconst mock = new Mock()\nmock.add({\n  method: 'GET',\n  path: '/_cat/health'\n}, () => {\n  return new errors.ResponseError({\n    body: { errors: {}, status: 500 },\n    statusCode: 500\n  })\n})\n```\n\n## License\n\nThis software is licensed under the [Apache 2 license](./LICENSE).\n","readmeFilename":"README.md"}