superagent-lite 1.0.0
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/LICENSE +21 -0
- package/README.md +219 -0
- package/dist/index.d.ts +127 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +434 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# superagent-lite
|
|
2
|
+
|
|
3
|
+
Zero-dependency, lightweight drop-in replacement for [superagent](https://github.com/ladjs/superagent) built on native `fetch`.
|
|
4
|
+
|
|
5
|
+
Takes the best from **superagent**, **axios**, **got**, and **ky** while fixing their weaknesses.
|
|
6
|
+
|
|
7
|
+
## Why?
|
|
8
|
+
|
|
9
|
+
| Feature | superagent | axios | got | ky | **superagent-lite** |
|
|
10
|
+
|---------|------------|-------|-----|-----|---------------------|
|
|
11
|
+
| Bundle size | 19.9KB | 11KB | 48KB | 3KB | **~4KB** |
|
|
12
|
+
| Dependencies | 9 | 3 | 11 | 0 | **0** |
|
|
13
|
+
| Native fetch | ❌ | ❌ | ❌ | ✅ | **✅** |
|
|
14
|
+
| TypeScript | Partial | ✅ | ✅ | ✅ | **✅** |
|
|
15
|
+
| Chaining API | ✅ | ❌ | ❌ | ❌ | **✅** |
|
|
16
|
+
| Hooks/Interceptors | ❌ | ✅ | ✅ | ✅ | **✅** |
|
|
17
|
+
| Instance creation | ❌ | ✅ | ✅ | ✅ | **✅** |
|
|
18
|
+
| Smart retry | ❌ | ❌ | ✅ | ✅ | **✅** |
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install superagent-lite
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import request from 'superagent-lite';
|
|
30
|
+
|
|
31
|
+
// GET request
|
|
32
|
+
const res = await request.get('https://api.example.com/users');
|
|
33
|
+
console.log(res.body);
|
|
34
|
+
|
|
35
|
+
// POST with JSON body
|
|
36
|
+
await request
|
|
37
|
+
.post('https://api.example.com/users')
|
|
38
|
+
.send({ name: 'John', email: 'john@example.com' });
|
|
39
|
+
|
|
40
|
+
// Chaining (superagent-style)
|
|
41
|
+
await request
|
|
42
|
+
.get('https://api.example.com/posts')
|
|
43
|
+
.query({ page: 1, limit: 10 })
|
|
44
|
+
.set('Authorization', 'Bearer token')
|
|
45
|
+
.accept('json');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Superagent Drop-in Replacement
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
// Before (superagent)
|
|
52
|
+
import request from 'superagent';
|
|
53
|
+
|
|
54
|
+
// After (superagent-lite) - same API!
|
|
55
|
+
import request from 'superagent-lite';
|
|
56
|
+
|
|
57
|
+
request
|
|
58
|
+
.post('/api/users')
|
|
59
|
+
.set('Content-Type', 'application/json')
|
|
60
|
+
.send({ name: 'John' })
|
|
61
|
+
.then(res => console.log(res.body));
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API
|
|
65
|
+
|
|
66
|
+
### HTTP Methods
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
request.get(url)
|
|
70
|
+
request.post(url)
|
|
71
|
+
request.put(url)
|
|
72
|
+
request.patch(url)
|
|
73
|
+
request.delete(url) // or request.del(url)
|
|
74
|
+
request.head(url)
|
|
75
|
+
request.options(url)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Chaining Methods
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
.set(header, value) // Set header
|
|
82
|
+
.set({ headers }) // Set multiple headers
|
|
83
|
+
.query({ params }) // Add query parameters
|
|
84
|
+
.query('key=value') // Add query string
|
|
85
|
+
.send(data) // Set request body
|
|
86
|
+
.type('json') // Set Content-Type
|
|
87
|
+
.accept('json') // Set Accept header
|
|
88
|
+
.timeout(ms) // Set timeout
|
|
89
|
+
.timeout({ request, response })
|
|
90
|
+
.retry(count) // Enable retries
|
|
91
|
+
.retry({ limit, methods, statusCodes, delay })
|
|
92
|
+
.auth(user, pass) // Basic auth
|
|
93
|
+
.auth(token, { type: 'bearer' }) // Bearer token
|
|
94
|
+
.hook(name, fn) // Add hook
|
|
95
|
+
.abort() // Abort request
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Response Object
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
res.status // HTTP status code
|
|
102
|
+
res.statusCode // Alias for status
|
|
103
|
+
res.ok // true if status 2xx
|
|
104
|
+
res.body // Parsed response body
|
|
105
|
+
res.text // Raw response text
|
|
106
|
+
res.headers // Response headers object
|
|
107
|
+
res.type // Content-Type
|
|
108
|
+
res.get(header) // Get specific header
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Create Instance (axios-style)
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const api = request.create({
|
|
115
|
+
baseURL: 'https://api.example.com',
|
|
116
|
+
headers: {
|
|
117
|
+
'Authorization': 'Bearer token'
|
|
118
|
+
},
|
|
119
|
+
timeout: 5000,
|
|
120
|
+
retry: 2
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// All requests use the base configuration
|
|
124
|
+
await api.get('/users');
|
|
125
|
+
await api.post('/users').send({ name: 'John' });
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Hooks (got/ky-style)
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// Per-request hooks
|
|
132
|
+
await request
|
|
133
|
+
.get('/api/data')
|
|
134
|
+
.hook('beforeRequest', (req) => {
|
|
135
|
+
console.log('Sending request...');
|
|
136
|
+
})
|
|
137
|
+
.hook('afterResponse', (res) => {
|
|
138
|
+
console.log('Got response:', res.status);
|
|
139
|
+
return res;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Instance-level hooks
|
|
143
|
+
const api = request.create({
|
|
144
|
+
hooks: {
|
|
145
|
+
beforeRequest: [(req) => { /* modify request */ }],
|
|
146
|
+
afterResponse: [(res) => { /* modify response */ }],
|
|
147
|
+
beforeRetry: [(error, retryCount) => { /* log retry */ }],
|
|
148
|
+
beforeError: [(error) => { /* transform error */ }]
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Retry with Exponential Backoff
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
await request
|
|
157
|
+
.get('/api/flaky-endpoint')
|
|
158
|
+
.retry({
|
|
159
|
+
limit: 3,
|
|
160
|
+
methods: ['GET', 'PUT', 'DELETE'],
|
|
161
|
+
statusCodes: [408, 429, 500, 502, 503, 504],
|
|
162
|
+
delay: (attempt) => Math.min(1000 * 2 ** attempt, 30000)
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Error Handling
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import request, { HTTPError, TimeoutError } from 'superagent-lite';
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
await request.get('/api/data').timeout(5000);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (error instanceof HTTPError) {
|
|
175
|
+
console.log('HTTP error:', error.status);
|
|
176
|
+
console.log('Response:', error.response.body);
|
|
177
|
+
} else if (error instanceof TimeoutError) {
|
|
178
|
+
console.log('Request timed out');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## TypeScript
|
|
184
|
+
|
|
185
|
+
Full TypeScript support with exported types:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import request, {
|
|
189
|
+
Request,
|
|
190
|
+
Response,
|
|
191
|
+
HTTPError,
|
|
192
|
+
TimeoutError,
|
|
193
|
+
RequestOptions,
|
|
194
|
+
Hooks,
|
|
195
|
+
InstanceOptions
|
|
196
|
+
} from 'superagent-lite';
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Migration from superagent
|
|
200
|
+
|
|
201
|
+
Most superagent code works without changes:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
// ✅ Works
|
|
205
|
+
request.get(url).query(params).set(headers).then(...)
|
|
206
|
+
request.post(url).send(data).type('json').then(...)
|
|
207
|
+
request.put(url).auth(user, pass).send(data).then(...)
|
|
208
|
+
|
|
209
|
+
// ✅ Also works - legacy callback style
|
|
210
|
+
request.get(url).end((err, res) => { ... });
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## License
|
|
214
|
+
|
|
215
|
+
MIT
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
If you find this useful, consider [buying me a coffee](https://buymeacoffee.com/willzhangfly)!
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* superagent-lite
|
|
3
|
+
* Zero-dependency, native fetch HTTP client
|
|
4
|
+
* Drop-in superagent replacement with modern features from ky, got, and axios
|
|
5
|
+
*/
|
|
6
|
+
export interface RequestOptions {
|
|
7
|
+
method?: string;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
body?: any;
|
|
10
|
+
timeout?: number | TimeoutOptions;
|
|
11
|
+
retry?: number | RetryOptions;
|
|
12
|
+
hooks?: Hooks;
|
|
13
|
+
throwHttpErrors?: boolean;
|
|
14
|
+
parseJson?: (text: string) => any;
|
|
15
|
+
stringifyJson?: (data: any) => string;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
export interface TimeoutOptions {
|
|
19
|
+
request?: number;
|
|
20
|
+
response?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface RetryOptions {
|
|
23
|
+
limit?: number;
|
|
24
|
+
methods?: string[];
|
|
25
|
+
statusCodes?: number[];
|
|
26
|
+
delay?: (attemptCount: number) => number;
|
|
27
|
+
}
|
|
28
|
+
export interface Hooks {
|
|
29
|
+
beforeRequest?: BeforeRequestHook[];
|
|
30
|
+
afterResponse?: AfterResponseHook[];
|
|
31
|
+
beforeRetry?: BeforeRetryHook[];
|
|
32
|
+
beforeError?: BeforeErrorHook[];
|
|
33
|
+
}
|
|
34
|
+
export type BeforeRequestHook = (request: Request) => Request | void | Promise<Request | void>;
|
|
35
|
+
export type AfterResponseHook = (response: Response) => Response | void | Promise<Response | void>;
|
|
36
|
+
export type BeforeRetryHook = (error: HTTPError, retryCount: number) => void | Promise<void>;
|
|
37
|
+
export type BeforeErrorHook = (error: HTTPError) => HTTPError | Promise<HTTPError>;
|
|
38
|
+
export interface ResponseHeaders {
|
|
39
|
+
[key: string]: string;
|
|
40
|
+
}
|
|
41
|
+
export declare class Response {
|
|
42
|
+
private readonly _response;
|
|
43
|
+
readonly status: number;
|
|
44
|
+
readonly statusCode: number;
|
|
45
|
+
readonly ok: boolean;
|
|
46
|
+
readonly statusText: string;
|
|
47
|
+
readonly headers: ResponseHeaders;
|
|
48
|
+
readonly type: string;
|
|
49
|
+
readonly charset: string;
|
|
50
|
+
body: any;
|
|
51
|
+
text: string;
|
|
52
|
+
constructor(_response: globalThis.Response, text: string, body: any);
|
|
53
|
+
private extractCharset;
|
|
54
|
+
get(header: string): string | undefined;
|
|
55
|
+
}
|
|
56
|
+
export declare class HTTPError extends Error {
|
|
57
|
+
readonly response: Response;
|
|
58
|
+
readonly status: number;
|
|
59
|
+
constructor(response: Response);
|
|
60
|
+
}
|
|
61
|
+
export declare class TimeoutError extends Error {
|
|
62
|
+
constructor(message?: string);
|
|
63
|
+
}
|
|
64
|
+
export declare class Request implements PromiseLike<Response> {
|
|
65
|
+
private _url;
|
|
66
|
+
private _method;
|
|
67
|
+
private _headers;
|
|
68
|
+
private _query;
|
|
69
|
+
private _body;
|
|
70
|
+
private _timeout;
|
|
71
|
+
private _retry;
|
|
72
|
+
private _hooks;
|
|
73
|
+
private _throwHttpErrors;
|
|
74
|
+
private _parseJson;
|
|
75
|
+
private _stringifyJson;
|
|
76
|
+
private _abortController;
|
|
77
|
+
private _externalSignal;
|
|
78
|
+
constructor(method: string, url: string, options?: RequestOptions);
|
|
79
|
+
private mergeHooks;
|
|
80
|
+
set(field: string | Record<string, string>, value?: string): this;
|
|
81
|
+
query(params: Record<string, any> | string): this;
|
|
82
|
+
send(data: any): this;
|
|
83
|
+
type(contentType: string): this;
|
|
84
|
+
accept(acceptType: string): this;
|
|
85
|
+
timeout(ms: number | TimeoutOptions): this;
|
|
86
|
+
retry(count: number | RetryOptions): this;
|
|
87
|
+
auth(user: string, pass?: string, options?: {
|
|
88
|
+
type?: 'basic' | 'bearer';
|
|
89
|
+
}): this;
|
|
90
|
+
hook(name: keyof Hooks, fn: any): this;
|
|
91
|
+
abort(): this;
|
|
92
|
+
private execute;
|
|
93
|
+
private doFetch;
|
|
94
|
+
then<TResult1 = Response, TResult2 = never>(onfulfilled?: ((value: Response) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
95
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<Response | TResult>;
|
|
96
|
+
finally(onfinally?: (() => void) | null): Promise<Response>;
|
|
97
|
+
end(callback?: (err: Error | null, res?: Response) => void): void;
|
|
98
|
+
}
|
|
99
|
+
export interface InstanceOptions extends RequestOptions {
|
|
100
|
+
baseURL?: string;
|
|
101
|
+
headers?: Record<string, string>;
|
|
102
|
+
}
|
|
103
|
+
export interface RequestInstance {
|
|
104
|
+
(method: string, url: string): Request;
|
|
105
|
+
get: (url: string) => Request;
|
|
106
|
+
post: (url: string) => Request;
|
|
107
|
+
put: (url: string) => Request;
|
|
108
|
+
patch: (url: string) => Request;
|
|
109
|
+
delete: (url: string) => Request;
|
|
110
|
+
del: (url: string) => Request;
|
|
111
|
+
head: (url: string) => Request;
|
|
112
|
+
options: (url: string) => Request;
|
|
113
|
+
create: (options?: InstanceOptions) => RequestInstance;
|
|
114
|
+
defaults: InstanceOptions;
|
|
115
|
+
}
|
|
116
|
+
declare function createInstance(options?: InstanceOptions): RequestInstance;
|
|
117
|
+
declare const request: RequestInstance;
|
|
118
|
+
export default request;
|
|
119
|
+
export { request, createInstance as create };
|
|
120
|
+
export declare const get: (url: string) => Request;
|
|
121
|
+
export declare const post: (url: string) => Request;
|
|
122
|
+
export declare const put: (url: string) => Request;
|
|
123
|
+
export declare const patch: (url: string) => Request;
|
|
124
|
+
export declare const del: (url: string) => Request;
|
|
125
|
+
export declare const head: (url: string) => Request;
|
|
126
|
+
export declare const options: (url: string) => Request;
|
|
127
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,GAAG,cAAc,CAAC;IAClC,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;IAC9B,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,GAAG,CAAC;IAClC,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC;IACtC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,KAAK,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,MAAM,CAAC;CAC1C;AAED,MAAM,WAAW,KAAK;IACpB,aAAa,CAAC,EAAE,iBAAiB,EAAE,CAAC;IACpC,aAAa,CAAC,EAAE,iBAAiB,EAAE,CAAC;IACpC,WAAW,CAAC,EAAE,eAAe,EAAE,CAAC;IAChC,WAAW,CAAC,EAAE,eAAe,EAAE,CAAC;CACjC;AAED,MAAM,MAAM,iBAAiB,GAAG,CAAC,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|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* superagent-lite
|
|
3
|
+
* Zero-dependency, native fetch HTTP client
|
|
4
|
+
* Drop-in superagent replacement with modern features from ky, got, and axios
|
|
5
|
+
*/
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Response Class (superagent compatible)
|
|
8
|
+
// ============================================================================
|
|
9
|
+
export class Response {
|
|
10
|
+
_response;
|
|
11
|
+
status;
|
|
12
|
+
statusCode;
|
|
13
|
+
ok;
|
|
14
|
+
statusText;
|
|
15
|
+
headers;
|
|
16
|
+
type;
|
|
17
|
+
charset;
|
|
18
|
+
body;
|
|
19
|
+
text;
|
|
20
|
+
constructor(_response, text, body) {
|
|
21
|
+
this._response = _response;
|
|
22
|
+
this.status = _response.status;
|
|
23
|
+
this.statusCode = _response.status;
|
|
24
|
+
this.statusText = _response.statusText;
|
|
25
|
+
this.ok = _response.ok;
|
|
26
|
+
this.text = text;
|
|
27
|
+
this.body = body;
|
|
28
|
+
const contentType = _response.headers.get('content-type') || '';
|
|
29
|
+
this.type = contentType.split(';')[0].trim();
|
|
30
|
+
this.charset = this.extractCharset(contentType);
|
|
31
|
+
// Convert headers to plain object
|
|
32
|
+
this.headers = {};
|
|
33
|
+
_response.headers.forEach((value, key) => {
|
|
34
|
+
this.headers[key.toLowerCase()] = value;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
extractCharset(contentType) {
|
|
38
|
+
const match = contentType.match(/charset=([^\s;]+)/i);
|
|
39
|
+
return match ? match[1].trim() : 'utf-8';
|
|
40
|
+
}
|
|
41
|
+
get(header) {
|
|
42
|
+
return this.headers[header.toLowerCase()];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// ============================================================================
|
|
46
|
+
// Error Classes
|
|
47
|
+
// ============================================================================
|
|
48
|
+
export class HTTPError extends Error {
|
|
49
|
+
response;
|
|
50
|
+
status;
|
|
51
|
+
constructor(response) {
|
|
52
|
+
super(`Request failed with status ${response.status}: ${response.statusText}`);
|
|
53
|
+
this.name = 'HTTPError';
|
|
54
|
+
this.response = response;
|
|
55
|
+
this.status = response.status;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export class TimeoutError extends Error {
|
|
59
|
+
constructor(message = 'Request timed out') {
|
|
60
|
+
super(message);
|
|
61
|
+
this.name = 'TimeoutError';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// ============================================================================
|
|
65
|
+
// Default Configuration
|
|
66
|
+
// ============================================================================
|
|
67
|
+
const DEFAULT_RETRY_METHODS = ['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE'];
|
|
68
|
+
const DEFAULT_RETRY_STATUS_CODES = [408, 413, 429, 500, 502, 503, 504];
|
|
69
|
+
const DEFAULT_RETRY_LIMIT = 2;
|
|
70
|
+
function defaultRetryDelay(attemptCount) {
|
|
71
|
+
return Math.min(1000 * 2 ** (attemptCount - 1), 30000);
|
|
72
|
+
}
|
|
73
|
+
// ============================================================================
|
|
74
|
+
// Request Class (superagent compatible chaining API)
|
|
75
|
+
// ============================================================================
|
|
76
|
+
export class Request {
|
|
77
|
+
_url;
|
|
78
|
+
_method;
|
|
79
|
+
_headers = {};
|
|
80
|
+
_query = {};
|
|
81
|
+
_body = undefined;
|
|
82
|
+
_timeout = {};
|
|
83
|
+
_retry = { limit: 0 };
|
|
84
|
+
_hooks = {
|
|
85
|
+
beforeRequest: [],
|
|
86
|
+
afterResponse: [],
|
|
87
|
+
beforeRetry: [],
|
|
88
|
+
beforeError: []
|
|
89
|
+
};
|
|
90
|
+
_throwHttpErrors = true;
|
|
91
|
+
_parseJson = JSON.parse;
|
|
92
|
+
_stringifyJson = JSON.stringify;
|
|
93
|
+
_abortController = null;
|
|
94
|
+
_externalSignal = null;
|
|
95
|
+
constructor(method, url, options = {}) {
|
|
96
|
+
this._method = method.toUpperCase();
|
|
97
|
+
this._url = url;
|
|
98
|
+
if (options.headers)
|
|
99
|
+
this._headers = { ...options.headers };
|
|
100
|
+
if (options.timeout) {
|
|
101
|
+
this._timeout = typeof options.timeout === 'number'
|
|
102
|
+
? { request: options.timeout }
|
|
103
|
+
: options.timeout;
|
|
104
|
+
}
|
|
105
|
+
if (options.retry !== undefined) {
|
|
106
|
+
this._retry = typeof options.retry === 'number'
|
|
107
|
+
? { limit: options.retry }
|
|
108
|
+
: options.retry;
|
|
109
|
+
}
|
|
110
|
+
if (options.hooks)
|
|
111
|
+
this.mergeHooks(options.hooks);
|
|
112
|
+
if (options.throwHttpErrors !== undefined)
|
|
113
|
+
this._throwHttpErrors = options.throwHttpErrors;
|
|
114
|
+
if (options.parseJson)
|
|
115
|
+
this._parseJson = options.parseJson;
|
|
116
|
+
if (options.stringifyJson)
|
|
117
|
+
this._stringifyJson = options.stringifyJson;
|
|
118
|
+
if (options.signal)
|
|
119
|
+
this._externalSignal = options.signal;
|
|
120
|
+
}
|
|
121
|
+
mergeHooks(hooks) {
|
|
122
|
+
if (hooks.beforeRequest)
|
|
123
|
+
this._hooks.beforeRequest.push(...hooks.beforeRequest);
|
|
124
|
+
if (hooks.afterResponse)
|
|
125
|
+
this._hooks.afterResponse.push(...hooks.afterResponse);
|
|
126
|
+
if (hooks.beforeRetry)
|
|
127
|
+
this._hooks.beforeRetry.push(...hooks.beforeRetry);
|
|
128
|
+
if (hooks.beforeError)
|
|
129
|
+
this._hooks.beforeError.push(...hooks.beforeError);
|
|
130
|
+
}
|
|
131
|
+
// ============================================================================
|
|
132
|
+
// Chaining Methods (superagent compatible)
|
|
133
|
+
// ============================================================================
|
|
134
|
+
set(field, value) {
|
|
135
|
+
if (typeof field === 'object') {
|
|
136
|
+
for (const key of Object.keys(field)) {
|
|
137
|
+
this._headers[key.toLowerCase()] = field[key];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if (value !== undefined) {
|
|
141
|
+
this._headers[field.toLowerCase()] = value;
|
|
142
|
+
}
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
query(params) {
|
|
146
|
+
if (typeof params === 'string') {
|
|
147
|
+
const searchParams = new URLSearchParams(params);
|
|
148
|
+
searchParams.forEach((value, key) => {
|
|
149
|
+
this._query[key] = value;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
for (const key of Object.keys(params)) {
|
|
154
|
+
const value = params[key];
|
|
155
|
+
if (value !== undefined && value !== null) {
|
|
156
|
+
this._query[key] = String(value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
send(data) {
|
|
163
|
+
if (this._body && typeof this._body === 'object' && typeof data === 'object') {
|
|
164
|
+
this._body = { ...this._body, ...data };
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
this._body = data;
|
|
168
|
+
}
|
|
169
|
+
// Auto-set content-type for objects
|
|
170
|
+
if (typeof data === 'object' && !this._headers['content-type']) {
|
|
171
|
+
this._headers['content-type'] = 'application/json';
|
|
172
|
+
}
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
type(contentType) {
|
|
176
|
+
const types = {
|
|
177
|
+
json: 'application/json',
|
|
178
|
+
form: 'application/x-www-form-urlencoded',
|
|
179
|
+
html: 'text/html',
|
|
180
|
+
text: 'text/plain',
|
|
181
|
+
xml: 'application/xml'
|
|
182
|
+
};
|
|
183
|
+
this._headers['content-type'] = types[contentType] || contentType;
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
accept(acceptType) {
|
|
187
|
+
const types = {
|
|
188
|
+
json: 'application/json',
|
|
189
|
+
html: 'text/html',
|
|
190
|
+
text: 'text/plain',
|
|
191
|
+
xml: 'application/xml'
|
|
192
|
+
};
|
|
193
|
+
this._headers['accept'] = types[acceptType] || acceptType;
|
|
194
|
+
return this;
|
|
195
|
+
}
|
|
196
|
+
timeout(ms) {
|
|
197
|
+
this._timeout = typeof ms === 'number' ? { request: ms } : ms;
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
retry(count) {
|
|
201
|
+
this._retry = typeof count === 'number' ? { limit: count } : count;
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
auth(user, pass, options) {
|
|
205
|
+
const type = options?.type || (pass === undefined ? 'bearer' : 'basic');
|
|
206
|
+
if (type === 'bearer') {
|
|
207
|
+
this._headers['authorization'] = `Bearer ${user}`;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const credentials = Buffer.from(`${user}:${pass || ''}`).toString('base64');
|
|
211
|
+
this._headers['authorization'] = `Basic ${credentials}`;
|
|
212
|
+
}
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
// ============================================================================
|
|
216
|
+
// Hook Methods (inspired by got/ky)
|
|
217
|
+
// ============================================================================
|
|
218
|
+
hook(name, fn) {
|
|
219
|
+
this._hooks[name].push(fn);
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
// ============================================================================
|
|
223
|
+
// Execution Control
|
|
224
|
+
// ============================================================================
|
|
225
|
+
abort() {
|
|
226
|
+
this._abortController?.abort();
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
// ============================================================================
|
|
230
|
+
// Request Execution
|
|
231
|
+
// ============================================================================
|
|
232
|
+
async execute() {
|
|
233
|
+
const retryLimit = this._retry.limit ?? 0;
|
|
234
|
+
const retryMethods = this._retry.methods ?? DEFAULT_RETRY_METHODS;
|
|
235
|
+
const retryStatusCodes = this._retry.statusCodes ?? DEFAULT_RETRY_STATUS_CODES;
|
|
236
|
+
const retryDelay = this._retry.delay ?? defaultRetryDelay;
|
|
237
|
+
let lastError = null;
|
|
238
|
+
for (let attempt = 0; attempt <= retryLimit; attempt++) {
|
|
239
|
+
try {
|
|
240
|
+
return await this.doFetch();
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
lastError = error;
|
|
244
|
+
// Don't retry if not a retryable method
|
|
245
|
+
if (!retryMethods.includes(this._method)) {
|
|
246
|
+
throw lastError;
|
|
247
|
+
}
|
|
248
|
+
// Don't retry on timeout
|
|
249
|
+
if (lastError instanceof TimeoutError) {
|
|
250
|
+
throw lastError;
|
|
251
|
+
}
|
|
252
|
+
// Check if status code is retryable
|
|
253
|
+
if (lastError instanceof HTTPError) {
|
|
254
|
+
if (!retryStatusCodes.includes(lastError.status)) {
|
|
255
|
+
throw lastError;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// Don't retry on last attempt
|
|
259
|
+
if (attempt >= retryLimit) {
|
|
260
|
+
throw lastError;
|
|
261
|
+
}
|
|
262
|
+
// Call beforeRetry hooks
|
|
263
|
+
for (const hook of this._hooks.beforeRetry) {
|
|
264
|
+
await hook(lastError, attempt + 1);
|
|
265
|
+
}
|
|
266
|
+
// Wait before retry
|
|
267
|
+
const delay = retryDelay(attempt + 1);
|
|
268
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
throw lastError;
|
|
272
|
+
}
|
|
273
|
+
async doFetch() {
|
|
274
|
+
// Build URL with query params
|
|
275
|
+
let url = this._url;
|
|
276
|
+
const queryKeys = Object.keys(this._query);
|
|
277
|
+
if (queryKeys.length > 0) {
|
|
278
|
+
const searchParams = new URLSearchParams(this._query);
|
|
279
|
+
const separator = url.includes('?') ? '&' : '?';
|
|
280
|
+
url = `${url}${separator}${searchParams.toString()}`;
|
|
281
|
+
}
|
|
282
|
+
// Prepare body
|
|
283
|
+
let body;
|
|
284
|
+
if (this._body !== undefined) {
|
|
285
|
+
const contentType = this._headers['content-type'] || '';
|
|
286
|
+
if (typeof this._body === 'object') {
|
|
287
|
+
if (contentType.includes('form-urlencoded')) {
|
|
288
|
+
body = new URLSearchParams(this._body).toString();
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
body = this._stringifyJson(this._body);
|
|
292
|
+
if (!this._headers['content-type']) {
|
|
293
|
+
this._headers['content-type'] = 'application/json';
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
body = String(this._body);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// Setup abort controller for timeout
|
|
302
|
+
this._abortController = new AbortController();
|
|
303
|
+
const signal = this._abortController.signal;
|
|
304
|
+
// Link external signal if provided
|
|
305
|
+
if (this._externalSignal) {
|
|
306
|
+
this._externalSignal.addEventListener('abort', () => this._abortController?.abort());
|
|
307
|
+
}
|
|
308
|
+
let timeoutId;
|
|
309
|
+
const timeoutMs = this._timeout.request || this._timeout.response;
|
|
310
|
+
if (timeoutMs) {
|
|
311
|
+
timeoutId = setTimeout(() => {
|
|
312
|
+
this._abortController?.abort();
|
|
313
|
+
}, timeoutMs);
|
|
314
|
+
}
|
|
315
|
+
// Call beforeRequest hooks
|
|
316
|
+
for (const hook of this._hooks.beforeRequest) {
|
|
317
|
+
await hook(this);
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
const fetchResponse = await fetch(url, {
|
|
321
|
+
method: this._method,
|
|
322
|
+
headers: this._headers,
|
|
323
|
+
body,
|
|
324
|
+
signal
|
|
325
|
+
});
|
|
326
|
+
// Parse response
|
|
327
|
+
const text = await fetchResponse.text();
|
|
328
|
+
let responseBody = text;
|
|
329
|
+
const contentType = fetchResponse.headers.get('content-type') || '';
|
|
330
|
+
if (contentType.includes('application/json') && text) {
|
|
331
|
+
try {
|
|
332
|
+
responseBody = this._parseJson(text);
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
// Keep as text if JSON parsing fails
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
let response = new Response(fetchResponse, text, responseBody);
|
|
339
|
+
// Call afterResponse hooks
|
|
340
|
+
for (const hook of this._hooks.afterResponse) {
|
|
341
|
+
const result = await hook(response);
|
|
342
|
+
if (result)
|
|
343
|
+
response = result;
|
|
344
|
+
}
|
|
345
|
+
// Throw on HTTP errors if enabled
|
|
346
|
+
if (this._throwHttpErrors && !fetchResponse.ok) {
|
|
347
|
+
let error = new HTTPError(response);
|
|
348
|
+
for (const hook of this._hooks.beforeError) {
|
|
349
|
+
error = await hook(error);
|
|
350
|
+
}
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
return response;
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
if (error.name === 'AbortError') {
|
|
357
|
+
throw new TimeoutError();
|
|
358
|
+
}
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
finally {
|
|
362
|
+
if (timeoutId)
|
|
363
|
+
clearTimeout(timeoutId);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
// ============================================================================
|
|
367
|
+
// Promise Interface
|
|
368
|
+
// ============================================================================
|
|
369
|
+
then(onfulfilled, onrejected) {
|
|
370
|
+
return this.execute().then(onfulfilled, onrejected);
|
|
371
|
+
}
|
|
372
|
+
catch(onrejected) {
|
|
373
|
+
return this.execute().catch(onrejected);
|
|
374
|
+
}
|
|
375
|
+
finally(onfinally) {
|
|
376
|
+
return this.execute().finally(onfinally);
|
|
377
|
+
}
|
|
378
|
+
// Legacy callback interface (superagent compatibility)
|
|
379
|
+
end(callback) {
|
|
380
|
+
this.execute()
|
|
381
|
+
.then(res => callback?.(null, res))
|
|
382
|
+
.catch(err => callback?.(err));
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
function createInstance(options = {}) {
|
|
386
|
+
const defaults = { ...options };
|
|
387
|
+
const resolveUrl = (url) => {
|
|
388
|
+
if (defaults.baseURL && !url.startsWith('http://') && !url.startsWith('https://')) {
|
|
389
|
+
const base = defaults.baseURL.endsWith('/') ? defaults.baseURL.slice(0, -1) : defaults.baseURL;
|
|
390
|
+
const path = url.startsWith('/') ? url : `/${url}`;
|
|
391
|
+
return `${base}${path}`;
|
|
392
|
+
}
|
|
393
|
+
return url;
|
|
394
|
+
};
|
|
395
|
+
const createRequest = (method, url) => {
|
|
396
|
+
const req = new Request(method, resolveUrl(url), {
|
|
397
|
+
headers: defaults.headers,
|
|
398
|
+
timeout: defaults.timeout,
|
|
399
|
+
retry: defaults.retry,
|
|
400
|
+
hooks: defaults.hooks,
|
|
401
|
+
throwHttpErrors: defaults.throwHttpErrors,
|
|
402
|
+
parseJson: defaults.parseJson,
|
|
403
|
+
stringifyJson: defaults.stringifyJson
|
|
404
|
+
});
|
|
405
|
+
return req;
|
|
406
|
+
};
|
|
407
|
+
const instance = ((method, url) => createRequest(method, url));
|
|
408
|
+
instance.get = (url) => createRequest('GET', url);
|
|
409
|
+
instance.post = (url) => createRequest('POST', url);
|
|
410
|
+
instance.put = (url) => createRequest('PUT', url);
|
|
411
|
+
instance.patch = (url) => createRequest('PATCH', url);
|
|
412
|
+
instance.delete = (url) => createRequest('DELETE', url);
|
|
413
|
+
instance.del = (url) => createRequest('DELETE', url);
|
|
414
|
+
instance.head = (url) => createRequest('HEAD', url);
|
|
415
|
+
instance.options = (url) => createRequest('OPTIONS', url);
|
|
416
|
+
instance.create = (opts) => createInstance({ ...defaults, ...opts });
|
|
417
|
+
instance.defaults = defaults;
|
|
418
|
+
return instance;
|
|
419
|
+
}
|
|
420
|
+
// ============================================================================
|
|
421
|
+
// Default Export
|
|
422
|
+
// ============================================================================
|
|
423
|
+
const request = createInstance();
|
|
424
|
+
export default request;
|
|
425
|
+
export { request, createInstance as create };
|
|
426
|
+
// Named exports for convenience
|
|
427
|
+
export const get = request.get;
|
|
428
|
+
export const post = request.post;
|
|
429
|
+
export const put = request.put;
|
|
430
|
+
export const patch = request.patch;
|
|
431
|
+
export const del = request.delete;
|
|
432
|
+
export const head = request.head;
|
|
433
|
+
export const options = request.options;
|
|
434
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA+CH,+EAA+E;AAC/E,yCAAyC;AACzC,+EAA+E;AAE/E,MAAM,OAAO,QAAQ;IAY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|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"dev": "tsx src/index.ts",
|
|
23
|
+
"prepublishOnly": "npm run build",
|
|
24
|
+
"clean": "rm -rf dist"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"superagent",
|
|
28
|
+
"http",
|
|
29
|
+
"https",
|
|
30
|
+
"request",
|
|
31
|
+
"client",
|
|
32
|
+
"api",
|
|
33
|
+
"rest",
|
|
34
|
+
"ajax",
|
|
35
|
+
"fetch",
|
|
36
|
+
"lightweight",
|
|
37
|
+
"drop-in",
|
|
38
|
+
"replacement",
|
|
39
|
+
"typescript"
|
|
40
|
+
],
|
|
41
|
+
"author": "",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/willzhangfly/superagent-lite.git"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/willzhangfly/superagent-lite/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/willzhangfly/superagent-lite#readme",
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=18.0.0"
|
|
53
|
+
},
|
|
54
|
+
"funding": {
|
|
55
|
+
"type": "buymeacoffee",
|
|
56
|
+
"url": "https://buymeacoffee.com/willzhangfly"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"typescript": "^5.3.0",
|
|
60
|
+
"@types/node": "^20.11.0",
|
|
61
|
+
"tsx": "^4.7.0"
|
|
62
|
+
},
|
|
63
|
+
"sideEffects": false
|
|
64
|
+
}
|