Skip to content

Commit 897c8b5

Browse files
committed
✨ Add 'base' option, fix 'null' origin
1 parent 4880369 commit 897c8b5

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
],
7171
"rules": {
7272
"prettier/prettier": 2,
73+
"@typescript-eslint/no-floating-promises": 0,
7374
"@typescript-eslint/consistent-type-definitions": 0,
7475
"@typescript-eslint/explicit-function-return-type": 0,
7576
"@typescript-eslint/method-signature-style": 0,

src/index.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ interface RequestMethod<P extends Payload> {
3434
}
3535

3636
interface RequiredOptions<P extends Payload> extends RequestInit {
37+
/**
38+
* Base of the request URL, default to `location.origin` if available.
39+
* Provide a valid url if you want to use relative `resource` path
40+
* when module loaded in `file://`, `about:blank` or Node.js environment.
41+
*/
42+
base?: string
3743
/**
3844
* Part of the request URL
3945
*/
@@ -124,6 +130,10 @@ const CONTENT_TYPES = {
124130
} as const
125131

126132
const DEFAULTS: RequiredOptions<Payload> = {
133+
base:
134+
typeof location !== 'undefined' && location.origin !== 'null'
135+
? location.origin
136+
: undefined,
127137
highWaterMark: 1024 * 1024 * 10, // 10mb
128138
onRequest: () => {},
129139
onResponse(result) {
@@ -208,20 +218,17 @@ function request<P extends Payload>(
208218
baseOptions: Options<P>
209219
): ResponsePromise<P> {
210220
const options: RequestOptions<P> = mergeOptions(DEFAULTS, baseOptions)
211-
const promise = new Promise<Response<P>>((resolve, reject) => {
212-
const url = new URL(
213-
options.resource,
214-
typeof location === 'undefined' ? undefined : location.origin
215-
)
216221

222+
let timerID: ReturnType<typeof setTimeout>
223+
const promise = new Promise<Response<P>>((resolve, reject) => {
224+
const url = new URL(options.resource, options.base)
217225
url.search += options.params
218226

219227
if (options.json != null) {
220228
options.body = JSON.stringify(options.json)
221229
options.headers.set('content-type', CONTENT_TYPES.json)
222230
}
223231

224-
let timerID: ReturnType<typeof setTimeout>
225232
if (options.timeout! > 0) {
226233
const controller = new AbortController()
227234

@@ -240,14 +247,11 @@ function request<P extends Payload>(
240247
options.signal = controller.signal
241248
}
242249

243-
// Running fetch in next tick allow us to set headers after creating promise
244-
setTimeout(() =>
245-
Promise.resolve(options.onRequest(url, options))
246-
.then(() => fetch(url, options))
247-
.then((response) => Object.assign(response, { options }))
248-
.then(resolve, reject)
249-
.then(() => clearTimeout(timerID))
250-
)
250+
Promise.resolve(options.onRequest(url, options))
251+
.then(() => fetch(url, options))
252+
.then((response) => Object.assign(response, { options }))
253+
.then(resolve, reject)
254+
.then(() => clearTimeout(timerID))
251255
})
252256
.then(options.onResponse)
253257
.then(options.onSuccess, options.onFailure) as ResponsePromise<P>

0 commit comments

Comments
 (0)