1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| export interface User { id: string; name: string; email: string; createdAt: Date; }
export interface Product { id: string; name: string; price: number; category: string; inStock: boolean; }
export type ApiResponse<T> = { success: boolean; data: T; message?: string; };
export enum UserRole { Admin = 'admin', User = 'user', Guest = 'guest' }
export class ValidationUtils { static isEmail(email: string): boolean { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } static isStrongPassword(password: string): boolean { const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; return strongPasswordRegex.test(password); } static sanitizeString(input: string): string { return input.replace(/[<>]/g, ''); } }
export class DateUtils { static formatDate(date: Date, format: string = 'YYYY-MM-DD'): string { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return format .replace('YYYY', year.toString()) .replace('MM', month) .replace('DD', day); } static addDays(date: Date, days: number): Date { const result = new Date(date); result.setDate(result.getDate() + days); return result; } static diffInDays(date1: Date, date2: Date): number { const timeDiff = Math.abs(date2.getTime() - date1.getTime()); return Math.ceil(timeDiff / (1000 * 3600 * 24)); } }
export class HttpClient { private baseURL: string; private defaultHeaders: Record<string, string>; constructor(baseURL: string, defaultHeaders: Record<string, string> = {}) { this.baseURL = baseURL; this.defaultHeaders = defaultHeaders; } private async request<T>( method: string, url: string, data?: any, headers?: Record<string, string> ): Promise<ApiResponse<T>> { const fullURL = `${this.baseURL}${url}`; const requestHeaders = { ...this.defaultHeaders, ...headers }; try { const response = await fetch(fullURL, { method, headers: { 'Content-Type': 'application/json', ...requestHeaders }, body: data ? JSON.stringify(data) : undefined }); const result = await response.json(); return { success: response.ok, data: result, message: response.ok ? 'Success' : 'Request failed' }; } catch (error) { return { success: false, data: null as any, message: error instanceof Error ? error.message : 'Unknown error' }; } } async get<T>(url: string, headers?: Record<string, string>): Promise<ApiResponse<T>> { return this.request<T>('GET', url, undefined, headers); } async post<T>(url: string, data: any, headers?: Record<string, string>): Promise<ApiResponse<T>> { return this.request<T>('POST', url, data, headers); } async put<T>(url: string, data: any, headers?: Record<string, string>): Promise<ApiResponse<T>> { return this.request<T>('PUT', url, data, headers); } async delete<T>(url: string, headers?: Record<string, string>): Promise<ApiResponse<T>> { return this.request<T>('DELETE', url, undefined, headers); } }
import { User, ApiResponse } from './types'; import { HttpClient } from './http-client'; import { ValidationUtils } from './utils';
export class UserService { private httpClient: HttpClient; constructor(baseURL: string) { this.httpClient = new HttpClient(baseURL); } async createUser(userData: Omit<User, 'id' | 'createdAt'>): Promise<ApiResponse<User>> { if (!ValidationUtils.isEmail(userData.email)) { return { success: false, data: null as any, message: 'Invalid email format' }; } return this.httpClient.post<User>('/users', userData); } async getUserById(id: string): Promise<ApiResponse<User>> { return this.httpClient.get<User>(`/users/${id}`); } async updateUser(id: string, userData: Partial<User>): Promise<ApiResponse<User>> { return this.httpClient.put<User>(`/users/${id}`, userData); } async deleteUser(id: string): Promise<ApiResponse<void>> { return this.httpClient.delete<void>(`/users/${id}`); } async getAllUsers(): Promise<ApiResponse<User[]>> { return this.httpClient.get<User[]>('/users'); } }
export * from './types'; export * from './utils'; export * from './http-client'; export * from './user-service';
export { UserService as default } from './user-service';
|