Skip to content
This repository was archived by the owner on Feb 11, 2022. It is now read-only.

Commit ea824e9

Browse files
committed
feat: 支持更多HTTP请求方法
1 parent 10a79a0 commit ea824e9

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

package-lock.json

+12-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ApiSharp.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const removeUndefinedValue = target => {
1919
.reduce((obj, key) => Object.assign(obj, { [key]: target[key] }), {})
2020
}
2121

22+
const httpMethodRegExp = /GET|POST|DELETE|HEAD|OPTIONS|PUT|PATCH/i
23+
2224
export interface ApiResponse<T> extends IResponse<T> {
2325
/**
2426
* 请求接口描述符
@@ -50,7 +52,7 @@ interface CommonApiDescriptor {
5052
/**
5153
* HTTP 请求方法
5254
*
53-
* 支持 `"GET" | "POST"`
55+
* 支持 `GET|POST|DELETE|HEAD|OPTIONS|PUT|PATCH`
5456
*
5557
* 默认`"GET"`
5658
*/
@@ -441,7 +443,7 @@ export class ApiSharp {
441443

442444
_api.baseURL = _api.baseURL.replace(/\/+$/, "")
443445

444-
invariant(/get|post/i.test(_api.method), `method 期望值为 get|post 其一,实际值为"${_api.method}"`)
446+
invariant(httpMethodRegExp.test(_api.method), `无效的 HTTP 方法:"${_api.method}"`)
445447
_api.method = _api.method.toUpperCase() as HttpMethod
446448

447449
warning(

src/http_client/IHttpClient.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { ProcessedApiDescriptor } from "ApiSharp"
22

3-
export type HttpMethod = "get" | "GET" | "post" | "POST"
4-
// | 'delete'
5-
// | 'DELETE'
6-
// | 'head'
7-
// | 'HEAD'
8-
// | 'options'
9-
// | 'OPTIONS'
10-
// | 'put'
11-
// | 'PUT'
12-
// | 'patch'
13-
// | 'PATCH'
3+
export type HttpMethod =
4+
| "get"
5+
| "GET"
6+
| "post"
7+
| "POST"
8+
| "delete"
9+
| "DELETE"
10+
| "head"
11+
| "HEAD"
12+
| "options"
13+
| "OPTIONS"
14+
| "put"
15+
| "PUT"
16+
| "patch"
17+
| "PATCH"
1418

1519
export type HttpHeader = { [key: string]: string }
1620

test/ApiSharp.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ describe("测试 ApiSharp.processApi() 方法", () => {
185185
test('api.method === "post" 时使用 "POST" 方法', () => {
186186
expect(apiSharp.processApi({ ...api, method: "post" }).method).toBe("POST")
187187
})
188+
test('api.method === "options" 时使用 "OPTIONS" 方法', () => {
189+
expect(apiSharp.processApi({ ...api, method: "options" }).method).toBe("OPTIONS")
190+
})
188191
})
189192

190193
test("测试 api.description 取值", () => {

0 commit comments

Comments
 (0)