From e180558b257c7e5484f449a0576e67122e74be6d Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Mon, 10 Apr 2023 15:03:59 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E6=A8=A1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/ISSUE_TEMPLATE/bug_report.yml | 49 +++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 1 + .github/ISSUE_TEMPLATE/feature_request.yml | 44 +++++++++++++++++ CODE_OF_CONDUCT.md | 45 +++++++++++++++++ CONTRIBUTING.md | 56 ++++++++++++++++++++++ src/adapter.ts | 4 +- src/helpers/buildURL.ts | 6 --- test/axios.api.test.ts | 22 +++++++++ test/core/dispatchRequest.test.ts | 33 +++++++++++++ test/helpers/buildURL.test.ts | 6 +-- 10 files changed, 252 insertions(+), 14 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.yml create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/ISSUE_TEMPLATE/feature_request.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml new file mode 100644 index 0000000..ebf6ff4 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -0,0 +1,49 @@ +name: 🐞 问题报告 +description: 报告 axios-miniprogram 的问题。 +labels: [pending triage] +body: + - type: markdown + attributes: + value: | + 感谢您花时间填写此错误报告! + - type: textarea + id: bug-description + attributes: + label: 描述错误 + description: 对错误是什么的清晰简洁的描述。如果您打算为此问题提交 PR,请在描述中告诉我们。谢谢! + placeholder: 发表评论 + validations: + required: true + - type: textarea + id: additional-context + attributes: + label: 附加上下文 + description: 有关问题的任何其他上下文或屏幕截图。 + - type: dropdown + id: package-manager + attributes: + label: 使用过的包管理器 + description: 选择使用的包管理器 + options: + - npm + - yarn + - pnpm + - 源码包(指直接下载 zip 文件的方式) + validations: + required: true + - type: checkboxes + id: checkboxes + attributes: + label: 验证 + description: 在提交问题之前,请确保您执行以下操作 + options: + - label: 遵守我们的[行为准则 ](https://github.com/zjx0905/axios-miniprogram/blob/main/CODE_OF_CONDUCT.md) + required: true + - label: 阅读[贡献指南](https://github.com/zjx0905/axios-miniprogram/blob/main/CONTRIBUTING.md). + required: true + - label: 阅读[文档](https://axios-miniprogram.com). + required: true + - label: 检查是否还没有报告相同错误的[问题](https://github.com/zjx0905/axios-miniprogram/issues)以避免创建重复。 + required: true + - label: 确保这是 axios-miniprogram 问题而不是特定于框架的问题。 + required: true diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..3ba13e0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1 @@ +blank_issues_enabled: false diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml new file mode 100644 index 0000000..cf7e04f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -0,0 +1,44 @@ +name: 🚀 新功能提案 +description: 提出要添加到 axios-miniprogram 的新功能 +labels: [enhancement] +body: + - type: markdown + attributes: + value: | + 感谢您对该项目的关注并抽出宝贵时间填写此专题报告! + - type: textarea + id: feature-description + attributes: + label: 清晰简洁的问题描述 + description: '作为使用 axios-miniprogram 的开发人员,我想要 [目标/愿望] 以便 [受益]。如果您打算为此问题提交 PR,请在描述中告诉我们。谢谢!' + validations: + required: true + - type: textarea + id: suggested-solution + attributes: + label: 建议的解决方案 + description: '在模块 [xy] 中,我们可以提供以下实现......' + validations: + required: true + - type: textarea + id: alternative + attributes: + label: 选择 + description: 清晰简洁地描述您考虑过的任何替代解决方案或功能。 + - type: textarea + id: additional-context + attributes: + label: 附加上下文 + description: 有关功能请求的任何其他上下文或屏幕截图。 + - type: checkboxes + id: checkboxes + attributes: + label: 验证 + description: 在提交问题之前,请确保您执行以下操作 + options: + - label: 遵守我们的[行为准则 ](https://github.com/zjx0905/axios-miniprogram/blob/main/CODE_OF_CONDUCT.md) + required: true + - label: 阅读[贡献指南](https://github.com/zjx0905/axios-miniprogram/blob/main/CONTRIBUTING.md). + required: true + - label: 阅读[文档](https://axios-miniprogram.com). + required: true diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..c8d119a --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,45 @@ +# 贡献者契约行为准则 + +## 我们的承诺 + +为了营造一个开放和热情的环境,我们作为贡献者和维护者承诺,无论年龄、体型、残疾、种族、性别特征、性别认同如何,参与我们的项目和我们的社区对每个人来说都是一种无骚扰的体验和表达、经验水平、教育、社会经济地位、国籍、个人外表、种族、宗教或性认同和取向。 + +## 我们的标准 + +有助于创造积极环境的行为示例包括: + +- 使用欢迎和包容的语言 +- 尊重不同的观点和经验 +- 优雅地接受建设性的批评 +- 专注于对社区最有利的事情 +- 对其他社区成员表示同情 + +参与者不可接受的行为示例包括: + +- 使用色情语言或图像以及不受欢迎的性关注或挑逗 +- 拖钓、侮辱/贬损评论以及人身或政治攻击 +- 公开或私下骚扰 +- 未经明确许可发布他人的私人信息,例如物理地址或电子地址 +- 在专业环境中可以合理地认为不适当的其他行为 + +## 我们的责任 + +项目维护者负责阐明可接受行为的标准,并期望采取适当和公平的纠正措施来响应任何不可接受的行为实例。 + +项目维护者有权利和责任删除、编辑或拒绝不符合本行为准则的评论、提交、代码、维基编辑、问题和其他贡献,或暂时或永久禁止任何贡献者的其他行为他们认为不合适的、威胁性的、冒犯性的或有害的。 + +## 范围 + +当个人代表项目或其社区时,本行为准则适用于项目空间和公共空间。代表项目或社区的示例包括使用官方项目电子邮件地址、通过官方社交媒体帐户发帖,或在在线或离线活动中担任指定代表。项目维护者可以进一步定义和阐明项目的表示。 + +## 执法 + +可通过 954270063@qq.com 联系项目团队报告辱骂、骚扰或其他不可接受的行为。所有投诉都将得到审查和调查,并将根据情况做出必要且适当的回应。项目团队有义务为事件的报告者保密。具体执行政策的更多细节可能会单独发布。 + +不善意遵守或执行行为准则的项目维护者可能会面临由项目领导的其他成员确定的暂时或永久的影响。 + +## 归因 + +本行为准则改编自 [Contributor Covenant](https://www.contributor-covenant.org) 1.4 版,可在 https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 获取 + +有关此行为准则的常见问题的答案,请参阅 https://www.contributor-covenant.org/faq diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..1b70b01 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,56 @@ +# 贡献 + +感谢您有兴趣为这个项目做出贡献! + +## 发展 + +### 设置 + +将此 repo 克隆到本地计算机并安装依赖项。 + +```bash +pnpm install +``` + +我们使用 VitePress 进行快速开发和文档编制。您可以通过以下方式在本地启动它。 + +```bash +pnpm docs:dev +``` + +我们使用 Vitest 进行单元测试。您可以通过以下方式在本地启动它。 + +```bash +pnpm test:watch +``` + +## 贡献 + +### 现有功能 + +随意增强现有功能。请尽量不要引入重大更改。 + +### 新功能 + +添加新功能有一些注意事项。 + +- 在你开始工作之前,最好先开一个 [issue](https://github.com/zjx0905/axios-miniprogram/issues) 来讨论。 +- 应该对新功能进行单元测试。 +- 应该在文档中添加相关的使用介绍及方法。 + +## Project Structure + +``` +src/ - 源代码 +docs/ - 文档 +test/ - 单元测试 +scripts/ - 脚本及工具函数 +``` + +## 代码风格 + +不用担心代码风格,只要安装好开发依赖即可。Git 钩子将在提交时为您格式化和修复它们。 + +## 谢谢 + +再次感谢您对本项目感兴趣!你太棒了! diff --git a/src/adapter.ts b/src/adapter.ts index 3859092..aaacef9 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -4,7 +4,7 @@ import { isPlainObject, isUndefined, } from './helpers/isTypes'; -import { assert, throwError } from './helpers/error'; +import { assert } from './helpers/error'; import { AxiosProgressCallback, AxiosRequestFormData, @@ -203,8 +203,6 @@ export function createAdapter(platform: AxiosPlatform) { return processUpload(platform.upload, baseOptions); case 'download': return processDownload(platform.download, baseOptions); - default: - throwError(`无法识别的请求类型 ${config.type}`); } } diff --git a/src/helpers/buildURL.ts b/src/helpers/buildURL.ts index 8063c3d..ec28264 100644 --- a/src/helpers/buildURL.ts +++ b/src/helpers/buildURL.ts @@ -5,16 +5,10 @@ export function buildURL( params?: AnyObject, paramsSerializer = defaultSerializer, ): string { - const hashIndex = url.indexOf('#'); - if (hashIndex !== -1) { - url = url.slice(0, hashIndex); - } - const paramsStr = paramsSerializer(params); if (paramsStr) { url = `${url}${url.indexOf('?') === -1 ? '?' : '&'}${paramsStr}`; } - return url; } diff --git a/test/axios.api.test.ts b/test/axios.api.test.ts index c84fca0..64bf5ab 100644 --- a/test/axios.api.test.ts +++ b/test/axios.api.test.ts @@ -4,6 +4,7 @@ import { CancelToken, isCancel } from '@/core/cancel'; import { isAxiosError } from '@/core/createError'; import { createAdapter } from '@/adapter'; import axios from '@/axios'; +import defaults from '@/defaults'; describe('src/axios.ts', () => { test('应该有这些静态属性', () => { @@ -14,4 +15,25 @@ describe('src/axios.ts', () => { expect(axios.isCancel).toBe(isCancel); expect(axios.isAxiosError).toBe(isAxiosError); }); + + test('应该可以创建实例', () => { + const i1 = axios.create(); + const i2 = axios.create({ + baseURL: 'http://api.com', + }); + expect(i1.defaults).toEqual(defaults); + expect(i2.defaults).toEqual({ ...defaults, baseURL: 'http://api.com' }); + }); + + test('创建的实例应该有这些实例属性及方法', () => { + expect(axios.defaults).toBe(defaults); + expect(axios.interceptors).toBeTypeOf('object'); + expect(axios.getUri).toBeTypeOf('function'); + expect(axios.fork).toBeTypeOf('function'); + expect(axios.request).toBeTypeOf('function'); + + [...Axios.as, ...Axios.asp, ...Axios.asd].forEach((k) => { + expect(axios[k]).toBeTypeOf('function'); + }); + }); }); diff --git a/test/core/dispatchRequest.test.ts b/test/core/dispatchRequest.test.ts index 13f86b2..75bbe1e 100644 --- a/test/core/dispatchRequest.test.ts +++ b/test/core/dispatchRequest.test.ts @@ -145,6 +145,39 @@ describe('src/core/dispatchRequest.ts', () => { } }); + test('应该支持异步的自定义异常处理器', async () => { + const e1 = vi.fn(); + const e2 = vi.fn(); + const c1 = { + ...defaults, + adapter: mockAdapterError(), + url: 'test', + errorHandler: e1, + }; + const c2 = { + ...defaults, + adapter: mockAdapterFail(), + url: 'test', + errorHandler: e2, + }; + + try { + await dispatchRequest(c1); + } catch (err) { + expect(e1).toBeCalled(); + expect(e1.mock.calls[0][0]).toBe(err); + expect(axios.isAxiosError(err)).toBeTruthy(); + } + + try { + await dispatchRequest(c2); + } catch (err) { + expect(e2).toBeCalled(); + expect(e2.mock.calls[0][0]).toBe(err); + expect(axios.isAxiosError(err)).toBeTruthy(); + } + }); + test('请求发送前取消请求应该抛出异常', async () => { const cb = vi.fn(); const { cancel, token } = axios.CancelToken.source(); diff --git a/test/helpers/buildURL.test.ts b/test/helpers/buildURL.test.ts index 86474d1..1b241a7 100644 --- a/test/helpers/buildURL.test.ts +++ b/test/helpers/buildURL.test.ts @@ -6,13 +6,9 @@ describe('src/helpers/buildURL.ts', () => { expect(buildURL('/test')).toBe('/test'); }); - test('应该清理哈希值', () => { - expect(buildURL('/test#hash')).toBe('/test'); - }); - test('应该对参数进行系列化', () => { expect( - buildURL('/test#hash', { + buildURL('/test', { v1: 1, v2: undefined, v3: null,