{"id":17109121,"url":"https://github.com/shaddix/nock-swagger","last_synced_at":"2025-04-13T03:19:41.681Z","repository":{"id":60162643,"uuid":"433933482","full_name":"Shaddix/nock-swagger","owner":"Shaddix","description":"Generate convenient methods to mock API requests using nock (based on Swagger API definition)","archived":false,"fork":false,"pushed_at":"2023-11-21T09:45:25.000Z","size":574,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T20:21:16.782Z","etag":null,"topics":["nock","openapi","swagger"],"latest_commit_sha":null,"homepage":"","language":"Liquid","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/Shaddix.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}},"created_at":"2021-12-01T18:04:44.000Z","updated_at":"2023-05-25T16:49:04.000Z","dependencies_parsed_at":"2023-01-19T21:56:45.644Z","dependency_job_id":null,"html_url":"https://github.com/Shaddix/nock-swagger","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaddix%2Fnock-swagger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaddix%2Fnock-swagger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaddix%2Fnock-swagger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shaddix%2Fnock-swagger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shaddix","download_url":"https://codeload.github.com/Shaddix/nock-swagger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631684,"owners_count":21136562,"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":["nock","openapi","swagger"],"created_at":"2024-10-14T16:22:10.595Z","updated_at":"2025-04-13T03:19:41.664Z","avatar_url":"https://github.com/Shaddix.png","language":"Liquid","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nock-swagger\n\u003cp\u003e\n\u003ca href=\"https://www.npmjs.com/package/nock-swagger\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/nock-swagger.svg\" alt=\"Stable Release\" /\u003e\u003c/a\u003e\n\u003ca href=\"./LICENSE\"\u003e\u003cimg allt=\"MIT License\" src=\"https://badgen.now.sh/badge/license/MIT\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nThis projects autogenerates strongly typed methods to easily set up [nock](https://github.com/nock/nock) mocks for API calls based on Swagger API definitions.\nSo, given that you have a [petstore-like API definition](https://petstore.swagger.io/), you could autogenerate a list of [nock helper methods](https://github.com/Shaddix/nock-swagger/blob/master/examples/pet-client/src/_tests/nock-helpers.ts), to easily mock methods from the API.\n\n\n****This library requires, [dotnet runtime](https://dotnet.microsoft.com/download/dotnet/2.1) (2.1) installed on your machine! If you have .NET Core 3+ or 5+ you'll need to add a switch (e.g. /runtime:Net50) to all commands.****\n## How to use\nCheck out [tests](https://github.com/Shaddix/nock-swagger/blob/963de857fd6abbe42ad1172f47cd118b2efe3a05/examples/pet-client/src/_tests/App.test.tsx#L11) for few examples.\nShortly, mocking GET requests will look like this:\n```ts\nNock.getPetById({ id: 1 } /* GET parameters with type \u0026 intellisense */)\n  .reply({ id:1, name: 'mypet' });\n```\nIt's not that different from the Nock itself, just that:\n- you don't have to remember/write the URL (it will be filled in automatically)\n- you don't have to remember query parameters \u0026 their types (intellisense will help you)\n- you will have intellisense for Reply as well (so you know the shape of the object to return)\n\nDon't forget to set the base url by calling `setBaseUrl('https://localhost')`.\nAlso go read the [nock docs](https://github.com/nock/nock#nock) if you have any problem regarding how Nock works.\n\n### More details:\n- You get an autogenerated list of functions (function per API endpoint). E.g. `Nock.getPetById`, `Nock.findPetByStatus`, `Nock.addPet`. The 'Nock' part actually depends on your Swagger definition (it's a Controller name, unless it's not empty).\n- Each function has the same typings as `nock.post/get/put`, except that the first parameter is not `uri: string`, but a typed interface with path/query parameters. E.g. \n    ```\n  getPetById: (\n      queryParams: GetPetByIdNockParameters, /* Strongly typed, not just `url: string`  */\n      requestBody?: RequestBodyMatcher,\n      interceptorOptions?: Options \u0026 { preservePreviousInterceptors?: boolean },\n  )\n  type GetPetByIdNockParameters = {\n      petId: number;\n  };\n  ```\n- Each function returns exactly the same Interceptor as `nock.post(...)`, but typings are different. E.g. `reply` function has the following typing:\n```ts\n    reply(\n      responseCode?: StatusCode,\n      body?: IPet, /* strongly typed, not just `Body` */\n      headers?: ReplyHeaders,\n    ): Scope;\n```\n  All other `reply` overloads are correctly typed as well. Another example (for POST `/store/order`):\n```ts\n    reply(\n      replyFnWithCallback: (\n        this: ReplyFnContext,\n        uri: string,\n        body: Order, /* body is correctly typed */\n        callback: (\n          err: NodeJS.ErrnoException | null,\n          result: ReplyFnResultGeneric\u003cIOrder\u003e, /* result function is typed as well */\n        ) =\u003e void,\n      ) =\u003e void,\n    ): Scope;\n```\nNotice, however, that `uri` is still not typed in all `reply` overloads. If you want the actual values of your query parameters, please use `new URLSearchParams(url)`, or some other parsing library.\n`nock-swagger` might someday implement it as well, but it will require rewriting the `reply` function, while currently only typings are different (the `Interceptor` itself is exactly what is returned from nock).\n \n## Perks\n### Auto-removing old interceptors \nNock by default doesn't remove the 'old' interceptors when you define a new one for the same route/query params.\nIt wasn't convenient for me, so nock-swagger removes old interceptors by default.\nIf you want to preserve previously configured interceptors for the same root (God knows why are you adding a new Interceptor if you don't want it to work, but still),\nyou need to pass `preservePreviousInterceptors: true` as part of `interceptorOptions` (usually 3rd argument of every nock setup call).\n\n## How to add\nInstall the package into your project using yarn/npm (as a dev-dependency). You'll also need to add react-query (which you probably already have if you are interested in this library).\n```\nyarn add -D nock-swagger\n```\nThen create/update your autogenerated hooks by calling (adjusting the URL and output path)\n```\nyarn nock-swagger /input:https://petstore.swagger.io/v2/swagger.json /output:__tests__/nock-helpers.ts\n```\nYou would probably want to add this script to your package.json to call it every time your API changes.\n\n## Configuration\n##### setBaseUrl(baseUrl: string)\nSets base URL for all methods\n\n## How does it work\nUnder the cover it's just 2 template files for [NSwag](https://github.com/RicoSuter/NSwag) and a small script to easily use them.\n\n## Contributions and support\nIssues and Pull Requests are welcome.\n\nFor any kind of private consulting or support you could contact [Artur Drobinskiy](https://github.com/Shaddix) directly via email.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaddix%2Fnock-swagger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaddix%2Fnock-swagger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaddix%2Fnock-swagger/lists"}