|
| 1 | +--- |
| 2 | +title: Ecency SDK |
| 3 | +description: Framework-agnostic data layer for Hive apps with first-class React Query support. |
| 4 | +--- |
| 5 | + |
| 6 | +# Ecency SDK |
| 7 | + |
| 8 | +The Ecency SDK is a framework-agnostic data layer for Hive applications with first-class |
| 9 | +**React Query** support. It provides composable query option builders, mutation hooks, and a |
| 10 | +shared configuration layer for Hive RPC and caching. |
| 11 | + |
| 12 | +## What’s inside |
| 13 | + |
| 14 | +- Query and mutation option builders powered by [@tanstack/react-query](https://tanstack.com/query) |
| 15 | +- Modular APIs: accounts, posts, communities, market, wallet, notifications, analytics, |
| 16 | + integrations, core, auth, bridge, games, hive-engine, operations, points, private-api, |
| 17 | + promotions, proposals, resource-credits, search, spk, witnesses |
| 18 | +- Central configuration via `CONFIG` / `ConfigManager` (RPC client, QueryClient) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```sh |
| 23 | +yarn add @ecency/sdk |
| 24 | +# or |
| 25 | +npm install @ecency/sdk |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick start (React Query) |
| 29 | + |
| 30 | +```ts |
| 31 | +import { getAccountFullQueryOptions } from "@ecency/sdk"; |
| 32 | +import { useQuery } from "@tanstack/react-query"; |
| 33 | + |
| 34 | +const { data, isLoading } = useQuery(getAccountFullQueryOptions("ecency")); |
| 35 | +``` |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +The SDK uses a shared configuration singleton for the Hive RPC client and query client. |
| 40 | + |
| 41 | +```ts |
| 42 | +import { ConfigManager, makeQueryClient } from "@ecency/sdk"; |
| 43 | + |
| 44 | +// Optional: provide your own QueryClient |
| 45 | +ConfigManager.setQueryClient(makeQueryClient()); |
| 46 | +``` |
| 47 | + |
| 48 | +If your app sets up a custom Hive client, configure it at startup so all SDK modules |
| 49 | +share the same instance. |
| 50 | + |
| 51 | +## Query options vs direct calls |
| 52 | + |
| 53 | +Most APIs are exposed as **query option builders** to keep caching consistent: |
| 54 | + |
| 55 | +```ts |
| 56 | +import { getPostsRankedQueryOptions } from "@ecency/sdk"; |
| 57 | + |
| 58 | +// Use in React Query hooks |
| 59 | +useQuery(getPostsRankedQueryOptions("trending", "", "", 20)); |
| 60 | +``` |
| 61 | + |
| 62 | +Direct request helpers still exist for non-React contexts (e.g., server jobs). |
| 63 | +Prefer query options in UI code. |
| 64 | + |
| 65 | +## Mutations |
| 66 | + |
| 67 | +Mutations are provided as hooks that wrap `useMutation`: |
| 68 | + |
| 69 | +```ts |
| 70 | +import { useAccountUpdate } from "@ecency/sdk"; |
| 71 | + |
| 72 | +const auth = { |
| 73 | + accessToken, |
| 74 | + postingKey, |
| 75 | + loginType, |
| 76 | +}; |
| 77 | + |
| 78 | +const { mutateAsync } = useAccountUpdate(username, auth); |
| 79 | +await mutateAsync({ metadata }); |
| 80 | +``` |
| 81 | + |
| 82 | +`AuthContext` properties are optional. The example above works without an explicit |
| 83 | +`broadcast` function; the SDK will use `postingKey` or `accessToken` if provided. If your |
| 84 | +app needs custom broadcasting (Keychain, HiveAuth, mobile), add the optional `broadcast` |
| 85 | +function as shown below. |
| 86 | + |
| 87 | +## Broadcasting and auth context |
| 88 | + |
| 89 | +The SDK does not manage storage or platform-specific signers. Instead, it uses an |
| 90 | +`AuthContext` that you pass from the app: |
| 91 | + |
| 92 | +```ts |
| 93 | +import type { AuthContext } from "@ecency/sdk"; |
| 94 | + |
| 95 | +const auth: AuthContext = { |
| 96 | + accessToken, |
| 97 | + postingKey, |
| 98 | + loginType, |
| 99 | + broadcast: async (operations, authority = "posting") => { |
| 100 | + // App-specific broadcaster (Keychain, HiveAuth, mobile wallet) |
| 101 | + return myBroadcaster(operations, authority); |
| 102 | + }, |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +If `auth.broadcast` is provided, the SDK will call it for posting broadcasts and |
| 107 | +keychain/hiveauth flows. Otherwise it falls back to: |
| 108 | + |
| 109 | +- `postingKey` (direct signing via dhive) |
| 110 | +- `accessToken` (Hivesigner) |
| 111 | + |
| 112 | +## Active/owner key signing |
| 113 | + |
| 114 | +For operations that must be signed with Active or Owner authority, use |
| 115 | +`useSignOperationByKey` and pass the appropriate private key: |
| 116 | + |
| 117 | +```ts |
| 118 | +import { useSignOperationByKey } from "@ecency/sdk"; |
| 119 | + |
| 120 | +const { mutateAsync } = useSignOperationByKey(username); |
| 121 | +await mutateAsync({ |
| 122 | + operation: ["account_update", { /* ... */ }], |
| 123 | + keyOrSeed: activeKey, |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +If you want the SDK to broadcast Active/Owner operations through an external signer |
| 128 | +(Keychain, HiveAuth, mobile), provide an `auth.broadcast` handler and use the |
| 129 | +`authority` argument: |
| 130 | + |
| 131 | +```ts |
| 132 | +const auth = { |
| 133 | + broadcast: (ops, authority = "posting") => { |
| 134 | + return myBroadcaster(ops, authority); |
| 135 | + }, |
| 136 | +}; |
| 137 | +``` |
| 138 | + |
| 139 | +## Module layout |
| 140 | + |
| 141 | +```text |
| 142 | +src/modules/ |
| 143 | + accounts/ account data, relationships, mutations |
| 144 | + analytics/ activity tracking and stats |
| 145 | + auth/ login, tokens, and auth helpers |
| 146 | + bridge/ bridge API helpers |
| 147 | + communities/ community queries and utils |
| 148 | + core/ config, client, query manager, helpers |
| 149 | + games/ game-related endpoints |
| 150 | + hive-engine/ hive-engine data helpers |
| 151 | + integrations/ external integrations (hivesigner, 3speak, etc.) |
| 152 | + market/ market data and pricing |
| 153 | + notifications/ notification queries and enums |
| 154 | + operations/ operation signing helpers |
| 155 | + points/ points queries and mutations |
| 156 | + posts/ post queries, mutations, utils |
| 157 | + private-api/ private API helpers |
| 158 | + promotions/ promotion queries |
| 159 | + proposals/ proposal queries and mutations |
| 160 | + resource-credits/ RC stats helpers |
| 161 | + search/ search queries |
| 162 | + spk/ SPK data helpers |
| 163 | + wallet/ wallet-related queries and types |
| 164 | + witnesses/ witness queries and votes |
| 165 | +``` |
| 166 | + |
| 167 | +## SSR / RSC notes |
| 168 | + |
| 169 | +- Query options are safe to use on the server, but hooks are client-only. |
| 170 | +- If you use Next.js App Router, keep hook usage in client components. |
0 commit comments