Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ofetch

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ofetch - npm Package Compare versions

Comparing version 0.4.21 to 1.0.0

dist/error-8a55452d.d.ts

7

dist/index.d.ts

@@ -1,3 +0,3 @@

import { $ as $Fetch } from './error-ba32121a.js';
export { $ as $Fetch, C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-ba32121a.js';
import { $ as $Fetch } from './error-8a55452d.js';
export { $ as $Fetch, C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-8a55452d.js';

@@ -9,4 +9,5 @@ declare const fetch: typeof globalThis.fetch;

};
declare const ofetch: $Fetch;
declare const $fetch: $Fetch;
export { $fetch, Headers, fetch };
export { $fetch, Headers, fetch, ofetch };

@@ -1,3 +0,3 @@

import { $ as $Fetch } from './error-ba32121a.js';
export { $ as $Fetch, C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-ba32121a.js';
import { $ as $Fetch } from './error-8a55452d.js';
export { $ as $Fetch, C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-8a55452d.js';

@@ -10,4 +10,5 @@ declare function createNodeFetch(): (input: RequestInfo, init?: RequestInit) => any;

};
declare const ofetch: $Fetch;
declare const $fetch: $Fetch;
export { $fetch, Headers, createNodeFetch, fetch };
export { $fetch, Headers, createNodeFetch, fetch, ofetch };
{
"name": "ofetch",
"version": "0.4.21",
"description": "oh-my-fetch",
"version": "1.0.0",
"description": "A better fetch API. Works on node, browser and workers.",
"repository": "unjs/ofetch",

@@ -26,7 +26,2 @@ "license": "MIT",

"require": "./cjs/node.cjs"
},
"./undici": {
"types": "./dist/undici.d.ts",
"import": "./dist/undici.mjs",
"require": "./cjs/undici.cjs"
}

@@ -40,3 +35,2 @@ },

"node.d.ts",
"undici.d.ts",
"cjs"

@@ -56,4 +50,3 @@ ],

"node-fetch-native": "^1.0.1",
"ufo": "^1.0.0",
"undici": "^5.12.0"
"ufo": "^1.0.0"
},

@@ -60,0 +53,0 @@ "devDependencies": {

@@ -9,3 +9,3 @@ # ofetch

> A better fetch API
> A better fetch API. Works on node, browser and workers.

@@ -28,6 +28,6 @@ ## 🚀 Quick Start

// ESM / Typescript
import { $fetch } from 'ofetch'
import { ofetch } from 'ofetch'
// CommonJS
const { $fetch } = require('ofetch')
const { ofetch } = require('ofetch')
```

@@ -45,12 +45,2 @@

### `undici` support
In order to use experimental fetch implementation from [nodejs/undici](https://github.com/nodejs/undici), You can import from `ofetch/undici`.
```js
import { $fetch } from 'ofetch/undici'
```
On Node.js versions older than `16.5`, node-fetch will be used as the fallback.
### `keepAlive` support

@@ -64,9 +54,9 @@

`$fetch` will smartly parse JSON and native values using [destr](https://github.com/unjs/destr), falling back to text if it fails to parse.
`ofetch` will smartly parse JSON and native values using [destr](https://github.com/unjs/destr), falling back to text if it fails to parse.
```js
const { users } = await $fetch('/api/users')
const { users } = await ofetch('/api/users')
```
For binary content types, `$fetch` will instead return a `Blob` object.
For binary content types, `ofetch` will instead return a `Blob` object.

@@ -77,9 +67,9 @@ You can optionally provide a different parser than destr, or specify `blob`, `arrayBuffer` or `text` to force parsing the body with the respective `FetchResponse` method.

// Use JSON.parse
await $fetch('/movie?lang=en', { parseResponse: JSON.parse })
await ofetch('/movie?lang=en', { parseResponse: JSON.parse })
// Return text as is
await $fetch('/movie?lang=en', { parseResponse: txt => txt })
await ofetch('/movie?lang=en', { parseResponse: txt => txt })
// Get the blob version of the response
await $fetch('/api/generate-image', { responseType: 'blob' })
await ofetch('/api/generate-image', { responseType: 'blob' })
```

@@ -89,6 +79,6 @@

`$fetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` and `Accept` headers (for `put`, `patch` and `post` requests).
`ofetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` and `Accept` headers (for `put`, `patch` and `post` requests).
```js
const { users } = await $fetch('/api/users', { method: 'POST', body: { some: 'json' } })
const { users } = await ofetch('/api/users', { method: 'POST', body: { some: 'json' } })
```

@@ -98,3 +88,3 @@

`$fetch` Automatically throw errors when `response.ok` is `false` with a friendly error message and compact stack (hiding internals).
`ofetch` Automatically throw errors when `response.ok` is `false` with a friendly error message and compact stack (hiding internals).

@@ -104,3 +94,3 @@ Parsed error body is available with `error.data`. You may also use `FetchError` type.

```ts
await $fetch('http://google.com/404')
await ofetch('http://google.com/404')
// FetchError: 404 Not Found (http://google.com/404)

@@ -113,3 +103,3 @@ // at async main (/project/playground.ts:4:3)

```ts
await $fetch(...).catch((error) => error.data)
await ofetch(...).catch((error) => error.data)
```

@@ -119,6 +109,6 @@

`$fetch` Automatically retries the request if an error happens. Default is `1` (except for `POST`, `PUT` and `PATCH` methods that is `0`)
`ofetch` Automatically retries the request if an error happens. Default is `1` (except for `POST`, `PUT` and `PATCH` methods that is `0`)
```ts
await $fetch('http://google.com/404', {
await ofetch('http://google.com/404', {
retry: 3

@@ -133,3 +123,3 @@ })

```ts
const article = await $fetch<Article>(`/api/article/${id}`)
const article = await ofetch<Article>(`/api/article/${id}`)
// Auto complete working with article.id

@@ -140,6 +130,6 @@ ```

By using `baseURL` option, `$fetch` prepends it with respecting to trailing/leading slashes and query search params for baseURL using [ufo](https://github.com/unjs/ufo):
By using `baseURL` option, `ofetch` prepends it with respecting to trailing/leading slashes and query search params for baseURL using [ufo](https://github.com/unjs/ufo):
```js
await $fetch('/config', { baseURL })
await ofetch('/config', { baseURL })
```

@@ -149,6 +139,6 @@

By using `query` option (or `params` as alias), `$fetch` adds query search params to URL by preserving query in request itself using [ufo](https://github.com/unjs/ufo):
By using `query` option (or `params` as alias), `ofetch` adds query search params to URL by preserving query in request itself using [ufo](https://github.com/unjs/ufo):
```js
await $fetch('/movie?lang=en', { query: { id: 123 } })
await ofetch('/movie?lang=en', { query: { id: 123 } })
```

@@ -158,12 +148,12 @@

It is possible to provide async interceptors to hook into lifecycle events of `$fetch` call.
It is possible to provide async interceptors to hook into lifecycle events of `ofetch` call.
You might want to use `$fetch.create` to set set shared interceptors.
You might want to use `ofetch.create` to set set shared interceptors.
### `onRequest({ request, options })`
`onRequest` is called as soon as `$fetch` is being called, allowing to modify options or just do simple logging.
`onRequest` is called as soon as `ofetch` is being called, allowing to modify options or just do simple logging.
```js
await $fetch('/api', {
await ofetch('/api', {
async onRequest({ request, options }) {

@@ -185,3 +175,3 @@ // Log request

```js
await $fetch('/api', {
await ofetch('/api', {
async onRequestError({ request, options, error }) {

@@ -200,3 +190,3 @@ // Log error

```js
await $fetch('/api', {
await ofetch('/api', {
async onResponse({ request, response, options }) {

@@ -214,3 +204,3 @@ // Log response

```js
await $fetch('/api', {
await ofetch('/api', {
async onResponseError({ request, response, options }) {

@@ -230,5 +220,5 @@ // Log error

```js
const apiFetch = $fetch.create({ baseURL: '/api' })
const apiFetch = ofetch.create({ baseURL: '/api' })
apiFetch('/test') // Same as $fetch('/test', { baseURL: '/api' })
apiFetch('/test') // Same as ofetch('/test', { baseURL: '/api' })
```

@@ -238,6 +228,6 @@

By using `headers` option, `$fetch` adds extra headers in addition to the request default headers:
By using `headers` option, `ofetch` adds extra headers in addition to the request default headers:
```js
await $fetch('/movies', {
await ofetch('/movies', {
headers: {

@@ -252,6 +242,6 @@ Accept: 'application/json',

If you need to access raw response (for headers, etc), can use `$fetch.raw`:
If you need to access raw response (for headers, etc), can use `ofetch.raw`:
```js
const response = await $fetch.raw('/sushi')
const response = await ofetch.raw('/sushi')

@@ -263,2 +253,10 @@ // response.data

## Native fetch
As a shortcut, you can use `ofetch.native` that provides native `fetch` API
```js
const json = await ofetch.native('/sushi').then(r => r.json())
```
## 📦 Bundler Notes

@@ -273,3 +271,3 @@

**Why export is called `$fetch` instead of `fetch`?**
**Why export is called `ofetch` instead of `fetch`?**

@@ -282,3 +280,3 @@ Using the same name of `fetch` can be confusing since API is different but still it is a fetch so using closest possible alternative. You can however, import `{ fetch }` from `ofetch` which is auto polyfilled for Node.js and using native otherwise.

This also guarantees we can introduce more utils without breaking the package and also encourage using `$fetch` name.
This also guarantees we can introduce more utils without breaking the package and also encourage using `ofetch` name.

@@ -285,0 +283,0 @@ **Why not transpiled?**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc