feat(convex): add internal url and remote access fixes

This commit is contained in:
Esdras Renan 2025-11-11 16:06:11 -03:00
parent feb31d48c1
commit da46fa448b
17 changed files with 73 additions and 92 deletions

View file

@ -9,8 +9,14 @@ export class ConvexConfigurationError extends Error {
}
}
function isServerSide() {
return typeof window === "undefined"
}
export function requireConvexUrl(): string {
const url = env.NEXT_PUBLIC_CONVEX_URL
const url = isServerSide()
? env.CONVEX_INTERNAL_URL ?? env.NEXT_PUBLIC_CONVEX_URL
: env.NEXT_PUBLIC_CONVEX_URL
if (!url) {
throw new ConvexConfigurationError()
}
@ -21,4 +27,3 @@ export function createConvexClient(): ConvexHttpClient {
const url = requireConvexUrl()
return new ConvexHttpClient(url)
}

View file

@ -3,9 +3,9 @@ import { ConvexHttpClient } from "convex/browser"
import { api } from "@/convex/_generated/api"
import type { Id } from "@/convex/_generated/dataModel"
import { DEFAULT_TENANT_ID } from "@/lib/constants"
import { env } from "@/lib/env"
import { ensureMachineAccount } from "@/server/machines-auth"
import { auth } from "@/lib/auth"
import { requireConvexUrl } from "@/server/convex-client"
export type MachineSessionContext = {
machine: {
@ -38,11 +38,7 @@ export class MachineInactiveError extends Error {
}
export async function createMachineSession(machineToken: string, rememberMe = true): Promise<MachineSessionContext> {
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
if (!convexUrl) {
throw new Error("Convex não configurado")
}
const convexUrl = requireConvexUrl()
const client = new ConvexHttpClient(convexUrl)
const resolved = await client.mutation(api.devices.resolveToken, { machineToken })

View file

@ -4,9 +4,9 @@ import { ConvexHttpClient } from "convex/browser"
import { api } from "@/convex/_generated/api"
import type { Id } from "@/convex/_generated/dataModel"
import { env } from "@/lib/env"
import { buildXlsxWorkbook } from "@/lib/xlsx"
import { REPORT_EXPORT_DEFINITIONS, type ReportExportKey } from "@/lib/report-definitions"
import { requireConvexUrl } from "@/server/convex-client"
export type { ReportExportKey }
type ViewerIdentity = {
@ -24,11 +24,7 @@ export type ConvexReportContext = {
}
export async function createConvexContext(identity: ViewerIdentity): Promise<ConvexReportContext> {
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
if (!convexUrl) {
throw new Error("Convex URL não configurada para exportações")
}
const client = new ConvexHttpClient(convexUrl)
const client = new ConvexHttpClient(requireConvexUrl())
const ensuredUser = await client.mutation(api.users.ensureUser, {
tenantId: identity.tenantId,
name: identity.name,