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

Commit

Permalink
feat: 支持更多HTTP请求方法
Browse files Browse the repository at this point in the history
  • Loading branch information
whinc committed Sep 29, 2019
1 parent f47ab15 commit 80c6ced
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
43 changes: 12 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/ApiSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const removeUndefinedValue = target => {
.reduce((obj, key) => Object.assign(obj, { [key]: target[key] }), {})
}

const httpMethodRegExp = /GET|POST|DELETE|HEAD|OPTIONS|PUT|PATCH/i

export interface ApiResponse<T> extends IResponse<T> {
/**
* 请求接口描述符
Expand Down Expand Up @@ -50,7 +52,7 @@ interface CommonApiDescriptor {
/**
* HTTP 请求方法
*
* 支持 `"GET" | "POST"`
* 支持 `GET|POST|DELETE|HEAD|OPTIONS|PUT|PATCH`
*
* 默认`"GET"`
*/
Expand Down Expand Up @@ -441,7 +443,7 @@ export class ApiSharp {

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

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

warning(
Expand Down
26 changes: 15 additions & 11 deletions src/http_client/IHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { ProcessedApiDescriptor } from "ApiSharp"

export type HttpMethod = "get" | "GET" | "post" | "POST"
// | 'delete'
// | 'DELETE'
// | 'head'
// | 'HEAD'
// | 'options'
// | 'OPTIONS'
// | 'put'
// | 'PUT'
// | 'patch'
// | 'PATCH'
export type HttpMethod =
| "get"
| "GET"
| "post"
| "POST"
| "delete"
| "DELETE"
| "head"
| "HEAD"
| "options"
| "OPTIONS"
| "put"
| "PUT"
| "patch"
| "PATCH"

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

Expand Down
3 changes: 3 additions & 0 deletions test/ApiSharp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ describe("测试 ApiSharp.processApi() 方法", () => {
test('api.method === "post" 时使用 "POST" 方法', () => {
expect(apiSharp.processApi({ ...api, method: "post" }).method).toBe("POST")
})
test('api.method === "options" 时使用 "OPTIONS" 方法', () => {
expect(apiSharp.processApi({ ...api, method: "options" }).method).toBe("OPTIONS")
})
})

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

0 comments on commit 80c6ced

Please sign in to comment.