Migrating to OpenAPI Qraft v2
This guide outlines the steps required to migrate your project from OpenAPI Qraft v1 to v2. The new version introduces several breaking changes designed to improve developer experience and enhance the library's functionality.
Speed up your migration process with our automated Codemod tool ๐ช!
This Migration Guide is currently a work in progress ๐งช
Key Changesโ
1. Removal of QraftContext
โ
- Change: The
QraftContext
has been removed in favor of a more streamlined API design increateAPIClient
. - Impact: You'll now need to provide
baseUrl
andqueryClient
directly to thecreateAPIClient
function. This change ensures consistent API client configuration throughout your project.
2. Simplified QueryClient
Usageโ
- Change: The ability to pass a
QueryClient
for individual hooks or methods has been removed. - Impact: A single
QueryClient
instance is now associated withcreateAPIClient
, simplifying the API and preventing potential configuration conflicts.
3. Deprecation and Removal of mutationFn
and queryFn
Methodsโ
- Change: The legacy
mutationFn
andqueryFn
methods have been removed. - Impact: You'll need to update your code to use the new API methods introduced in v2.
4. Error Handling Enhancementsโ
- Change: The
requestFn
now returns a structured{ data, error, response }
object instead of using Promise rejection. - Impact: This change enables more robust error handling within your application. Update your error handling logic accordingly.
5. Updates to qraftAPIClient
โ
- Change: The
qraftAPIClient(...)
now only returns services corresponding to methods with provided callbacks. - Impact: Review your service definitions to ensure compatibility with this behavior.
Migration Stepsโ
- Update
QraftContext
Usage: Replace allQraftContext
implementations by passing configuration directly tocreateAPIClient
. - Consolidate QueryClient Usage: Remove any per-hook or per-method
QueryClient
configurations and use the instance provided tocreateAPIClient
. - Update Legacy Methods: Replace any
mutationFn
andqueryFn
usage with their v2 equivalents, referring to the core Query and Mutation documentation. - Enhance Error Handling: Update your error handling to work with the new structured response format from
requestFn
. - Review Service Definitions: Verify that your service definitions align with the new
qraftAPIClient(...)
behavior. - Test Thoroughly: Perform comprehensive testing to ensure all changes work as expected and no regressions are introduced.
Automated Migration ๐ชโ
We provide a Codemod script powered by jscodeshift
to automate most of the migration process.
Run this command in your project root to apply the migrations:
npx jscodeshift@latest --extensions=tsx,ts --parser=tsx -t https://raw.githubusercontent.com/OpenAPI-Qraft/openapi-qraft/main/packages/react-client/src/migrate-to-v2-codemod.ts ./src/
This command will transform all .tsx
and .ts
files in your src
directory.
After running the script, review the changes and make any necessary manual adjustments.
Following these steps and utilizing the automated migration tool will help you smoothly transition to OpenAPI Qraft v2 while taking advantage of its improved features and capabilities.