{"name":"micro-ftch","version":"0.3.0","keywords":["fetch","http","https","node","browser","ajax","request"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"micro-ftch@0.3.0","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"dist":{"shasum":"67364fc190c494fe211b1b59d5b0852c1bfa6aa3","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-0.3.0.tgz","fileCount":5,"integrity":"sha512-NWeRAy2BzMHdB6w63UG2xZVvj4WTJAkyJhI/LQB6ZZZQbJ0wbMA3djpVGO8GG8r0+oo0vOYWNgvhjRkobireuA==","signatures":[{"sig":"MEUCIEFn5UANNBAXsWoNlOT8XT6X7N2cPnlbjWQVuJFbl/+mAiEA2rZT1waChIbNmcTQN4uClCrl3PReWkH5JL8WGJ7+SXs=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":11639,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg4+K3CRA9TVsSAnZWagAAEVsP/jPEvBlyo+uI7zDOtnfw\nxHIGrj3CrsP24AV1kop26HS6V03sKPY21U/sq1GsGlYEV/w6GYRw/heo0NI4\nN7DMI0FjmbSVeoQ8Tyh1w5vePq3w/lpYelYMOrzwKvDah3lFOWqb9clBCSkh\nErFjOx7Kw9zfh6M+hpzUsCpMm2qTKDdNPOsGNRBycwtL6lWk4DcY0d9saRv1\nZGXRnDHYcI+6/lH45mhU0MuwZskqK+aSkpT9lRJmX2aNgeNNCC7FKTThtCzP\nriy4JPJr5o8+REby+IIFip7dZRLtYhEOy4MJNN6Cb1zMEujgAjM3HJpMcNvy\nPEHQsvXnEuyXP/WG5SL2s4THHxUQ8Tr3PYx/wojrBAt5xbvb9Mla/et8QRkp\nprEuwgvjk1j4z99Zy2zZXA6dVq4kxfn03rnvi08s0NS17F+zDjOcyPwgbHVM\n+otox3N7soJMOoec065+vRk3Ibashf4IZkBTkgpoaPc18jS2Z+dJONApG/p4\nki393NjD6ZUYlTJuneUyYcoF1MTyU28qihzc5GuZBWtnSGiMlC9p04y1c/XB\n2oZtdOO8vA+NF5UJXw1cC1COZvbSrogpEklQK1SjIYd8pwr1ASXB4W/UK5pa\n7Ysvp/NhSjCsrIukFoZt7Fb/UQEkNwfBBeuPdVjAqNbEEKRjnnOVllrAvYmM\nFkJi\r\n=NhqM\r\n-----END PGP SIGNATURE-----\r\n","size":4232,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-0.3.0.tgz"},"main":"index.js","gitHead":"9f6e2055b076117ef4432e481262b5bbee10a907","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.5","@types/node":"^16.0"},"_npmOperationalInternal":{"tmp":"tmp/micro-ftch_0.3.0_1625547447368_0.9509188418192105","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":1625547447477,"_cnpm_publish_time":1625547447477,"dist-tags":{"latest":"1.2.1"}}