{"id":15649592,"url":"https://github.com/paulmillr/micro-ftch","last_synced_at":"2025-04-04T20:10:11.954Z","repository":{"id":244607018,"uuid":"814916856","full_name":"paulmillr/micro-ftch","owner":"paulmillr","description":"Wrappers for built-in fetch() enabling killswitch, logging, concurrency limit and other features.","archived":false,"fork":false,"pushed_at":"2025-03-25T10:18:47.000Z","size":119,"stargazers_count":46,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T19:07:52.339Z","etag":null,"topics":["cache","concurrency","fetch","ftch","jsonrpc","killswitch","logging","mock","parallel","replay"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulmillr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"paulmillr"}},"created_at":"2024-06-14T01:35:16.000Z","updated_at":"2025-03-25T10:18:51.000Z","dependencies_parsed_at":"2024-06-16T03:01:01.094Z","dependency_job_id":"df0e7a56-095b-4a9f-9237-9b14ad13a8f8","html_url":"https://github.com/paulmillr/micro-ftch","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"8fab53e27eb250d4edd3f1698a83421edea1eef1"},"previous_names":["paulmillr/micro-ftch"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-ftch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-ftch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-ftch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fmicro-ftch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulmillr","download_url":"https://codeload.github.com/paulmillr/micro-ftch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247242678,"owners_count":20907134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","concurrency","fetch","ftch","jsonrpc","killswitch","logging","mock","parallel","replay"],"created_at":"2024-10-03T12:30:22.848Z","updated_at":"2025-04-04T20:10:11.938Z","avatar_url":"https://github.com/paulmillr.png","language":"JavaScript","funding_links":["https://github.com/sponsors/paulmillr"],"categories":[],"sub_categories":[],"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\u003e `npm install micro-ftch`\n\n\u003e `jsr add jsr:@paulmillr/micro-ftch`\n\n```ts\nimport { ftch, jsonrpc, replayable } from 'micro-ftch';\n\nlet enabled = false;\nconst net = ftch(fetch, {\n  isValidRequest: () =\u003e enabled,\n  log: (url, options) =\u003e console.log(url, options),\n  timeout: 5000,\n  concurrencyLimit: 10,\n});\nconst res = await net('https://example.com');\n\n// Composable\nconst rpc = jsonrpc(net, 'http://rpc_node/', {\n  headers: {},\n  batchSize: 20,\n});\nconst res1 = await rpc.call('method', 'arg0', 'arg1');\nconst res2 = await rpc.callNamed('method', { arg0: '0', arg1: '1' }); // named arguments\nconst testRpc = replayable(rpc);\n// Basic auth auto-parsing\nawait net('https://user:pwd@httpbin.org/basic-auth/user/pwd');\n```\n\n- [ftch](#ftch)\n  - [isValidRequest](#isValidRequest)\n  - [log](#log)\n  - [timeout](#timeout)\n  - [concurrencyLimit](#concurrencyLimit)\n  - [Basic auth](#basic-auth)\n- [jsonrpc](#jsonrpc)\n- [replayable](#replayable)\n- [Privacy](#privacy)\n- [License](#license)\n\nThere are three wrappers over `fetch()`:\n\n1. `ftch(fetch)` - isValidRequest, logging, timeouts, concurrency limits, basic auth\n2. `jsonrpc(fetch)` - batched JSON-RPC functionality\n3. `replayable(fetch)` - log \u0026 replay network requests without actually calling network code.\n\n## ftch\n\nBasic wrapper over `fetch()`.\n\n### isValidRequest\n\nWhen isValidRequest killswitch 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, { isValidRequest: () =\u003e ENABLED });\nf('http://localhost'); // ok\nENABLED = false;\nf('http://localhost'); // throws\nENABLED = true;\nf('http://localhost'); // ok\n```\n\n### log\n\n```ts\nconst f = ftch(fetch, { log: (url, opts) =\u003e console.log('fetching', url, opts) });\nf('http://url/'); // will print request information\n```\n\n### timeout\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### concurrencyLimit\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 \u0026 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmillr%2Fmicro-ftch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulmillr%2Fmicro-ftch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmillr%2Fmicro-ftch/lists"}