{"name":"micro-ftch","version":"0.2.0","keywords":["fetch","http","https","node","browser","ajax","request"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"micro-ftch@0.2.0","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"dist":{"shasum":"67b2a4138c066328ed8765c2adf5f84b93e8b85d","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-0.2.0.tgz","fileCount":5,"integrity":"sha512-WEWd4lYSlKNwqaOla9MP8XG/F78wW3m4fXdyOQUKrNKIhG4LF0Z2JBbJdwVTO3+xOdO5LcOmXV3t1aPnjs28UQ==","signatures":[{"sig":"MEUCIDYxp5BZhKo41kf2f0GQLVe8GTSKURjPc1ygkEUXSd2TAiEA6a6SUNCRG4nY/JmiJxq29+4aIkEfbpm21R3R+kUbzoo=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":8147,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJguukuCRA9TVsSAnZWagAAYiYQAIsE0zCLnx7aCNR4hxeo\nHC5Z77HhT1uOEEdH3zbSs3Aw6jPa+l3rVyqY6cOjrKsaao7YbFTiQMGKLzFQ\nedo64iX553LnhbmVuTdJg9ztNJu/0Nk8FnE49DK7NFGuTUxFRRQx0QMTWuZW\nn9qZujfxel5Zu6a55X4oxgfKiSC3XvT/vHc7Gm6X3GTyLD6I1JaUgFnGbzZw\nc+Ue5oX45FqbQRx5yQ8WnXU/pwonMYixBV5d3kw+1pItph8ndp6GBVPn9WWy\nDjDLw5AswzXK0X7sJgYPCFDo/PvklS1XWfeAE2kY6jckGpXuSQsBcDvKf0BH\nyFIsCJxI5BOCgAqBY5R6xciA6iVdgvrLQCT19CBmG1bkmlvEn4mq1igeoME0\nvyOz5HqUk9sjd4P0i3WlvfF73DgoM0RAtcVDOhZZprpJJZHoKMBKD+97oyPD\nbQuRmeXY294pDpS2EZl4dBPc6Fw3xCOZiL0jr70HUMwiriNLZQNG7nDQBT/H\nkytOXjhSc55HNDz/UVAKeagmlxIpkaxGlocIG6Fopn77b/Vr8J/G0WTx0eoY\nzqsEoVy663ykuhuyO6Fl773zwbx5ah3vC9mA99hjekGHzrosbkeP0iK0IAK+\n9FZZi/sVAFGKUT2gsUkQqqJsGfoB/JUUUHmGfDJyRjCryzyNEuBJN7VPqJ+Q\nQVko\r\n=MXo4\r\n-----END PGP SIGNATURE-----\r\n","size":3230,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-0.2.0.tgz"},"main":"index.js","gitHead":"5d11ff5b010948050685c95729d8deaff04c27e0","scripts":{"test":"node test/node.js","bench":"node test/bench.js","build":"tsc -d"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"_npmVersion":"7.16.0","description":"Wraps nodejs built-in modules and browser fetch into one function.","directories":{},"_nodeVersion":"16.2.0","_hasShrinkwrap":false,"devDependencies":{"typescript":"^4.3.2","@types/node":"^14.0.14"},"_npmOperationalInternal":{"tmp":"tmp/micro-ftch_0.2.0_1622862126073_0.0019365359258167736","host":"s3://npm-registry-packages"},"readme":"# micro-ftch\n\nWrappers for [built-in fetch()](https://developer.mozilla.org/en-US/docs/Web/API/fetch) enabling killswitch, logging, concurrency limit and other features.\n\nfetch is great, however, its usage in secure environments is complicated. The library makes it simple.\n\n## Usage\n\nA standalone file\n[micro-ftch.js](https://github.com/paulmillr/micro-ftch/releases) is also available.\n\n> npm install micro-ftch\n\nThere are three wrappers over `fetch()`:\n\n1. `ftch(fetch)` - killswitch, logging, timeouts, concurrency limits, basic auth\n2. `jsonrpc(fetch)` - batched JSON-RPC functionality\n3. `replayable(fetch)` - log & replay network requests without actually calling network code.\n\n```ts\nimport { ftch, jsonrpc, replayable } from 'micro-ftch';\n```\n\n- [Killswitch: instantly enable and disable network](#killswitch-instantly-enable-and-disable-network)\n- [Logging](#logging)\n- [Timeouts](#timeouts)\n- [Concurrency limit](#concurrency-limit)\n- [Basic auth](#basic-auth)\n- [jsonrpc](#jsonrpc)\n- [replayable](#replayable)\n- [Privacy](#privacy)\n- [License](#license)\n\n### Killswitch: instantly enable and disable network\n\nWhen kill-switch is enabled, all requests will throw an error.\nYou can dynamically enable and disable it any any time.\n\n```ts\nlet ENABLED = true;\nconst f = ftch(fetch, { killswitch: () => ENABLED });\nf('http://localhost'); // ok\nENABLED = false;\nf('http://localhost'); // throws\nENABLED = true;\nf('http://localhost'); // ok\n```\n\n### Logging\n\n```ts\nconst f = ftch(fetch, { log: (url, opts) => console.log('fetching', url, opts) });\nf('http://url/'); // will print request information\n```\n\n### Timeouts\n\n```ts\n// browser and OS may have additional timeouts, we cannot override them\n// a: per-request timeout\nconst f = ftch(fetch);\nconst res = await f('http://url/', { timeout: 1000 }); // throws if request takes more than one second\n\n// b: timeout for all\nconst f = ftch(fetch, { timeout: 1000 });\nconst res = await f('http://url/'); // throws if request takes more than one second\n```\n\n### Concurrency limit\n\nAllows to not accidentally hit rate limits or do DoS.\n\n```ts\n// browser and OS may have additional limits, we cannot override them\nconst f = ftch(fetch, { concurrencyLimit: 1 });\nconst res = await Promise.all([f('http://url1/'), f('http://url2/')]); // these would be processed sequentially\n```\n\n### Basic auth\n\n```ts\nconst f = ftch(fetch);\nconst res = await f('https://user:pwd@httpbin.org/basic-auth/user/pwd'); // supports basic auth!\n```\n\n### jsonrpc\n\nSupports batching multiple HTTP requests into one \"Batched\" JSON RPC HTTP request. Can massively speed-up when servers are single-threaded, has small per-user limits\n\n```ts\nconst rpc = jsonrpc(fetch, 'http://rpc_node/', {\n  headers: {},\n  batchSize: 20,\n});\nconst res = await rpc.call('method', 'arg0', 'arg1');\nconst res2 = await rpc.callNamed('method', { arg0: '0', arg1: '1' }); // named arguments\n```\n\n### replayable\n\nSmall utility to log & replay network requests in tests, without actually calling network code.\n\n```ts\nconst ftch = ftch(fetch);\nconst replayCapture = replayable(ftch); // wraps fetch\nawait replayCapture('http://url/1'); // real network\nawait replayCapture('http://url/2');\nconst logs = replayCapture.export(); // Exports logs\n\n// When logs provided - use cached version (faster)\nconst replayTest = replayable(ftch, JSON.parse(logs));\nawait replayTest('http://url/1'); // cached\nawait replayTest('http://url/2'); // cached\nawait replayTest('http://url/3'); // real network\n\n// When done and everything is captured, turn on 'offline' mode to throw on network requests:\nconst replayTestOffline = replayable(ftch, JSON.parse(logs), {\n  offline: true,\n});\nawait replayTest('http://url/1'); // cached\nawait replayTest('http://url/2'); // cached\nawait replayTest('http://url/3'); // throws!\n```\n\n## Privacy\n\nftch() disables referrer by default by setting `referrerPolicy: 'no-referrer'`.\n\n## License\n\nMIT (c) Paul Miller [(https://paulmillr.com)](https://paulmillr.com), see LICENSE file.\n","publish_time":1622862126247,"_cnpm_publish_time":1622862126247,"dist-tags":{"latest":"1.2.1"}}