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

Commit 10314d3

Browse files
authored
Deploy to Vercel with mocks (#17)
* Add env vars * Move txs.json to mocks dir * Add msw dep * Add API mock handler * Add API mock worker * Add generated mockServiceWorker * Add MockApiProvider * Remove comments from sample envfile * Fix render in provider * Add envvar to playwright config
1 parent ba0bebe commit 10314d3

13 files changed

+993
-11
lines changed

.env.sample

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NEXT_PUBLIC_API_URL=http://localhost:4000/api/v1
2+
NEXT_PUBLIC_MOCK_API=FALSE

hooks/api.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"use client";
2-
1+
import { env } from "@/lib/env";
32
import { Tx, TxSchema } from "@/types/txs";
43
import {
54
keepPreviousData,
@@ -8,9 +7,6 @@ import {
87
} from "@tanstack/react-query";
98
import { z } from "zod";
109

11-
//TODO - use an envfile
12-
const API_URL = "http://localhost:4000/api/v1";
13-
1410
export const useTxs = (operationName?: string, tags?: Map<string, string>) => {
1511
const tagsObj = tags ? Object.fromEntries(tags.entries()) : undefined;
1612

@@ -25,7 +21,9 @@ export const useTxs = (operationName?: string, tags?: Map<string, string>) => {
2521
return useQuery<Readonly<Array<Tx>>>({
2622
queryKey: ["txs", { operationName, tags: tagsObj }],
2723
queryFn: () =>
28-
fetch(`${API_URL}/txs${params.size ? `?${params.toString()}` : ""}`)
24+
fetch(
25+
`${env.NEXT_PUBLIC_API_URL}/txs${params.size ? `?${params.toString()}` : ""}`,
26+
)
2927
.then((res) => res.json())
3028
.then(({ txs }) => z.array(TxSchema).parse(txs)),
3129
placeholderData: keepPreviousData,
@@ -38,7 +36,7 @@ export const useTx = (id: string) => {
3836
return useQuery<Readonly<Array<Tx>>>({
3937
queryKey: ["tx", id],
4038
queryFn: () =>
41-
fetch(`${API_URL}/txs?traceID=${id}`)
39+
fetch(`${env.NEXT_PUBLIC_API_URL}/txs?traceID=${id}`)
4240
.then((res) => res.json())
4341
.then(({ txs }) => z.array(TxSchema).parse(txs)),
4442
placeholderData: () => {

lib/env.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from "zod";
2+
3+
//NOTE - Webpack disallows reading process.env as a whole, so we need to parse individual variables
4+
export const env = {
5+
NEXT_PUBLIC_API_URL: z.string().url().parse(process.env.NEXT_PUBLIC_API_URL),
6+
NEXT_PUBLIC_MOCK_API: z
7+
.optional(z.enum(["TRUE", "FALSE"]))
8+
.parse(process.env.NEXT_PUBLIC_MOCK_API),
9+
};

0 commit comments

Comments
 (0)