{"id":13496251,"url":"https://github.com/rametta/rapini","last_synced_at":"2025-04-11T23:18:06.993Z","repository":{"id":39616622,"uuid":"494138104","full_name":"rametta/rapini","owner":"rametta","description":":leafy_green: OpenAPI to React Query (or SWR) \u0026 Axios","archived":false,"fork":false,"pushed_at":"2025-03-17T13:26:03.000Z","size":500,"stargazers_count":171,"open_issues_count":0,"forks_count":19,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-11T23:17:57.813Z","etag":null,"topics":["axios","codegen","generator","javascript","nextjs","openapi","react","react-query","remix","swagger","swr","tanstack","tanstack-query","tanstack-react-query","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rametta.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","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":"2022-05-19T15:57:05.000Z","updated_at":"2025-03-23T14:48:58.000Z","dependencies_parsed_at":"2023-02-17T21:30:44.865Z","dependency_job_id":"3be6f147-61b7-4fe4-95f5-90f38eb83ca8","html_url":"https://github.com/rametta/rapini","commit_stats":{"total_commits":50,"total_committers":5,"mean_commits":10.0,"dds":0.12,"last_synced_commit":"84fb9bf8d73faeb97585b5846376c2190d373735"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Frapini","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Frapini/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Frapini/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rametta%2Frapini/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rametta","download_url":"https://codeload.github.com/rametta/rapini/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248492885,"owners_count":21113163,"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":["axios","codegen","generator","javascript","nextjs","openapi","react","react-query","remix","swagger","swr","tanstack","tanstack-query","tanstack-react-query","typescript"],"created_at":"2024-07-31T19:01:44.591Z","updated_at":"2025-04-11T23:18:06.975Z","avatar_url":"https://github.com/rametta.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/rapini.svg)](http://npm.im/rapini)\n[![License](https://img.shields.io/github/license/rametta/rapini)](https://opensource.org/licenses/Apache-2.0)\n[![PR Test](https://github.com/rametta/rapini/actions/workflows/test.yml/badge.svg)](https://github.com/rametta/rapini/actions/workflows/test.yml)\n\n# :leafy_green: Rapini - OpenAPI to React Query (or SWR) \u0026 Axios\n\nRapini is a tool that generates [React Query](https://tanstack.com/query/latest/) (or [SWR](https://swr.vercel.app/)) hooks, [Axios](https://axios-http.com/) requests and [Typescript](https://www.typescriptlang.org/) types, based on an [OpenAPI](https://www.openapis.org/) spec file.\nThe generated code is packaged conveniently so that it can be published as a package on any NPM registry.\n\n## Features\n\n- :bicyclist: Generates axios calls for every endpoint, with typed payload.\n- :golfing: Generates custom react hooks that use React Query's `useQuery` and `useMutation` hooks for each axios call. Optional to generate custom hooks that use SWR's `useSWR` hook.\n- :rowboat: Generates query keys for every hook.\n- :weight_lifting: Generates strong typescript types for all inputs, outputs, and options.\n\n## Getting Started\n\nRapini is a CLI tool so you can execute the remote npm package directly for convenience\n\n```sh\nnpx rapini [library] [options]\n```\n\neg:\n\n```sh\n# For React Query V3\nnpx rapini react-query -p path/to/openapi.yaml\n\n# For TanStack Query 4\nnpx rapini react-query v4 -p path/to/openapi.yaml\n\n# For TanStack Query 5\nnpx rapini react-query v5 -p path/to/openapi.yaml\n\n# For SWR\nnpx rapini swr -p path/to/openapi.yaml\n```\n\nThis will generate the package code based on an OpenAPI file at `path/to/openapi.yaml`. The outputted code will be packaged in a way to just publish it as your own NPM package and then import it in your React project.\n\n## CLI Arguments \u0026 Options\n\n### `rapini help` outputs the following:\n\n```\nUsage: rapini [options] [command]\n\nGenerate a package based on OpenAPI\n\nOptions:\n  -V, --version                    output the version number\n  -h, --help                       display help for command\n\nCommands:\n  react-query [options] [version]  Generate a Package for TanStack Query V4 or React Query V3\n  swr [options]                    Generate a Package for SWR (stale-while-revalidate)\n  help [command]                   display help for command\n```\n\n### `rapini help react-query` outputs the following:\n\n```\nUsage: rapini react-query [options] [react-query-version]\n\nGenerate a Package for TanStack Query V4 or V5, or legacy React Query V3\n\nOptions:\n  -p, --path \u003cpath\u003e                          Path to OpenAPI file\n  -n, --name [name]                          Name to use for the generated package (default: \"rapini-generated-package\")\n  -pv, --package-version [version]           Semver version to use for the generated package (default: \"1.0.0\")\n  -o, --output-dir [directory]               Directory to output the generated package (default: \"rapini-generated-package\")\n  -b, --base-url [url]                       Prefix every request with this url\n  -r, --replacer [oldString] [newString...]  Replace part(s) of any route's path with simple string replacements. Ex: `-r /api/v1 /api/v2` would replace the v1 with v2 in every route\n  -h, --help                                 display help for command\n```\n\n### `rapini help swr` outputs the following:\n\n```\nUsage: rapini swr [options]\n\nGenerate a Package for SWR (stale-while-revalidate)\n\nOptions:\n  -p, --path \u003cpath\u003e                          Path to OpenAPI file\n  -n, --name [name]                          Name to use for the generated package (default: \"rapini-generated-package\")\n  -pv, --package-version [version]           Semver version to use for the generated package (default: \"1.0.0\")\n  -o, --output-dir [directory]               Directory to output the generated package (default: \"rapini-generated-package\")\n  -b, --base-url [url]                       Prefix every request with this url\n  -r, --replacer [oldString] [newString...]  Replace part(s) of any route's path with simple string replacements. Ex: `-r /api/v1 /api/v2` would replace the v1 with v2 in every route\n  -h, --help                                 display help for command\n```\n\n## Example Usage\n\nLet's say you have an OpenAPI file that looks like [this one](./example-openapi.yaml).\n\nOnce you run the CLI tool to generate the React Query code, you can then `cd` into the generated directory, run `npm install \u0026\u0026 npm run build` then `npm publish` with your own package name to publish it to your own registry, then import and use it like this:\n\n```tsx\nimport { initialize } from \"your-custom-package\";\nimport { axiosInstance } from \"./your-custom-axios-instance\";\n\n// Can even import the generated Typescript Types if needed\nimport type { Pet } from \"your-custom-package\";\n\nconst config = initialize(axiosInstance);\n\nconst { usePets } = config.queries;\n\nconst MyComponent = () =\u003e {\n  const { data, isLoading, isError } = usePets();\n\n  return (\n    \u003cul\u003e\n      {data.pets.map((pet) =\u003e (\n        \u003cli key={pet.id}\u003e{pet.name}\u003c/li\u003e\n      ))}\n    \u003c/ul\u003e\n  );\n};\n```\n\nYou must call `initialize(axiosInstance)` with your custom axios instance. The return value from the `initialize` will give you an object with everything you need, here is the return value with examples:\n\n```ts\nimport { queryKeys } from \"generated-package\";\n// queryKeys = { pets: () =\u003e ['pets'] } ...\n\nconst rapini = initialize(axiosInstance);\nrapini.queries; // { usePets, usePetById } ...\nrapini.mutations; // { useUpdatePet, useDeletePet } ... if generated by SWR, there will be no property `mutations`\nrapini.requests; // { pets: () =\u003e axios.get\u003cPet[]\u003e(...) } ...\n```\n\n### With Global Config\n\nThere may be times when you want extra functionality hooked into each hook's callbacks. You can do this normally by passing `options` to each hook, but if you want something more global - a config can be provided to the `initialize` function.\n\n```ts\nimport { initialize, type Config } from \"your-custom-package\";\nimport type { QueryClient } from \"react-query\";\n\nconst config: Config = {\n  mutations: {\n    useCreatePet: (queryClient: QueryClient) =\u003e ({\n      onSuccess: () =\u003e showSuccessNotification(),\n      onError: () =\u003e showErrorNotification(),\n    }),\n  },\n};\n\nconst rapini = initialize(axiosInstance, config);\n```\n\n## Important Notes\n\n- Every request must have an `operationId` defined. The `operationId` is used in many places in the final generated code.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frametta%2Frapini","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frametta%2Frapini","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frametta%2Frapini/lists"}