{"name":"ohmyfetch","version":"0.4.2","description":"oh-my-fetch","repository":{"type":"git","url":"git+https://github.com/unjs/ohmyfetch.git"},"license":"MIT","sideEffects":false,"type":"module","exports":{".":{"node":{"import":"./dist/node.mjs","require":"./cjs/node.cjs"},"default":{"import":"./dist/index.mjs","require":"./cjs/index.cjs"}},"./node":{"import":"./dist/node.mjs","require":"./cjs/node.cjs"}},"main":"./cjs/node.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","scripts":{"build":"unbuild","prepack":"yarn build","lint":"eslint --ext .ts .","play":"jiti playground/index.ts","release":"yarn test && standard-version && git push --follow-tags && npm publish","test":"yarn build && mocha ./test/*.test.*"},"dependencies":{"destr":"^1.1.0","node-fetch":"^3.0.0","ufo":"^0.7.9"},"devDependencies":{"@nuxtjs/eslint-config-typescript":"latest","@types/flat":"latest","@types/mocha":"^9.0.0","@types/node":"latest","@types/node-fetch":"^3.0.3","chai":"^4.3.4","eslint":"^7","h3":"latest","jiti":"latest","listhen":"latest","mocha":"^9.1.3","standard-version":"latest","typescript":"latest","unbuild":"latest"},"gitHead":"788d24745ae4aa12a3478518a9c63ee16ad10454","bugs":{"url":"https://github.com/unjs/ohmyfetch/issues"},"homepage":"https://github.com/unjs/ohmyfetch#readme","_id":"ohmyfetch@0.4.2","_nodeVersion":"14.18.1","_npmVersion":"8.1.0","dist":{"integrity":"sha512-wlZ12IB4L8qvr7KBnA4MdqqoUovgfOKUJXoZMSwrTp54jTs20LENDoFXmyW0Qwg9fydrB/sT2qD8a/0iFehQOw==","shasum":"955cc560ccb2bb99fc9de22552332fc6c208364e","tarball":"http://registry-npm.ywwl.com/ohmyfetch/download/ohmyfetch-0.4.2.tgz","fileCount":11,"unpackedSize":14270,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBnVg2uDORiIxD6/G4RCGL3PDmVh8OnO5kYTXwYmchctAiA7Xd6pPU9R44/D+cDLQhMB3eKQFIGGNKKQLkHSMIHlMQ=="}],"size":5374,"noattachment":false,"key":"/ohmyfetch/-/ohmyfetch-0.4.2.tgz"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"directories":{},"maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ohmyfetch_0.4.2_1634921355291_0.3452680570385642"},"_hasShrinkwrap":false,"deprecated":"Package renamed to https://github.com/unjs/ofetch","readme":"\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![Github Actions][github-actions-src]][github-actions-href]\n[![Codecov][codecov-src]][codecov-href]\n[![bundle][bundle-src]][bundle-href]\n\n![😱 ohmyfetch](.github/banner.svg)\n\n## 🚀 Quick Start\n\nInstall:\n\n```bash\n# npm\nnpm i ohmyfetch\n\n# yarn\nyarn add ohmyfetch\n```\n\nImport:\n\n```js\n// ESM / Typescript\nimport { $fetch } from 'ohmyfetch'\n\n// CommonJS\nconst { $fetch } = require('ohmyfetch')\n```\n\n<details>\n  <summary>Spoiler</summary>\n  <img src=\"https://media.giphy.com/media/Dn1QRA9hqMcoMz9zVZ/giphy.gif\">\n</details>\n\n## ✔️ Works with Node.js\n\nWe use [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) to detect Node.js\n and automatically use [unjs/node-fetch-native](https://github.com/unjs/node-fetch-native). If `globalThis.fetch` is available, will be used instead. To leverage Node.js 17.5.0 experimental native fetch API use [`--experimental-fetch` flag](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch).\n\n### `undici` support\n\nIn order to use experimental fetch implementation from [nodejs/undici](https://github.com/nodejs/undici), You can import from `ohmyfetch/undici`.\n\n```js\nimport { $fetch } from 'ohmyfetch/undici'\n```\n\nOn Node.js versions older than `16.5`, node-fetch will be used as the fallback.\n\n### `keepAlive` support\n\nBy setting the `FETCH_KEEP_ALIVE` environment variable to `true`, an http/https agent will be registered that keeps sockets around even when there are no outstanding requests, so they can be used for future requests without having to reestablish a TCP connection.\n\n**Note:** This option can potentially introduce memory leaks. Please check [node-fetch/node-fetch#1325](https://github.com/node-fetch/node-fetch/pull/1325).\n\n## ✔️ Parsing Response\n\n`$fetch` will smartly parse JSON and native values using [destr](https://github.com/unjs/destr), falling back to text if it fails to parse.\n\n```js\nconst { users } = await $fetch('/api/users')\n```\n\nFor binary content types, `$fetch` will instead return a `Blob` object.\n\nYou can optionally provide a different parser than destr, or specify `blob`, `arrayBuffer` or `text` to force parsing the body with the respective `FetchResponse` method.\n\n```js\n// Use JSON.parse\nawait $fetch('/movie?lang=en', { parseResponse: JSON.parse })\n\n// Return text as is\nawait $fetch('/movie?lang=en', { parseResponse: txt => txt })\n\n// Get the blob version of the response\nawait $fetch('/api/generate-image', { responseType: 'blob' })\n```\n\n## ✔️ JSON Body\n\n`$fetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` and `Accept` headers (for `put`, `patch` and `post` requests).\n\n```js\nconst { users } = await $fetch('/api/users', { method: 'POST', body: { some: 'json' } })\n```\n\n## ✔️ Handling Errors\n\n`$fetch` Automatically throw errors when `response.ok` is `false` with a friendly error message and compact stack (hiding internals).\n\nParsed error body is available with `error.data`. You may also use `FetchError` type.\n\n```ts\nawait $fetch('http://google.com/404')\n// FetchError: 404 Not Found (http://google.com/404)\n//     at async main (/project/playground.ts:4:3)\n```\n\nIn order to bypass errors as response you can use `error.data`:\n\n```ts\nawait $fetch(...).catch((error) => error.data)\n```\n\n## ✔️ Auto Retry\n\n`$fetch` Automatically retries the request if an error happens. Default is `1` (except for `POST`, `PUT` and `PATCH` methods that is `0`)\n\n```ts\nawait $fetch('http://google.com/404', {\n  retry: 3\n})\n```\n\n## ✔️ Type Friendly\n\nResponse can be type assisted:\n\n```ts\nconst article = await $fetch<Article>(`/api/article/${id}`)\n// Auto complete working with article.id\n```\n\n## ✔️ Adding `baseURL`\n\nBy 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):\n\n```js\nawait $fetch('/config', { baseURL })\n```\n\n## ✔️ Adding Query Search Params\n\nBy 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):\n\n```js\nawait $fetch('/movie?lang=en', { query: { id: 123 } })\n```\n\n## ✔️ Interceptors\n\nIt is possible to provide async interceptors to hook into lifecycle events of `$fetch` call.\n\nYou might want to use `$fetch.create` to set set shared interceptors.\n\n### `onRequest({ request, options })`\n\n`onRequest` is called as soon as `$fetch` is being called, allowing to modify options or just do simple logging.\n\n```js\nawait $fetch('/api', {\n  async onRequest({ request, options }) {\n    // Log request\n    console.log('[fetch request]', request, options)\n\n    // Add `?t=1640125211170` to query search params\n    options.query = options.query || {}\n    options.query.t = new Date()\n  }\n})\n```\n\n### `onRequestError({ request, options, error })`\n\n`onRequestError` will be called when fetch request fails.\n\n```js\nawait $fetch('/api', {\n  async onRequestError({ request, options, error }) {\n    // Log error\n    console.log('[fetch request error]', request, error)\n  }\n})\n```\n\n\n### `onResponse({ request, options, response })`\n\n`onResponse` will be called after `fetch` call and parsing body.\n\n```js\nawait $fetch('/api', {\n  async onResponse({ request, response, options }) {\n    // Log response\n    console.log('[fetch response]', request, response.status, response.body)\n  }\n})\n```\n\n### `onResponseError({ request, options, response })`\n\n`onResponseError` is same as `onResponse` but will be called when fetch happens but `response.ok` is not `true`.\n\n```js\nawait $fetch('/api', {\n  async onResponseError({ request, response, options }) {\n    // Log error\n    console.log('[fetch response error]', request, response.status, response.body)\n  }\n})\n```\n\n## ✔️ Create fetch with default options\n\nThis utility is useful if you need to use common options across serveral fetch calls.\n\n**Note:** Defaults will be cloned at one level and inherrited. Be careful about nested options like `headers`.\n\n```js\nconst apiFetch = $fetch.create({ baseURL: '/api' })\n\napiFetch('/test') // Same as $fetch('/test', { baseURL: '/api' })\n```\n\n## 💡 Adding headers\n\nBy using `headers` option, `$fetch` adds extra headers in addition to the request default headers:\n\n```js\nawait $fetch('/movies', {\n  headers: {\n    Accept: 'application/json',\n    'Cache-Control': 'no-cache'\n  }\n})\n```\n\n## 🍣 Access to Raw Response\n\nIf you need to access raw response (for headers, etc), can use `$fetch.raw`:\n\n```js\nconst response = await $fetch.raw('/sushi')\n\n// response.data\n// response.headers\n// ...\n```\n\n## 📦 Bundler Notes\n\n- All targets are exported with Module and CommonJS format and named exports\n- No export is transpiled for sake of modern syntax\n  - You probably need to transpile `ohmyfetch`, `destr` and `ufo` packages with babel for ES5 support\n- You need to polyfill `fetch` global for supporting legacy browsers like using [unfetch](https://github.com/developit/unfetch)\n\n## ❓ FAQ\n\n**Why export is called `$fetch` instead of `fetch`?**\n\nUsing 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 `ohmyfetch` which is auto polyfilled for Node.js and using native otherwise.\n\n**Why not having default export?**\n\nDefault exports are always risky to be mixed with CommonJS exports.\n\nThis also guarantees we can introduce more utils without breaking the package and also encourage using `$fetch` name.\n\n**Why not transpiled?**\n\nBy keep transpiling libraries we push web backward with legacy code which is unneeded for most of the users.\n\nIf you need to support legacy users, you can optionally transpile the library in your build pipeline.\n\n## License\n\nMIT. Made with 💖\n\n<!-- Badges -->\n[npm-version-src]: https://img.shields.io/npm/v/ohmyfetch?style=flat-square\n[npm-version-href]: https://npmjs.com/package/ohmyfetch\n\n[npm-downloads-src]: https://img.shields.io/npm/dm/ohmyfetch?style=flat-square\n[npm-downloads-href]: https://npmjs.com/package/ohmyfetch\n\n[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/ohmyfetch/ci/main?style=flat-square\n[github-actions-href]: https://github.com/unjs/ohmyfetch/actions?query=workflow%3Aci\n\n[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/ohmyfetch/main?style=flat-square\n[codecov-href]: https://codecov.io/gh/unjs/ohmyfetch\n\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/ohmyfetch?style=flat-square\n[bundle-href]: https://bundlephobia.com/result?p=ohmyfetch\n","publish_time":1634921355646,"_cnpm_publish_time":1634921355646,"dist-tags":{"latest":"0.4.21"}}