{"id":16294022,"url":"https://github.com/axetroy/wxapp-http","last_synced_at":"2025-03-20T04:30:24.858Z","repository":{"id":57123335,"uuid":"95180818","full_name":"axetroy/wxapp-http","owner":"axetroy","description":"微信小程序的http模块，Tiny but Powerful","archived":false,"fork":false,"pushed_at":"2019-04-08T13:00:27.000Z","size":2458,"stargazers_count":42,"open_issues_count":0,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-28T23:01:43.413Z","etag":null,"topics":["http","request","wx","wxapp"],"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/axetroy.png","metadata":{"files":{"readme":"README-0.x.x.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-23T03:45:47.000Z","updated_at":"2023-04-11T03:45:58.000Z","dependencies_parsed_at":"2022-08-26T17:21:57.033Z","dependency_job_id":null,"html_url":"https://github.com/axetroy/wxapp-http","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axetroy%2Fwxapp-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axetroy%2Fwxapp-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axetroy%2Fwxapp-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axetroy%2Fwxapp-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/axetroy","download_url":"https://codeload.github.com/axetroy/wxapp-http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244047647,"owners_count":20389206,"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":["http","request","wx","wxapp"],"created_at":"2024-10-10T20:13:54.395Z","updated_at":"2025-03-20T04:30:24.234Z","avatar_url":"https://github.com/axetroy.png","language":"JavaScript","readme":"# wxapp-http\n[![Build Status](https://travis-ci.org/axetroy/wxapp-http.svg?branch=master)](https://travis-ci.org/axetroy/wxapp-http)\n[![Dependency](https://david-dm.org/axetroy/wxapp-http.svg)](https://david-dm.org/axetroy/wxapp-http)\n![License](https://img.shields.io/badge/license-MIT-green.svg)\n[![Prettier](https://img.shields.io/badge/Code%20Style-Prettier-green.svg)](https://github.com/prettier/prettier)\n![Node](https://img.shields.io/badge/node-%3E=6.0-blue.svg?style=flat-square)\n[![npm version](https://badge.fury.io/js/@axetroy/wxapp-http.svg)](https://badge.fury.io/js/wxapp-http)\n\n微信小程序的http模块，机智得“绕过”最大5个http并发的限制.\n\n![sceenshot](https://github.com/axetroy/wxapp-http/raw/master/screenshot.gif)\n\n## Installation\n```bash\nnpm install wxapp-http\n```\n\n[example](https://github.com/axetroy/wxapp-http/tree/master/example)\n\n## Features\n\n- [x] 更优雅的API\n- [x] http请求的拦截器\n- [x] http请求的事件监听器\n- [x] http请求返回promise\n- [x] http请求队列化，规避小程序的并发限制\n- [x] 自定义http请求的最高并发数量\n\nTODO: \n\u003e 1.0.0正式版本将基于[@axetroy/event-emitter.js](https://github.com/axetroy/event-emitter.js) 事件管理将实现真正的发布/订阅者模式\n\n## Usage\n\n```javascript\n\n// es6\nimport http from 'wxapp-http';\n\n// commonJS\nconst http = require('wxapp-http').default;\n\nhttp.get('https://www.google.com')\n    .then(function(response){\n      \n    })\n    .catch(function(error){\n      console.error(error);\n    });\n```\n\n## API\n\n### Response\n\nResponse返回的结构体\n\n```typescript\ninterface Response${\n  data: any,\n  errMsg: string,\n  header: Object,\n  statusCode: number\n}\n```\n\n#### http.request\n\n```typescript\nHttp.prototype.request = function(method:string, url:string, body?:Object | string=\"\", headers?: Object={}, dataType?: String=\"json\"): Promise\u003cResponse$\u003e{\n  \n}\n```\n\n#### http.get\n\n```typescript\nHttp.prototype.get = function(url:string, body?:Object | string=\"\", headers?: Object={}, dataType?: String=\"json\"): Promise\u003cResponse$\u003e{\n  \n}\n```\n\n#### http.post\n\n```typescript\nHttp.prototype.post = function(url:string, body?:Object | string=\"\", headers?: Object={}, dataType?: String=\"json\"): Promise\u003cResponse$\u003e{\n  \n}\n```\n\n...\n\n#### 以及OPTIONS, HEAD, PUT, DELETE, TRACE, CONNECT 请求, 参数同上\n\n### 拦截器\n\n配置文件字段\n\n```typescript\ninterface Config${\n  method: string,\n  url: string,\n  data: Object | string,\n  header: Object,\n  dataType: string\n}\n\n```\n\n#### 请求拦截器\n\n返回布尔值，如果为true，则允许发送请求，如果为false，则拒绝发送请求，并且返回的promise进入reject阶段\n\n```typescript\nHttp.prototype.requestInterceptor = function(func:(config: Config$)=\u003e boolean): void{\n  \n}\n\nhttp.requestInterceptor(function(config){\n  // 只允许发送https请求\n  if(config.url.indexOf('https')===0){\n    return true;\n  }else{\n    return false;\n  }\n});\n```\n\n#### 响应拦截器\n\n返回布尔值，如果为true，则返回的promise进入resolve阶段，如果为false，则进入reject阶段\n\n```typescript\nHttp.prototype.responseInterceptor = function(func:(config: Config$, response: Response$)=\u003e boolean): void{\n  \n}\n\nhttp.responseInterceptor(function(config, response){\n  // 如果服务器返回null，则进入reject\n  if(response \u0026\u0026 response.data!==null){\n    return true;\n  }else{\n    return false;\n  }\n});\n```\n\n### 监听器\n\n监听全局的http请求\n\n#### 请求发出前\n\n```typescript\nHttp.prototype.onRequest = function(func:(config: Config$)=\u003e void): void{\n  \n}\n\n\nhttp.onRequest(function(config){\n  console.log(`will send http request: `, config.url);\n});\n\n```\n\n#### 请求成功后\n\n```typescript\nHttp.prototype.onSuccess = function(func:(config: Config$, response: Response$)=\u003e void): void{\n  \n}\n\nhttp.onSuccess(function(config, response){\n  console.log(`http request done: `, config.url);\n});\n```\n\n#### 请求失败后\n\n```typescript\nHttp.prototype.onFail = function(func:(config: Config$, response: Response$)=\u003e void): void{\n  \n}\n\nhttp.onFail(function(config, response){\n  console.log(`http request fail: `, config.url);\n});\n```\n\n#### 请求完成后，无论成功或者失败\n\n```typescript\nHttp.prototype.onComplete = function(func:(config: Config$, response: Response$)=\u003e void): void{\n  \n}\n\nhttp.onComplete(function(config, response){\n  console.log(`http request complete: `, config.url);\n});\n```\n\n#### 错误监听\n\n```typescript\nHttp.prototype.onError = function(func:(error: Error)=\u003e void): void{\n  \n}\n\nhttp.onError(function(error){\n  console.error(error);\n});\n```\n\n### 生命周期\n\n```\n        requestInterceptor \n                ↓\n            onRequest\n            ↙    ↘\n     onSuccess    onFail\n            ↘    ↙\n        responseInterceptor\n                ↓\n            onComplete\n    (onError run in hole life circle)\n```\n\n### 清除请求队列\n\n适用于小程序页面切换后，取消掉未发出去的http请求.\n\n```typescript\nHttp.prototype.clean = function() : void{\n  \n}\n\nhttp.clean();\n```\n\n### 自定义最高并发数量\n\n最高并发数量默认为5个\n\n```javascript\nimport {Http} from 'wxapp-http';\n\nconst http = new Http(3); // 设置最高并发3个\n\nhttp.get('https://www.google.com')\n    .then(function(response){\n      \n    })\n    .catch(function(error){\n      console.error(error);\n    });\n```\n\n## Contributing\n\n```bash\ngit clone https://github.com/axetroy/wxapp-http.git\ncd ./wxapp-http\nyarn\nyarn run start\n```\n\n1. 打开微信web开发者工具， 加载wxapp-http/example目录\n2. 修改index.js\n\nYou can flow [Contribute Guide](https://github.com/axetroy/wxapp-http/blob/master/contributing.md)\n\n## Contributors\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n| [\u003cimg src=\"https://avatars1.githubusercontent.com/u/9758711?v=3\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003eAxetroy\u003c/sub\u003e](http://axetroy.github.io)\u003cbr /\u003e[💻](https://github.com/gpmer/gpm.js/commits?author=axetroy) 🔌 [⚠️](https://github.com/gpmer/gpm.js/commits?author=axetroy) [🐛](https://github.com/gpmer/gpm.js/issues?q=author%3Aaxetroy) 🎨 |\n| :---: |\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\n## License\n\nThe [MIT License](https://github.com/axetroy/wxapp-http/blob/master/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxetroy%2Fwxapp-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faxetroy%2Fwxapp-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxetroy%2Fwxapp-http/lists"}