@terreno/rtk
v57.16.2
Published
OpenAPI SDK, Better Auth Redux, and feature flags for Terreno frontends — part of Terreno, the batteries-included TypeScript framework for universal apps.
Maintainers
Readme
@terreno/rtk
Redux Toolkit Query utilities for @terreno/api backends.
Deprecation notice (56.0.0):
@terreno/rtkis deprecated for data synchronization as of version 56.0.0. It remains published with a deprecation notice through the current major line (56.x beta and stable 0.x) and will not be published in the next major Terreno release. Superseded by@terreno/syncdbfor collection reads/writes, offline sync, and realtime convergence.Still use @terreno/rtk for: generated OpenAPI SDK hooks (
bun run sdk) on non-synced routes, Better Auth session Redux, feature flags, sockets, and legacy JWT auth during migration. See the migration guide.
Removed in Terreno 58:
realtimeList,realtimeDocument,setRealtimeSocket, andgetRealtimeSocket.modelRouterrealtimeis also removed then.RealtimeAppstays for syncdb sockets.
Historical note: the offline mutation queue (
createOfflineMiddleware,offlineSlice,configureOfflineMutationEndpoints) and realtime cache patching (realtimeList,realtimeDocument) were superseded by@terreno/syncdbbefore this deprecation window.
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
Install
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
Feature flags (OpenFeature)
useFeatureFlags and useTerrenoFeatureFlags wire Terreno’s GET /feature-flags/flagConfiguration into OpenFeature. Peer dependencies: @openfeature/react-sdk and @openfeature/web-sdk (install them in your app; do not rely on transitive copies).
Full upgrade steps (backend + frontend + SDK regen) live in Add feature flags.
Documentation
Archived RTK reference: docs/reference/legacy/rtk.md
License and Contributing
Licensed under the MIT License. See CONTRIBUTING.md for contribution guidelines.
