From 01e4167ad49d1befa0278653c2c61413901a7754 Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Mon, 27 Mar 2023 21:29:14 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E4=BF=AE=E6=94=B9=20readme=20=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/config/adapter.md | 39 +++++++++++++---- package.json | 1 + test/helpers/is.test.ts | 19 +++++---- test/helpers/url.test.ts | 87 -------------------------------------- test/helpers/utils.test.ts | 15 ++++--- tsconfig.json | 3 +- 7 files changed, 53 insertions(+), 113 deletions(-) delete mode 100644 test/helpers/url.test.ts diff --git a/README.md b/README.md index 2e55ae4..02a2f0d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![npm version](https://badge.fury.io/js/axios-miniprogram.svg)](https://badge.fury.io/js/axios-miniprogram) [![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT) -[中文文档](axios-miniprogram.com) +[中文文档](https://axios-miniprogram.com) ## 安装 diff --git a/docs/config/adapter.md b/docs/config/adapter.md index 16dffcb..3ac8f00 100644 --- a/docs/config/adapter.md +++ b/docs/config/adapter.md @@ -7,6 +7,8 @@ ```typescript axios.defaults.adapter = function adapter(adapterConfig) { const { + // 请求类型 + type, // 请求地址 url, // 请求方法 @@ -38,13 +40,34 @@ axios.defaults.adapter = function adapter(adapterConfig) { } = adapterConfig; // 在 adapterConfig 中选择您需要的参数发送请求 - return wx.request({ - url, - method, - data, - headers, - success, - fail, - }); + switch (type) { + case 'request': // 数据请求 + return wx.request({ + url, + method, + data, + headers, + success, + fail, + }); + case 'upload': // 上传 + return wx.uploadFile({ + url, + method, + data, + headers, + success, + fail, + }); + case 'download': // 下载 + return wx.downloadFile({ + url, + method, + data, + headers, + success, + fail, + }); + } }; ``` diff --git a/package.json b/package.json index 5165bdd..2acdb2b 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", "upload:asset": "esno scripts/upload-asset.ts", "test": "vitest run", + "test:watch": "vitest", "lint": "eslint --cache .", "lint:fix": "pnpm lint --fix", "docs:dev": "pnpm -C docs dev", diff --git a/test/helpers/is.test.ts b/test/helpers/is.test.ts index eb18904..549c91b 100644 --- a/test/helpers/is.test.ts +++ b/test/helpers/is.test.ts @@ -1,3 +1,4 @@ +import { describe, test, expect } from 'vitest'; import { isArray, isDate, @@ -11,7 +12,7 @@ import { } from '../../src/helpers/is'; describe('对 src/helpers/is.ts 进行测试', () => { - it('测试 isArray() 执行结果是否符合预期', () => { + test('传入数组应该返回 true,其他参数应该返回 fasle', () => { expect(isArray([0])).toBe(true); expect(isArray([])).toBe(true); expect(isArray({})).toBe(false); @@ -21,7 +22,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isArray(null)).toBe(false); }); - it('测试 isDate() 执行结果是否符合预期', () => { + test('传入 Date 实例应该返回 true,其他参数应该返回 fasle', () => { expect(isDate(new Date())).toBe(true); expect(isDate({})).toBe(false); expect(isDate([])).toBe(false); @@ -31,7 +32,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isDate(null)).toBe(false); }); - it('测试 isEmptyArray() 执行结果是否符合预期', () => { + test('传入空数组应该返回 true,其他参数应该返回 fasle', () => { expect(isEmptyArray([])).toBe(true); expect(isEmptyArray([0])).toBe(false); expect(isEmptyArray({})).toBe(false); @@ -41,7 +42,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isEmptyArray(null)).toBe(false); }); - it('测试 isEmptyObject() 执行结果是否符合预期', () => { + test('传入空对象应该返回 true,其他参数应该返回 fasle', () => { expect(isEmptyObject({})).toBe(true); expect(isEmptyObject({ a: 0 })).toBe(false); expect(isEmptyObject([0])).toBe(false); @@ -52,7 +53,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isEmptyObject(null)).toBe(false); }); - it('测试 isFunction() 执行结果是否符合预期', () => { + test('传入空函数应该返回 true,其他参数应该返回 fasle', () => { expect( isFunction(() => { return; @@ -71,7 +72,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isFunction(null)).toBe(false); }); - it('测试 isNull() 执行结果是否符合预期', () => { + test('传入空 null 应该返回 true,其他参数应该返回 fasle', () => { expect(isNull(null)).toBe(true); expect(isNull({ a: 0 })).toBe(false); expect(isNull([0])).toBe(false); @@ -81,7 +82,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isNull(undefined)).toBe(false); }); - it('测试 isPlainObject() 执行结果是否符合预期', () => { + test('传入普通对象应该返回 true,其他参数应该返回 fasle', () => { expect(isPlainObject({})).toBe(true); expect(isPlainObject({ a: 0 })).toBe(true); expect(isPlainObject([0])).toBe(false); @@ -92,7 +93,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isPlainObject(null)).toBe(false); }); - it('测试 isString() 执行结果是否符合预期', () => { + test('传入字符串应该返回 true,其他参数应该返回 fasle', () => { expect(isString('')).toBe(true); expect(isString({})).toBe(false); expect(isString({ a: 0 })).toBe(false); @@ -103,7 +104,7 @@ describe('对 src/helpers/is.ts 进行测试', () => { expect(isString(null)).toBe(false); }); - it('测试 isUndefined() 执行结果是否符合预期', () => { + test('传入 undefined 应该返回 true,其他参数应该返回 fasle', () => { expect(isUndefined(undefined)).toBe(true); expect(isUndefined('')).toBe(false); expect(isUndefined({})).toBe(false); diff --git a/test/helpers/url.test.ts b/test/helpers/url.test.ts deleted file mode 100644 index 14dbbac..0000000 --- a/test/helpers/url.test.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { - buildURL, - combineURL, - dynamicInterpolation, - isAbsoluteURL, - isDynamicURL, -} from '../../src/helpers/url'; - -describe('对 src/helpers/url.ts 进行测试', () => { - it('测试 buildURL() 执行结果是否符合预期', () => { - expect(buildURL('/api')).toBe('/api'); - expect(buildURL('/api', {})).toBe('/api'); - expect(buildURL('/api#id=1', {})).toBe('/api'); - expect( - buildURL('/api', { - id: 1, - }), - ).toBe('/api?id=1'); - expect(buildURL('/api', { id: 100 }, () => 'id=1')).toBe('/api?id=1'); - expect( - buildURL('/api?sid=0', { - id: 1, - }), - ).toBe('/api?sid=0&id=1'); - expect(buildURL('/api?sid=0', { id: 100 }, () => 'id=1')).toBe( - '/api?sid=0&id=1', - ); - }); - - it('测试 combineURL() 执行结果是否符合预期', () => { - expect(combineURL('https://www.server.com', 'api')).toBe( - 'https://www.server.com/api', - ); - expect(combineURL('https://www.server.com/', '/api')).toBe( - 'https://www.server.com/api', - ); - expect(combineURL('https://www.server.com:8080//', '//api//')).toBe( - 'https://www.server.com:8080/api/', - ); - }); - - it('测试 dynamicInterpolation() 执行结果是否符合预期', () => { - expect( - dynamicInterpolation('https://www.server.com/api/user/:id', { - id: 1, - name: 'user', - }), - ).toBe('https://www.server.com/api/user/1'); - expect( - dynamicInterpolation('https://www.server.com:8080/api/user/:id', { - id: 1, - name: 'user', - }), - ).toBe('https://www.server.com:8080/api/user/1'); - expect( - dynamicInterpolation('https://www.server.com/api/user/:id/:name', { - id: 1, - }), - ).toBe('https://www.server.com/api/user/1/undefined'); - expect( - dynamicInterpolation('https://www.server.com/api/user/:id:name', { - id: 1, - }), - ).toBe('https://www.server.com/api/user/1undefined'); - }); - - it('测试 isAbsoluteURL() 执行结果是否符合预期', () => { - expect(isAbsoluteURL('')).toBe(false); - expect(isAbsoluteURL('/api')).toBe(false); - expect(isAbsoluteURL('http:')).toBe(false); - expect(isAbsoluteURL('//file')).toBe(true); - expect(isAbsoluteURL('https://www.server.com')).toBe(true); - expect(isAbsoluteURL('file://')).toBe(true); - }); - - it('测试 isDynamicURL() 执行结果是否符合预期', () => { - expect(isDynamicURL('')).toBe(false); - expect(isDynamicURL(':id')).toBe(true); - expect(isDynamicURL(':8080')).toBe(false); - expect(isDynamicURL('/:id')).toBe(true); - expect(isDynamicURL('/:8080')).toBe(false); - expect(isDynamicURL('https://www.server.com:8080')).toBe(false); - expect(isDynamicURL('/api')).toBe(false); - expect(isDynamicURL('/api:id')).toBe(true); - expect(isDynamicURL('/api/:id')).toBe(true); - }); -}); diff --git a/test/helpers/utils.test.ts b/test/helpers/utils.test.ts index 3948c9a..d74690a 100644 --- a/test/helpers/utils.test.ts +++ b/test/helpers/utils.test.ts @@ -1,3 +1,4 @@ +import { describe, test, expect } from 'vitest'; import { assert, deepMerge, @@ -9,13 +10,13 @@ import { } from '../../src/helpers/utils'; describe('对 src/helpers/utils.ts 进行测试', () => { - it('测试 assert() 执行结果是否符合预期', () => { + test('测试 assert() 是否符合预期', () => { expect(assert(true, '')).toBeUndefined(); expect(() => assert(false, '')).toThrow(); expect(() => assert(false, 'msg')).toThrowError('[axios-miniprogram]: msg'); }); - it('测试 deepMerge() 执行结果是否符合预期', () => { + test('测试 deepMerge() 是否符合预期', () => { expect(deepMerge({})).toEqual({}); expect(deepMerge({ a: 1 }, { b: 2 })).toEqual({ a: 1, b: 2 }); expect(deepMerge({ a: { a: 1 } }, { a: { b: 2 } })).toEqual({ @@ -29,19 +30,19 @@ describe('对 src/helpers/utils.ts 进行测试', () => { }); }); - it('测试 omit() 执行结果是否符合预期', () => { + test('测试 omit() 是否符合预期', () => { expect(omit({})).toEqual({}); expect(omit({ a: 1, b: 1 }, 'a')).toEqual({ b: 1 }); expect(omit({ a: 1, b: 1 }, 'a', 'b')).toEqual({}); }); - it('测试 pick() 执行结果是否符合预期', () => { + test('测试 pick() 是否符合预期', () => { expect(pick({})).toEqual({}); expect(pick({ a: 1, b: 1 }, 'a')).toEqual({ a: 1 }); expect(pick({ a: 1, b: 1 }, 'a', 'b')).toEqual({ a: 1, b: 1 }); }); - it('测试 throwError() 执行结果是否符合预期', () => { + test('测试 throwError() 是否符合预期', () => { expect(() => throwError('')).toThrowError('[axios-miniprogram]: '); expect(() => throwError('msg')).toThrowError('[axios-miniprogram]: msg'); expect(() => throwError(' msg ')).toThrowError( @@ -49,14 +50,14 @@ describe('对 src/helpers/utils.ts 进行测试', () => { ); }); - it('测试 toLowerCase() 执行结果是否符合预期', () => { + test('测试 toLowerCase() 是否符合预期', () => { expect(toLowerCase('', 'GET')).toBe(''); expect(toLowerCase(undefined, 'GET')).toBe('get'); expect(toLowerCase('GET', '')).toBe('get'); expect(toLowerCase('Get', '')).toBe('get'); }); - it('测试 toUpperCase() 执行结果是否符合预期', () => { + test('测试 toUpperCase() 是否符合预期', () => { expect(toUpperCase('', 'get')).toBe(''); expect(toUpperCase(undefined, 'get')).toBe('GET'); expect(toUpperCase('get', '')).toBe('GET'); diff --git a/tsconfig.json b/tsconfig.json index cf59f9d..505f2ca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "target": "ESNext", "module": "ESNext", "strict": true, - "noEmit": true + "noEmit": true, + "moduleResolution": "node" }, "include": ["./src", "./test", "./global.d.ts"], "exclude": ["node_modules"]