@npora/request 0.5.0 → 1.0.1

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 CHANGED
@@ -1,47 +1,25 @@
1
1
  # @npora/request
2
2
 
3
- > A modern, TypeScript-first HTTP client built on top of the Fetch API.
3
+ A TypeScript-first HTTP client built on the standard Fetch API.
4
4
 
5
- Npora Request provides a consistent, extensible and production-ready request layer for modern JavaScript applications.
5
+ Npora Request adds typed configuration, a predictable request lifecycle,
6
+ interceptors, plugins, unified errors, retries, caching, authentication, and
7
+ upload/download progress without adding runtime dependencies.
6
8
 
7
- It does **not** replace Fetch.
9
+ ## Install
8
10
 
9
- It builds on top of the Fetch API while keeping the native request model.
10
-
11
- ---
12
-
13
- # Features
14
-
15
- - Fetch First
16
- - TypeScript First
17
- - Lightweight
18
- - Extensible
19
- - Zero Runtime Dependency
20
- - Plugin Architecture
21
- - Typed Extension Configuration
22
- - Client Default Inheritance
23
- - Retry, Cache and Authentication
24
- - Custom Adapter
25
- - Unified Error Handling
26
- - Browser and Web Worker Support
27
-
28
- ---
29
-
30
- # Installation
31
-
32
- ```bash
11
+ ```sh
33
12
  pnpm add @npora/request
34
13
  ```
35
14
 
36
- or
37
-
38
- ```bash
15
+ ```sh
39
16
  npm install @npora/request
40
17
  ```
41
18
 
42
- ---
19
+ Node.js 22 or newer is required. Modern Chromium, Edge, Safari, WebKit, and Web
20
+ Workers are supported through their native Fetch implementations.
43
21
 
44
- # Quick Start
22
+ ## Quick start
45
23
 
46
24
  ```ts
47
25
  import { createClient } from '@npora/request'
@@ -51,80 +29,52 @@ interface User {
51
29
  name: string
52
30
  }
53
31
 
54
- const request = createClient({
55
- baseURL: 'https://api.example.com'
32
+ const api = createClient({
33
+ baseURL: 'https://api.example.com',
34
+ timeout: 5000,
35
+ headers: {
36
+ 'x-app': 'dashboard'
37
+ }
56
38
  })
57
39
 
58
- const user = await request.get<User>('/users/1')
59
-
60
- console.log(user)
40
+ const user = await api.get<User>('/users/1')
61
41
  ```
62
42
 
63
- ---
64
-
65
- # Public API
43
+ Data-first methods return the parsed response body. Use a response method when
44
+ status, headers, or the native `Response` is needed:
66
45
 
67
46
  ```ts
68
- const request = createClient(options)
69
-
70
- const childRequest = request.extend(options)
71
-
72
- request.request(config)
73
-
74
- request.requestResponse(config)
75
-
76
- request.get(url)
77
-
78
- request.getResponse(url)
79
-
80
- request.post(url)
81
-
82
- request.put(url)
83
-
84
- request.patch(url)
85
-
86
- request.delete(url)
87
-
88
- request.head(url)
89
-
90
- request.options(url)
91
-
92
- request.use(plugin)
93
-
94
- request.unuse(pluginName)
95
-
96
- request.hasPlugin(pluginName)
97
-
98
- request.interceptors.request.use()
99
-
100
- request.interceptors.response.use()
47
+ const response = await api.getResponse<User>('/users/1')
101
48
 
102
- request.interceptors.error.use()
49
+ console.log(response.data)
50
+ console.log(response.status)
51
+ console.log(response.headers)
52
+ console.log(response.raw)
103
53
  ```
104
54
 
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
55
+ ## Request configuration
112
56
 
113
57
  ```ts
114
- const api = createClient({
115
- baseURL: 'https://api.example.com',
116
- timeout: 5000,
117
- headers: {
118
- 'x-app': 'dashboard'
119
- },
58
+ await api.post('/users', {
120
59
  query: {
121
- locale: 'en'
60
+ notify: true
61
+ },
62
+ json: {
63
+ name: 'Npora'
122
64
  },
123
65
  fetchOptions: {
124
66
  credentials: 'include'
125
67
  }
126
68
  })
69
+ ```
70
+
71
+ The body options `body`, `json`, `form`, and `formData` are mutually exclusive.
72
+ `GET` and `HEAD` requests cannot contain a body. Invalid configuration throws a
73
+ `RequestError` before the adapter sends a request.
127
74
 
75
+ Create isolated clients with inherited defaults:
76
+
77
+ ```ts
128
78
  const adminApi = api.extend({
129
79
  baseURL: 'https://api.example.com/admin',
130
80
  headers: {
@@ -133,45 +83,49 @@ const adminApi = api.extend({
133
83
  })
134
84
  ```
135
85
 
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.
86
+ The adapter and configuration are inherited. Plugins and interceptors remain
87
+ isolated to each client.
142
88
 
143
- ---
144
-
145
- # Complete Responses
89
+ ## HTTP methods
146
90
 
147
91
  ```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)
92
+ api.request(config)
93
+ api.requestResponse(config)
94
+
95
+ api.get(url, config)
96
+ api.post(url, config)
97
+ api.put(url, config)
98
+ api.patch(url, config)
99
+ api.delete(url, config)
100
+ api.head(url, config)
101
+ api.options(url, config)
102
+
103
+ api.getResponse(url, config)
104
+ api.postResponse(url, config)
105
+ api.putResponse(url, config)
106
+ api.patchResponse(url, config)
107
+ api.deleteResponse(url, config)
108
+ api.headResponse(url, config)
109
+ api.optionsResponse(url, config)
154
110
  ```
155
111
 
156
- ---
157
-
158
- # Plugins
112
+ ## Plugins
159
113
 
160
114
  ```ts
161
115
  import {
162
116
  authPlugin,
163
117
  cachePlugin,
118
+ createClient,
164
119
  retryPlugin
165
120
  } from '@npora/request'
166
121
 
167
- const request = createClient({
168
- baseURL: 'https://api.example.com'
169
- })
122
+ const cache = cachePlugin()
123
+ const request = createClient()
170
124
  .use(retryPlugin({
171
125
  retries: 2,
172
126
  delay: 200
173
127
  }))
174
- .use(cachePlugin())
128
+ .use(cache)
175
129
  .use(authPlugin({
176
130
  token: () => accessToken,
177
131
  refreshToken
@@ -185,163 +139,98 @@ const user = await request.get<User>('/users/1', {
185
139
  }
186
140
  }
187
141
  })
142
+
143
+ cache.clear()
188
144
  ```
189
145
 
190
- Plugin configuration belongs under `extensions`. Top-level plugin fields are
191
- not part of `RequestConfig`.
146
+ Built-in plugins:
147
+
148
+ - `retryPlugin()`
149
+ - `cachePlugin()`
150
+ - `authPlugin()`
151
+ - `loggerPlugin()`
152
+ - `uploadPlugin()`
153
+ - `downloadPlugin()`
154
+
155
+ Plugin-owned request options belong under `extensions`. Third-party plugins can
156
+ augment `RequestExtensions` through TypeScript module augmentation.
157
+
158
+ ## Interceptors
192
159
 
193
- ---
160
+ ```ts
161
+ api.interceptors.request.use(config => config)
162
+ api.interceptors.response.use(response => response)
163
+ api.interceptors.error.use(error => error)
164
+ ```
194
165
 
195
- # Error Handling
166
+ An optional numeric priority controls execution order. Higher priorities run
167
+ first; equal priorities preserve registration order.
168
+
169
+ ## Errors
196
170
 
197
171
  ```ts
198
172
  import { RequestError } from '@npora/request'
199
173
 
200
174
  try {
201
- await request.get('/users/missing')
175
+ await api.get('/users/missing')
202
176
  } catch (error) {
203
177
  if (error instanceof RequestError) {
204
178
  console.error(error.code)
205
179
  console.error(error.status)
206
180
  console.error(error.data)
207
- console.error(error.response)
208
181
  }
209
182
  }
210
183
  ```
211
184
 
212
- Errors use stable codes including `CONFIG_ERROR`, `HTTP_ERROR`,
213
- `NETWORK_ERROR`, `TIMEOUT_ERROR`, `ABORT_ERROR` and `PARSER_ERROR`.
214
-
215
- ---
185
+ Stable request error codes:
216
186
 
217
- # Examples
187
+ - `CONFIG_ERROR`
188
+ - `HTTP_ERROR`
189
+ - `NETWORK_ERROR`
190
+ - `TIMEOUT_ERROR`
191
+ - `ABORT_ERROR`
192
+ - `PARSER_ERROR`
218
193
 
219
- See the `examples` directory.
220
-
221
- ```
222
- examples
223
- ├── basic.ts
224
- ├── custom-plugin.ts
225
- ├── error-handling.ts
226
- └── plugins.ts
227
- ```
194
+ ## Documentation
228
195
 
229
- Examples are typechecked in CI with `pnpm test:examples`.
196
+ - [API reference](https://github.com/npora/request/blob/main/docs/api.md)
197
+ - [Architecture](https://github.com/npora/request/blob/main/docs/architecture.md)
198
+ - [Migration from 0.x](https://github.com/npora/request/blob/main/docs/migration.md)
199
+ - [Security model](https://github.com/npora/request/blob/main/docs/security.md)
200
+ - [Testing and release gates](https://github.com/npora/request/blob/main/docs/testing.md)
201
+ - [Performance benchmarks](https://github.com/npora/request/blob/main/docs/benchmark.md)
202
+ - [Package size budgets](https://github.com/npora/request/blob/main/docs/package-size.md)
203
+ - [Changelog](https://github.com/npora/request/blob/main/CHANGELOG.md)
230
204
 
231
- ---
205
+ Project policies:
232
206
 
233
- # Performance
207
+ - [Contributing](https://github.com/npora/request/blob/main/CONTRIBUTING.md)
208
+ - [Security reporting](https://github.com/npora/request/blob/main/SECURITY.md)
234
209
 
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:
210
+ ## Development
247
211
 
248
212
  ```sh
213
+ pnpm install --frozen-lockfile
214
+ pnpm typecheck
215
+ pnpm test:coverage
249
216
  pnpm build
250
- pnpm test:size
217
+ pnpm test:package
218
+ pnpm test:browser
251
219
  ```
252
220
 
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
-
262
- ---
263
-
264
- # Documentation
265
-
266
- Project documentation is located in the `docs` directory.
267
-
268
- ```
269
- docs
270
- ├── benchmark.md
271
- ├── blueprint.md
272
- ├── comparison.md
273
- ├── features.md
274
- ├── package-size.md
275
- ├── structure.md
276
- └── api.md
277
- ```
278
-
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
283
- - **Structure** — Directory responsibilities
284
- - **Features** — Product roadmap
285
- - **API** — Public API contract
286
-
287
- ---
288
-
289
- # Browser Support
290
-
291
- Supported runtimes:
292
-
293
- - Chrome
294
- - Edge
295
- - Safari
296
- - Node.js 22+
297
- - Web Workers
298
-
299
- Automated runtime coverage uses Node.js 22, 24, and 26 integration and package
300
- tests plus Playwright browser tests for Chromium and WebKit. Firefox is
301
- outside the current verification scope.
302
-
303
- Internet Explorer is **not supported**.
304
-
305
- ---
306
-
307
- # Roadmap
308
-
309
- ## v0.1
310
-
311
- Core
312
-
313
- - Client
314
- - Config
315
- - Pipeline
316
- - Adapter
317
- - Error
318
-
319
- ## v0.2
320
-
321
- Request
322
-
323
- - Timeout
324
- - Abort
325
- - Retry
326
- - Cache
327
-
328
- ## v0.3
329
-
330
- Business
331
-
332
- - Auth
333
- - Logger
334
- - Upload
335
- - Download
336
-
337
- ## v1.0
221
+ See the
222
+ [testing and release gates](https://github.com/npora/request/blob/main/docs/testing.md)
223
+ for the complete matrix.
338
224
 
339
- Stable API
225
+ ## Versioning
340
226
 
341
- Production Ready
227
+ `@npora/request` follows Semantic Versioning. Package-root exports, their
228
+ TypeScript declarations, documented behavior, stable error codes, and plugin
229
+ lifecycle are public API. Breaking changes require a new major version.
342
230
 
343
- ---
231
+ Internal modules that are not exported from the package root are not public
232
+ API.
344
233
 
345
- # License
234
+ ## License
346
235
 
347
236
  MIT © Npora Team
package/dist/index.cjs CHANGED
@@ -2164,17 +2164,18 @@ var SENSITIVE_QUERY_KEYS = /* @__PURE__ */ new Set([
2164
2164
  "token"
2165
2165
  ]);
2166
2166
  function redactURL(url) {
2167
- const queryIndex = url.indexOf("?");
2167
+ const safeURL = redactCredentials(url);
2168
+ const queryIndex = safeURL.indexOf("?");
2168
2169
  if (queryIndex === -1) {
2169
- return url;
2170
+ return safeURL;
2170
2171
  }
2171
- const hashIndex = url.indexOf("#", queryIndex);
2172
- const pathname = url.slice(0, queryIndex);
2173
- const query = url.slice(
2172
+ const hashIndex = safeURL.indexOf("#", queryIndex);
2173
+ const pathname = safeURL.slice(0, queryIndex);
2174
+ const query = safeURL.slice(
2174
2175
  queryIndex + 1,
2175
2176
  hashIndex === -1 ? void 0 : hashIndex
2176
2177
  );
2177
- const hash = hashIndex === -1 ? "" : url.slice(hashIndex);
2178
+ const hash = hashIndex === -1 ? "" : safeURL.slice(hashIndex);
2178
2179
  const params = new URLSearchParams(query);
2179
2180
  for (const key of [...params.keys()]) {
2180
2181
  if (SENSITIVE_QUERY_KEYS.has(key.toLowerCase())) {
@@ -2184,6 +2185,31 @@ function redactURL(url) {
2184
2185
  const redactedQuery = params.toString();
2185
2186
  return `${pathname}${redactedQuery ? `?${redactedQuery}` : ""}${hash}`;
2186
2187
  }
2188
+ function redactCredentials(url) {
2189
+ const schemeIndex = url.indexOf("://");
2190
+ const authorityStart = schemeIndex === -1 ? url.startsWith("//") ? 2 : -1 : schemeIndex + 3;
2191
+ if (authorityStart === -1) {
2192
+ return url;
2193
+ }
2194
+ const authorityEnd = findAuthorityEnd(url, authorityStart);
2195
+ const credentialEnd = url.lastIndexOf(
2196
+ "@",
2197
+ authorityEnd - 1
2198
+ );
2199
+ if (credentialEnd < authorityStart) {
2200
+ return url;
2201
+ }
2202
+ return `${url.slice(0, authorityStart)}[REDACTED]@` + url.slice(credentialEnd + 1);
2203
+ }
2204
+ function findAuthorityEnd(url, authorityStart) {
2205
+ for (let index = authorityStart; index < url.length; index += 1) {
2206
+ const character = url[index];
2207
+ if (character === "/" || character === "?" || character === "#") {
2208
+ return index;
2209
+ }
2210
+ }
2211
+ return url.length;
2212
+ }
2187
2213
  function createErrorLog(error) {
2188
2214
  if (error instanceof RequestError) {
2189
2215
  return {
package/dist/index.js CHANGED
@@ -2126,17 +2126,18 @@ var SENSITIVE_QUERY_KEYS = /* @__PURE__ */ new Set([
2126
2126
  "token"
2127
2127
  ]);
2128
2128
  function redactURL(url) {
2129
- const queryIndex = url.indexOf("?");
2129
+ const safeURL = redactCredentials(url);
2130
+ const queryIndex = safeURL.indexOf("?");
2130
2131
  if (queryIndex === -1) {
2131
- return url;
2132
+ return safeURL;
2132
2133
  }
2133
- const hashIndex = url.indexOf("#", queryIndex);
2134
- const pathname = url.slice(0, queryIndex);
2135
- const query = url.slice(
2134
+ const hashIndex = safeURL.indexOf("#", queryIndex);
2135
+ const pathname = safeURL.slice(0, queryIndex);
2136
+ const query = safeURL.slice(
2136
2137
  queryIndex + 1,
2137
2138
  hashIndex === -1 ? void 0 : hashIndex
2138
2139
  );
2139
- const hash = hashIndex === -1 ? "" : url.slice(hashIndex);
2140
+ const hash = hashIndex === -1 ? "" : safeURL.slice(hashIndex);
2140
2141
  const params = new URLSearchParams(query);
2141
2142
  for (const key of [...params.keys()]) {
2142
2143
  if (SENSITIVE_QUERY_KEYS.has(key.toLowerCase())) {
@@ -2146,6 +2147,31 @@ function redactURL(url) {
2146
2147
  const redactedQuery = params.toString();
2147
2148
  return `${pathname}${redactedQuery ? `?${redactedQuery}` : ""}${hash}`;
2148
2149
  }
2150
+ function redactCredentials(url) {
2151
+ const schemeIndex = url.indexOf("://");
2152
+ const authorityStart = schemeIndex === -1 ? url.startsWith("//") ? 2 : -1 : schemeIndex + 3;
2153
+ if (authorityStart === -1) {
2154
+ return url;
2155
+ }
2156
+ const authorityEnd = findAuthorityEnd(url, authorityStart);
2157
+ const credentialEnd = url.lastIndexOf(
2158
+ "@",
2159
+ authorityEnd - 1
2160
+ );
2161
+ if (credentialEnd < authorityStart) {
2162
+ return url;
2163
+ }
2164
+ return `${url.slice(0, authorityStart)}[REDACTED]@` + url.slice(credentialEnd + 1);
2165
+ }
2166
+ function findAuthorityEnd(url, authorityStart) {
2167
+ for (let index = authorityStart; index < url.length; index += 1) {
2168
+ const character = url[index];
2169
+ if (character === "/" || character === "?" || character === "#") {
2170
+ return index;
2171
+ }
2172
+ }
2173
+ return url.length;
2174
+ }
2149
2175
  function createErrorLog(error) {
2150
2176
  if (error instanceof RequestError) {
2151
2177
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npora/request",
3
- "version": "0.5.0",
3
+ "version": "1.0.1",
4
4
  "description": "A modern, TypeScript-first HTTP client powered by the Fetch API.",
5
5
  "keywords": [
6
6
  "npora",
@@ -64,13 +64,18 @@
64
64
  "example:plugins": "tsx examples/plugins.ts",
65
65
  "test:coverage": "vitest run --coverage",
66
66
  "test:run": "vitest run",
67
+ "test:security": "vitest run test/security.test.ts",
68
+ "test:docs": "node test/docs.mjs",
67
69
  "test:types": "pnpm build && tsc -p test/types/tsconfig.json",
68
70
  "test:examples": "tsc -p examples/tsconfig.json",
69
- "test:package": "node test/package/esm.mjs && node test/package/cjs.cjs && node test/package/api-exports.mjs && node test/package/npm-pack-result.test.mjs && node test/package/manifest.mjs && pnpm test:size",
71
+ "test:package": "node test/package/esm.mjs && node test/package/cjs.cjs && node test/package/api-exports.mjs && node test/package/api-contract.mjs && node test/package/npm-pack-result.test.mjs && node test/package/manifest.mjs && pnpm test:size",
72
+ "test:registry": "node test/package/registry-install.mjs",
70
73
  "test:size": "node test/package/size.mjs",
71
74
  "test:browser": "playwright test --config=browser/playwright.config.ts",
72
75
  "test:browser:ui": "playwright test --config=browser/playwright.config.ts --ui",
73
76
  "test:browser:report": "playwright show-report playwright-report",
77
+ "audit:dependencies": "pnpm audit --audit-level high",
78
+ "audit:signatures": "pnpm audit signatures",
74
79
  "benchmark": "tsx benchmark/request.ts",
75
80
  "benchmark:types": "tsc -p benchmark/tsconfig.json"
76
81
  }