Skip to content

Commit

Permalink
Update documentation to support only the enhanceApi function
Browse files Browse the repository at this point in the history
  • Loading branch information
otahontas committed Mar 20, 2024
1 parent a31c87c commit e3f6bd1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
7 changes: 7 additions & 0 deletions .changeset/dry-buses-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"trpc-rtk-query": minor
---

Remove createApi function and support only enhancing existing apis. This way we have to support only
one method of generating hooks for apis. There's now a `createEmptyApi` helper to make it easier to
create a base api without any endpoints, which can be then passed to api.
89 changes: 47 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@

## **[RTK Query](https://redux-toolkit.js.org/rtk-query/overview) support for [tRPC](https://trpc.io/)** 🧩

- Automatically generate **typesafe** `RTK Query hooks` from your `tRPC` procedures.
- Automatically generate **typesafe** `RTK Query hooks` (for react) from your `tRPC` procedures.
- Perfect for incremental adoption.
- Any feedback, issues, or pull requests are highly appreciated

```typescript

import { createApi } from "trpc-rtk-query";

const api = createApi({
client: trpcClient, /* 👈 This is the magic */
/* ⬇️ You can still pass all the RTK config properties */
tagTypes: ["User"],
reducerPath: "trpcApi"
const api = enhanceApi({
api: rtkApi, /* Your api created with rtk query */
client: trpcClient, /* 👈 The trpc magic happens when passing in the typed client ✨ */
});

export { useUserListQuery } from api; // Automatically typed hooks thanks to the power of tRPC + RTK!
Expand All @@ -45,12 +41,9 @@ npm install trpc-rtk-query @reduxjs/toolkit @trpc/client @trpc/server
yarn add trpc-rtk-query @reduxjs/toolkit @trpc/client @trpc/server
```

Note the minimum versions for packages:

- @reduxjs/toolkit: `1.9.7`
- @trpc/client & @trpc/server `10.38.5`
Note the minimum versions for packages, we only support trpc v10 and rtk query v2.

**2. Initialize the `tRPC router`.**
**2. Use your `tRPC router`.**

```typescript
/* server.ts */
Expand Down Expand Up @@ -94,22 +87,54 @@ const client = createTRPCProxyClient<AppRouter>({

```

**4. Create a new automatically typed API.**
**4. Create a trpc-rtk query api.**

```typescript
// api.ts
import { createApi } from "trpc-rtk-query";
import { createApi } from "@reduxjs/toolkit/query/react";
import { enhanceApi } from "trpc-rtk-query";

const api = createApi({
client, /* 👈 This is the magic */
/* ⬇️ You can still pass all the RTK config properties */
tagTypes: ["User"],
reducerPath: "trpcApi"
// Use function provided by rtk to create your api
const rtkApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/" }),
endpoints: () => ({}),
})

// Enhance the api with hooks
export const api = enhanceApi({
client, // <- typed client from step 3
api: rtkApi // <- api from rtk
// pass in any endpoint specific options, such as providesTags or onQueryStarted for optimistic updates
endpointOptions: {
userList: {
providesTags: ["User"]
}
}
});

export { useUserListQuery } from api; // Automatically typed hooks thanks to the power of tRPC + RTK!
export { useUserListQuery } from api; // <- export your typed hooks!
```

You can also use `createEmptyApi` helper function as follows:

```typescript
// api.ts
import { createEmptyApi, enhanceApi } from "trpc-rtk-query";

// Enhance an empty api with hooks
export const api = enhanceApi({
client, // <- typed client from step 3
api: createEmptyApi(), // <- createEmptyApi generates base api without any endpoints.
// pass in any endpoint specific options, such as providesTags or onQueryStarted for optimistic updates
endpointOptions: {
userList: {
providesTags: ["User"]
}
}
});

export { useUserListQuery } from api; // <- export your typed hooks!

**5. Add the typesafe API to the store.**
This is the same step as you would [normally do](https://redux-toolkit.js.org/rtk-query/overview).

Expand Down Expand Up @@ -142,26 +167,6 @@ const App = () => {
}
```

### Using existing api

You might already have an RTK query API instance for a non-tRPC backend. In this case, you can pass the previous API in with the tRPC client, and new endpoints will be generated similarly as above.

```typescript
import { enhanceApi } from "trpc-rtk-query";
export const api = enhanceApi({
client, // <- your typed client from step 1
api: existingApi // <- your existing api
// pass in any endpoint specific options, such as providesTags or onQueryStarted for optimistic updates
endpointOptions: {
userList: {
providesTags: ["User"]
}
}
});

export { useUserListQuery } from api; // <- export your typed hooks
```

# Development status

This library is currently in the alpha stage. 0.0.x versions are being published to npm for people to try this out, but you shouldn't consider it ready for production anytime soon. See the [0.1.0 project](https://github.com/users/otahontas/projects/2) for what's under development and planned to be done before 0.1.0 can be released.
This library is currently in the alpha stage. 0.x.x versions are being published to npm for people to try this out, but you shouldn't consider it ready for production anytime soon. See the [project status](https://github.com/users/otahontas/projects/2) for what's under development and planned to be done before first major 1.0.0 will be released.
10 changes: 10 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type Api,
type BaseQueryFn,
type EndpointDefinitions,
createApi,
} from "@reduxjs/toolkit/query/react";
import { type AnyRouter } from "@trpc/server";

Expand Down Expand Up @@ -71,3 +72,12 @@ export const enhanceApi = <
endpointOptions: options.endpointOptions,
tRPCClientOptions: options,
});

/*
* Helper to create base api with no endpoints
*/
export const createEmptyApi = () =>
createApi({
baseQuery: () => ({ data: undefined }),
endpoints: () => ({}),
});

0 comments on commit e3f6bd1

Please sign in to comment.