|
| 1 | +## @databuddy/rpc |
| 2 | + |
| 3 | +### Overview |
| 4 | +- Uses tRPC for type-safe server procedures. |
| 5 | +- Exports: |
| 6 | + - `appRouter`, `type AppRouter` |
| 7 | + - `createTRPCContext`, `createTRPCRouter`, `publicProcedure`, `protectedProcedure`, `rateLimitedProtectedProcedure`, `rateLimitedAdminProcedure` |
| 8 | + - `getRateLimitIdentifier`, `rateLimiters` |
| 9 | + |
| 10 | +### Context |
| 11 | +```ts |
| 12 | +import { createTRPCContext } from '@databuddy/rpc'; |
| 13 | + |
| 14 | +// Called per request (headers required) |
| 15 | +const ctx = await createTRPCContext({ headers: request.headers }); |
| 16 | +// ctx = { db, auth, session, user, headers } |
| 17 | +``` |
| 18 | + |
| 19 | +### Routers |
| 20 | +```ts |
| 21 | +import { createTRPCRouter, publicProcedure, protectedProcedure } from '@databuddy/rpc'; |
| 22 | + |
| 23 | +export const exampleRouter = createTRPCRouter({ |
| 24 | + hello: publicProcedure.query(() => 'world'), |
| 25 | + me: protectedProcedure.query(({ ctx }) => ctx.user), |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +The `appRouter` mounts feature routers like `websites`, `funnels`, `preferences`, `goals`, `autocomplete`, `apikeys`, `experiments`. |
| 30 | + |
| 31 | +### Rate Limiting Utilities |
| 32 | +- `getRateLimitIdentifier(userId?: string, headers?: Headers): string` |
| 33 | +- `RateLimiter` class with methods: |
| 34 | + - `checkLimit(identifier: string): Promise<{ success; limit; remaining; reset; }>` |
| 35 | + - `getStatus(identifier: string)` |
| 36 | + - `reset(identifier: string)` |
| 37 | +- Built-in `rateLimiters`: `api`, `auth`, `expensive`, `admin`, `public`. |
| 38 | + |
| 39 | +Use within procedures via `rateLimitedProtectedProcedure` or in HTTP middleware (see `apps/api`). |
| 40 | + |
| 41 | +### Referrer Utilities |
| 42 | +- `parseReferrer(referrerUrl: string | null | undefined, currentDomain?: string): { type; name; url; domain }` |
| 43 | +- `categorizeReferrer(info): string` |
| 44 | +- `isInternalReferrer(referrerUrl: string, websiteHostname?: string): boolean` |
| 45 | + |
| 46 | +Example: |
| 47 | +```ts |
| 48 | +import { parseReferrer, categorizeReferrer } from '@databuddy/rpc/utils/referrer'; |
| 49 | + |
| 50 | +const info = parseReferrer('https://www.google.com/search?q=databuddy', 'example.com'); |
| 51 | +// { type: 'search', name: 'www.google.com', ... } |
| 52 | +const category = categorizeReferrer(info); // 'Search Engine' |
| 53 | +``` |
| 54 | + |
| 55 | +### Auth Utilities |
| 56 | +- `authorizeWebsiteAccess(ctx, websiteId, permission: 'read'|'update'|'delete'|'transfer')` |
| 57 | + - Ensures the current user (from tRPC context) has access to the given website, considering public sites, owner, admin, or org permissions; throws `TRPCError` otherwise. |
| 58 | +```ts |
| 59 | +import { authorizeWebsiteAccess } from '@databuddy/rpc/utils/auth'; |
| 60 | + |
| 61 | +export const websitesRouter = createTRPCRouter({ |
| 62 | + get: protectedProcedure.input(z.string()).query(async ({ ctx, input }) => { |
| 63 | + const site = await authorizeWebsiteAccess(ctx, input, 'read'); |
| 64 | + return site; |
| 65 | + }) |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### Billing Utilities |
| 70 | +- `checkAndTrackWebsiteCreation(customerId: string)` → `{ allowed: boolean, error?: string }` |
| 71 | +- `trackWebsiteUsage(customerId: string, value: number)` → `{ success: boolean }` |
| 72 | +- `getBillingCustomerId(userId: string, organizationId?: string|null): Promise<string>` |
| 73 | + |
| 74 | +```ts |
| 75 | +import { getBillingCustomerId, checkAndTrackWebsiteCreation } from '@databuddy/rpc/utils/billing'; |
| 76 | + |
| 77 | +const customerId = await getBillingCustomerId(ctx.user.id, ctx.user.organizationId); |
| 78 | +const { allowed } = await checkAndTrackWebsiteCreation(customerId); |
| 79 | +if (!allowed) throw new TRPCError({ code: 'FORBIDDEN' }); |
| 80 | +``` |
| 81 | + |
| 82 | +### Cache Invalidation Helpers |
| 83 | +- `invalidateBasicWebsiteCaches(websiteId, websiteCache)` |
| 84 | +- `invalidateWebsiteCaches(websiteId, userId, reason?)` |
| 85 | + |
| 86 | +```ts |
| 87 | +import { createDrizzleCache, redis } from '@databuddy/redis'; |
| 88 | +import { invalidateBasicWebsiteCaches, invalidateWebsiteCaches } from '@databuddy/rpc/utils/cache-invalidation'; |
| 89 | + |
| 90 | +const websiteCache = createDrizzleCache({ redis, namespace: 'websites' }); |
| 91 | +await invalidateBasicWebsiteCaches(websiteId, websiteCache); |
| 92 | +await invalidateWebsiteCaches(websiteId, ctx.user.id, 'website updated'); |
| 93 | +``` |
0 commit comments