{"id":15405118,"url":"https://github.com/fnando/http","last_synced_at":"2025-04-17T00:51:50.690Z","repository":{"id":66031428,"uuid":"163702532","full_name":"fnando/http","owner":"fnando","description":"Simple HTTP client for both Node.js and the browser.","archived":false,"fork":false,"pushed_at":"2022-04-03T01:58:26.000Z","size":340,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T05:51:12.131Z","etag":null,"topics":["browser","http-client","javascript","nodejs","rest","xhr"],"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/fnando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2018-12-31T23:19:34.000Z","updated_at":"2022-10-30T22:09:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"cf3b8688-b2ff-44bd-a6bd-303c84bd9849","html_url":"https://github.com/fnando/http","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"4813401103464704ba34eecab82edfa072f31e2e"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fhttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fhttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fhttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fhttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/http/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249294918,"owners_count":21246009,"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":["browser","http-client","javascript","nodejs","rest","xhr"],"created_at":"2024-10-01T16:15:04.382Z","updated_at":"2025-04-17T00:51:50.673Z","avatar_url":"https://github.com/fnando.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![@fnando/http: Simple HTTP client for both Node.js and the browser.](https://github.com/fnando/http/raw/main/http.png)\n\n\u003cp align=\"center\"\u003e\n  \u003ca rel=\"noreferrer noopener\" href=\"https://travis-ci.org/fnando/http\"\u003e\u003cimg src=\"https://travis-ci.org/fnando/http.svg\" alt=\"Travis-CI\" /\u003e\u003c/a\u003e\n  \u003ca rel=\"noreferrer noopener\" href=\"https://www.npmjs.com/package/@fnando/http\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@fnando/http.svg\" alt=\"NPM Package Version\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codeclimate.com/github/fnando/http\"\u003e\u003cimg src=\"https://codeclimate.com/github/fnando/http/badges/gpa.svg\" alt=\"Code Climate\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codeclimate.com/github/fnando/http/coverage\"\u003e\u003cimg src=\"https://codeclimate.com/github/fnando/http/badges/coverage.svg\" alt=\"Test Coverage\"\u003e\u003c/a\u003e\n  \u003cimg src=\"https://img.shields.io/badge/license-MIT-orange.svg\" alt=\"License: MIT\"\u003e\n  \u003cimg src=\"http://img.badgesize.io/fnando/http/master/dist/http.js.svg?label=min+size\" alt=\"Minified size\"\u003e\n  \u003cimg src=\"http://img.badgesize.io/fnando/http/master/dist/http.js.svg?compression=gzip\u0026label=min%2Bgzip+size\" alt=\"Minified+gzip size\"\u003e\n\u003c/p\u003e\n\n## Instalation\n\nThis lib is available as a NPM package. To install it, use the following command:\n\n```\nnpm install @fnando/http --save\n```\n\nIf you're using Yarn (and you should):\n\n```\nyarn add @fnando/http\n```\n\n## Importing HTTP\n\nIf you're using `import`:\n\n```js\nimport http from \"@fnando/http\";\n\nconst client = http.client(http.adapter);\n\nclient\n  .get(\"/me\")\n  .then(response =\u003e console.log(response))\n  .catch(error =\u003e console.log(error));\n```\n\n## More examples\n\n```js\nimport { client, adapter } from \"@fnando/http\";\nconst http = client(adapter);\n\n// The request middleware can modify the options\n// that represent the request.\nhttp.middleware(\"request\", options =\u003e {\n  options.headers[\"user-agent\"] = \"reporteo\";\n  return options;\n});\n\n// The response middleware can modify the response\n// object.\nhttp.middleware(\"response\", response =\u003e {\n  response.passedByMiddleware = true;\n  return response;\n});\n\n// The error middleware will receive the error object.\n// The request object is available under `error.request`.\nhttp.middleware(\"error\", err =\u003e {\n  console.log(\"ERROR HANDLED BY MIDDLEWARE:\", err);\n});\n\nfunction success(description) {\n  return function(response) {\n    console.log(description, response.data);\n  };\n}\n\nfunction error(description) {\n  return function(err) {\n    console.log(description, err);\n  };\n}\n\nhttp\n  .get(\"https://httpbin.org/get\", {number: 42, message: \"hello there!\"})\n  .then(success(\"GET with query string\"))\n  .catch(error(\"GET with query string\"));\n\nhttp\n  .get(\"https://httpbin.org/get\")\n  .then(success(\"GET\"))\n  .catch(error(\"GET\"));\n\nhttp\n  .post(\"https://httpbin.org/post\", {number: 42, message: \"hello there!\"})\n  .then(success(\"POST\"))\n  .catch(error(\"POST\"));\n\nhttp\n  .post(\"https://httpbin.org/post\", {message: \"hello from json\"}, {headers: {\"content-type\": \"application/json\"}})\n  .then(success(\"POST with JSON body\"))\n  .catch(error(\"POST with JSON body\"));\n\nconst formData = new FormData();\nformData.append(\"description\", \"Using FormData\");\nformData.append(\"file\", new Blob([\"a file upload\"], {type: \"text/plain\"}), \"readme.txt\");\n\nhttp\n  .post(\"https://httpbin.org/post\", formData)\n  .then(success(\"POST with Form Data\"))\n  .catch(error(\"POST with Form Data\"));\n\nhttp\n  .put(\"https://httpbin.org/put\", {name: \"John Doe\"})\n  .then(success(\"PUT\"))\n  .catch(error(\"PUT\"));\n\nhttp\n  .patch(\"https://httpbin.org/patch\", {name: \"John Doe\"})\n  .then(success(\"PATCH\"))\n  .catch(error(\"PATCH\"));\n\nhttp\n  .delete(\"https://httpbin.org/delete\", {name: \"John Doe\"}, {headers: {\"user-agent\": \"reporteo - delete\"}})\n  .then(success(\"DELETE\"))\n  .catch(error(\"DELETE\"));\n\n// To abort an in-flight request, you call `.abort()` on\n// the response object.\nconst response = http.get(\"https://httpbin.org/get\");\n                     .catch(error =\u003e console.log(error, error.aborted));\nresponse.abort();\n```\n\n## Node.js\n\nInstall \u003chttps://github.com/evanw/node-source-map-support\u003e to enable sourcemaps support in Node.js.\n\n```console\n$ yarn add -DE source-map-support\n```\n\n## API\n\n### Response\n\nThe `response` object will carry the following attributes:\n\n- `body`: The response's body string as it is.\n- `status`: The status code (integer).\n- `request`: The request object (`XMLHttpRequest` for browser, `http.ClientRequest` for Node.js).\n\nMiddleware chain may modify this objects and add/remove properties. By default, _http_ will add a few middleware functions that change the response. Read more about it under \"Default middleware chain\".\n\n### Middleware\n\n_http_ comes with three distinct middleware chains: `request`, `response`, and `error`.\n\n#### `request` middleware functions\n\nBefore performing a request, the `request` chain will be executed with the `options` object that represents that request. A middleware function may add headers, change parameters, change the request body, modify the request timeout, and change the URL.\n\nA middleware function must have the following signature:\n\n```js\nfunction requestMiddlewareFunction(options) {\n  return options;\n}\n```\n\n`options` will have the following properties by default:\n\n- `options.auth`: An object like `{username, password}` that will be used for basic authentication.\n- `options.body`: A string that represents the request body.\n- `options.headers`: An object representing the request headers. The recommended form for header names is lowercase (e.g. `content-type`).\n- `options.method`: A string representing the request method (e.g. `get`).\n- `options.params`: The params for that given request. When performing `GET`/`HEAD`/`DELETE` requests, parameters will be appended to the URL. The middleware function `bodyMiddleware` will set the body out of the `params` object. The `jsonBodyMiddleware` will set the body to `JSON.stringify(params)` if your `content-type` header is `application/json`.\n- `options.timeout`: The request timeout in milliseconds. Defaults to `0` (no timeout).\n- `options.url`: A string representing the request URL.\n\n#### `response` middleware functions\n\nAfter performing a request, the `response` chain will be executed with an object representing the response. A middleware function may add new properties, modify the response body, serialize the response body, and change the response status code.\n\nA middleware function must have the following signature:\n\n```js\nfunction responseMiddlewareFunction(response) {\n  return response;\n}\n```\n\n`response` will have the following properties by default:\n\n- `response.status`: An integer representing the response status code.\n- `response.body`: A string representing the response body.\n- `response.method`: A string representing the request method (this is added for convenience, as the request method is not available on the request object in any form).\n- `response.headers`: An object representing the response headers.\n- `response.request`: The object that represents the request.\n- `response.response`: The object that represents the response. For client-side, this will be the same object as `response.request`.\n\n#### `error` middleware functions\n\nWhen a request fails (e.g. timeout), the `error` chain will be executed with an error object. A middleware function may instantiate a new error, add new properties, etc.\n\nA middleware function must have the following signature:\n\n```js\nfunction errorMiddlewareFunction(error) {\n  return error;\n}\n```\n\n`error` may have the following properties, depending on the error:\n\n- `error.request`: The request object.\n- `error.timeout`: A boolean signaling that the error comes from a timed out request.\n- `error.aborted`: A boolean signaling that the error comes from an aborted request.\n\nNotice that client-side won't have the original error object because `XMLHttpRequest` doesn't make it available; `new Error(\"Request errored out\")` will be returned instead.\n\n#### Default Middleware chain\n\n`request` chain:\n\n- `basicAuthMiddleware`: set `Authorization` header out of `{auth: {username, password}` option.\n- `jsonBodyMiddleware`: set the request body to the string representation of `params` when `{headers: {\"content-type\": \"application/json\"}}` is set.\n- `bodyMiddleware`: set the request body based on the request method and the `params` object. `FormData` objects will be sent as it is. This will also set `Content-Type` header to `application/x-www-form-urlencoded` if not previously defined to anything else.\n\n`response` chain:\n\n- `jsonResponseMiddleware`: parses the response body with `JSON.parse(body)` and sets `request.data` to it whenever the response's `Content-Type` header matches `application/json`.\n- `responseStatusMiddleware`: sets `success` to status codes that range from 2xx-3xx. Also set `response.redirect` and `response.location` when 3xx status is returned.\n\n## Icon\n\nIcon made by [xnimrodx](https://www.flaticon.com/authors/xnimrodx) from [Flaticon](https://www.flaticon.com/) is licensed by Creative Commons BY 3.0.\n\n## License\n\n(The MIT License)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fhttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Fhttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fhttp/lists"}