Skip to content

Commit

Permalink
moved all web envs to next runtime env (#3146)
Browse files Browse the repository at this point in the history
  • Loading branch information
chitalian authored Jan 21, 2025
1 parent bd03822 commit 4526319
Show file tree
Hide file tree
Showing 47 changed files with 197 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ interface AnthropicProxyProps {
apiKey: string;
}

const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || "";

const CODE_CONVERTS = {
curl: (key: string) => `
curl --request POST \\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { DiffHighlight } from "../../diffHighlight";
import { Button } from "@/components/ui/button";
import Link from "next/link";

const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || "";

const CODE_CONVERTS = {
typescript: (key: string) => `import Anthropic from "@anthropic-ai/sdk";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { DiffHighlight } from "../../diffHighlight";
import { Button } from "@/components/ui/button";
import Link from "next/link";

const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || "";

const CODE_CONVERTS = {
typescript: (key: string) => `import OpenAI from "openai";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useState } from "react";
import { DiffHighlight } from "../../diffHighlight";
import { Button } from "@/components/ui/button";
import Link from "next/link";

const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || "";
import { env } from "next-runtime-env";
const BASE_PATH = env("NEXT_PUBLIC_BASE_PATH");

const CODE_CONVERTS = {
curl: (key: string) => `curl "${BASE_PATH}/chat/completions" \\
Expand Down
4 changes: 2 additions & 2 deletions web/lib/api/organization/hasAccess.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { supabaseServer } from "../../supabaseServer";
import { getSupabaseServer } from "../../supabaseServer";
import { dbExecute } from "../db/dbExecute";

async function _checkAccessToOrg(
Expand All @@ -23,7 +23,7 @@ export async function checkAccessToMutateOrg(
orgId: string,
userId: string
): Promise<boolean> {
const orgToCheck = await supabaseServer
const orgToCheck = await getSupabaseServer()
.from("organization")
.select("*")
.eq("id", orgId)
Expand Down
3 changes: 2 additions & 1 deletion web/lib/clients/jawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ORG_ID_COOKIE_KEY } from "../constants";
import { getHeliconeCookie } from "../cookies";
import type { paths as publicPaths } from "./jawnTypes/public";
import type { paths as privatePaths } from "./jawnTypes/private";
import { env } from "next-runtime-env";

export type JawnFilterNode = any;

Expand All @@ -22,7 +23,7 @@ export function getJawnClient(orgId?: string | "none") {
: {};

return createClient<publicPaths & privatePaths>({
baseUrl: process.env.NEXT_PUBLIC_HELICONE_JAWN_SERVICE,
baseUrl: env("NEXT_PUBLIC_HELICONE_JAWN_SERVICE"),
headers,
});
}
Expand Down
6 changes: 3 additions & 3 deletions web/lib/clients/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { supabaseServer } from "../supabaseServer";
import { getSupabaseServer } from "../supabaseServer";

export async function getOpenAIKeyFromAdmin(): Promise<string | undefined> {
const { data } = await supabaseServer
const { data } = await getSupabaseServer()
.from("helicone_settings")
.select("*")
.eq("name", "openai:apiKey");
Expand All @@ -10,7 +10,7 @@ export async function getOpenAIKeyFromAdmin(): Promise<string | undefined> {
}

export async function getAnthropicKeyFromAdmin(): Promise<string | undefined> {
const { data } = await supabaseServer
const { data } = await getSupabaseServer()
.from("helicone_settings")
.select("*")
.eq("name", "anthropic:apiKey");
Expand Down
10 changes: 2 additions & 8 deletions web/lib/supabaseServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,5 @@ export const supabaseUrl =
process.env.SUPABASE_URL ?? process.env.NEXT_PUBLIC_SUPABASE_URL ?? "";
const supabaseServiceKey = process.env.SUPABASE_SERVICE_KEY ?? "n/a";

if (supabaseUrl === "" || supabaseServiceKey === "") {
throw new Error(`URL or Anon ENV not set for Server - ${supabaseServiceKey}`);
}

export const supabaseServer = createClient<Database>(
supabaseUrl,
supabaseServiceKey
);
export const getSupabaseServer = () =>
createClient<Database>(supabaseUrl, supabaseServiceKey);
4 changes: 4 additions & 0 deletions web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});

const { configureRuntimeEnv } = require("next-runtime-env/build/configure");

configureRuntimeEnv();

/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
Expand Down
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"moment": "^2.29.4",
"monaco-editor": "^0.52.0",
"next": "^14.2.13",
"next-runtime-env": "^1.7.1",
"next-sitemap": "^4.2.3",
"next-themes": "^0.3.0",
"octokit": "^3.1.2",
Expand Down
47 changes: 38 additions & 9 deletions web/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createBrowserSupabaseClient } from "@supabase/auth-helpers-nextjs";
import { SessionContextProvider } from "@supabase/auth-helpers-react";
import { Session, SessionContextProvider } from "@supabase/auth-helpers-react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { AppProps } from "next/app";
import { ReactElement, ReactNode, useState } from "react";
Expand All @@ -21,6 +21,7 @@ import Script from "next/script";
import { PostHogProvider } from "posthog-js/react";
import { TooltipProvider } from "@/components/ui/tooltip";
import { Inter } from "next/font/google";
import { env } from "next-runtime-env";

const inter = Inter({ subsets: ["latin"] });

Expand Down Expand Up @@ -53,23 +54,51 @@ export function PHProvider({ children }: { children: React.ReactNode }) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
}

export function SupabaseProvider({
children,
initialSession,
}: {
children: React.ReactNode;
initialSession?: Session | null;
}) {
const [supabaseClient] = useState(() => {
if (
env("NEXT_PUBLIC_SUPABASE_URL") &&
env("NEXT_PUBLIC_SUPABASE_ANON_KEY")
) {
return createBrowserSupabaseClient({
supabaseUrl: env("NEXT_PUBLIC_SUPABASE_URL"),
supabaseKey: env("NEXT_PUBLIC_SUPABASE_ANON_KEY"),
});
}
return null;
});

if (!supabaseClient) {
return <>{children}</>;
}

return (
<SessionContextProvider
supabaseClient={supabaseClient}
initialSession={initialSession}
>
{children}
</SessionContextProvider>
);
}

export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
const queryClient = new QueryClient();

// Create a new supabase browser client on every first render.
const [supabaseClient] = useState(() => createBrowserSupabaseClient());

const getLayout = Component.getLayout ?? ((page) => page);

const trackingEnabled = process.env.NEXT_PUBLIC_TRACKING_ENABLED || false;

return (
<>
<PHProvider>
<SessionContextProvider
supabaseClient={supabaseClient}
initialSession={pageProps.initialSession}
>
<SupabaseProvider>
<QueryClientProvider client={queryClient}>
<NotificationProvider>
<DndProvider backend={HTML5Backend}>
Expand All @@ -86,7 +115,7 @@ export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
</DndProvider>
</NotificationProvider>
</QueryClientProvider>
</SessionContextProvider>
</SupabaseProvider>
</PHProvider>
{trackingEnabled && <Analytics />}
{trackingEnabled && (
Expand Down
5 changes: 4 additions & 1 deletion web/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class MyDocument extends Document {

return (
<Html className={isOpenStatsPage ? "open-stats-html" : ""}>
<Head />
<Head>
{/* eslint-disable-next-line @next/next/no-sync-scripts */}
<script src="/__ENV.js" />
</Head>
<body className={isOpenStatsPage ? "open-stats-body" : ""}>
<Main />
<NextScript />
Expand Down
4 changes: 2 additions & 2 deletions web/pages/admin/feature-flags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { User } from "@supabase/auth-helpers-nextjs";
import { ReactElement } from "react";
import AdminLayout from "../../components/layout/admin/adminLayout";
import { withAuthSSR } from "../../lib/api/handlerWrappers";
import { supabaseServer } from "../../lib/supabaseServer";
import { getSupabaseServer } from "../../lib/supabaseServer";
import { AdminFeatureFlags } from "../../components/templates/admin/adminFeatureFlags";

interface AdminProps {
Expand All @@ -28,7 +28,7 @@ export const getServerSideProps = withAuthSSR(async (options) => {

// const { data } = await jawn.GET("/v1/admin/admins/query");

const { data, error } = await supabaseServer.from("admins").select("*");
const { data, error } = await getSupabaseServer().from("admins").select("*");

const admins = data?.map((admin) => admin.user_id || "") || [];

Expand Down
4 changes: 2 additions & 2 deletions web/pages/admin/governance-orgs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { User } from "@supabase/auth-helpers-nextjs";
import { ReactElement } from "react";
import AdminLayout from "../../components/layout/admin/adminLayout";
import { withAuthSSR } from "../../lib/api/handlerWrappers";
import { supabaseServer } from "../../lib/supabaseServer";
import { getSupabaseServer } from "../../lib/supabaseServer";
import { AdminGovernanceOrgsPage } from "@/components/templates/admin/onPrem/GovernanceOrgs";

interface AdminProps {
Expand All @@ -28,7 +28,7 @@ export const getServerSideProps = withAuthSSR(async (options) => {

// const { data } = await jawn.GET("/v1/admin/admins/query");

const { data, error } = await supabaseServer.from("admins").select("*");
const { data, error } = await getSupabaseServer().from("admins").select("*");

const admins = data?.map((admin) => admin.user_id || "") || [];

Expand Down
4 changes: 2 additions & 2 deletions web/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReactElement } from "react";
import { withAuthSSR } from "../../lib/api/handlerWrappers";
import AdminLayout from "../../components/layout/admin/adminLayout";
import AdminPage from "../../components/templates/admin/adminPage";
import { supabaseServer } from "../../lib/supabaseServer";
import { getSupabaseServer } from "../../lib/supabaseServer";

interface AdminProps {
user: User;
Expand All @@ -28,7 +28,7 @@ export const getServerSideProps = withAuthSSR(async (options) => {

// const { data } = await jawn.GET("/v1/admin/admins/query");

const { data, error } = await supabaseServer.from("admins").select("*");
const { data, error } = await getSupabaseServer().from("admins").select("*");

const admins = data?.map((admin) => admin.user_id || "") || [];

Expand Down
4 changes: 2 additions & 2 deletions web/pages/admin/metrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { User } from "@supabase/auth-helpers-nextjs";
import { ReactElement } from "react";
import AdminLayout from "../../components/layout/admin/adminLayout";
import { withAuthSSR } from "../../lib/api/handlerWrappers";
import { supabaseServer } from "../../lib/supabaseServer";
import { getSupabaseServer } from "../../lib/supabaseServer";
import AdminMetrics from "../../components/templates/admin/adminMetrics";

interface AdminProps {
Expand All @@ -28,7 +28,7 @@ export const getServerSideProps = withAuthSSR(async (options) => {

// const { data } = await jawn.GET("/v1/admin/admins/query");

const { data, error } = await supabaseServer.from("admins").select("*");
const { data, error } = await getSupabaseServer().from("admins").select("*");

const admins = data?.map((admin) => admin.user_id || "") || [];

Expand Down
4 changes: 2 additions & 2 deletions web/pages/admin/on-prem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReactElement } from "react";
import AdminLayout from "../../components/layout/admin/adminLayout";
import { AdminOnPremPage } from "../../components/templates/admin/onPrem/adminOnPrem";
import { withAuthSSR } from "../../lib/api/handlerWrappers";
import { supabaseServer } from "../../lib/supabaseServer";
import { getSupabaseServer } from "../../lib/supabaseServer";

interface AdminProps {
user: User;
Expand All @@ -28,7 +28,7 @@ export const getServerSideProps = withAuthSSR(async (options) => {

// const { data } = await jawn.GET("/v1/admin/admins/query");

const { data, error } = await supabaseServer.from("admins").select("*");
const { data, error } = await getSupabaseServer().from("admins").select("*");

const admins = data?.map((admin) => admin.user_id || "") || [];

Expand Down
4 changes: 2 additions & 2 deletions web/pages/admin/stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { User } from "@supabase/auth-helpers-nextjs";
import { ReactElement } from "react";
import AdminLayout from "../../components/layout/admin/adminLayout";
import { withAuthSSR } from "../../lib/api/handlerWrappers";
import { supabaseServer } from "../../lib/supabaseServer";
import { getSupabaseServer } from "../../lib/supabaseServer";
import AdminStats from "../../components/templates/admin/adminStats";

interface AdminProps {
Expand All @@ -28,7 +28,7 @@ export const getServerSideProps = withAuthSSR(async (options) => {

// const { data } = await jawn.GET("/v1/admin/admins/query");

const { data, error } = await supabaseServer.from("admins").select("*");
const { data, error } = await getSupabaseServer().from("admins").select("*");

const admins = data?.map((admin) => admin.user_id || "") || [];

Expand Down
15 changes: 8 additions & 7 deletions web/pages/api/alerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ import {
withAuth,
} from "../../../lib/api/handlerWrappers";
import { Result } from "../../../lib/result";
import { supabaseServer } from "../../../lib/supabaseServer";
import { getSupabaseServer } from "../../../lib/supabaseServer";

async function handler({
req,
res,
userData: { orgId },
}: HandlerWrapperOptions<Result<any, string>>) {
let { data: alert, error: alertError } = await supabaseServer
let { data: alert, error: alertError } = await getSupabaseServer()
.from("alert")
.select("*")
.eq("org_id", orgId)
.not("soft_delete", "eq", true);

let { data: alertHistory, error: alertHistoryError } = await supabaseServer
.from("alert_history")
.select("*")
.eq("org_id", orgId)
.not("soft_delete", "eq", true);
let { data: alertHistory, error: alertHistoryError } =
await getSupabaseServer()
.from("alert_history")
.select("*")
.eq("org_id", orgId)
.not("soft_delete", "eq", true);

if (alertError || !alert) {
res.status(500).json({ error: alertError?.message || "", data: null });
Expand Down
20 changes: 11 additions & 9 deletions web/pages/api/contact/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { supabaseServer } from "../../../lib/supabaseServer";
import { getSupabaseServer } from "../../../lib/supabaseServer";

export default async function handler(
req: NextApiRequest,
Expand All @@ -12,14 +12,16 @@ export default async function handler(
const { firstName, lastName, email, companyName, companyDescription, tag } =
req.body;

const { error } = await supabaseServer.from("contact_submissions").insert({
first_name: firstName,
last_name: lastName,
email_address: email,
company_name: companyName,
company_description: companyDescription,
tag: tag,
});
const { error } = await getSupabaseServer()
.from("contact_submissions")
.insert({
first_name: firstName,
last_name: lastName,
email_address: email,
company_name: companyName,
company_description: companyDescription,
tag: tag,
});

if (error) {
console.error("Failed to submit contact form");
Expand Down
Loading

0 comments on commit 4526319

Please sign in to comment.