feat: 支持 HTTP PATCH 请求
parent
6550415e29
commit
22bcefcb97
|
@ -34,6 +34,7 @@ export default defineConfig({
|
|||
{ text: 'HEAD 请求', link: '/basics/head' },
|
||||
{ text: 'POST 请求', link: '/basics/post' },
|
||||
{ text: 'PUT 请求', link: '/basics/put' },
|
||||
{ text: 'PATCH 请求', link: '/basics/patch' },
|
||||
{ text: 'DELETE 请求', link: '/basics/delete' },
|
||||
{ text: 'TRACE 请求', link: '/basics/trace' },
|
||||
{ text: 'CONNECT 请求', link: '/basics/connect' },
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
---
|
||||
title: PATCH 请求
|
||||
---
|
||||
|
||||
# {{ $frontmatter.title }}
|
||||
|
||||
::: tip {{ $frontmatter.title }}
|
||||
在 HTTP 协议中,请求方法 PATCH 用于对资源进行部分修改。
|
||||
|
||||
在 HTTP 协议中, PUT 方法已经被用来表示对资源进行整体覆盖,而 POST 方法则没有对标准的补丁格式的提供支持。不同于 PUT 方法,而与 POST 方法类似,PATCH 方法是非幂等的,这就意味着连续多个相同的请求会产生不同的效果。
|
||||
:::
|
||||
|
||||
## 普通的 `PATCH` 请求
|
||||
|
||||
您可以传入第一个参数 `url` 发送 `PATCH` 请求。
|
||||
|
||||
```ts
|
||||
import axios from 'axios-miniprogram';
|
||||
|
||||
axios
|
||||
.patch('https://api.com/test')
|
||||
.then((response) => {
|
||||
// 成功之后做些什么
|
||||
})
|
||||
.catch((error) => {
|
||||
// 失败之后做些什么
|
||||
});
|
||||
```
|
||||
|
||||
## 携带数据的 `PATCH` 请求
|
||||
|
||||
您也可以额外传入第二个参数 `data` 发送携带数据的 `PATCH` 请求。
|
||||
|
||||
```ts
|
||||
import axios from 'axios-miniprogram';
|
||||
|
||||
axios
|
||||
.patch('https://api.com/test/:id', {
|
||||
id: 1,
|
||||
name: 'test',
|
||||
})
|
||||
.then((response) => {
|
||||
// 成功之后做些什么
|
||||
})
|
||||
.catch((error) => {
|
||||
// 失败之后做些什么
|
||||
});
|
||||
```
|
||||
|
||||
## 携带自定义配置的 `PATCH` 请求
|
||||
|
||||
您也可以额外传入第三个参数 `config` 发送携带自定义配置的 `PATCH` 请求。
|
||||
|
||||
```ts
|
||||
import axios from 'axios-miniprogram';
|
||||
|
||||
axios
|
||||
.patch(
|
||||
'https://api.com/test/:id',
|
||||
{
|
||||
id: 1,
|
||||
name: 'test',
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
)
|
||||
.then((response) => {
|
||||
// 成功之后做些什么
|
||||
})
|
||||
.catch((error) => {
|
||||
// 失败之后做些什么
|
||||
});
|
||||
```
|
||||
|
||||
## 兼容性
|
||||
|
||||
<VPCompatibility tt='2.42.0' />
|
|
@ -55,7 +55,7 @@ import axios from 'axios-miniprogram';
|
|||
|
||||
axios
|
||||
.put(
|
||||
'https://api.com/test/id',
|
||||
'https://api.com/test/:id',
|
||||
{
|
||||
id: 1,
|
||||
name: 'test',
|
||||
|
|
|
@ -15,8 +15,7 @@
|
|||
"noUnusedLocals": true,
|
||||
"strictNullChecks": true,
|
||||
"isolatedModules": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"types": ["vitepress"]
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"include": [
|
||||
"./.vitepress/**/*.ts",
|
||||
|
|
|
@ -20,6 +20,7 @@ export type AxiosAdapterRequestMethod =
|
|||
| 'HEAD'
|
||||
| 'POST'
|
||||
| 'PUT'
|
||||
| 'PATCH'
|
||||
| 'DELETE'
|
||||
| 'TRACE'
|
||||
| 'CONNECT';
|
||||
|
@ -61,16 +62,7 @@ export interface AxiosAdapterResponseError extends AnyObject {
|
|||
/**
|
||||
* 错误数据
|
||||
*/
|
||||
data?: {
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
errMsg: string;
|
||||
/**
|
||||
* Errno错误码
|
||||
*/
|
||||
errno: number;
|
||||
};
|
||||
data?: AnyObject;
|
||||
}
|
||||
|
||||
export interface AxiosAdapterRequestConfig extends AnyObject {
|
||||
|
|
|
@ -25,6 +25,7 @@ export type AxiosRequestMethod =
|
|||
| 'head'
|
||||
| 'post'
|
||||
| 'put'
|
||||
| 'patch'
|
||||
| 'delete'
|
||||
| 'trace'
|
||||
| 'connect';
|
||||
|
|
|
@ -88,7 +88,7 @@ export default class AxiosDomain {
|
|||
/**
|
||||
* 带请求数据的请求别名
|
||||
*/
|
||||
static asd = ['post', 'put'] as const;
|
||||
static asd = ['post', 'put', 'patch'] as const;
|
||||
|
||||
/**
|
||||
* 默认请求配置
|
||||
|
@ -125,6 +125,11 @@ export default class AxiosDomain {
|
|||
*/
|
||||
put!: AxiosDomainAsRequestWithData;
|
||||
|
||||
/**
|
||||
* 发送 patch 请求
|
||||
*/
|
||||
patch!: AxiosDomainAsRequestWithData;
|
||||
|
||||
/**
|
||||
* 发送 delete 请求
|
||||
*/
|
||||
|
|
|
@ -20,6 +20,7 @@ export function flattenHeaders(
|
|||
'head',
|
||||
'post',
|
||||
'put',
|
||||
'patch',
|
||||
'delete',
|
||||
'trace',
|
||||
'connect',
|
||||
|
|
|
@ -12,6 +12,7 @@ const defaults: AxiosInstanceDefaults = {
|
|||
head: {},
|
||||
post: {},
|
||||
put: {},
|
||||
patch: {},
|
||||
delete: {},
|
||||
trace: {},
|
||||
connect: {},
|
||||
|
|
|
@ -18,7 +18,7 @@ describe('src/core/Axios.ts', () => {
|
|||
test('应该有这些静态属性', () => {
|
||||
expect(Axios.as).toEqual(['options', 'trace', 'connect']);
|
||||
expect(Axios.asp).toEqual(['head', 'get', 'delete']);
|
||||
expect(Axios.asd).toEqual(['post', 'put']);
|
||||
expect(Axios.asd).toEqual(['post', 'put', 'patch']);
|
||||
});
|
||||
|
||||
test('应该有这些实例属性及方法', () => {
|
||||
|
|
|
@ -7,7 +7,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
test('应该有这些静态属性', () => {
|
||||
expect(AxiosDomain.as).toEqual(['options', 'trace', 'connect']);
|
||||
expect(AxiosDomain.asp).toEqual(['head', 'get', 'delete']);
|
||||
expect(AxiosDomain.asd).toEqual(['post', 'put']);
|
||||
expect(AxiosDomain.asd).toEqual(['post', 'put', 'patch']);
|
||||
});
|
||||
|
||||
test('应该有这些实例属性', () => {
|
||||
|
|
|
@ -4,40 +4,9 @@ import Axios from '@/core/Axios';
|
|||
|
||||
describe('src/core/flattenHeaders.ts', () => {
|
||||
const keys = [...Axios.as, ...Axios.asp, ...Axios.asd];
|
||||
const baseHeaders = {
|
||||
options: {
|
||||
v1: 'options1',
|
||||
v2: 'options2',
|
||||
},
|
||||
trace: {
|
||||
v1: 'trace1',
|
||||
v2: 'trace2',
|
||||
},
|
||||
connect: {
|
||||
v1: 'connect1',
|
||||
v2: 'connect2',
|
||||
},
|
||||
head: {
|
||||
v1: 'head1',
|
||||
v2: 'head2',
|
||||
},
|
||||
get: {
|
||||
v1: 'get1',
|
||||
v2: 'get2',
|
||||
},
|
||||
delete: {
|
||||
v1: 'delete1',
|
||||
v2: 'delete2',
|
||||
},
|
||||
post: {
|
||||
v1: 'post1',
|
||||
v2: 'post2',
|
||||
},
|
||||
put: {
|
||||
v1: 'put1',
|
||||
v2: 'put2',
|
||||
},
|
||||
};
|
||||
const baseHeaders = Object.fromEntries(
|
||||
keys.map((k) => [k, { v1: `${k}1`, v2: `${k}2` }]),
|
||||
) as unknown as Record<(typeof keys)[number], AnyObject>;
|
||||
|
||||
test('应该支持空配置', () => {
|
||||
expect(flattenHeaders({})).toBeUndefined();
|
||||
|
|
|
@ -12,9 +12,8 @@
|
|||
"skipDefaultLibCheck": true,
|
||||
"paths": {
|
||||
"@/*": ["src/*"]
|
||||
},
|
||||
"types": ["vitest", "vitest/globals"]
|
||||
}
|
||||
},
|
||||
"include": ["./src", "./global.d.ts", "./global.variables.d.ts"],
|
||||
"include": ["./src", "./test", "./global.d.ts", "./global.variables.d.ts"],
|
||||
"exclude": ["node_modules", "**/*.md", "**/dist"]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue