@diniz/webcomponents 1.0.4 → 1.0.5
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/README.md +99 -0
- package/package.json +1 -1
- package/dist/404.html +0 -14
package/README.md
CHANGED
|
@@ -735,6 +735,105 @@ store.subscribe(state => {
|
|
|
735
735
|
});
|
|
736
736
|
```
|
|
737
737
|
|
|
738
|
+
### HTTP Client
|
|
739
|
+
|
|
740
|
+
Lightweight HTTP client with interceptor support for API requests:
|
|
741
|
+
|
|
742
|
+
```typescript
|
|
743
|
+
import { http } from '@diniz/webcomponents';
|
|
744
|
+
|
|
745
|
+
// Set base URL for all requests
|
|
746
|
+
http.setBaseURL('https://api.example.com');
|
|
747
|
+
|
|
748
|
+
// Set default headers
|
|
749
|
+
http.setDefaultHeaders({ 'Authorization': 'Bearer token' });
|
|
750
|
+
|
|
751
|
+
// Make requests
|
|
752
|
+
const users = await http.get<User[]>('/users');
|
|
753
|
+
const newUser = await http.post<User>('/users', { name: 'Alice' });
|
|
754
|
+
await http.put(`/users/${id}`, updatedData);
|
|
755
|
+
await http.delete(`/users/${id}`);
|
|
756
|
+
```
|
|
757
|
+
|
|
758
|
+
**Request Interceptors:**
|
|
759
|
+
|
|
760
|
+
```typescript
|
|
761
|
+
// Add auth token to every request
|
|
762
|
+
http.interceptors.request.use((config) => {
|
|
763
|
+
const token = localStorage.getItem('auth_token');
|
|
764
|
+
if (token) {
|
|
765
|
+
config.headers = config.headers || {};
|
|
766
|
+
config.headers['Authorization'] = `Bearer ${token}`;
|
|
767
|
+
}
|
|
768
|
+
return config;
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
// Handle request errors
|
|
772
|
+
http.interceptors.request.use(
|
|
773
|
+
(config) => config,
|
|
774
|
+
(error) => {
|
|
775
|
+
console.error('Request failed:', error);
|
|
776
|
+
throw error;
|
|
777
|
+
}
|
|
778
|
+
);
|
|
779
|
+
```
|
|
780
|
+
|
|
781
|
+
**Response Interceptors:**
|
|
782
|
+
|
|
783
|
+
```typescript
|
|
784
|
+
// Transform response data
|
|
785
|
+
http.interceptors.response.use((response) => {
|
|
786
|
+
// Unwrap API response if it's nested
|
|
787
|
+
if (response.data?.result) {
|
|
788
|
+
response.data = response.data.result;
|
|
789
|
+
}
|
|
790
|
+
return response;
|
|
791
|
+
});
|
|
792
|
+
|
|
793
|
+
// Handle errors globally
|
|
794
|
+
http.interceptors.response.use(
|
|
795
|
+
(response) => response,
|
|
796
|
+
(error) => {
|
|
797
|
+
if (error.response?.status === 401) {
|
|
798
|
+
// Handle unauthorized
|
|
799
|
+
localStorage.removeItem('auth_token');
|
|
800
|
+
window.location.href = '/login';
|
|
801
|
+
}
|
|
802
|
+
throw error;
|
|
803
|
+
}
|
|
804
|
+
);
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
**Methods:**
|
|
808
|
+
|
|
809
|
+
- `get<T>(url, config?)` - GET request
|
|
810
|
+
- `post<T>(url, data?, config?)` - POST request
|
|
811
|
+
- `put<T>(url, data?, config?)` - PUT request
|
|
812
|
+
- `patch<T>(url, data?, config?)` - PATCH request
|
|
813
|
+
- `delete<T>(url, config?)` - DELETE request
|
|
814
|
+
- `head<T>(url, config?)` - HEAD request
|
|
815
|
+
|
|
816
|
+
**Configuration:**
|
|
817
|
+
|
|
818
|
+
```typescript
|
|
819
|
+
interface RequestConfig {
|
|
820
|
+
method?: string;
|
|
821
|
+
headers?: Record<string, string>;
|
|
822
|
+
body?: string | FormData | null;
|
|
823
|
+
timeout?: number; // Default: 30000ms
|
|
824
|
+
}
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
**Features:**
|
|
828
|
+
|
|
829
|
+
- ✅ Request/response interceptors with error handling
|
|
830
|
+
- ✅ Automatic JSON serialization/deserialization
|
|
831
|
+
- ✅ Timeout support (default 30s)
|
|
832
|
+
- ✅ Global headers and base URL configuration
|
|
833
|
+
- ✅ FormData support for file uploads
|
|
834
|
+
- ✅ TypeScript generics for type-safe responses
|
|
835
|
+
- ✅ Automatic error messages with status codes
|
|
836
|
+
|
|
738
837
|
---
|
|
739
838
|
|
|
740
839
|
## Theming
|
package/package.json
CHANGED
package/dist/404.html
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
<!DOCTYPE html>
|
|
3
|
-
<html lang="en">
|
|
4
|
-
<head>
|
|
5
|
-
<meta charset="UTF-8" />
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
-
<title>Web Components SPA Starter</title>
|
|
8
|
-
<script type="module" crossorigin src="/webcomponents/assets/index-DJpTIOwN.js"></script>
|
|
9
|
-
<link rel="stylesheet" crossorigin href="/webcomponents/assets/index-uHZenGtA.css">
|
|
10
|
-
</head>
|
|
11
|
-
<body>
|
|
12
|
-
<div id="app"></div>
|
|
13
|
-
</body>
|
|
14
|
-
</html>
|