@npora/request 0.1.2 → 0.2.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/README.md +175 -2
- package/dist/index.cjs +1679 -164
- package/dist/index.d.cts +284 -85
- package/dist/index.d.ts +284 -85
- package/dist/index.js +1678 -164
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -18,8 +18,12 @@ It builds on top of the Fetch API while keeping the native request model.
|
|
|
18
18
|
- Extensible
|
|
19
19
|
- Zero Runtime Dependency
|
|
20
20
|
- Plugin Architecture
|
|
21
|
+
- Typed Extension Configuration
|
|
22
|
+
- Client Default Inheritance
|
|
23
|
+
- Retry, Cache and Authentication
|
|
21
24
|
- Custom Adapter
|
|
22
25
|
- Unified Error Handling
|
|
26
|
+
- Browser and Web Worker Support
|
|
23
27
|
|
|
24
28
|
---
|
|
25
29
|
|
|
@@ -63,10 +67,16 @@ console.log(user)
|
|
|
63
67
|
```ts
|
|
64
68
|
const request = createClient(options)
|
|
65
69
|
|
|
70
|
+
const childRequest = request.extend(options)
|
|
71
|
+
|
|
66
72
|
request.request(config)
|
|
67
73
|
|
|
74
|
+
request.requestResponse(config)
|
|
75
|
+
|
|
68
76
|
request.get(url)
|
|
69
77
|
|
|
78
|
+
request.getResponse(url)
|
|
79
|
+
|
|
70
80
|
request.post(url)
|
|
71
81
|
|
|
72
82
|
request.put(url)
|
|
@@ -75,8 +85,16 @@ request.patch(url)
|
|
|
75
85
|
|
|
76
86
|
request.delete(url)
|
|
77
87
|
|
|
88
|
+
request.head(url)
|
|
89
|
+
|
|
90
|
+
request.options(url)
|
|
91
|
+
|
|
78
92
|
request.use(plugin)
|
|
79
93
|
|
|
94
|
+
request.unuse(pluginName)
|
|
95
|
+
|
|
96
|
+
request.hasPlugin(pluginName)
|
|
97
|
+
|
|
80
98
|
request.interceptors.request.use()
|
|
81
99
|
|
|
82
100
|
request.interceptors.response.use()
|
|
@@ -84,6 +102,116 @@ request.interceptors.response.use()
|
|
|
84
102
|
request.interceptors.error.use()
|
|
85
103
|
```
|
|
86
104
|
|
|
105
|
+
Data-first methods return the parsed response body. Use `requestResponse()` or
|
|
106
|
+
an HTTP `*Response()` method when status, headers or the native `Response` is
|
|
107
|
+
needed.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
# Configuration
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const api = createClient({
|
|
115
|
+
baseURL: 'https://api.example.com',
|
|
116
|
+
timeout: 5000,
|
|
117
|
+
headers: {
|
|
118
|
+
'x-app': 'dashboard'
|
|
119
|
+
},
|
|
120
|
+
query: {
|
|
121
|
+
locale: 'en'
|
|
122
|
+
},
|
|
123
|
+
fetchOptions: {
|
|
124
|
+
credentials: 'include'
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const adminApi = api.extend({
|
|
129
|
+
baseURL: 'https://api.example.com/admin',
|
|
130
|
+
headers: {
|
|
131
|
+
'x-role': 'admin'
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`extend()` creates an isolated child client. It inherits configuration and the
|
|
137
|
+
adapter, while plugins and interceptors remain instance-scoped.
|
|
138
|
+
|
|
139
|
+
Request configuration overrides client defaults. Headers are merged
|
|
140
|
+
case-insensitively, while query parameters, native Fetch options and extension
|
|
141
|
+
configuration are merged by their documented rules.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
# Complete Responses
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const response = await api.getResponse<User>('/users/1')
|
|
149
|
+
|
|
150
|
+
console.log(response.data)
|
|
151
|
+
console.log(response.status)
|
|
152
|
+
console.log(response.headers)
|
|
153
|
+
console.log(response.raw)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
# Plugins
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import {
|
|
162
|
+
authPlugin,
|
|
163
|
+
cachePlugin,
|
|
164
|
+
retryPlugin
|
|
165
|
+
} from '@npora/request'
|
|
166
|
+
|
|
167
|
+
const request = createClient({
|
|
168
|
+
baseURL: 'https://api.example.com'
|
|
169
|
+
})
|
|
170
|
+
.use(retryPlugin({
|
|
171
|
+
retries: 2,
|
|
172
|
+
delay: 200
|
|
173
|
+
}))
|
|
174
|
+
.use(cachePlugin())
|
|
175
|
+
.use(authPlugin({
|
|
176
|
+
token: () => accessToken,
|
|
177
|
+
refreshToken
|
|
178
|
+
}))
|
|
179
|
+
|
|
180
|
+
const user = await request.get<User>('/users/1', {
|
|
181
|
+
extensions: {
|
|
182
|
+
cache: {
|
|
183
|
+
enabled: true,
|
|
184
|
+
ttl: 30000
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Plugin configuration belongs under `extensions`. Legacy top-level plugin
|
|
191
|
+
fields remain available during v0.x but are deprecated.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
# Error Handling
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { RequestError } from '@npora/request'
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
await request.get('/users/missing')
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (error instanceof RequestError) {
|
|
204
|
+
console.error(error.code)
|
|
205
|
+
console.error(error.status)
|
|
206
|
+
console.error(error.data)
|
|
207
|
+
console.error(error.response)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Errors use stable codes including `CONFIG_ERROR`, `HTTP_ERROR`,
|
|
213
|
+
`NETWORK_ERROR`, `TIMEOUT_ERROR`, `ABORT_ERROR` and `PARSER_ERROR`.
|
|
214
|
+
|
|
87
215
|
---
|
|
88
216
|
|
|
89
217
|
# Examples
|
|
@@ -93,9 +221,44 @@ See the `examples` directory.
|
|
|
93
221
|
```
|
|
94
222
|
examples
|
|
95
223
|
├── basic.ts
|
|
224
|
+
├── custom-plugin.ts
|
|
225
|
+
├── error-handling.ts
|
|
96
226
|
└── plugins.ts
|
|
97
227
|
```
|
|
98
228
|
|
|
229
|
+
Examples are typechecked in CI with `pnpm test:examples`.
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
# Performance
|
|
234
|
+
|
|
235
|
+
Run the request-pipeline benchmark:
|
|
236
|
+
|
|
237
|
+
```sh
|
|
238
|
+
pnpm benchmark
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
It reports sequential and concurrent throughput, latency percentiles and heap
|
|
242
|
+
movement for the adapter, client and plugin pipeline. CI stores a JSON report
|
|
243
|
+
for comparisons between equivalent runners. See
|
|
244
|
+
[`docs/benchmark.md`](docs/benchmark.md) for methodology and options.
|
|
245
|
+
|
|
246
|
+
Verify the built entrypoint, declaration, gzip and npm tarball size budgets:
|
|
247
|
+
|
|
248
|
+
```sh
|
|
249
|
+
pnpm build
|
|
250
|
+
pnpm test:size
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
See [`docs/package-size.md`](docs/package-size.md) for the checked metrics and
|
|
254
|
+
budget policy.
|
|
255
|
+
|
|
256
|
+
The ecosystem evaluation covers Axios and nine representative GitHub request
|
|
257
|
+
libraries with equivalent local HTTP workloads. Competitor packages were
|
|
258
|
+
removed after measurement and are not project dependencies. See
|
|
259
|
+
[`docs/comparison.md`](docs/comparison.md) for the snapshot and design
|
|
260
|
+
decisions.
|
|
261
|
+
|
|
99
262
|
---
|
|
100
263
|
|
|
101
264
|
# Documentation
|
|
@@ -104,13 +267,19 @@ Project documentation is located in the `docs` directory.
|
|
|
104
267
|
|
|
105
268
|
```
|
|
106
269
|
docs
|
|
270
|
+
├── benchmark.md
|
|
107
271
|
├── blueprint.md
|
|
108
|
-
├──
|
|
272
|
+
├── comparison.md
|
|
109
273
|
├── features.md
|
|
274
|
+
├── package-size.md
|
|
275
|
+
├── structure.md
|
|
110
276
|
└── api.md
|
|
111
277
|
```
|
|
112
278
|
|
|
113
279
|
- **Blueprint** — Project vision and architecture
|
|
280
|
+
- **Benchmark** — Performance measurement methodology
|
|
281
|
+
- **Comparison** — Ten-library ecosystem evaluation
|
|
282
|
+
- **Package Size** — Published asset budgets and regression checks
|
|
114
283
|
- **Structure** — Directory responsibilities
|
|
115
284
|
- **Features** — Product roadmap
|
|
116
285
|
- **API** — Public API contract
|
|
@@ -123,9 +292,13 @@ Supported runtimes:
|
|
|
123
292
|
|
|
124
293
|
- Chrome
|
|
125
294
|
- Edge
|
|
126
|
-
- Firefox
|
|
127
295
|
- Safari
|
|
128
296
|
- Node.js
|
|
297
|
+
- Web Workers
|
|
298
|
+
|
|
299
|
+
Automated runtime coverage uses Node.js integration tests and Playwright
|
|
300
|
+
browser tests for Chromium and WebKit. Firefox is outside the current
|
|
301
|
+
verification scope.
|
|
129
302
|
|
|
130
303
|
Internet Explorer is **not supported**.
|
|
131
304
|
|