{"name":"micro-ftch","version":"0.1.1","keywords":["fetch","http","https","node","browser","ajax","request"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"micro-ftch@0.1.1","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"dist":{"shasum":"99c560aaba661ed882fe0b1039391b69791e4da6","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-0.1.1.tgz","fileCount":5,"integrity":"sha512-Jcq7ZwSgzwnLGN8hHNgEcJQ3oFr976+efmQ9BMwsbxeGFGWd6t6LFrzT8YlpNftjajVmmwtqbR/u8Yjkuwo15A==","signatures":[{"sig":"MEQCIFhwV7Rk/IWNnFk3aHQFdYebHEEDVpkvT4Hj+z9es9E5AiAUqsuN/+qs+gcONU+8WPzZX1zYYJAwOzc8c8QPaHF8rQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":5255,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe80GUCRA9TVsSAnZWagAAU3EP/RbMzcfdiKhge73MGwrf\nGP790zm5XEwyxz1bX7CthB3gExMf4NNeGKuQ3ElIHd7msWH65ueDS4/jldcE\n9GqVR2ZBPsv3M1WvzSUC0u6hUqgQQtW+FMmHHatbx1Wa6baLtFFh5SQyJsxD\nzDJARCJM98kcL90cjWw75zd7Kav4Xz0d7iIAKv6kFVGoUYUaVvYvCKQUrJa6\nGwB00BoEg/hBXDBBQuvIy+Ya1tB4h78VZe5rEBdf8NgZ2lbGXrYAQPCCRTkk\nyiUGI6QZtdt9RGNm5PbLB8UBqUvewNU1AU38wAI2VXBVsrFOee+/9C6R+ia6\nwm5O0GhfMovSLLEQf4zo147zjyGtbePYj27ZzMPogD5/oi5pZ2C00OtLLDiN\nk8IzEsI8/dr8ArbA9P6IHSqZpRh0iClWHqWjCnYBSdJON2CyagYTfqEVMObY\nyy/ceCZbklHadpkjcsOgrvjG5YdNqWpOCZ5blZMCBhm3olzezBQ2sTsMfl5r\n6eeAyenJwYdykEo22Xur+rgenBDlWn3NQjjNOXvNVHYQtTBav01fv+5g/7Fr\nifraeCJd856FieBqH1hX/PUnpSRO7Y4rIlA6KCTxlmhe3KW+xGUAjndhd2dX\nPWFZnrssjsNMXJl0n9S/3YuGMYj8C6ErCwHRdPhfL4+zc2KUUjsY00nRAWk9\nbQLA\r\n=HWs9\r\n-----END PGP SIGNATURE-----\r\n","size":2426,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-0.1.1.tgz"},"main":"index.js","gitHead":"9bc2694d1329a62142805961afd210be738e4a15","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"tsc -d"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"_npmVersion":"6.14.5","description":"Wraps nodejs built-in modules and browser fetch into one function.","directories":{},"_nodeVersion":"14.2.0","_hasShrinkwrap":false,"devDependencies":{"typescript":"^3.9.5","@types/node":"^14.0.14"},"_npmOperationalInternal":{"tmp":"tmp/micro-ftch_0.1.1_1593000340430_0.7967527946119359","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":1593000340544,"_cnpm_publish_time":1593000340544,"dist-tags":{"latest":"1.2.1"}}