{"id":20253929,"url":"https://github.com/adajuly/cute-http","last_synced_at":"2025-04-10T23:43:45.427Z","repository":{"id":45638751,"uuid":"187840082","full_name":"adajuly/cute-http","owner":"adajuly","description":"一个基于axios封装的更易用的http库。","archived":false,"fork":false,"pushed_at":"2021-12-13T03:04:33.000Z","size":316,"stargazers_count":20,"open_issues_count":7,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T23:43:39.773Z","etag":null,"topics":["ajax","axios","http","http-client"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/adajuly.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":"2019-05-21T13:11:39.000Z","updated_at":"2025-03-02T13:44:37.000Z","dependencies_parsed_at":"2022-09-10T19:31:32.740Z","dependency_job_id":null,"html_url":"https://github.com/adajuly/cute-http","commit_stats":null,"previous_names":["concentjs/cute-http"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adajuly%2Fcute-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adajuly%2Fcute-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adajuly%2Fcute-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adajuly%2Fcute-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adajuly","download_url":"https://codeload.github.com/adajuly/cute-http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317726,"owners_count":21083527,"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":["ajax","axios","http","http-client"],"created_at":"2024-11-14T10:28:45.499Z","updated_at":"2025-04-10T23:43:45.410Z","avatar_url":"https://github.com/adajuly.png","language":"JavaScript","readme":"## cute-http\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#\"\u003e\n    \u003cimg width=\"460\" src=\"https://raw.githubusercontent.com/fantasticsoul/assets/master/img/cute-http.png\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n### cute-http，一个可爱的 http 请求库，易用\u0026简单\n\n- 可以设置请求超时后的重试次数\n- 可以对 get 请求设置缓存策略\n  \u003e cute 对缓存做了优化，同一个 url 的 get 请求，如果 query 参数不变，就优先取缓存结果，取不到再去后端要，如果发生变化，会删除之前的缓存结果看，并去后端请求新结果，这样防止缓存过多无用数据\n- 可以发起多个 get，多个 post 请求，多个 jsonp 请求，或者多个不同类型的请求\n- 针对并行请求，cute 有两种策略处理结果\n  \u003e 默认使用保证请求执行完毕，才返回结果，就算有其中一个请求出现错误，也不会影响其他请求，当然，此时用户需要遍历返回的结果数据，因为可能其中有一个是错误。\n  \u003e 用户也可以设置为发起多个请求时，只要有一个请求错误，就全部失败。\n\n### 怎么使用\n\n安装 cute-http\n\n```\nnpm i cute-http --save\n```\n\n引入\n\n```\nimport * as cute from 'cute-http';\n```\n\n### api\n\n#### 常量\n\n```\nconst {ONE_ERROR_ABORT_ALL, KEEP_ALL_BEEN_EXECUTED, LOCAL_STORAGE, MEMORY} = cute.const;\n```\n\n- ONE_ERROR_ABORT_ALL 一个报错，中断所有的请求\n- KEEP_ALL_BEEN_EXECUTED 一个报错，不影响其他的请求，调用者需要自己处理返回的错误\n- LOCAL_STORAGE 将 get 返回结果缓存在`localStorage`\n- MEMORY 将 get 返回结果缓存在`memory`\n\n#### setConfig\n\n顶层 api，为 cute 配置参数\n\n```\ncute setConfig({\n  retryCount: number,//重试次数\n  timeout: 1900,//超时时间（毫秒）\n  debug:true,//打开debug模式\n  // cacheType: MEMORY, // 默认无，如果开启了缓存，cute只针对get请求做缓存，如需穿透缓存，可以对query参数加随机值，或者调用cute.get时，配置第二个参数{cacheType:null}，表示针对这一次调用不读取缓存值\n  // failStrategy: ONE_ERROR_ABORT_ALL, //不设置的话，cute默认采用KEEP_ALL_BEEN_EXECUTED\n})\n```\n\n#### multi\n\n```\n/**\n * 发起多个post请求\n * @param {Array\u003c{type:'post', url:string, data:object} | {type:'get', url:string} | {type:'jsonp', url:string} \u003e} items\n * @param {{failStrategy?:number, retryCount?:number, callbackParamName?:string, [otherAxiosConfigKey]:any}} extendedAxiosConfig\n * otherAxiosConfigKey @see https://github.com/axios/axios\n */\ncute.multi(urls, extendedAxiosConfig)\n```\n\n#### get\n\n```\n// data 会被自动stringify拼接到url末尾\ncute.get(url:string, data:string|object, extendedAxiosConfig:ExtendedAxiosConfig)\n```\n\n#### multiGet\n\n```\ncute.multiGet(urls:string[] | {url:string, data:string|object}[], extendedAxiosConfig:ExtendedAxiosConfig)\n```\n\n#### post\n\n```\ncute.post(url:string, data:object, extendedAxiosConfig:ExtendedAxiosConfig)\n```\n\n#### multiPost\n\n```\ncute.multiPost({url:string, data:object}[], extendedAxiosConfig:ExtendedAxiosConfig)\n```\n\n#### jsonp\n\n```\n// data 会被自动stringify拼接到url末尾\ncute.jsonp(url:string, data:string|object, extendedAxiosConfig:ExtendedAxiosConfig)\n```\n\n#### multiJsonp\n\n```\ncute.multiJsonp(urls:string[] | {url:string, data:string|object}[], extendedAxiosConfig:ExtendedAxiosConfig)\n```\n\n### 以下代码在 test/test-api.js 中，用户可以执行 node \\${your_test_dir}/test-api.js 查看效果\n\n当然，你也可以注释掉某些代码，只看其中一个的效果\n\n```\nconst cute = require('../index');\nconst {ONE_ERROR_ABORT_ALL, KEEP_ALL_BEEN_EXECUTED, MEMORY} = cute.const;\nconst axios = cute.axios;//这个是axios模块的引用\n\ncute.setConfig({\n  retryCount: 3,//设置重试次数\n  timeout: 1900,//设置超时时间\n  debug: true,\n  dataVerifyRule: {//设置通用的响应数据校验规则，只支持校验json对象的第一层key的值和类型的校验\n    data: 'object',\n    code: 'number',\n    message: 'string',\n  },\n  pathDataVerifyRule:{//对某些请求设置独立的数据校验规则\n    '/staff/foo':{\n      reply:'object',\n      msg:'string',\n    }\n  }\n  cacheType: MEMORY, // 值为 'memory' | 'localStorage' 默认无\n  failStrategy: KEEP_ALL_BEEN_EXECUTED, //不设置的话，cute默认采用ONE_ERROR_ABORT_ALL\n})\n\nconst updateBook = 'http://localhost:8888/update-book';\nconst getBooksByUid = uid =\u003e `http://localhost:8888/get-books?uid=${uid}`;\n\nasync function main(){\n  const result1 = await cute.get(getBooksByUid(1));\n  const books = result.result1;\n\n  const result2 = await cute.multiGet([getBooksByUid(1), getBooksByUid(2)]);\n  const [reply1, reply2] = result2;\n\n  const result3 = await cute.post(updateBook, {id:1, name:'zk'});\n  const updateReply = result.result3;\n\n  const result4 = await cute.multiPost([{url:updateBook, body:{id:1, name:'zk'}}, {url:updateBook, body:{id:2, name:'wow'}}]);\n  const [updateReply1, updateReply2] = result4;\n\n}\n\n```\n\n\u003e 更多示例见/test/test-api.js\n\n### 运行测试用例\n\n运行模拟服务器\n\n```\n* npm start\n```\n\n执行测试脚本\n\n```\n* npm test\n```\n\n### 彩蛋,test 目录下内置了一个 mini-express,仅仅用于服务本测试用例^\\_^\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadajuly%2Fcute-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadajuly%2Fcute-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadajuly%2Fcute-http/lists"}