Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Deploy to Vercel with mocks #17

Merged
merged 10 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NEXT_PUBLIC_API_URL=http://localhost:4000/api/v1
NEXT_PUBLIC_MOCK_API=FALSE
12 changes: 5 additions & 7 deletions hooks/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use client";

import { env } from "@/lib/env";
import { Tx, TxSchema } from "@/types/txs";
import {
keepPreviousData,
Expand All @@ -8,9 +7,6 @@ import {
} from "@tanstack/react-query";
import { z } from "zod";

//TODO - use an envfile
const API_URL = "http://localhost:4000/api/v1";

export const useTxs = (operationName?: string, tags?: Map<string, string>) => {
const tagsObj = tags ? Object.fromEntries(tags.entries()) : undefined;

Expand All @@ -25,7 +21,9 @@ export const useTxs = (operationName?: string, tags?: Map<string, string>) => {
return useQuery<Readonly<Array<Tx>>>({
queryKey: ["txs", { operationName, tags: tagsObj }],
queryFn: () =>
fetch(`${API_URL}/txs${params.size ? `?${params.toString()}` : ""}`)
fetch(
`${env.NEXT_PUBLIC_API_URL}/txs${params.size ? `?${params.toString()}` : ""}`,
)
.then((res) => res.json())
.then(({ txs }) => z.array(TxSchema).parse(txs)),
placeholderData: keepPreviousData,
Expand All @@ -38,7 +36,7 @@ export const useTx = (id: string) => {
return useQuery<Readonly<Array<Tx>>>({
queryKey: ["tx", id],
queryFn: () =>
fetch(`${API_URL}/txs?traceID=${id}`)
fetch(`${env.NEXT_PUBLIC_API_URL}/txs?traceID=${id}`)
.then((res) => res.json())
.then(({ txs }) => z.array(TxSchema).parse(txs)),
placeholderData: () => {
Expand Down
9 changes: 9 additions & 0 deletions lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

//NOTE - Webpack disallows reading process.env as a whole, so we need to parse individual variables
export const env = {
NEXT_PUBLIC_API_URL: z.string().url().parse(process.env.NEXT_PUBLIC_API_URL),
NEXT_PUBLIC_MOCK_API: z
.optional(z.enum(["TRUE", "FALSE"]))
.parse(process.env.NEXT_PUBLIC_MOCK_API),
};
Loading