@gofreego/tsutils 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -511,6 +511,15 @@ interface HttpResponse<T = any> {
511
511
  statusText: string;
512
512
  headers: Headers;
513
513
  }
514
+ interface ErrorData {
515
+ code: number;
516
+ message: string;
517
+ }
518
+ interface HttpError extends Error {
519
+ status?: number;
520
+ statusText?: string;
521
+ data?: ErrorData;
522
+ }
514
523
 
515
524
  declare class HttpClient {
516
525
  private baseURL;
@@ -519,12 +528,16 @@ declare class HttpClient {
519
528
  constructor(config?: HttpClientConfig);
520
529
  private buildURL;
521
530
  private request;
531
+ setDefaultHeader(key: string, value: string): void;
532
+ removeDefaultHeader(key: string): void;
533
+ getDefaultHeaders(): Record<string, string>;
522
534
  get<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
523
535
  post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
524
536
  put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
525
537
  patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
526
538
  delete<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
527
539
  }
540
+ declare function extractErrorMessage(error: any): string;
528
541
 
529
542
  /**
530
543
  * Merges class names together
@@ -651,4 +664,4 @@ declare class PermissionManager {
651
664
  static hasPermission(permission: string): boolean;
652
665
  }
653
666
 
654
- export { type AuthContextType, AuthProvider, type AuthProviderProps, ConfirmDialog, type ConfirmDialogProps, HttpClient, type HttpClientConfig, type HttpResponse, LocalStorage, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, PermissionManager, ReadmeViewer, type RequestConfig, type ResolvedThemeMode, SidebarLayout, type SidebarLayoutProps, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, borderRadius, cn, darkTheme, debounce, elevation, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useAuth, useNotification, useTheme, zIndex };
667
+ export { type AuthContextType, AuthProvider, type AuthProviderProps, ConfirmDialog, type ConfirmDialogProps, type ErrorData, HttpClient, type HttpClientConfig, type HttpError, type HttpResponse, LocalStorage, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, PermissionManager, ReadmeViewer, type RequestConfig, type ResolvedThemeMode, SidebarLayout, type SidebarLayoutProps, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, borderRadius, cn, darkTheme, debounce, elevation, extractErrorMessage, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useAuth, useNotification, useTheme, zIndex };
package/dist/index.d.ts CHANGED
@@ -511,6 +511,15 @@ interface HttpResponse<T = any> {
511
511
  statusText: string;
512
512
  headers: Headers;
513
513
  }
514
+ interface ErrorData {
515
+ code: number;
516
+ message: string;
517
+ }
518
+ interface HttpError extends Error {
519
+ status?: number;
520
+ statusText?: string;
521
+ data?: ErrorData;
522
+ }
514
523
 
515
524
  declare class HttpClient {
516
525
  private baseURL;
@@ -519,12 +528,16 @@ declare class HttpClient {
519
528
  constructor(config?: HttpClientConfig);
520
529
  private buildURL;
521
530
  private request;
531
+ setDefaultHeader(key: string, value: string): void;
532
+ removeDefaultHeader(key: string): void;
533
+ getDefaultHeaders(): Record<string, string>;
522
534
  get<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
523
535
  post<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
524
536
  put<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
525
537
  patch<T = any>(url: string, data?: any, config?: RequestConfig): Promise<HttpResponse<T>>;
526
538
  delete<T = any>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>;
527
539
  }
540
+ declare function extractErrorMessage(error: any): string;
528
541
 
529
542
  /**
530
543
  * Merges class names together
@@ -651,4 +664,4 @@ declare class PermissionManager {
651
664
  static hasPermission(permission: string): boolean;
652
665
  }
653
666
 
654
- export { type AuthContextType, AuthProvider, type AuthProviderProps, ConfirmDialog, type ConfirmDialogProps, HttpClient, type HttpClientConfig, type HttpResponse, LocalStorage, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, PermissionManager, ReadmeViewer, type RequestConfig, type ResolvedThemeMode, SidebarLayout, type SidebarLayoutProps, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, borderRadius, cn, darkTheme, debounce, elevation, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useAuth, useNotification, useTheme, zIndex };
667
+ export { type AuthContextType, AuthProvider, type AuthProviderProps, ConfirmDialog, type ConfirmDialogProps, type ErrorData, HttpClient, type HttpClientConfig, type HttpError, type HttpResponse, LocalStorage, type MenuItem, type Notification, type NotificationContextType, NotificationProvider, type NotificationProviderProps, PermissionManager, ReadmeViewer, type RequestConfig, type ResolvedThemeMode, SidebarLayout, type SidebarLayoutProps, type Theme, type ThemeContextValue, type ThemeMode, ThemeProvider, ThemeToggle, borderRadius, cn, darkTheme, debounce, elevation, extractErrorMessage, fontSize, fontWeight, formatDate, getHighlighter, lightTheme, lineHeight, spacing, throttle, tokens, transition, useAuth, useNotification, useTheme, zIndex };
package/dist/index.js CHANGED
@@ -1187,7 +1187,7 @@ var HttpClient = class {
1187
1187
  try {
1188
1188
  error.data = await response.json();
1189
1189
  } catch {
1190
- error.data = await response.text();
1190
+ error.data = { code: response.status, message: await response.text() };
1191
1191
  }
1192
1192
  throw error;
1193
1193
  }
@@ -1206,6 +1206,15 @@ var HttpClient = class {
1206
1206
  throw error;
1207
1207
  }
1208
1208
  }
1209
+ setDefaultHeader(key, value) {
1210
+ this.defaultHeaders[key] = value;
1211
+ }
1212
+ removeDefaultHeader(key) {
1213
+ delete this.defaultHeaders[key];
1214
+ }
1215
+ getDefaultHeaders() {
1216
+ return { ...this.defaultHeaders };
1217
+ }
1209
1218
  get(url, config) {
1210
1219
  return this.request(url, { ...config, method: "GET" });
1211
1220
  }
@@ -1222,6 +1231,16 @@ var HttpClient = class {
1222
1231
  return this.request(url, { ...config, method: "DELETE" });
1223
1232
  }
1224
1233
  };
1234
+ function extractErrorMessage(error) {
1235
+ if (error instanceof Error) {
1236
+ const httpError = error;
1237
+ if (httpError.data) {
1238
+ return httpError.data.message;
1239
+ }
1240
+ return error.message;
1241
+ }
1242
+ return "An unexpected error occurred";
1243
+ }
1225
1244
 
1226
1245
  // src/utils/cn.ts
1227
1246
  function cn(...classes) {
@@ -1433,6 +1452,7 @@ exports.cn = cn;
1433
1452
  exports.darkTheme = darkTheme;
1434
1453
  exports.debounce = debounce;
1435
1454
  exports.elevation = elevation;
1455
+ exports.extractErrorMessage = extractErrorMessage;
1436
1456
  exports.fontSize = fontSize;
1437
1457
  exports.fontWeight = fontWeight;
1438
1458
  exports.formatDate = formatDate;