{"_id":"@atuttle/smart-fetch","_rev":"4-f0f522dd72276c4a03fdf82affffc877","name":"@atuttle/smart-fetch","dist-tags":{"latest":"2.0.1"},"versions":{"1.0.0":{"name":"@atuttle/smart-fetch","version":"1.0.0","keywords":[],"author":{"name":"Adam Tuttle","email":"https://github.com/atuttle/"},"license":"MIT","_id":"@atuttle/smart-fetch@1.0.0","maintainers":[{"name":"atuttle","email":"adam@fusiongrokker.com"}],"homepage":"https://github.com/atuttle/smart-fetch#readme","bugs":{"url":"https://github.com/atuttle/smart-fetch/issues"},"dist":{"shasum":"1f26250444b6ee77470475457bc4c89f6615eff6","tarball":"https://registry.npmjs.org/@atuttle/smart-fetch/-/smart-fetch-1.0.0.tgz","fileCount":7,"integrity":"sha512-Fdj2KIY/Cvnml3aI2tfAfLBLq7eiSFhY3ZJFthqdhy5gxv4pJAjj8fEvx3cbjK18ENHcgOkVTvxSXLGqpZxXbQ==","signatures":[{"sig":"MEYCIQDOuppxVxle2gOfxCcBHSBcifnIFlVQtpBj+Xfcsj74vQIhAOf1DC7W52kKeKbzduptZLuNtpo6FdtulVLa+loCcie3","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":8332,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJetHq0CRA9TVsSAnZWagAAZiQP/38m2SoyDeV+Uw77Oard\nj7uq3kX/AoXzn4yr8ypPtsvslUBN9jpviR53WBU2Hg9s9V/wR0hdaLjISw7G\nmCusT5cB2SEvmGdWsL9MDmPifwfN1DotNBlsDKJFqg9DNEzQ/hD4Mf99OlRa\nHqKRqquTEHCFTv79BxKyevt7KWK7mANb9r5wf7CyKBNE+j2NZKYyWjXCup76\ngEaJ3FM7NH6lmoaQqQqXYyeJETrEVl+XO1SxrsTiTz0JcNDfscjeKi1Y7NtI\n9rP1xBjNSVhysAyE+1Zw8f/46WhNCwskQEv61ec4XJDk3OQNPImfsDL+5nv8\ncdZcU5YCl3Fv9Gbn00YHyKiK/4dSbxw3QSdU7aK0lCC1GdKmobOaoyD6+2he\n5VB5uj6CTKbkwqGD+LPb80zq6AQ7YCXHrMIEUEOVXdeyNyMPaOtnutnK3oOW\nf2h7tYLFlHZlP8Hj44bL+1bdClHh6z8PbjwqCnW3Tt3kCpJrAFi00djr1k6Z\nbuOnnDkM+FfAmv5uqSUpbs3OAh8J/E6WP6BPM3gScZpVjoHbRwUARqbS5pqQ\nHCBq3k2C8vX2Z/GyIAO6P8ILrXIjKsfms2ccBh6M3QbykXG3eUHsVFszIs+g\no/UCLNx30RRhtFzOVVk/NLaf0s6cM1o8TFfxd2zE3TT19e3+Y/BEPXRzfhg2\n5HL9\r\n=GEFM\r\n-----END PGP SIGNATURE-----\r\n"},"main":"smart-fetch.js","gitHead":"980bfce9e6b92ec1721369aedc6fafc2a564bf04","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"atuttle","email":"adam@fusiongrokker.com"},"repository":{"url":"git+https://github.com/atuttle/smart-fetch.git","type":"git"},"_npmVersion":"6.14.4","description":"isomorphic fetch w/smart defaults, timeout support, and easy micro-api-client generation","directories":{},"_nodeVersion":"12.16.1","dependencies":{"debug":"^4.1.1","abort-controller":"^3.0.0","isomorphic-fetch":"^2.2.1"},"_hasShrinkwrap":false,"devDependencies":{},"_npmOperationalInternal":{"tmp":"tmp/smart-fetch_1.0.0_1588886195644_0.751574628454299","host":"s3://npm-registry-packages"}},"2.0.0":{"name":"@atuttle/smart-fetch","version":"2.0.0","keywords":[],"author":{"name":"Adam Tuttle","email":"https://github.com/atuttle/"},"license":"MIT","_id":"@atuttle/smart-fetch@2.0.0","maintainers":[{"name":"atuttle","email":"adam@fusiongrokker.com"}],"homepage":"https://github.com/atuttle/smart-fetch#readme","bugs":{"url":"https://github.com/atuttle/smart-fetch/issues"},"dist":{"shasum":"5f0b904130b3b4615341d0fedcb5a964a0541bc3","tarball":"https://registry.npmjs.org/@atuttle/smart-fetch/-/smart-fetch-2.0.0.tgz","fileCount":8,"integrity":"sha512-GYRH2VhEVpIvSpADcL5tRKMM9EEUAsFXY4j8L1N6yeynCsyaa71JasgX2GpEOzEfPk9cWBVNRMmtgt4CrsqxUA==","signatures":[{"sig":"MEYCIQCKYCFFdsVrv0QB/oNCtBsCa92Nh0y05Y25vgwkpMTydgIhALfi28FLi1dYx5l/9+zVO70R+C0dTgt9EXV/s1YK+Wga","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":10067,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfdhDcCRA9TVsSAnZWagAAnZUP/1Zvjf9Lv1KKM6pIgWE7\nUSZG3dRh3x1uhwxiElT6M239MlsrrsjoOIBOi2BylzOpz2L24HWLCZV22Toa\nI1V+02C/G0VAp/HIDt+2gja3aSgrsuVzXWrFfz6wNcex+9q4sRzGn9xeuPwh\n/lZVuHUk6jI0/OsrcP0alILMBMFWwDtiXKmyKwLfUp52qI8XLUf1fkqQKfYk\nIA+S66d7TQak+d2fn7ePiRC3yA0TUNYtprKbXFfP2mzv4J4lRso76i2WVGXl\nP3raZ1tM4kNeEWxKIeVy0ffMSWExa/sjw1rzQl6MNozj83WZrubCE97c1436\nhDi1zetb+CaudYSKFxzCx5rZ3ayuugv/JvPYdc7u5AIIcDVbmj4wZy6kel+D\n2lpIGj7I0mVF+A/7RGloMWxiV4NREQI9nInmPbIPxekQAS0SpxeWAZeeW8YR\nhH62ww33skeLFEa+O19uTjS/+SJbj1r5ma5WFqzcr7M+xrfU9vlNASsYjsxq\nAwiffX1OERAmxu4vLa6O37WvrO+GZSMLGmxeSK/IaZRbpGE2e0tCCuVkWUFz\nZt+SqzKw9W3ANqrhmgMvnEG0/5TnpN9Qupql2qilSTKNN5ZeuhE746nm9t5b\n2+d/b2gaVdirv7lP9ysdp6FW5HFxGelxSDlPN+v4G9BaaHSzltiEMAe0FRR4\ni/ty\r\n=VKvm\r\n-----END PGP SIGNATURE-----\r\n"},"main":"smart-fetch.js","gitHead":"695e10641784424773a62ac1b0a2c7f8657606e6","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"atuttle","email":"adam@fusiongrokker.com"},"repository":{"url":"git+https://github.com/atuttle/smart-fetch.git","type":"git"},"_npmVersion":"6.14.8","description":"isomorphic fetch w/smart defaults, timeout support, and easy micro-api-client generation","directories":{},"_nodeVersion":"14.10.1","dependencies":{"debug":"^4.1.1","abort-controller":"^3.0.0","isomorphic-fetch":"^3.0.0"},"_hasShrinkwrap":false,"devDependencies":{},"_npmOperationalInternal":{"tmp":"tmp/smart-fetch_2.0.0_1601573083900_0.09970289800735044","host":"s3://npm-registry-packages"}},"2.0.1":{"name":"@atuttle/smart-fetch","version":"2.0.1","description":"isomorphic fetch w/smart defaults, timeout support, and easy micro-api-client generation","main":"smart-fetch.js","scripts":{"test":"jest","test:watch":"jest --watch"},"keywords":[],"author":{"name":"Adam Tuttle","email":"https://github.com/atuttle/"},"license":"MIT","dependencies":{"abort-controller":"^3.0.0","debug":"^4.4.0","isomorphic-fetch":"^3.0.0"},"devDependencies":{"jest":"^29.7.0"},"repository":{"type":"git","url":"git+https://github.com/atuttle/smart-fetch.git"},"bugs":{"url":"https://github.com/atuttle/smart-fetch/issues"},"homepage":"https://github.com/atuttle/smart-fetch#readme","_id":"@atuttle/smart-fetch@2.0.1","gitHead":"308ea3df9d4a75917b796a89935f021d23aaa7d2","_nodeVersion":"22.14.0","_npmVersion":"11.2.0","dist":{"integrity":"sha512-ONr7Q/iL9q98/vfRIHuw2XfJ6QTqTuQfIvnTLvonxTRf+Na/gaaUcJBipDBZ5xwHro4YT+Vzt7N5pRdHm+7rkA==","shasum":"76fe22a176fe5bb6cfb2fbc5b862b15c2283be6f","tarball":"https://registry.npmjs.org/@atuttle/smart-fetch/-/smart-fetch-2.0.1.tgz","fileCount":11,"unpackedSize":12461,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQChXsKIQys5gEwpI3oi5LdkEqjPtn5NoHgQbsiR4GarlgIhAL4MYHwiMa2vwrwxR8wAGtn5yydpV2gFw8S/vmApp/MB"}]},"_npmUser":{"name":"atuttle","email":"adam@fusiongrokker.com"},"directories":{},"maintainers":[{"name":"atuttle","email":"adam@fusiongrokker.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/smart-fetch_2.0.1_1741700855001_0.02417926574417839"},"_hasShrinkwrap":false}},"time":{"created":"2020-05-07T21:16:35.610Z","modified":"2025-03-11T13:47:35.316Z","1.0.0":"2020-05-07T21:16:35.818Z","2.0.0":"2020-10-01T17:24:44.030Z","2.0.1":"2025-03-11T13:47:35.161Z"},"bugs":{"url":"https://github.com/atuttle/smart-fetch/issues"},"author":{"name":"Adam Tuttle","email":"https://github.com/atuttle/"},"license":"MIT","homepage":"https://github.com/atuttle/smart-fetch#readme","keywords":[],"repository":{"type":"git","url":"git+https://github.com/atuttle/smart-fetch.git"},"description":"isomorphic fetch w/smart defaults, timeout support, and easy micro-api-client generation","maintainers":[{"name":"atuttle","email":"adam@fusiongrokker.com"}],"readme":"# smart-fetch\n<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)\n<!-- ALL-CONTRIBUTORS-BADGE:END -->\n\nBased heavily on [this blog post by Kent C. Dodds][blog]; smart-fetch is a wrapper for [isomorphic-fetch][iso] that adds a few features to make it do the smart thing by default, and a bit more.\n\nAll changes here are non-destructively added onto the existing native Fetch api. The 1st argument is the URL (or resource that should be appended to the base url, if using the micro-client generated by the wrapper); and the second argument is your fetch options.\n\nLikewise, the wrapper takes the base url as its 1st argument, and options to use as defaults for all requests as the 2nd argument.\n\nAll current and future native fetch options are supported because they pass through smart-fetch into the same position of a native fetch usage.\n\n## Improvements from Kent\n\n- Everything is overridable, but...\n- A request body implies that the method should be `POST`\n- Rejects if `!response.ok` - response body still sent as the rejection object\n- API-friendly default Content-Type: `application/json`\n- A wrapper function that allows you to create api micro-clients with a base url and default configuration, removing as much boilerplate as possible from individual requests.\n\nI did not incorporate Kent's suggestion to bake in Bearer token headers using a localStorage key, but you could set that using the included wrapper function.\n\n## Improvements from me\n\n- Request timeout support using [AbortController][ac]. In the event of a timeout, smart-fetch will reject and the rejection error will have type `request-timeout`. By default there is no timeout.\n- Assumes the response will be JSON, but if it fails to parse then you can still get it as plain text. I've always hated that `response.text()` fails if you've already tried `response.json()`!\n- Use [debug][debug] to offer efficient async optional debug output of requests made. Use env var `DEBUG=smart-fetch` to see the debug output.\n\n# Install\n\n    npm install @atuttle/smart-fetch\n\n# Usage\n\n```js\nconst { fetch } = require('@atuttle/smart-fetch');\n\nconst data = await fetch('https://api.example.com/foo/bar', {\n\ttimeout: 5000, //ms\n\theaders: {\n\t\tapikey: 'keyboard-cat'\n\t}\n});\n```\n\nBecause there is no request body, the request method will be GET. If the api responds with JSON, you get the parsed JSON object back, else you get the response body back as plain text.\n\n```js\nconst { wrapFetch } = require('@atuttle/smart-fetch');\n\nconst myApi = wrapFetch('https://api.example.com', {\n\ttimeout: 10000, //ms\n\theaders: {\n\t\tapikey: 'keyboard-cat'\n\t}\n});\nconst data = await myApi('/foo/bar', { timeout: 5000 });\n```\n\nHere we've created a simple api client for an api at api.example.com, that will include an apikey header and use a 10 second timeout for every request... unless you override them at the time of the request. The request that follows overrides the timeout to 5 seconds, but still inherits the default apikey.\n\n# License\n\nisomorphic-fetch, which does the majority of the work, is released under the MIT License. Likewise, smart-fetch is open source under the MIT License. See the LICENSE file for details.\n\n[blog]: https://kentcdodds.com/blog/replace-axios-with-a-simple-custom-fetch-wrapper\n[iso]: https://github.com/matthew-andrews/isomorphic-fetch\n[ac]: https://github.com/mysticatea/abort-controller\n[debug]: https://github.com/visionmedia/debug\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->\n<!-- prettier-ignore-start -->\n<!-- markdownlint-disable -->\n<table>\n  <tr>\n    <td align=\"center\"><a href=\"http://adamtuttle.codes\"><img src=\"https://avatars2.githubusercontent.com/u/46990?v=4\" width=\"100px;\" alt=\"\"/><br /><sub><b>Adam Tuttle</b></sub></a><br /><a href=\"https://github.com/atuttle/smart-fetch/commits?author=atuttle\" title=\"Code\">💻</a> <a href=\"https://github.com/atuttle/smart-fetch/commits?author=atuttle\" title=\"Documentation\">📖</a></td>\n  </tr>\n</table>\n\n<!-- markdownlint-enable -->\n<!-- prettier-ignore-end -->\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!","readmeFilename":"README.md"}