{"name":"micro-ftch","version":"0.4.0","description":"Wrappers for built-in fetch() enabling killswitch, logging, concurrency limit and other features","main":"lib/index.js","module":"lib/esm/index.js","types":"lib/index.d.ts","exports":{".":{"types":"./lib/index.d.ts","import":"./lib/esm/index.js","default":"./lib/index.js"}},"devDependencies":{"@paulmillr/jsbt":"0.1.0","micro-should":"0.4.0","prettier":"3.1.1","typescript":"5.3.2"},"author":{"name":"Paul Miller","url":"https://paulmillr.com"},"license":"MIT","homepage":"https://github.com/paulmillr/micro-ftch","repository":{"type":"git","url":"git+https://github.com/paulmillr/micro-ftch.git"},"scripts":{"build":"tsc && tsc -p tsconfig.esm.json","lint":"prettier --check index.ts","format":"prettier --write index.ts","test":"node test/index.test.js"},"keywords":["fetch","network","killswitch","concurrent","parallel","jsonrpc","replay","ftch"],"funding":"https://paulmillr.com/funding/","_id":"micro-ftch@0.4.0","gitHead":"e65b884fcb7dfe8bd58ce3a78a93c70bf57bd054","bugs":{"url":"https://github.com/paulmillr/micro-ftch/issues"},"_nodeVersion":"20.14.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-1SEH+syTsMQMQByZGJeMAiBxJIRu/3gG1z1+tHexgtXtXgwQy1C5JVBtXRdMZOIx9flerUBCJtmtVl7szwWgxQ==","shasum":"b2c5320b5db934dcb9678eaa5461c24e992d97d6","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-0.4.0.tgz","fileCount":11,"unpackedSize":73266,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/micro-ftch@0.4.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCJ5V7+kMAo2AoDUC4jEMUX1POW7kcYsfVsOlAPZbqwSQIgbVYnDA/Lsti5ei0fefKpNQbdQbhZazpL4iPsbMPErZI="}],"size":14478,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-0.4.0.tgz"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"directories":{},"maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/micro-ftch_0.4.0_1718504046342_0.15267972369511895"},"_hasShrinkwrap":false,"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":1718504046521,"_cnpm_publish_time":1718504046521,"dist-tags":{"latest":"1.2.1"}}