{"id":13495575,"url":"https://github.com/jihchi/res-msw","last_synced_at":"2025-03-28T16:32:59.762Z","repository":{"id":38843761,"uuid":"264439321","full_name":"jihchi/res-msw","owner":"jihchi","description":"Mock Service Worker (MSW) bindings for ReScript","archived":true,"fork":false,"pushed_at":"2024-03-29T11:51:32.000Z","size":1258,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T21:55:30.947Z","etag":null,"topics":["bindings","bucklescript","bucklescript-bindings","mock","mocking","mocking-framework","mocking-library","msw","reasonml","rescript","rescript-bindings","service-worker"],"latest_commit_sha":null,"homepage":"","language":"ReScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jihchi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-16T13:07:00.000Z","updated_at":"2024-03-29T11:52:10.000Z","dependencies_parsed_at":"2023-02-08T12:01:30.598Z","dependency_job_id":"230d28c7-0c2c-4547-b9ca-522c4d9ccd79","html_url":"https://github.com/jihchi/res-msw","commit_stats":{"total_commits":77,"total_committers":4,"mean_commits":19.25,"dds":"0.49350649350649356","last_synced_commit":"550bd65c2b61f04868e2f924d6e37c1db135a67f"},"previous_names":["jihchi/bs-msw"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jihchi%2Fres-msw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jihchi%2Fres-msw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jihchi%2Fres-msw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jihchi%2Fres-msw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jihchi","download_url":"https://codeload.github.com/jihchi/res-msw/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246063114,"owners_count":20717748,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bindings","bucklescript","bucklescript-bindings","mock","mocking","mocking-framework","mocking-library","msw","reasonml","rescript","rescript-bindings","service-worker"],"created_at":"2024-07-31T19:01:36.104Z","updated_at":"2025-03-28T16:32:59.450Z","avatar_url":"https://github.com/jihchi.png","language":"ReScript","funding_links":[],"categories":["ReScript"],"sub_categories":[],"readme":"# res-msw\n\n[![Travis (.org)](https://img.shields.io/travis/jihchi/res-msw)](https://travis-ci.org/jihchi/res-msw) [![npm](https://img.shields.io/npm/v/res-msw)](https://www.npmjs.com/package/res-msw) [![Coveralls github](https://img.shields.io/coveralls/github/jihchi/res-msw)](https://coveralls.io/github/jihchi/res-msw) ![npm](https://img.shields.io/npm/dm/res-msw) ![NPM](https://img.shields.io/npm/l/res-msw)\n\n\u003e [msw](https://github.com/mswjs/msw) bindings for [ReScript](https://rescript-lang.org/) (formerly known as [BuckleScript](https://github.com/bloomberg/bucklescript) in [Reason](https://github.com/facebook/reason))\n\n# Installation\n\n**Prerequisite**: you have installed [msw](https://github.com/mswjs/msw).\n\n```sh\nnpm install -D res-msw\n```\n\nOr if you are using yarn:\n\n```sh\nyarn add -D res-msw\n```\n\n# Usage\n\nAdd `res-msw` to your `bsconfig.json`:\n\n```diff\n  ...\n  \"bs-dependencies\": [\n+   \"res-msw\"\n  ]\n  ...\n```\n\n## API\n\n`ResMsW.MSW` is the root namespace, includes following namespaces and modules:\n\n- `rest`\n- `graphql`\n- `Node` module\n- `ServiceWorker` module\n- `Rest` module\n- `GraphQL` module\n\n### `Node` module\n\n- `setup(array(requestHandler))`\n- `server |\u003e listen()`\n- `server |\u003e close()`\n- `server |\u003e resetHandlers()`\n- `server |\u003e restoreHandlers()`\n- `server |\u003e user(requestHandler)`\n- `rest |\u003e get(string, (req, res, ctx) =\u003e { });`\n- `rest |\u003e post(string, (req, res, ctx) =\u003e { });`\n- `rest |\u003e put(string, (req, res, ctx) =\u003e { });`\n- `rest |\u003e patch(string, (req, res, ctx) =\u003e { });`\n- `rest |\u003e delete(string, (req, res, ctx) =\u003e { });`\n- `rest |\u003e options(string, (req, res, ctx) =\u003e { });`\n\n### `ServiceWorker` module\n\n- `setup(array(requestHandler))`\n- `worker |\u003e start()`\n- `worker |\u003e stop()`\n- `worker |\u003e resetHandlers()`\n- `worker |\u003e restoreHandlers()`\n- `worker |\u003e user(requestHandler)`\n- `graphql |\u003e query(string, (req, res, ctx) =\u003e { });`\n- `graphql |\u003e mutation(string, (req, res, ctx) =\u003e { });`\n\n### `Rest` module\n\n- `res |\u003e mock(array(responseTransformer))`\n- `res |\u003e mockOnce(array(responseTransformer))`\n- `ctx |\u003e status(int)`\n- `ctx |\u003e set(string, string)`\n- `ctx |\u003e delay(int)`\n- `ctx |\u003e fetch(req)`\n- `ctx |\u003e text(string)`\n- `ctx |\u003e json(Js.Json.t)`\n- `ctx |\u003e xml(string)`\n\n### `GraphQL` module\n\n- `res |\u003e mock(array(responseTransformer))`\n- `res |\u003e mockOnce(array(responseTransformer))`\n- `ctx |\u003e status(int)`\n- `ctx |\u003e set(string, string)`\n- `ctx |\u003e delay(int)`\n- `ctx |\u003e fetch(req)`\n- `ctx |\u003e data(Js.Json.t)`\n- `ctx |\u003e errors(array(Js.Json.t))`\n\n### Example of Rest\n\n\u003e For more example, please refer to [`MSW_node_test.re`](/__tests__/MSW_node_test.re) and [`mocks.re`](/__tests__/support/mocks.re).\n\n```res\nopen ResMsw.MSW\n\nlet getRepoInfo = rest |\u003e get(\"https://api.github.com/repos/:owner/:repo\", (\n  req,\n  res,\n  ctx,\n) =\u003e {\n  let {params} = req\n  let owner = params-\u003eJs.Dict.get(\"owner\")-\u003eBelt.Option.getWithDefault(\"N/A\")\n  let repo = params-\u003eJs.Dict.get(\"repo\")-\u003eBelt.Option.getWithDefault(\"N/A\")\n\n  res |\u003e Rest.mock([\n    ctx |\u003e Rest.status(200),\n    ctx |\u003e Rest.text(`owner: ${owner}, repo: ${repo}`),\n  ])\n})\n\nlet getRepoInfoError =\n  rest |\u003e get(\"https://api.github.com/repos/:owner/:repo\", (req, res, ctx) =\u003e\n    res |\u003e Rest.mock([ctx |\u003e Rest.status(500), ctx |\u003e Rest.text(\"Oops\")])\n  )\n\nlet server = Node.setup([getRepoInfo])\n\nserver-\u003elisten()\nserver-\u003euse(getRepoInfoError)\nserver-\u003erestoreHandlers()\nserver-\u003eresetHandlers()\nserver-\u003eclose()\n```\n\n### Example of GraphQL\n\nFor more example, please refer to [`MSW_browser.re`](/__tests__/support/MSW_browser.re) and [`mocks.re`](/__tests__/support/mocks.re).\n\n```res\nopen ResMsw.MSW\n\nlet queryUserDetail = graphql |\u003e query(\"GetUserDetail\", (req, res, ctx) =\u003e {\n  let name = (\"name\", req.variables[\"name\"])\n  let data = Js.Dict.fromList(list{name}) |\u003e Js.Json.object_\n\n  res |\u003e GraphQL.mock([ctx |\u003e GraphQL.status(200), ctx |\u003e GraphQL.data(data)])\n})\n\nlet queryUserDetailError = graphql |\u003e query(\"GetUserDetail\", (\n  req,\n  res,\n  ctx,\n) =\u003e {\n  let message = (\n    \"message\",\n    Js.Json.string(`This is a mocked error: ${req.variables[\"name\"]}`),\n  )\n  let location = Js.Dict.fromList(list{\n    (\"line\", Js.Json.number(1.0)),\n    (\"column\", Js.Json.number(2.0)),\n  })\n  let locations = (\"locations\", Js.Json.objectArray([location]))\n  let error = Js.Dict.fromList(list{message, locations}) |\u003e Js.Json.object_\n\n  res |\u003e GraphQL.mock([\n    ctx |\u003e GraphQL.status(200),\n    ctx |\u003e GraphQL.errors([error]),\n  ])\n})\n\nlet worker = ServiceWorker.setup([queryUserDetail])\n\nworker-\u003estart()\nworker-\u003euse(GetUserDetailError)\nworker-\u003erestoreHandlers()\nworker-\u003eresetHandlers()\nworker-\u003estop()\n```\n\n# Testing the library\n\n```\nnpm test\n```\n\nthis will compile and execute tests with `bs-jest`\n\n# Contributions\n\nDon't hesitate to open a PR with a new binding - while bumping up the amount of covered bindings in the README.\nThere are tests, use them and write the most simple test you can think of to make sure that the bindings work correctly.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjihchi%2Fres-msw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjihchi%2Fres-msw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjihchi%2Fres-msw/lists"}