@nxtedition/nxt-undici 3.3.0 → 3.3.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/lib/index.js +45 -50
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -20,7 +20,7 @@ export const interceptors = {
|
|
|
20
20
|
export { parseHeaders } from './utils.js'
|
|
21
21
|
export { Client, Pool, Agent, getGlobalDispatcher, setGlobalDispatcher } from '@nxtedition/undici'
|
|
22
22
|
|
|
23
|
-
function
|
|
23
|
+
function wrapDispatcher(dispatcher) {
|
|
24
24
|
let wrappedDispatcher = dispatcherCache.get(dispatcher)
|
|
25
25
|
if (wrappedDispatcher == null) {
|
|
26
26
|
wrappedDispatcher = dispatcher.compose(
|
|
@@ -35,6 +35,45 @@ function wrapdDispatcher(dispatcher) {
|
|
|
35
35
|
interceptors.redirect(),
|
|
36
36
|
interceptors.cache(),
|
|
37
37
|
interceptors.proxy(),
|
|
38
|
+
(dispatch) => (opts, handler) => {
|
|
39
|
+
const headers = parseHeaders(opts.headers)
|
|
40
|
+
|
|
41
|
+
const userAgent = opts.userAgent ?? globalThis.userAgent
|
|
42
|
+
if (userAgent && headers?.['user-agent'] !== userAgent) {
|
|
43
|
+
headers['user-agent'] = userAgent
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const url = opts.url ? new URL(opts.url) : null
|
|
47
|
+
|
|
48
|
+
return dispatch(
|
|
49
|
+
{
|
|
50
|
+
id: opts.id,
|
|
51
|
+
origin: opts.origin ?? url?.origin,
|
|
52
|
+
path: opts.path ?? (url?.search ? `${url.pathname}${url.search}` : url?.pathname),
|
|
53
|
+
method: opts.method ?? (opts.body ? 'POST' : 'GET'),
|
|
54
|
+
body: opts.body,
|
|
55
|
+
query: opts.query,
|
|
56
|
+
headers,
|
|
57
|
+
signal: opts.signal,
|
|
58
|
+
reset: opts.reset ?? false,
|
|
59
|
+
blocking: opts.blocking ?? false,
|
|
60
|
+
headersTimeout: opts.headersTimeout,
|
|
61
|
+
bodyTimeout: opts.bodyTimeout,
|
|
62
|
+
idempotent: opts.idempotent,
|
|
63
|
+
retry: opts.retry ?? 8,
|
|
64
|
+
proxy: opts.proxy ?? false,
|
|
65
|
+
cache: opts.cache ?? false,
|
|
66
|
+
upgrade: opts.upgrade ?? false,
|
|
67
|
+
follow: opts.follow ?? 8,
|
|
68
|
+
error: opts.error ?? true,
|
|
69
|
+
verify: opts.verify ?? true,
|
|
70
|
+
logger: opts.logger ?? null,
|
|
71
|
+
lookup: opts.lookup ?? null,
|
|
72
|
+
dns: opts.dns ?? true,
|
|
73
|
+
},
|
|
74
|
+
handler,
|
|
75
|
+
)
|
|
76
|
+
},
|
|
38
77
|
)
|
|
39
78
|
dispatcherCache.set(dispatcher, wrappedDispatcher)
|
|
40
79
|
}
|
|
@@ -42,67 +81,23 @@ function wrapdDispatcher(dispatcher) {
|
|
|
42
81
|
}
|
|
43
82
|
|
|
44
83
|
export function dispatch(dispatcher, opts, handler) {
|
|
45
|
-
return
|
|
84
|
+
return wrapDispatcher(dispatcher).dispatch(opts, handler)
|
|
46
85
|
}
|
|
47
86
|
|
|
48
87
|
export async function request(url, opts) {
|
|
49
88
|
// TODO (fix): More argument validation...
|
|
50
89
|
|
|
51
90
|
if (typeof url === 'string') {
|
|
52
|
-
|
|
91
|
+
opts = { url: new URL(url), ...opts }
|
|
53
92
|
} else if (url instanceof URL) {
|
|
54
|
-
|
|
93
|
+
opts = { url, ...opts }
|
|
55
94
|
} else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
|
|
56
|
-
|
|
95
|
+
opts = { url, ...opts }
|
|
57
96
|
}
|
|
58
97
|
|
|
59
98
|
if (opts == null && typeof url === 'object' && url != null) {
|
|
60
99
|
opts = url
|
|
61
100
|
}
|
|
62
101
|
|
|
63
|
-
|
|
64
|
-
// Do nothing...
|
|
65
|
-
} else if (typeof opts.url === 'string') {
|
|
66
|
-
url = new URL(opts.url)
|
|
67
|
-
} else if (url.url instanceof URL) {
|
|
68
|
-
url = opts.url
|
|
69
|
-
} else if (typeof opts.origin === 'string' && typeof (opts.path ?? opts.pathname) === 'string') {
|
|
70
|
-
url = opts
|
|
71
|
-
} else {
|
|
72
|
-
throw new Error('missing url')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const method = opts.method ?? (opts.body ? 'POST' : 'GET')
|
|
76
|
-
const headers = parseHeaders(opts.headers)
|
|
77
|
-
|
|
78
|
-
const userAgent = opts.userAgent ?? globalThis.userAgent
|
|
79
|
-
if (userAgent && headers?.['user-agent'] !== userAgent) {
|
|
80
|
-
headers['user-agent'] = userAgent
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return await wrapdDispatcher(opts.dispatcher ?? undici.getGlobalDispatcher()).request({
|
|
84
|
-
id: opts.id,
|
|
85
|
-
origin: url.origin,
|
|
86
|
-
path: url.path ?? (url.search ? `${url.pathname}${url.search}` : url.pathname),
|
|
87
|
-
method,
|
|
88
|
-
body: opts.body,
|
|
89
|
-
query: opts.query,
|
|
90
|
-
headers,
|
|
91
|
-
signal: opts.signal,
|
|
92
|
-
reset: opts.reset ?? false,
|
|
93
|
-
blocking: opts.blocking ?? false,
|
|
94
|
-
headersTimeout: opts.headersTimeout,
|
|
95
|
-
bodyTimeout: opts.bodyTimeout,
|
|
96
|
-
idempotent: opts.idempotent,
|
|
97
|
-
retry: opts.retry ?? 8,
|
|
98
|
-
proxy: opts.proxy ?? false,
|
|
99
|
-
cache: opts.cache ?? false,
|
|
100
|
-
upgrade: opts.upgrade ?? false,
|
|
101
|
-
follow: opts.follow ?? 8,
|
|
102
|
-
error: opts.error ?? true,
|
|
103
|
-
verify: opts.verify ?? true,
|
|
104
|
-
logger: opts.logger ?? null,
|
|
105
|
-
lookup: opts.lookup ?? null,
|
|
106
|
-
dns: opts.dns ?? true,
|
|
107
|
-
})
|
|
102
|
+
return await wrapDispatcher(opts.dispatcher ?? undici.getGlobalDispatcher()).request(opts)
|
|
108
103
|
}
|