{"name":"micro-ftch","version":"0.3.1","keywords":["fetch","http","https","node","browser","ajax","request"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"micro-ftch@0.3.1","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"dist":{"shasum":"6cb83388de4c1f279a034fb0cf96dfc050853c5f","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-0.3.1.tgz","fileCount":5,"integrity":"sha512-/0LLxhzP0tfiR5hcQebtudP56gUurs2CLkGarnCiB/OqEyUFQ6U3paQi/tgLv0hBJYt2rnr9MNpxz4fiiugstg==","signatures":[{"sig":"MEUCIQCAcz+IOPSFb8lIwdtAsDHJZOSxZGJA/KQcbg4tfxR1NwIgdA1zZo/6+ZM3nn6sR04b9zhGAzJE3IOGPWVqFmf7yyU=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":12002,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg5nrSCRA9TVsSAnZWagAAOWwP/intb/wayfgJa+laMi3O\nGbAn6xfYq12TVWwsLoe1tfNU+HFndmb8/rCp/hIPepGbNh2Cu4A36p9gnyfc\njsNHE8UF30siPQGnfLPEfzu5SZkT9uejzOmPq9mw3Yvd0yuf/sMvkW0Da+S/\nMS0LcsHoK3Robbk5lPYZZFa/DeKFLok3XH5pruC6p40OgSF8+xpgZgARYU3H\nz63GG8uJqBSaJBLLcsW9yd/zAd7twLImEFj+ETZV4O6ZHTTxrOCg5zLW0LyV\nmpfj/SsyDGi3qO0QbyuyOID/0crMxWoitbAKSsDQ2bGw329cnK+nGSv2uxLM\nZWfHHF8SvfDvcjw1ciT87mGIN037yH1GusspppPbWEourpXxNLSPCh0IMfFu\nhfObFdcX/sNIqPn9SAJEFsNbxjx1jrX1Ilkcj80VQEWXRGqwoAQG5t7XEjUh\nocB5bYZyI8F3MEZvEr7lSq1VQpKS2ktDtgzegRVe6H1cntzmwXbCVuhAPaHn\nT8V1XoDlEKPWHx0kaAUELg2aaTYq1Ey1Q7hremdPr8oFz6OHO1AbrUmiMpL7\n25G8G2CJs5WZrMUvVhydkyPPw/51jKzhW3UNi2jBddVcksSAzjVdsSZXa0/O\nJbCJ5Bsg20Yn50InyKufVbuViavymlSNDn6J2IZAm/f2r+nMvslDjHAJSUud\nF5/9\r\n=WEbg\r\n-----END PGP SIGNATURE-----\r\n","size":4292,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-0.3.1.tgz"},"main":"index.js","gitHead":"d1e98faea55fd71b8e97353f673c1d1be6ead518","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.1_1625717457785_0.1708303544158265","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":1625717457976,"_cnpm_publish_time":1625717457976,"dist-tags":{"latest":"1.2.1"}}