{"id":26062573,"url":"https://github.com/aiden-fe/compass-telegram","last_synced_at":"2025-03-08T16:01:03.659Z","repository":{"id":85062991,"uuid":"606377916","full_name":"Aiden-FE/compass-telegram","owner":"Aiden-FE","description":"基于Axios封装的Http请求模块,支持浏览器环境与node双端使用","archived":false,"fork":false,"pushed_at":"2024-04-03T03:22:12.000Z","size":218,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-05T03:48:19.785Z","etag":null,"topics":["axios","axios-retry","browser","chain","http","http-client","http-request","node","request"],"latest_commit_sha":null,"homepage":"https://aiden-fe.github.io/compass-telegram/","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/Aiden-FE.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-02-25T10:00:57.000Z","updated_at":"2024-03-27T09:12:01.000Z","dependencies_parsed_at":"2024-03-27T09:47:16.965Z","dependency_job_id":null,"html_url":"https://github.com/Aiden-FE/compass-telegram","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"a0966d95eb0267219dc8792a969f2e5c614f6cd5"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aiden-FE%2Fcompass-telegram","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aiden-FE%2Fcompass-telegram/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aiden-FE%2Fcompass-telegram/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aiden-FE%2Fcompass-telegram/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aiden-FE","download_url":"https://codeload.github.com/Aiden-FE/compass-telegram/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242573496,"owners_count":20151707,"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":["axios","axios-retry","browser","chain","http","http-client","http-request","node","request"],"created_at":"2025-03-08T16:00:37.794Z","updated_at":"2025-03-08T16:01:03.633Z","avatar_url":"https://github.com/Aiden-FE.png","language":"TypeScript","readme":"# @compass-aiden/telegram\n\n\u003e 基于 [Axios](https://axios-http.com/) 封装的Http请求模块\n\n## Getting Started\n\n### web项目使用\n\nnpm方式安装:\n\n`npm install @compass-aiden/telegram`\n\n```typescript\n// 自动识别导入esm文件\nimport Telegram from '@compass-aiden/telegram';\n// 通过别名路径导入esm文件\nimport Telegram from '@compass-aiden/telegram/esm';\n```\n\n浏览器script标签安装:\n\n```html\n\u003c!-- 请根据个人需求采用unpkg或者jsdelivr链接 --\u003e\n\u003cscript src=\"https://unpkg.com/@compass-aiden/telegram@latest/dist/main.umd.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  console.log(window['@compass-aiden/telegram']);\n\u003c/script\u003e\n```\n\n### node项目使用\n\nnpm方式安装:\n\n`npm install @compass-aiden/telegram`\n\n```typescript\n// 自动导入cjs文件\nconst Telegram = require('@compass-aiden/telegram');\n\n/** 在type: module启用ESM环境下,请参考如下方式 */\n// 通过别名路径导入cjs文件,如果不能识别条件导出,tsconfig可设置 `{ \"moduleResolution\": \"bundler\" }`\nimport Telegram from '@compass-aiden/telegram/cjs';\n// 自动导入默认cjs文件, 当 tsconfig 配置包含 `{ \"moduleResolution\": \"NodeNext\" }`时可用\nimport Telegram from '@compass-aiden/telegram';\n```\n\n### 用法演示\n\n```typescript\nimport Telegram from '@compass-aiden/telegram';\n\n/**\n * const telegram = new Telegram([option]) 注册默认域配置\n * telegram.register('domain', [option]) 注册新的domain业务域配置\n *\n * 默认域及register注册的新域基础配置都包含如下内容,可被配置项二次合并或覆盖:\n *  {\n *    timeout: 1000 * 10,\n *    headers: {\n *      'Content-Type': 'application/json;charset=utf-8',\n *    },\n *    responseType: 'json',\n *  },\n */\nconst api = new Telegram()\n  // 注册 gatewayB 域的配置\n  .register('gatewayB', {\n    baseURL: 'https://api.test.com/api',\n  })\n  // 注册拦截器的演示域\n  .register('interceptor', {\n    baseURL: 'https://api.qqsuu.cn/api',\n    interceptors: {\n      request: [\n        async (req) =\u003e {\n          console.log('第一个异步拦截器Req: ', req);\n          req.test = 'interceptor 1';\n          return req;\n        },\n        (req) =\u003e {\n          console.log('第二个同步拦截器Req: ', req);\n          req.test2 = 'interceptor 2';\n          return req;\n        },\n        (req) =\u003e {\n          console.log('最终req为: ', req);\n          return req;\n        },\n      ],\n      requestError: (error) =\u003e {\n        console.log('发生了请求前的未知异常: ', error);\n        throw error;\n      },\n      response: [\n        (data, res) =\u003e {\n          if (res.config.customMeta?.skipResponseInterceptor) {\n            console.log('接收到跳过拦截器的配置');\n            return res;\n          }\n          if (res.status \u003e= 200 \u0026\u0026 res.status \u003c 300) {\n            return data;\n          }\n          throw new Error('异常');\n        },\n        (data, res) =\u003e {\n          if (res.config.customMeta?.skipResponseInterceptor) {\n            console.log('接收到跳过拦截器的配置');\n            return res;\n          }\n          if (data?.code === 200) {\n            return data.data;\n          }\n          throw new Error(data?.msg || '业务异常');\n        },\n      ],\n      responseError: (error) =\u003e {\n        console.log('请求级异常发生', error);\n        // return { test: 'test' } // 如果不throw异常,则此行会将该对象作为返回值\n        throw error;\n      },\n    },\n  })\n  // 请求编码 域配置演示\n  .register('serializer', {\n    // 统一对请求进行编码处理\n    paramsSerializer: (params) =\u003e {\n      return Qs.stringify(params, { arrayFormat: 'brackets' });\n    },\n  });\n\n// 使用底层API直接请求\napi\n  .request({\n    url: 'https://api.test.com/api/demo',\n  })\n  .then((result) =\u003e console.log(result));\n\n// 使用底层API采用gatewayB域直接请求并取消\n// 请求取消测试\nconst cancelExample = api.request({\n  domain: 'gatewayB', // 指定域配置\n  url: 'demo',\n});\ncancelExample.then((result) =\u003e console.log(result));\ncancelExample.abort(); // 可以在合适的时机取消未完成的请求\n\n// 使用链式请求\napi\n  .chain()\n  .get('https://api.test.com/api/demo')\n  .request()\n  .then((result) =\u003e console.log(result));\n\n// 使用链式请求配置路径参数,url查询参数,body参数\napi\n  .chain()\n  .get('https://api.test.com/api/:test')\n  .path('test', 'dm-xjj')\n  .searchParams({ type: 'json2' })\n  .body({ test: 'test' })\n  .request()\n  .then((result) =\u003e console.log(result));\n\n// 拦截器演示\napi\n  .chain()\n  .domain('interceptor')\n  .post('demo')\n  .request()\n  .then((result) =\u003e console.log(result));\n\n// 自定义跳过拦截器\napi\n  .chain()\n  .domain('interceptor')\n  .post('demo')\n  .config({ customMeta: { skipResponseInterceptor: true } })\n  .request()\n  .then((result) =\u003e console.log(result));\n\n// 接口异常重试\napi\n  .chain()\n  .post('demo')\n  .retry(3)\n  .request()\n  .then((result) =\u003e console.log(result));\n\n// 接口异常重试,自定义重试间隔\napi\n  .chain()\n  .post('demo')\n  .retry({\n    retries: 3,\n    // 自定义延时\n    retryDelay: (count) =\u003e 1000 * count,\n  })\n  .request()\n  .then((result) =\u003e console.log(result));\n```\n\n更多链式用法参考以下链的声明:\n\n```typescript\ndeclare class TelegramChain {\n  /** 用于在链条末尾发起请求 */\n  request\u003cResult = any\u003e(): Promise\u003cResult\u003e \u0026 {\n    abort: Function;\n  };\n  /** 设置请求异常重试 */\n  retry(option: number | HttpTelegramReqConfig['axios-retry']): this;\n  /** 设置路径参数 */\n  path(key: string, value: string): this;\n  /** 批量设置路径参数 */\n  paths(data: Record\u003cstring, string\u003e): this;\n  /** 采用指定业务域的配置 */\n  domain(domain: string | symbol): this;\n  /** 设置url参数 */\n  searchParams(params: any): this;\n  /** 设置请求体参数 */\n  body(data: any): this;\n  /** 设置请求头信息 */\n  headers(headers: Partial\u003cHttpTelegramReqConfig['headers']\u003e): this;\n  /** 设置自定义元数据 */\n  customMeta(data: HttpTelegramReqConfig['customMeta']): this;\n  /** 设置响应数据类型 */\n  responseType(type: HttpTelegramReqConfig['responseType']): this;\n  /** 直接设置更多的请求配置 */\n  config(config: Partial\u003cHttpTelegramReqConfig\u003e): this;\n  /** 设置请求url,并指定为 get 请求 */\n  get(url: string): this;\n  /** 设置请求url,并指定为 post 请求 */\n  post(url: string): this;\n  /** 设置请求url,并指定为 delete 请求 */\n  delete(url: string): this;\n  /** 设置请求url,并指定为 put 请求 */\n  put(url: string): this;\n  /** 设置请求url,并指定为 head 请求 */\n  head(url: string): this;\n  /** 设置请求url,并指定为 options 请求 */\n  options(url: string): this;\n  /** 设置请求url,并指定为 patch 请求 */\n  patch(url: string): this;\n  /** 设置请求url,并指定为 purge 请求 */\n  purge(url: string): this;\n  /** 设置请求url,并指定为 link 请求 */\n  link(url: string): this;\n  /** 设置请求url,并指定为 unlink 请求 */\n  unlink(url: string): this;\n}\n```\n\n## Contributes\n\n### Install\n\n`pnpm install`\n\n### Base commands\n\n- `pnpm dev` 启用开发模式\n- `pnpm build` 生产构建\n- `pnpm lint` 代码校验\n- `pnpm format` 代码格式化\n- `pnpm test` 执行单元测试\n- `pnpm build:doc` 构建文档\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faiden-fe%2Fcompass-telegram","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faiden-fe%2Fcompass-telegram","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faiden-fe%2Fcompass-telegram/lists"}