{"id":15645052,"url":"https://github.com/anttiviljami/react-openapi-client","last_synced_at":"2025-04-13T00:50:57.937Z","repository":{"id":41666575,"uuid":"257013957","full_name":"anttiviljami/react-openapi-client","owner":"anttiviljami","description":"Consume OpenAPI-enabled APIs with React Hooks","archived":false,"fork":false,"pushed_at":"2023-01-07T04:39:54.000Z","size":2385,"stargazers_count":58,"open_issues_count":29,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-26T18:52:21.052Z","etag":null,"topics":["hacktoberfest","openapi","react","react-hooks","swagger"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/anttiviljami.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"anttiviljami","custom":["https://buymeacoff.ee/anttiviljami"]}},"created_at":"2020-04-19T14:01:41.000Z","updated_at":"2025-02-18T04:59:59.000Z","dependencies_parsed_at":"2023-02-06T11:55:16.507Z","dependency_job_id":null,"html_url":"https://github.com/anttiviljami/react-openapi-client","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anttiviljami%2Freact-openapi-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anttiviljami%2Freact-openapi-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anttiviljami%2Freact-openapi-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anttiviljami%2Freact-openapi-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anttiviljami","download_url":"https://codeload.github.com/anttiviljami/react-openapi-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650435,"owners_count":21139672,"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":["hacktoberfest","openapi","react","react-hooks","swagger"],"created_at":"2024-10-03T12:04:13.193Z","updated_at":"2025-04-13T00:50:57.913Z","avatar_url":"https://github.com/anttiviljami.png","language":"TypeScript","funding_links":["https://github.com/sponsors/anttiviljami","https://buymeacoff.ee/anttiviljami"],"categories":[],"sub_categories":[],"readme":"# React OpenAPI Client\n\n[![CI](https://github.com/anttiviljami/react-openapi-client/workflows/CI/badge.svg)](https://github.com/anttiviljami/react-openapi-client/actions?query=workflow%3ACI)\n[![npm version](https://img.shields.io/npm/v/react-openapi-client.svg)](https://www.npmjs.com/package/react-openapi-client)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/react-openapi-client?label=gzip%20bundle)](https://bundlephobia.com/package/react-openapi-client)\n[![License](http://img.shields.io/:license-mit-blue.svg)](https://github.com/anttiviljami/react-openapi-client/blob/master/LICENSE)\n[![Buy me a coffee](https://img.shields.io/badge/donate-buy%20me%20a%20coffee-orange)](https://buymeacoff.ee/anttiviljami)\n\nConsume OpenAPI-enabled APIs with React Hooks\n\nUses [`openapi-client-axios`](https://www.npmjs.com/package/openapi-client-axios) under the hood.\n\n## Why?\n\nYou can do this:\n\n```jsx\nimport React, { useEffect } from 'react';\nimport { useOperation } from 'react-openapi-client';\n\nconst MyComponent = (props) =\u003e {\n  const { loading, data, error } = useOperation('getPetById', props.id);\n  // ...\n};\n```\n\nInstead of:\n\n```jsx\nimport React, { useEffect, useState } from 'react';\n\nconst MyComponent = (props) =\u003e {\n  const [loading, setLoading] = useState(false);\n  const [data, setData] = useState();\n  const [error, setError] = useState();\n\n  useEffect(() =\u003e {\n    (async () =\u003e {\n      setLoading(true);\n      try {\n        const res = await fetch(`https://petstore.swagger.io/api/v3/pet/${props.id}`, {\n          method: 'GET',\n          headers: {\n            'Content-Type': 'application/json',\n          },\n          credentials: 'include',\n        });\n        const data = await res.json();\n        setData(data);\n      } catch (err) {\n        setError(err);\n      }\n      setLoading(false);\n    })();\n  }, [props.id]);\n\n  // ...\n};\n```\n\n## Getting Started\n\nInstall `react-openapi-client` as a dependency\n\n```\nnpm install --save react-openapi-client axios\n```\n\nWrap your React App with an `OpenAPIProvider`, passing your OpenAPI definition as a prop.\n\n```jsx\nimport React from 'react';\nimport { render } from 'react-dom';\nimport { OpenAPIProvider } from 'react-openapi-client';\n\nconst App = () =\u003e (\n  \u003cOpenAPIProvider definition=\"http://petstore.swagger.io:8080/api/v3/openapi.json\"\u003e\n    \u003cPetDetails id={1} /\u003e\n  \u003c/OpenAPIProvider\u003e\n);\n```\n\nNow you can start using the `useOperation` and `useOperationMethod` hooks in your components.\n\n```jsx\nimport { useOperation } from 'react-openapi-client';\n\nconst PetDetails = (props) =\u003e {\n  const { loading, data, error } = useOperation('getPetById', props.id);\n\n  if (loading) {\n    return \u003cdiv\u003eLoading...\u003c/div\u003e;\n  }\n\n  if (error) {\n    return \u003cdiv\u003eError: {error}\u003c/div\u003e;\n  }\n\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003cimg src={data.image} alt={data.name} /\u003e\n      \u003ch3\u003e{data.name}\u003c/h3\u003e\n      \u003cul\u003e\n        \u003cli\u003e\n          \u003cstrong\u003eid:\u003c/strong\u003e {data.id}\n        \u003c/li\u003e\n        \u003cli\u003e\n          \u003cstrong\u003estatus:\u003c/strong\u003e {data.status}\n        \u003c/li\u003e\n      \u003c/ul\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\nSee a full Create-React-App example under [`examples/`](https://github.com/anttiviljami/react-openapi-client/tree/master/examples/)\n\n## useOperation hook\n\nThe `useOperation` hook is a great way to declaratively fetch data from your API.\n\nImportant! Calling `useOperation()` always immediately calls the API endpoint.\n\nParameters:\n\n`useOperation` passes the arguments to an OpenAPI Client Axios [`Operation Method`](https://github.com/anttiviljami/openapi-client-axios#operation-methods)\nmatching the operationId given as the first parameter.\n\n- [**operationId**](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-8) (string) Required. the operationId of the operation to call\n- [**parameters**](https://github.com/anttiviljami/openapi-client-axios#parameters) (object | string | number) Optional. Parameters for the operation\n- [**data**](https://github.com/anttiviljami/openapi-client-axios#data) (object | string | Buffer) Optional. Request payload for the operation\n- [**config**](https://github.com/anttiviljami/openapi-client-axios#config-object) (AxiosRequestConfig) Optional. Request payload for the operation\n\nReturn value:\n\n`useOperation` returns an object containing the following state properties:\n\n- **loading** (boolean) whether the API request is currently ongoing.\n- **data** (any) the parsed response data for the operation.\n- **response** (any) the raw axios response object for the operation.\n- **error** (Error) contains an error object, in case the request fails\n- **api** (OpenAPIClientAxios) reference to the API client class instance\n\nExample usage:\n\n```javascript\nconst { loading, data, error } = useOperation('getPetById', 1, null, { headers: { 'x-api-key': 'secret' } });\n```\n\n## useOperationMethod hook\n\nThe `useOperationMethod` hook can be used to obtain a callable operation method.\n\nUnlike `useOperation`, calling `useOperationMethod()` has no side effects.\n\nParameters:\n\n`useOperationMethod` gets the corresponding OpenAPI Client Axios [`Operation Method`](https://github.com/anttiviljami/openapi-client-axios#operation-methods)\nmatching the operationId.\n\n- [**operationId**](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-8) (string) Required. the operationId of the operation to call\n\nReturn value:\n\n`useOperationMethod` returns a tuple (javascript array), where the first\nelement is the callable operation method, and the second method contains the\nsame object as `useOperation`'s return value.\n\nSee [OpenAPI Client Axios documentation](https://github.com/anttiviljami/openapi-client-axios/blob/master/DOCS.md#operation-method)\nfor more details on how to use the Operation Methods.\n\nExample usage:\n\n```javascript\nconst [createPet, { loading, response, error }] = useOperationMethod('createPet');\n```\n\n## OpenAPIProvider\n\nThe `OpenAPIProvider` component provides `OpenAPIContext` to all nested components in the\nReact DOM so they can use the `useOperation` and `useOperationMethod` hooks.\n\nInternally, the Provider instantiates an instance of OpenAPIClientAxios, which\nis then used by the hooks to call the API operations.\n\nIn addition to the definition file, you can pass any [constructor options](https://github.com/anttiviljami/openapi-client-axios/blob/master/DOCS.md#class-openapiclientaxios)\naccepted by OpenAPIClientAxios as props to the `OpenAPIProvider` component.\n\nExample usage:\n\n```jsx\nconst App = () =\u003e (\n  \u003cOpenAPIProvider definition=\"http://petstore.swagger.io:8080/api/v3/openapi.json\" axiosConfigDefaults={{ withCredentials: true }}\u003e\n    \u003cApplicationLayout /\u003e\n  \u003c/OpenAPIProvider\u003e\n)\n```\n\nYou can also access the `OpenAPIClientAxios` instance by using the React `useContext` hook:\n\n```jsx\nimport React, { useContext } from 'react';\nimport { OpenAPIContext } from 'react-openapi-client';\n\nconst MyComponent = () =\u003e {\n  const { api } = useContext(OpenAPIContext);\n  const client = api.client;\n  const definition = api.definition;\n  // ...\n}\n```\n\n## Contributing\n\nReact OpenAPI Client is Free and Open Source Software. Issues and pull requests are more than welcome!\n\n[\u003cimg alt=\"The Chilicorn\" src=\"http://spiceprogram.org/assets/img/chilicorn_sticker.svg\" width=\"250\" height=\"250\"\u003e](https://spiceprogram.org/oss-sponsorship)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanttiviljami%2Freact-openapi-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanttiviljami%2Freact-openapi-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanttiviljami%2Freact-openapi-client/lists"}