{"id":15467893,"url":"https://github.com/rangermauve/make-fetch","last_synced_at":"2025-05-07T17:29:47.582Z","repository":{"id":38389126,"uuid":"288295367","full_name":"RangerMauve/make-fetch","owner":"RangerMauve","description":"Implement your own `fetch()` with async iterators.","archived":false,"fork":false,"pushed_at":"2023-04-12T01:06:23.000Z","size":41,"stargazers_count":9,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-28T20:45:49.765Z","etag":null,"topics":["async-iterator","fetch"],"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/RangerMauve.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2020-08-17T22:01:28.000Z","updated_at":"2023-03-14T23:10:31.000Z","dependencies_parsed_at":"2024-06-19T00:28:51.651Z","dependency_job_id":"71dc8915-54ba-4490-b17f-3deb02b052e2","html_url":"https://github.com/RangerMauve/make-fetch","commit_stats":{"total_commits":36,"total_committers":4,"mean_commits":9.0,"dds":"0.16666666666666663","last_synced_commit":"66eae46e09625910c05e728addbc117dce3869a0"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmake-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmake-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmake-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RangerMauve%2Fmake-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RangerMauve","download_url":"https://codeload.github.com/RangerMauve/make-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252926284,"owners_count":21826283,"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":["async-iterator","fetch"],"created_at":"2024-10-02T01:29:55.922Z","updated_at":"2025-05-07T17:29:47.540Z","avatar_url":"https://github.com/RangerMauve.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# make-fetch\nImplement your own `fetch()` with node.js streams\n\n## Usage\n\n```\nnpm i --save make-fetch\n```\n\nBasic example:\n\n```javascript\nimport { makeFetch } from 'make-fetch'\nconst fetch = makeFetch(async (request) =\u003e {\n  const {\n    url, // String representing request URL\n    headers, // An object mapping header titles to values\n    referrer, // An optional string specify the referrer \n    method, // The HTTP method, will always be uppercase, default is `GET`\n    body, // An optional async iterable of buffers for the request body\n    signal // An optional AbortSignal that you might want to listen to for cancellation\n  } = request\n\n  return {\n    status: 200, // Should specify the status code to send back\n    headers: { // Optional object mapping response header titles to values\n      \"something\": \"whatever\"\n    },\n    body: asyncIterator // Required async iterable for the response body, can be empty\n  }\n})\n\nconst response = await fetch('myscheme://whatever/foobar')\nconsole.log(await response.text())\n```\n\nRouted example:\n\n```JavaScript\n\nimport {makeRoutedFetch} from \"make-fetch\"\n\nconst {fetch, router} = makeRoutedFetch()\n\nrouter.get('example://somehost/**', (request) =\u003e {\n  return {\n    body: \"hello world\",\n    headers: {example: \"Whatever\"}\n  }\n})\n\n// You can have wildcards in the protocol, hostname,\n// or individual segments in the pathname\nrouter.post('*://*/foo/*/bar/, () =\u003e {\n  return {body: 'Goodbye world'}\n})\n\n// Match first handler\nfetch('example://somehost/example/path/here')\n\n// Match second handler\nfetch('whatever://something/foo/whatever/bar/')\n\n```\n\n### API:\n\n`makeFetch(async (Request) =\u003e ResponseOptions) =\u003e fetch()`\n\nThe main API is based on the handler which takes a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object, and must return options for constructing a response based on the [Response](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) constructor.\n\nInstead of having a separate parameter for the body and the response options, the fetch handler should return both in one object.\n\nThis will then return a standard [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) API which takes request info, and returns responses.\n\n`makeRoutedFetch({onNotFound, onError}) =\u003e {router: Router, fetch}`\n\nIf you want to have an easier way of routing different methods/hostnames/paths, you can use a routed make-fetch which can make it easier to register handlers for different routes.\nThis will creat a Router, and a `fetch()` instance for that router.\nHandlers you add on the router will be useful to match URLs+methods from the fetch request and will use the matched handler to generate the response.\nYou can optionally supply a `onNotFound` handler to be invoked if no other routes match.\nYou can optionally supply a `onError` handler to be invoked when an error is thrown from your handlers which will take the `Error` instance and the `Request` instance as arguments.\nThe default `onError` handler will print the stack trace to the body with a `500` status code.\n\n`router.add(method, urlPattern, handler) =\u003e Router`\n\nYou can add routes for specific methods, and use URL patterns.\nThen you can pass in the same basic handler as in makeFetch.\nYou can chain multiple add requests since the router returns itself when adding a route.\n\n`router.get/head/put/post/delete(urlPattern, handler) =\u003e Router`\n\nYou can also use shorthands for methods with a similar API.\n\n`router.any(urlPattern, handler)`\n\nYou can register handlers for any method.\n\nFor example `router.any('*://*/**', handler)` will register a handler that will be invoked on any method/protocol scheme/hostname/path.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frangermauve%2Fmake-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frangermauve%2Fmake-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frangermauve%2Fmake-fetch/lists"}