-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathindex.ts
35 lines (29 loc) · 1.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { createFetch } from "./base";
export * from "./base";
export type * from "./types";
// ref: https://github.com/tc39/proposal-global
const _globalThis = (function () {
if (typeof globalThis !== "undefined") {
return globalThis;
}
/* eslint-disable unicorn/prefer-global-this */
if (typeof self !== "undefined") {
return self;
}
if (typeof window !== "undefined") {
return window;
}
if (typeof global !== "undefined") {
return global;
}
/* eslint-enable unicorn/prefer-global-this */
throw new Error("unable to locate global object");
})();
// ref: https://github.com/unjs/ofetch/issues/295
export const fetch = _globalThis.fetch
? (...args: Parameters<typeof globalThis.fetch>) => _globalThis.fetch(...args)
: () => Promise.reject(new Error("[ofetch] global.fetch is not supported!"));
export const Headers = _globalThis.Headers;
export const AbortController = _globalThis.AbortController;
export const ofetch = createFetch({ fetch, Headers, AbortController });
export const $fetch = ofetch;