diff --git a/README.md b/README.md index 07e8d67..c23400a 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,14 @@ const response = await $fetch.raw('/sushi') // ... ``` +## Native fetch + +As a shortcut, you can use `$fetch.native` that provides native `fetch` API + +```js +const json = await $fetch.native('/sushi').then(r => r.json()) +``` + ## 📦 Bundler Notes - All targets are exported with Module and CommonJS format and named exports diff --git a/cjs/index.cjs b/cjs/index.cjs index 1ce4e32..54e2e7b 100644 --- a/cjs/index.cjs +++ b/cjs/index.cjs @@ -1,6 +1,7 @@ -const getExport = name => import('../dist/index.mjs').then(r => r[name]) -const createCaller = name => (input, init) => getExport(name).then(fn => fn(input, init)) +const getExport = name => import("../dist/index.mjs").then(r => r[name]); +const createCaller = name => (input, init) => getExport(name).then(function_ => function_(input, init)); -exports.fetch = createCaller('fetch') -exports.$fetch = createCaller('$fetch') -exports.$fetch.raw = (input, init) => getExport('$fetch').then($fetch => $fetch.raw(input, init)) +exports.fetch = createCaller("fetch"); +exports.$fetch = createCaller("$fetch"); +exports.$fetch.raw = (input, init) => getExport("$fetch").then($fetch => $fetch.raw(input, init)); +exports.$fetch.native = (input, init) => getExport("$fetch").then($fetch => $fetch.native(input, init)); diff --git a/cjs/node.cjs b/cjs/node.cjs index dbd360c..ed79384 100644 --- a/cjs/node.cjs +++ b/cjs/node.cjs @@ -1,6 +1,7 @@ -const getExport = name => import('../dist/node.mjs').then(r => r[name]) -const createCaller = name => (input, init) => getExport(name).then(fn => fn(input, init)) +const getExport = name => import("../dist/node.mjs").then(r => r[name]); +const createCaller = name => (input, init) => getExport(name).then(function_ => function_(input, init)); -exports.fetch = createCaller('fetch') -exports.$fetch = createCaller('$fetch') -exports.$fetch.raw = (input, init) => getExport('$fetch').then($fetch => $fetch.raw(input, init)) +exports.fetch = createCaller("fetch"); +exports.$fetch = createCaller("$fetch"); +exports.$fetch.raw = (input, init) => getExport("$fetch").then($fetch => $fetch.raw(input, init)); +exports.$fetch.native = (input, init) => getExport("$fetch").then($fetch => $fetch.native(input, init)); diff --git a/cjs/undici.cjs b/cjs/undici.cjs index 6181f13..42005c9 100644 --- a/cjs/undici.cjs +++ b/cjs/undici.cjs @@ -1,6 +1,7 @@ -const getExport = name => import('../dist/undici.mjs').then(r => r[name]) -const createCaller = name => (input, init) => getExport(name).then(fn => fn(input, init)) +const getExport = name => import("../dist/undici.mjs").then(r => r[name]); +const createCaller = name => (input, init) => getExport(name).then(function_ => function_(input, init)); -exports.fetch = createCaller('fetch') -exports.$fetch = createCaller('$fetch') -exports.$fetch.raw = (input, init) => getExport('$fetch').then($fetch => $fetch.raw(input, init)) +exports.fetch = createCaller("fetch"); +exports.$fetch = createCaller("$fetch"); +exports.$fetch.raw = (input, init) => getExport("$fetch").then($fetch => $fetch.raw(input, init)); +exports.$fetch.native = (input, init) => getExport("$fetch").then($fetch => $fetch.native(input, init)); diff --git a/src/fetch.ts b/src/fetch.ts index e408b8e..c519b77 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -42,6 +42,7 @@ export interface FetchOptions extends Omi export interface $Fetch { (request: FetchRequest, options?: FetchOptions): Promise> raw (request: FetchRequest, options?: FetchOptions): Promise>> + native: Fetch create (defaults: FetchOptions): $Fetch } @@ -171,6 +172,8 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch { $fetch.raw = $fetchRaw; + $fetch.native = fetch; + $fetch.create = (defaultOptions = {}) => createFetch({ ...globalOptions, defaults: {