Skip to content

Commit

Permalink
validate env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
alan2207 committed May 3, 2024
1 parent d2373d7 commit d99d6d9
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VITE_APP_API_URL=https://api.bulletproofapp.com
VITE_APP_API_MOCKING=true
VITE_APP_ENABLE_API_MOCKING=true
37 changes: 37 additions & 0 deletions src/config/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as z from 'zod';

const createEnv = () => {
const EnvSchema = z.object({
API_URL: z.string(),
ENABLE_API_MOCKING: z
.string()
.refine((s) => s === 'true' || s === 'false')
.transform((s) => s === 'true')
.optional(),
});

const envVars = Object.entries(import.meta.env).reduce<Record<string, string>>((acc, curr) => {
const [key, value] = curr;
if (key.startsWith('VITE_APP_')) {
acc[key.replace('VITE_APP_', '')] = value;
}
return acc;
}, {});

const parsedEnv = EnvSchema.safeParse(envVars);

if (!parsedEnv.success) {
throw new Error(
`Invalid env provided.
The following variables are missing or invalid:
${Object.entries(parsedEnv.error.flatten().fieldErrors)
.map(([k, v]) => `- ${k}: ${v}`)
.join('\n')}
`
);
}

return parsedEnv.data;
};

export const env = createEnv();
2 changes: 0 additions & 2 deletions src/config/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/axios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Axios, { InternalAxiosRequestConfig } from 'axios';

import { API_URL } from '@/config';
import { env } from '@/config/env';
import { useNotificationStore } from '@/stores/notifications';
import storage from '@/utils/storage';

Expand All @@ -18,7 +18,7 @@ function authRequestInterceptor(config: InternalAxiosRequestConfig) {
}

export const axios = Axios.create({
baseURL: API_URL,
baseURL: env.API_URL,
});

axios.interceptors.request.use(authRequestInterceptor);
Expand Down
8 changes: 4 additions & 4 deletions src/test/server/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpResponse, http } from 'msw';
import { nanoid } from 'nanoid';

import { API_URL } from '@/config';
import { env } from '@/config/env';

import { db, persistDb } from '../db';
import { authenticate, hash, requireAuth } from '../utils';
Expand All @@ -21,7 +21,7 @@ type LoginBody = {
};

export const authHandlers = [
http.post(`${API_URL}/auth/register`, async ({ request }) => {
http.post(`${env.API_URL}/auth/register`, async ({ request }) => {
try {
const userObject = (await request.json()) as RegisterBody;

Expand Down Expand Up @@ -84,7 +84,7 @@ export const authHandlers = [
}
}),

http.post(`${API_URL}/auth/login`, async ({ request }) => {
http.post(`${env.API_URL}/auth/login`, async ({ request }) => {
try {
const credentials = (await request.json()) as LoginBody;
const result = authenticate(credentials);
Expand All @@ -94,7 +94,7 @@ export const authHandlers = [
}
}),

http.get(`${API_URL}/auth/me`, ({ request }) => {
http.get(`${env.API_URL}/auth/me`, ({ request }) => {
try {
const user = requireAuth(request);
return HttpResponse.json(user);
Expand Down
8 changes: 4 additions & 4 deletions src/test/server/handlers/comments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpResponse, http } from 'msw';
import { nanoid } from 'nanoid';

import { API_URL } from '@/config';
import { env } from '@/config/env';

import { db, persistDb } from '../db';
import { requireAuth, sanitizeUser } from '../utils';
Expand All @@ -12,7 +12,7 @@ type CreateCommentBody = {
};

export const commentsHandlers = [
http.get(`${API_URL}/comments`, ({ request }) => {
http.get(`${env.API_URL}/comments`, ({ request }) => {
try {
requireAuth(request);
const url = new URL(request.url);
Expand Down Expand Up @@ -44,7 +44,7 @@ export const commentsHandlers = [
}
}),

http.post(`${API_URL}/comments`, async ({ request }) => {
http.post(`${env.API_URL}/comments`, async ({ request }) => {
try {
const user = requireAuth(request);
const data = (await request.json()) as CreateCommentBody;
Expand All @@ -61,7 +61,7 @@ export const commentsHandlers = [
}
}),

http.delete(`${API_URL}/comments/:commentId`, ({ request, params }) => {
http.delete(`${env.API_URL}/comments/:commentId`, ({ request, params }) => {
try {
const user = requireAuth(request);
const commentId = params.commentId as string;
Expand Down
12 changes: 6 additions & 6 deletions src/test/server/handlers/discussions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpResponse, http } from 'msw';
import { nanoid } from 'nanoid';

import { API_URL } from '@/config';
import { env } from '@/config/env';

import { db, persistDb } from '../db';
import { requireAuth, requireAdmin, sanitizeUser } from '../utils';
Expand All @@ -12,7 +12,7 @@ type DiscussionBody = {
};

export const discussionsHandlers = [
http.get(`${API_URL}/discussions`, async ({ request }) => {
http.get(`${env.API_URL}/discussions`, async ({ request }) => {
try {
const user = requireAuth(request);
const result = db.discussion
Expand Down Expand Up @@ -42,7 +42,7 @@ export const discussionsHandlers = [
}
}),

http.get(`${API_URL}/discussions/:discussionId`, async ({ request, params }) => {
http.get(`${env.API_URL}/discussions/:discussionId`, async ({ request, params }) => {
try {
const user = requireAuth(request);
const discussionId = params.discussionId as string;
Expand Down Expand Up @@ -82,7 +82,7 @@ export const discussionsHandlers = [
}
}),

http.post(`${API_URL}/discussions`, async ({ request }) => {
http.post(`${env.API_URL}/discussions`, async ({ request }) => {
try {
const user = requireAuth(request);
const data = (await request.json()) as DiscussionBody;
Expand All @@ -101,7 +101,7 @@ export const discussionsHandlers = [
}
}),

http.patch(`${API_URL}/discussions/:discussionId`, async ({ request, params }) => {
http.patch(`${env.API_URL}/discussions/:discussionId`, async ({ request, params }) => {
try {
const user = requireAuth(request);
const data = (await request.json()) as DiscussionBody;
Expand All @@ -125,7 +125,7 @@ export const discussionsHandlers = [
}
}),

http.delete(`${API_URL}/discussions/:discussionId`, async ({ request, params }) => {
http.delete(`${env.API_URL}/discussions/:discussionId`, async ({ request, params }) => {
try {
const user = requireAuth(request);
const discussionId = params.discussionId as string;
Expand Down
4 changes: 2 additions & 2 deletions src/test/server/handlers/teams.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { HttpResponse, http } from 'msw';

import { API_URL } from '@/config';
import { env } from '@/config/env';

import { db } from '../db';

export const teamsHandlers = [
http.get(`${API_URL}/teams`, () => {
http.get(`${env.API_URL}/teams`, () => {
try {
const result = db.team.getAll();
return HttpResponse.json(result);
Expand Down
8 changes: 4 additions & 4 deletions src/test/server/handlers/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse, http } from 'msw';

import { API_URL } from '@/config';
import { env } from '@/config/env';

import { db, persistDb } from '../db';
import { requireAuth, requireAdmin, sanitizeUser } from '../utils';
Expand All @@ -13,7 +13,7 @@ type ProfileBody = {
};

export const usersHandlers = [
http.get(`${API_URL}/users`, async ({ request }) => {
http.get(`${env.API_URL}/users`, async ({ request }) => {
try {
const user = requireAuth(request);
const result = db.user
Expand All @@ -32,7 +32,7 @@ export const usersHandlers = [
}
}),

http.patch(`${API_URL}/users/profile`, async ({ request }) => {
http.patch(`${env.API_URL}/users/profile`, async ({ request }) => {
try {
const user = requireAuth(request);
const data = (await request.json()) as ProfileBody;
Expand All @@ -51,7 +51,7 @@ export const usersHandlers = [
}
}),

http.delete(`${API_URL}/users/:userId`, async ({ request, params }) => {
http.delete(`${env.API_URL}/users/:userId`, async ({ request, params }) => {
try {
const user = requireAuth(request);
const userId = params.userId as string;
Expand Down
4 changes: 3 additions & 1 deletion src/test/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { env } from '@/config/env';

export const enableMocking = async () => {
if (import.meta.env.VITE_APP_API_MOCKING === 'true') {
if (env.ENABLE_API_MOCKING) {
const { worker } = await import('./browser');
return worker.start();
}
Expand Down

0 comments on commit d99d6d9

Please sign in to comment.