@czf0613/http_client 0.0.1 → 0.0.2
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 +171 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1 +1,171 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @czf0613/http_client
|
|
2
|
+
|
|
3
|
+
A simple HTTP client library built with fetch API, supporting a modified SSE (Server-Sent Events) protocol for browser environments.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- **Runtime**: ES2018+ compatible browsers
|
|
8
|
+
- **Module System**: ES Module only (not compatible with CommonJS)
|
|
9
|
+
- **Environment**: Browser only (Chrome, Firefox, Safari)
|
|
10
|
+
- **Browser Support**: Works well on all major modern browsers
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Install the package using your preferred package manager:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# npm
|
|
18
|
+
npm install @czf0613/http_client
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add @czf0613/http_client
|
|
22
|
+
|
|
23
|
+
# yarn
|
|
24
|
+
yarn add @czf0613/http_client
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### `joinUrlWithParams`
|
|
30
|
+
|
|
31
|
+
Concatenates a URL with query parameters, automatically handling URL encoding.
|
|
32
|
+
|
|
33
|
+
**Parameters:**
|
|
34
|
+
- `url` (string): The base URL without any query parameters
|
|
35
|
+
- `queryParams` (Record<string, string | number | boolean>): Query parameters as key-value pairs
|
|
36
|
+
|
|
37
|
+
**Returns:** string - The complete URL with query parameters
|
|
38
|
+
|
|
39
|
+
**Example:**
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
import { joinUrlWithParams } from '@czf0613/http_client';
|
|
43
|
+
|
|
44
|
+
const url = joinUrlWithParams('https://api.example.com/users', {
|
|
45
|
+
page: 1,
|
|
46
|
+
limit: 10,
|
|
47
|
+
search: 'hello world'
|
|
48
|
+
});
|
|
49
|
+
// Result: 'https://api.example.com/users?page=1&limit=10&search=hello%20world'
|
|
50
|
+
|
|
51
|
+
// Automatic URL encoding for special characters
|
|
52
|
+
const url2 = joinUrlWithParams('https://api.example.com/search', {
|
|
53
|
+
keyword: '你好世界'
|
|
54
|
+
});
|
|
55
|
+
// Result: 'https://api.example.com/search?keyword=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `makeHttpRequest`
|
|
59
|
+
|
|
60
|
+
Makes an HTTP request with default configuration using the Fetch API.
|
|
61
|
+
|
|
62
|
+
**Parameters:**
|
|
63
|
+
- `url` (string): The request URL (without query parameters)
|
|
64
|
+
- `method` (HttpMethod): HTTP method ('GET', 'POST', 'PUT', 'DELETE', 'HEAD'). Default: 'GET'
|
|
65
|
+
- `queryParams` (Record<string, string | number> | null): Query parameters to be appended to the URL. Default: null
|
|
66
|
+
- `customHeaders` (Record<string, string> | null): Custom headers (Content-Type is handled automatically). **Important: Do NOT include Content-Type in customHeaders as it will interfere with the automatic Content-Type generation.** Default: null
|
|
67
|
+
- `body` (any | null): Request body for POST/PUT requests. Default: null
|
|
68
|
+
- String or number: sent as `text/plain`
|
|
69
|
+
- Object: sent as `application/json`
|
|
70
|
+
- FormData: sent as `multipart/form-data`
|
|
71
|
+
- `timeoutMs` (number): Timeout in milliseconds. Default: 5000
|
|
72
|
+
|
|
73
|
+
**Returns:** Promise<Response> - Fetch API Response object
|
|
74
|
+
|
|
75
|
+
**Note:** This function does not handle exceptions by default. You should wrap it in a try-catch block.
|
|
76
|
+
|
|
77
|
+
**Example:**
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
import { makeHttpRequest } from '@czf0613/http_client';
|
|
81
|
+
|
|
82
|
+
// GET request with query parameters
|
|
83
|
+
const response = await makeHttpRequest(
|
|
84
|
+
'https://api.example.com/users',
|
|
85
|
+
'GET',
|
|
86
|
+
{ page: 1, limit: 10 }
|
|
87
|
+
);
|
|
88
|
+
const data = await response.json();
|
|
89
|
+
|
|
90
|
+
// POST request with JSON body
|
|
91
|
+
const response = await makeHttpRequest(
|
|
92
|
+
'https://api.example.com/users',
|
|
93
|
+
'POST',
|
|
94
|
+
null,
|
|
95
|
+
null,
|
|
96
|
+
{ name: 'John', age: 30 }
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// POST request with FormData
|
|
100
|
+
const formData = new FormData();
|
|
101
|
+
formData.append('file', fileInput.files[0]);
|
|
102
|
+
const response = await makeHttpRequest(
|
|
103
|
+
'https://api.example.com/upload',
|
|
104
|
+
'POST',
|
|
105
|
+
null,
|
|
106
|
+
null,
|
|
107
|
+
formData
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Custom timeout
|
|
111
|
+
const response = await makeHttpRequest(
|
|
112
|
+
'https://api.example.com/slow-endpoint',
|
|
113
|
+
'GET',
|
|
114
|
+
null,
|
|
115
|
+
null,
|
|
116
|
+
null,
|
|
117
|
+
10000 // 10 seconds timeout
|
|
118
|
+
);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `makeSSERequest`
|
|
122
|
+
|
|
123
|
+
Makes an SSE (Server-Sent Events) request and returns an async generator that yields streaming responses. This is an enhanced version of the browser's EventSource with additional features, but it's not fully compatible with the standard SSE protocol.
|
|
124
|
+
|
|
125
|
+
**Parameters:**
|
|
126
|
+
- `url` (string): The request URL (without query parameters)
|
|
127
|
+
- `method` (HttpMethod): HTTP method ('GET', 'POST', 'PUT', 'DELETE', 'HEAD'). Default: 'GET'
|
|
128
|
+
- `queryParams` (Record<string, string | number> | null): Query parameters to be appended to the URL. Default: null
|
|
129
|
+
- `customHeaders` (Record<string, string> | null): Custom headers. **Important: Do NOT include Content-Type in customHeaders.** Default: null
|
|
130
|
+
- `body` (any | null): Request body for POST/PUT requests. Default: null
|
|
131
|
+
|
|
132
|
+
**Note:** For detailed parameter descriptions, see [`makeHttpRequest`](#makehttprequest) above. This function only handles responses in the format `data: xxx\n\n`. It does not throw exceptions by default; success/failure is indicated in the generator's return value.
|
|
133
|
+
|
|
134
|
+
**Returns:** AsyncGenerator<string, boolean, undefined> - An async generator that yields each message as a string, and returns a boolean indicating success (true) or failure (false)
|
|
135
|
+
|
|
136
|
+
**Example:**
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
import { makeSSERequest } from '@czf0613/http_client';
|
|
140
|
+
|
|
141
|
+
async function streamChat() {
|
|
142
|
+
const generator = makeSSERequest(
|
|
143
|
+
'https://api.example.com/chat',
|
|
144
|
+
'POST',
|
|
145
|
+
null,
|
|
146
|
+
null,
|
|
147
|
+
{ message: 'Hello' }
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// Manually iterate to receive chunks
|
|
151
|
+
while(true) {
|
|
152
|
+
const { done, value } = await generator.next();
|
|
153
|
+
|
|
154
|
+
if (done) {
|
|
155
|
+
// Check if the stream completed successfully
|
|
156
|
+
if (!value) {
|
|
157
|
+
console.error('SSE stream failed');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Get the message value
|
|
164
|
+
console.log('Received:', value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@czf0613/http_client",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.2",
|
|
5
5
|
"description": "Simple http client with fetch, supporting modified SSE protocol",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"http client",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc",
|
|
23
|
-
"prepack": "
|
|
23
|
+
"prepack": "npm run build",
|
|
24
24
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
25
25
|
},
|
|
26
26
|
"repository": {
|