Skip to content

Commit

Permalink
fix: move supabase query to netlify function
Browse files Browse the repository at this point in the history
  • Loading branch information
nvm1410 committed Dec 19, 2024
1 parent e42970e commit 6651b6e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
38 changes: 38 additions & 0 deletions web/netlify/functions/supabase-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { HandlerContext, HandlerEvent } from "@netlify/functions";
import { createClient } from "@supabase/supabase-js";

require("dotenv").config();

export const handler = async (event: HandlerEvent, _context: HandlerContext) => {
const [tableName, recordId] = event.path
.split("/")
.slice(event.path.split("/").indexOf("supabase-query") + 1, event.path.split("/").indexOf("supabase-query") + 3);
if (!tableName) {
return {
statusCode: 500,
body: JSON.stringify({ message: "No table selected." }),
};
}
// Create a single supabase client for interacting with your database
try {
const supabase = createClient(process.env.VITE_SUPABASE_PROJECT_URL!, process.env.VITE_SUPABASE_API_KEY!);
const { data, error } = await (recordId
? supabase.from(tableName).select().eq("id", recordId)
: supabase.from(tableName).select());
if (error) {
return {
statusCode: 500,
body: JSON.stringify({ message: error.message }),
};
}
return {
statusCode: 200,
body: JSON.stringify({ data }),
};
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify({ message: e.message }),
};
}
};
11 changes: 5 additions & 6 deletions web/src/hooks/useMarkets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SUPPORTED_CHAINS, SupportedChain } from "@/lib/chains";
import { ITEMS_PER_PAGE, searchGraphMarkets, searchOnChainMarkets, sortMarkets } from "@/lib/markets-search";
import { queryClient } from "@/lib/query-client";
import { createClient } from "@supabase/supabase-js";
import { useQuery } from "@tanstack/react-query";
import { Address } from "viem";
import { useAccount } from "wagmi";
Expand All @@ -11,9 +10,6 @@ import { Market, VerificationStatus, getUseGraphMarketKey } from "./useMarket";
import { MarketStatus } from "./useMarketStatus";
import useMarketsSearchParams from "./useMarketsSearchParams";

// Create a single supabase client for interacting with your database
const supabase = createClient(import.meta.env.VITE_SUPABASE_PROJECT_URL, import.meta.env.VITE_SUPABASE_API_KEY);

const useOnChainMarkets = (
chainsList: Array<string | "all">,
marketName: string,
Expand Down Expand Up @@ -61,8 +57,11 @@ const useGraphMarkets = (
).flat();
let marketToLiquidityCheckMapping: { [key: string]: boolean } | undefined;
try {
const { data } = await supabase.from("markets").select();
marketToLiquidityCheckMapping = data?.reduce(
const { data } = await fetch("https://app.seer.pm/.netlify/functions/supabase-query/markets").then((res) =>
res.json(),
);
const markets = data as { id: string; odds: (number | null)[] }[];
marketToLiquidityCheckMapping = markets.reduce(
(acc, curr) => {
acc[curr.id] = curr.odds.some((odd: number | null) => (odd ?? 0) > 0);
return acc;
Expand Down

0 comments on commit 6651b6e

Please sign in to comment.