Skip to content

Commit

Permalink
feat: expose $fetch.native
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 15, 2022
1 parent 14a723f commit ff697d7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -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));
11 changes: 6 additions & 5 deletions cjs/node.cjs
Original file line number Diff line number Diff line change
@@ -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));
11 changes: 6 additions & 5 deletions cjs/undici.cjs
Original file line number Diff line number Diff line change
@@ -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));
3 changes: 3 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface FetchOptions<R extends ResponseType = ResponseType> extends Omi
export interface $Fetch {
<T = any, R extends ResponseType = "json"> (request: FetchRequest, options?: FetchOptions<R>): Promise<MappedType<R, T>>
raw<T = any, R extends ResponseType = "json"> (request: FetchRequest, options?: FetchOptions<R>): Promise<FetchResponse<MappedType<R, T>>>
native: Fetch
create (defaults: FetchOptions): $Fetch
}

Expand Down Expand Up @@ -171,6 +172,8 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {

$fetch.raw = $fetchRaw;

$fetch.native = fetch;

$fetch.create = (defaultOptions = {}) => createFetch({
...globalOptions,
defaults: {
Expand Down

0 comments on commit ff697d7

Please sign in to comment.