{"id":41942420,"url":"https://github.com/leonwgc/xhr-fetch-lib","last_synced_at":"2026-01-25T18:39:50.770Z","repository":{"id":57401403,"uuid":"381047153","full_name":"leonwgc/xhr-fetch-lib","owner":"leonwgc","description":"Delightful ajax fetch","archived":false,"fork":false,"pushed_at":"2021-10-12T08:10:37.000Z","size":101,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-14T20:11:52.625Z","etag":null,"topics":["ajax","fetch","xhr","xmlhttprequest"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leonwgc.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-28T13:47:00.000Z","updated_at":"2023-02-01T09:19:08.000Z","dependencies_parsed_at":"2022-09-19T04:51:36.831Z","dependency_job_id":null,"html_url":"https://github.com/leonwgc/xhr-fetch-lib","commit_stats":null,"previous_names":["leonwgc/fetch-xhr"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/leonwgc/xhr-fetch-lib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonwgc%2Fxhr-fetch-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonwgc%2Fxhr-fetch-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonwgc%2Fxhr-fetch-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonwgc%2Fxhr-fetch-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leonwgc","download_url":"https://codeload.github.com/leonwgc/xhr-fetch-lib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonwgc%2Fxhr-fetch-lib/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28756442,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-25T16:32:25.380Z","status":"ssl_error","status_checked_at":"2026-01-25T16:32:09.189Z","response_time":113,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["ajax","fetch","xhr","xmlhttprequest"],"created_at":"2026-01-25T18:39:50.708Z","updated_at":"2026-01-25T18:39:50.760Z","avatar_url":"https://github.com/leonwgc.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n简单好用的 xhr 请求库，包含 get,post,put,delete, upload 文件上传方法\n\n1.安装 [npm](https://npmjs.org/) / [yarn](https://yarnpkg.com) 安装\n\n```js\nnpm install xhr-fetch-lib\nyarn add xhr-fetch-lib\n```\n\n2.ts类型定义\n\n- 默认导出的fetch\n\n```js\ndeclare const fetch: ({ method, url, data, headers, withCredentials, responseParser, xhrSetting, }: Options) =\u003e Promise\u003cRecord\u003cstring, unknown\u003e | string\u003e;\n\nexport declare type Options = {\n    /** 请求方法 */\n    method?: 'get' | 'post' | 'put' | 'delete' | 'head';\n    /** 请求url */\n    url: string;\n    /** 请求数据,对于get请求，data用object默认会转为 key=value\u0026key1=value1的格式 */\n    data?: Record\u003cstring, unknown\u003e | string;\n    /** 请求头 */\n    headers?: Record\u003cstring, string\u003e;\n    /** withCredentials设置，默认true */\n    withCredentials?: boolean;\n    /** 处理xhr响应 */\n    responseParser?: (xhr: XMLHttpRequest) =\u003e Record\u003cstring, unknown\u003e | string;\n    /** xhr设置，e.g. responseType,timeout等设置  */\n    xhrSetting?: XHRSetting;\n};\n\nexport default fetch;\n\n```\n\n- get/post/put/delete 方法\n\n```js\ndeclare type simpleRequest = (\n/** 请求url */\nurl: string,\n/** 请求数据, 对于get请求，data用object默认会转为 key=value\u0026key1=value1的格式 */\ndata: Record\u003cstring, unknown\u003e | string,\n/** 请求头 */\nheaders: Record\u003cstring, string\u003e) =\u003e Promise\u003cRecord\u003cstring, unknown\u003e | string\u003e;\n\nexport declare const get: simpleRequest;\nexport declare const post: simpleRequest;\nexport declare const put: simpleRequest;\nexport declare const del: simpleRequest;\n\n```\n\n- upload文件上传\n\n```js\n/**\n * 上传文件\n *\n * @export\n * @param {string} url api地址\n * @param {(Record\u003cstring, unknown\u003e | null)} data 数据\n * @param {(File | Blob)} file 待上传文件\n * @param {(Record\u003cstring, string\u003e | null)} [headers] 自定义请求头部\n * @param {((e: ProgressEvent \u0026 { percent: number }) =\u003e void)} [onProgress] 上传进度回调\n * @return {*}  {Promise\u003cXMLHttpRequest\u003e}\n */\nexport default function upload(url: string, data: Record\u003cstring, unknown\u003e | null, file: File | Blob, headers?: Record\u003cstring, string\u003e | null, onProgress?: (e: ProgressEvent \u0026 {\n    percent: number;\n}) =\u003e void): Promise\u003cXMLHttpRequest\u003e;\n```\n\n3. 使用示例\n\n```js\nimport fetch, { get, post, put, del, upload } from 'xhr-fetch-lib';\n\n// 直接调用get post put del\nget(apiUrl).then((res) =\u003e {\n  responseHandler(res);\n});\n\n// 用fetch设置返回blob实现下载\nfetch({\n  url,\n  method,\n  data,\n  xhrSetting: { responseType: 'blob' },\n  responseParser: (xhr) =\u003e xhr.response,\n}).then((blob) =\u003e {\n  const url = window.URL.createObjectURL(blob);\n  const a = document.createElement('a');\n  a.href = url;\n  a.download = fileName;\n  a.click();\n});\n\n//文件上传\nconst file = e.target.files[0];\nupload(api, { key: 'value' }, file, null, (e) =\u003e console.log(e.percent));\n```\n\n4. 返回值\n\n默认是 json-bigint parse 的 object 对象 , 同 JSON.parse 返回值\n\n```js\nimport JSONbig from 'json-bigint';\n\nconst JSONBig = JSONbig({ storeAsString: true });\n\nfunction parseResponse(xhr: XMLHttpRequest): unknown {\n  let result;\n\n  try {\n    result = JSONBig.parse(xhr.responseText);\n  } catch (e) {\n    result = xhr.responseText;\n  }\n  return result;\n}\n```\n\n5. 对于 get 请求，data 用 object 默认会转为 key=value\u0026key1=value1 的格式\n\n6. 默认导出的 fetch 函数，可以自定义 responseParser，默认是JSONBig.parse(xhr.responseText)\n\n7. fetch 可以自定义 XMLHttpRequest 属性, 例如 responseType,timeout 等\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonwgc%2Fxhr-fetch-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonwgc%2Fxhr-fetch-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonwgc%2Fxhr-fetch-lib/lists"}