@terreno/rtk
v0.0.14
Published
Redux Toolkit Query utilities for @terreno/api backends
Readme
@terreno/rtk
Redux Toolkit Query utilities for @terreno/api backends with React Native / Expo support.
Features
- Authentication slice with JWT token management
- Secure token storage (SecureStore on mobile, AsyncStorage on web)
- Automatic token refresh
- Socket.io connection management with auth
- RTK Query base API with auth header injection
- OpenAPI SDK generation support
Installation
This package is part of the terreno workspace. Add it as a dependency:
bun install @terreno/rtkUsage
Setting up the store
import {generateAuthSlice} from "@terreno/rtk";
import {configureStore} from "@reduxjs/toolkit";
import {openapi} from "./openApiSdk";
const {authReducer, middleware} = generateAuthSlice(openapi);
export const store = configureStore({
reducer: {
auth: authReducer,
[openapi.reducerPath]: openapi.reducer,
},
middleware: (getDefault) =>
getDefault().concat(openapi.middleware, ...middleware),
});Generating an OpenAPI SDK
Create an openapi-config.ts in your project:
import type {ConfigFile} from "@rtk-query/codegen-openapi";
const config: ConfigFile = {
apiFile: "@terreno/rtk",
apiImport: "emptySplitApi",
argSuffix: "Args",
exportName: "openapi",
flattenArg: true,
hooks: true,
outputFile: "./store/openApiSdk.ts",
responseSuffix: "Res",
schemaFile: "http://localhost:3000/openapi.json",
tag: true,
};
export default config;Then run the codegen:
npx @rtk-query/codegen-openapi openapi-config.tsUsing socket connections
import {useSocketConnection, getAuthToken, baseUrl} from "@terreno/rtk";
const {socket, isSocketConnected} = useSocketConnection({
baseUrl,
getAuthToken,
shouldConnect: !!userId,
onConnect: () => console.log("Connected"),
onDisconnect: () => console.log("Disconnected"),
});Exports
generateAuthSlice- Creates auth slice with login/logout/token managementgenerateProfileEndpoints- RTK Query endpoints for auth operationsemptySplitApi- Base RTK Query API with authuseSocketConnection- Socket.io connection hookgetAuthToken- Get current auth tokenbaseUrl,baseWebsocketsUrl,baseTasksUrl- URL constants from Expo configIsWeb- Platform detection helpergenerateTags- RTK Query tag generator for cache invalidationListResponse,populateId- Mongoose list response utilities
