Initial commit
This commit is contained in:
42
src/lib/axios.ts
Normal file
42
src/lib/axios.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import axios from "axios";
|
||||
|
||||
export const api = axios.create({
|
||||
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || "/api",
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
// Request interceptor to add auth token
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
// Example: Add auth token from localStorage (replace with your auth logic)
|
||||
const token = localStorage.getItem("authToken");
|
||||
if (token) {
|
||||
config.headers["Authorization"] = `Bearer token`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Response interceptor for global error handling
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
// Example: Handle 401 Unauthorized globally
|
||||
if (error.response?.status === 401) {
|
||||
// Optionally, you can redirect to login page or show a toast
|
||||
console.error("Unauthorized - redirecting to login");
|
||||
if (typeof window !== "undefined") {
|
||||
localStorage.removeItem("authToken"); // Clear token on unauthorized
|
||||
window.location.href = "/login";
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
26
src/lib/env.ts
Normal file
26
src/lib/env.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { z } from "zod";
|
||||
|
||||
// Client-side environment variables (must be prefixed with NEXT_PUBLIC_)
|
||||
export const clientEnvSchema = z.object({
|
||||
NEXT_PUBLIC_APP_NAME: z.string().default("Demo App"),
|
||||
NEXT_PUBLIC_APP_URL: z.string().url().default("http://localhost:3000"),
|
||||
NEXT_PUBLIC_API_BASE_URL: z.string().url().default("http://localhost:3000/api"),
|
||||
});
|
||||
|
||||
// Server-side environment variables (not prefixed)
|
||||
export const serverEnvSchema = z.object({
|
||||
NODE_ENV: z
|
||||
.enum(["development", "production", "test"])
|
||||
.default("development"),
|
||||
});
|
||||
|
||||
// Only parse server variables on the server, and client variables on the client
|
||||
export const serverEnv =
|
||||
typeof window === "undefined" ?
|
||||
serverEnvSchema.parse({NODE_ENV: process.env.NODE_ENV}) :
|
||||
({} as z.infer<typeof serverEnvSchema>);
|
||||
|
||||
export const clientEnv =
|
||||
typeof window !== "undefined" ?
|
||||
clientEnvSchema.parse({NODE_ENV: process.env.NODE_ENV}) :
|
||||
({} as z.infer<typeof clientEnvSchema>);
|
||||
34
src/lib/query-client.ts
Normal file
34
src/lib/query-client.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
QueryClient,
|
||||
defaultShouldDehydrateQuery,
|
||||
isServer,
|
||||
} from '@tanstack/react-query';
|
||||
|
||||
function makeQueryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
retry: 1, // Retry failed requests once
|
||||
refetchOnWindowFocus: false, // Disable refetch on window focus
|
||||
},
|
||||
dehydrate: {
|
||||
shouldDehydrateQuery: (query) =>
|
||||
defaultShouldDehydrateQuery(query) || query.state.status === 'pending'
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let browserQueryClient: QueryClient | undefined;
|
||||
|
||||
export function getQueryClient() {
|
||||
if (isServer) {
|
||||
return makeQueryClient();
|
||||
}
|
||||
// On the client, reuse the same QueryClient instance
|
||||
if (!browserQueryClient) {
|
||||
browserQueryClient = makeQueryClient();
|
||||
}
|
||||
return browserQueryClient;
|
||||
}
|
||||
6
src/lib/utils.ts
Normal file
6
src/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
21
src/lib/validations.ts
Normal file
21
src/lib/validations.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { z } from "zod";
|
||||
|
||||
// Example: login form validation schema
|
||||
export const loginSchema = z.object({
|
||||
email: z.email("Invalid email address"),
|
||||
password: z.string().min(6, "Password must be at least 6 characters"),
|
||||
});
|
||||
|
||||
export type LoginFormValues = z.infer<typeof loginSchema>;
|
||||
|
||||
// Example: Contact form validation schema
|
||||
export const contactSchema = z.object({
|
||||
name: z.string().min(2, "Name must be at least 2 characters"),
|
||||
email: z.email("Invalid email address"),
|
||||
message: z
|
||||
.string()
|
||||
.min(10, "Message must be at least 10 characters")
|
||||
.max(500, "Message must be less than 500 characters"),
|
||||
});
|
||||
|
||||
export type ContactFormValues = z.infer<typeof contactSchema>;
|
||||
Reference in New Issue
Block a user