mhy-http-ts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Fang WenBin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # Install
2
+
3
+ ---
4
+
5
+ ```
6
+ npm install @types/f-axios
7
+ ```
8
+
9
+ # Usage
10
+
11
+ ---
12
+
13
+ ```
14
+ import type { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
15
+ import { MethodEnum, ContentTypeEnum } from '@types/f-axios/enum';
16
+
17
+ import { Create as AxiosCreate, Canceler as AxiosCanceler, Retry as AxiosRetry } from '@types/f-axios';
18
+
19
+ const defaultRequestConfig: any = {
20
+ baseURL: '/api',
21
+ timeout: 10 * 1000,
22
+ headers: { 'Content-Type': ContentTypeEnum.JSON },
23
+ responseType: "json"
24
+ }
25
+
26
+ const defaultInterceptors: any = {
27
+ requestInterceptors: (config: AxiosRequestConfig, options?: any) => {
28
+ const token = ...;
29
+ const { withToken } = config || {};
30
+ if(withToken) {
31
+ config.headers.Authorization = token;
32
+ }
33
+ return config;
34
+ },
35
+
36
+ requestInterceptorsCatch: (error: AxiosError) => { ... },
37
+
38
+ responseInterceptors: (response: AxiosResponse<any>) => {
39
+ return response;
40
+ },
41
+
42
+ responseInterceptorsCatch: (error: AxiosError, instance: AxiosInstance) => {
43
+ const { config } = error || {};
44
+ const { method } = config || {};
45
+ // const retryRequest = new AxiosRetry();
46
+ // retryRequest.retry(instance, error);
47
+ return Promise.reject(error);
48
+ },
49
+
50
+ beforeRequestHook: (config: any) => {
51
+ const { method, params } = config || {};
52
+ if(method.toUpperCase() === MethodEnum.GET) {
53
+ ...
54
+ } else {
55
+ ...
56
+ }
57
+ return config;
58
+ },
59
+
60
+ afterRequestHook: (response: any) => {
61
+ const { data } = response || {};
62
+ const { code, } = data || {};
63
+ return code ? Promise.resolve(data) : Promise.reject(data);
64
+ },
65
+
66
+ requestCatchHook: (error: AxiosError, config: AxiosRequestConfig) => { ... }
67
+ }
68
+
69
+ const defApi = new AxiosCreate(defaultRequestConfig, defaultInterceptors);
70
+ ```
71
+
72
+ # API
73
+
74
+ ---
75
+ ## get
76
+
77
+ ---
78
+ ```
79
+ const getApi = defApi.get({
80
+ url: '/api/test/get',
81
+ params,
82
+ ...
83
+ })
84
+ ```
85
+
86
+ ## post
87
+
88
+ ---
89
+ ```
90
+ const postApi = defApi.post({
91
+ url: '/api/test/post',
92
+ data,
93
+ ...
94
+ })
95
+
96
+ const download = defApi.post({
97
+ url: '/api/test/download',
98
+ data,
99
+ responseType: 'blob'
100
+ })
101
+ ```
102
+
103
+ ## put
104
+
105
+ ---
106
+ ```
107
+ const putApi = defApi.post({
108
+ url: '/api/test/put',
109
+ data,
110
+ ...
111
+ })
112
+ ```
113
+
114
+ ## delete
115
+
116
+ ---
117
+ ```
118
+ const deleteApi = defApi.post({
119
+ url: '/api/test/delete',
120
+ data,
121
+ ...
122
+ })
123
+ ```
124
+
125
+ # License
126
+
127
+ ---
128
+ MIT
package/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Create } from './lib/class/Creator'
2
+ import { Canceler } from './lib/class/Canceler'
3
+ import { Retry } from './lib/class/Retry'
4
+
5
+ export { Create, Canceler, Retry }
@@ -0,0 +1,23 @@
1
+ import type { InternalAxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance } from 'axios'
2
+
3
+ /**
4
+ * @description 拦截器
5
+ */
6
+ export abstract class InterceptorsHook {
7
+ /**
8
+ * @description 请求拦截器设置
9
+ */
10
+ requestInterceptors?: (config: InternalAxiosRequestConfig, options?: any) => InternalAxiosRequestConfig
11
+ /**
12
+ * @description 请求拦截器异常处理
13
+ */
14
+ requestInterceptorsCatch?: (error: AxiosError | Error) => void
15
+ /**
16
+ * @description 响应拦截器设置
17
+ */
18
+ responseInterceptors?: (response: AxiosResponse<any>) => AxiosResponse<any>
19
+ /**
20
+ * @description 响应拦截器异常处理
21
+ */
22
+ responseInterceptorsCatch?: (error: AxiosError | Error, instance: AxiosInstance) => void
23
+ }
@@ -0,0 +1,19 @@
1
+ import type { AxiosRequestConfig, AxiosResponse } from 'axios'
2
+
3
+ /**
4
+ * @description
5
+ */
6
+ export abstract class RequestHook {
7
+ /**
8
+ * @description 发送请求前处理(它可以根据需要修改请求配置)
9
+ */
10
+ beforeRequestHook?: (config: AxiosRequestConfig, options?: any) => AxiosRequestConfig
11
+ /**
12
+ * @description 发送请求后处理(响应处理)
13
+ */
14
+ afterRequestHook?: (response: AxiosResponse<any>, options?: any) => any
15
+ /**
16
+ * @description 请求异常处理
17
+ */
18
+ requestCatchHook?: (error: Error, options?: any) => Promise<any>
19
+ }
@@ -0,0 +1,68 @@
1
+ import type { AxiosRequestConfig } from "axios";
2
+ import qs from "qs"
3
+
4
+ const getPendingUrl = (config: AxiosRequestConfig): string => {
5
+ const { method, url, params, data } = config || {}
6
+ return [method, url, qs.stringify(params), qs.stringify(data)].join("&");
7
+ };
8
+
9
+ /**
10
+ * @description 取消请求
11
+ */
12
+ export class Canceler {
13
+ private pendingMap: Map<string, AbortController>
14
+
15
+ constructor() {
16
+ // 用于存储每个请求的标识和取消函数
17
+ this.pendingMap = new Map<string, AbortController>();
18
+ }
19
+ /**
20
+ * @description 添加请求
21
+ * @param config 请求配置
22
+ */
23
+ public addPending(config: AxiosRequestConfig): void {
24
+ this.removePending(config);
25
+ const url = getPendingUrl(config);
26
+ const controller = new AbortController();
27
+ config.signal = controller.signal;
28
+ if (!this.pendingMap.has(url)) {
29
+ // 如果当前请求不在等待中,将其添加到等待中
30
+ this.pendingMap.set(url, controller);
31
+ }
32
+ }
33
+
34
+ /**
35
+ * @description 移除请求
36
+ * @param config 请求配置
37
+ */
38
+ public removePending(config: AxiosRequestConfig): void {
39
+ const url = getPendingUrl(config);
40
+ if (this.pendingMap.has(url)) {
41
+ // 如果当前请求在等待中,取消它并将其从等待中移除
42
+ const abortController = this.pendingMap.get(url);
43
+ if (abortController) {
44
+ abortController.abort(url);
45
+ }
46
+ this.pendingMap.delete(url);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * @description 清除所有等待中的请求
52
+ */
53
+ public removeAllPending(): void {
54
+ this.pendingMap.forEach((abortController) => {
55
+ if (abortController) {
56
+ abortController.abort();
57
+ }
58
+ });
59
+ this.reset();
60
+ }
61
+
62
+ /**
63
+ * @description 重置
64
+ */
65
+ public reset(): void {
66
+ this.pendingMap.clear();
67
+ }
68
+ }
@@ -0,0 +1,153 @@
1
+ import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2
+ import { RequestHook } from '../abstract/RequestHook';
3
+ import { InterceptorsHook } from '../abstract/InterceptorsHook';
4
+ import { cloneDeep as _cloneDeep, isFunction as _isFunction } from 'lodash'
5
+ import { MethodEnum } from "../enum"
6
+
7
+ /**
8
+ * @description axios创建
9
+ */
10
+ export class Create {
11
+ // ⬇️ axios实例
12
+ private instance: AxiosInstance
13
+ // ⬇️ axios配置
14
+ private readonly requestConfig: AxiosRequestConfig
15
+ // ⬇️ 自定义拦截器
16
+ private customInterceptors: RequestHook & InterceptorsHook
17
+ // ⬇️ 构造器
18
+ constructor(requestConfig: AxiosRequestConfig, customInterceptors: RequestHook & InterceptorsHook) {
19
+ this.requestConfig = requestConfig // ⬅️ 配置参数
20
+ this.customInterceptors = customInterceptors // ⬅️ 配置自定义拦截器
21
+
22
+ this.createAxios(this.requestConfig) // ⬅️ 创建实例
23
+ this.setInterceptors() // ⬅️ 配置拦截器
24
+ }
25
+ /**
26
+ * @description 实例创建
27
+ * @param config AxiosRequestConfig
28
+ */
29
+ private createAxios(config: AxiosRequestConfig): void {
30
+ this.instance = axios.create(config)
31
+ }
32
+ /**
33
+ * @description 重新设置实例
34
+ * @param config AxiosRequestConfig
35
+ */
36
+ resetAxios(config: AxiosRequestConfig) {
37
+ if (!this.instance) return
38
+ return this.createAxios(config)
39
+ }
40
+ /**
41
+ * @description 获取实例
42
+ * @returns
43
+ */
44
+ getInstance(): AxiosInstance {
45
+ return this.instance
46
+ }
47
+ /**
48
+ * @description 获取拦截器配置
49
+ */
50
+ getInterceptors(): RequestHook & InterceptorsHook {
51
+ return this.customInterceptors
52
+ }
53
+ /**
54
+ * @description 拦截器设置
55
+ */
56
+ private setInterceptors() {
57
+ if (!this.instance || !this.customInterceptors) return
58
+ const {
59
+ requestInterceptors, // ⬅️ 请求拦截器设置
60
+ requestInterceptorsCatch, // ⬅️ 请求拦截器异常处理
61
+ responseInterceptors, // ⬅️ 响应拦截器设置
62
+ responseInterceptorsCatch, // ⬅️ 响应拦截器异常处理
63
+ } = this.customInterceptors || {}
64
+ // 请求拦截器
65
+ this.instance.interceptors.request.use(
66
+ (config: InternalAxiosRequestConfig) => {
67
+ if (requestInterceptors && _isFunction(requestInterceptors)) {
68
+ config = requestInterceptors(config)
69
+ }
70
+ return config
71
+ },
72
+ (error: AxiosError | Error) => {
73
+ requestInterceptorsCatch
74
+ && _isFunction(requestInterceptorsCatch)
75
+ && requestInterceptorsCatch(error)
76
+ }
77
+ )
78
+ // 响应拦截器
79
+ this.instance.interceptors.response.use(
80
+ (res: AxiosResponse<any>) => {
81
+ if (responseInterceptors && _isFunction(responseInterceptors)) {
82
+ res = responseInterceptors(res)
83
+ }
84
+ return res
85
+ },
86
+ (error: AxiosError | Error) => {
87
+ responseInterceptorsCatch
88
+ && _isFunction(responseInterceptorsCatch)
89
+ && responseInterceptorsCatch(this.instance, error)
90
+ }
91
+ )
92
+ }
93
+ /**
94
+ * @description 通用请求
95
+ * @param config AxiosRequestConfig
96
+ */
97
+ request<T = any>(config: AxiosRequestConfig): Promise<T> {
98
+ let conf: AxiosRequestConfig = _cloneDeep(config)
99
+ const { beforeRequestHook, afterRequestHook, requestCatchHook } = this.customInterceptors || {}
100
+ if (beforeRequestHook && _isFunction(beforeRequestHook)) {
101
+ conf = beforeRequestHook(conf)
102
+ }
103
+ return new Promise((resolve, reject) => {
104
+ this.instance.request<any, AxiosResponse<any>>(conf)
105
+ .then((res: AxiosResponse<any>) => {
106
+ if (afterRequestHook && _isFunction(afterRequestHook)) {
107
+ try {
108
+ resolve(afterRequestHook(res))
109
+ } catch (e) {
110
+ reject(e || new Error('request has error'))
111
+ }
112
+ } else {
113
+ resolve(res as unknown as Promise<T>)
114
+ }
115
+ })
116
+ .catch((error: AxiosError | Error) => {
117
+ requestCatchHook && _isFunction(requestCatchHook) ? reject(requestCatchHook(error, conf)) : reject(error)
118
+ })
119
+ })
120
+ }
121
+ /**
122
+ * @description get请求
123
+ * @param config AxiosRequestConfig
124
+ * @returns Promise
125
+ */
126
+ get<T = any>(config: AxiosRequestConfig): Promise<T> {
127
+ return this.request({ ...config, method: MethodEnum.GET })
128
+ }
129
+ /**
130
+ * @description post请求
131
+ * @param config AxiosRequestConfig
132
+ * @returns Promise
133
+ */
134
+ post<T = any>(config: AxiosRequestConfig): Promise<T> {
135
+ return this.request({ ...config, method: MethodEnum.POST })
136
+ }
137
+ /**
138
+ * @description put请求
139
+ * @param config AxiosRequestConfig
140
+ * @returns Promise
141
+ */
142
+ put<T = any>(config: AxiosRequestConfig): Promise<T> {
143
+ return this.request({ ...config, method: MethodEnum.PUT })
144
+ }
145
+ /**
146
+ * @description delete请求
147
+ * @param config AxiosRequestConfig
148
+ * @returns Promise
149
+ */
150
+ delete<T = any>(config: AxiosRequestConfig): Promise<T> {
151
+ return this.request({ ...config, method: MethodEnum.DELETE })
152
+ }
153
+ }
@@ -0,0 +1,30 @@
1
+ import { AxiosError, AxiosInstance } from "axios";
2
+
3
+ /**
4
+ * @description 请求重试机制
5
+ */
6
+ export class Retry {
7
+ /**
8
+ * @description 发起重试
9
+ * @param instance
10
+ * @param error
11
+ * @returns Promise
12
+ */
13
+ async retry(instance: AxiosInstance, error: AxiosError) {
14
+ // @ts-ignore
15
+ const { config } = error.response;
16
+ const { waitTime, count } = config?.requestOptions?.retryRequest ?? {};
17
+ config.__retryCount = config.__retryCount || 0;
18
+ if (config.__retryCount >= count) {
19
+ return Promise.reject(error);
20
+ }
21
+ config.__retryCount += 1;
22
+ //请求返回后config的header不正确造成重试请求失败,删除返回headers采用默认headers
23
+ delete config.headers;
24
+ await this.delay(waitTime);
25
+ return await instance(config);
26
+ }
27
+ private delay(waitTime: number) {
28
+ return new Promise((resolve) => setTimeout(resolve, waitTime))
29
+ }
30
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @description MethodEnum
3
+ */
4
+ enum MethodEnum {
5
+ GET = 'GET',
6
+ POST = 'POST',
7
+ PUT = 'PUT',
8
+ DELETE = 'DELETE'
9
+ }
10
+
11
+ /**
12
+ * @description ContentTypeEnum
13
+ */
14
+ enum ContentTypeEnum {
15
+ // json
16
+ JSON = 'application/json;charset=UTF-8',
17
+ // form-data qs
18
+ FORM_URLENCODED = 'application/x-www-form-urlencoded;charset=UTF-8',
19
+ // form-data upload
20
+ FORM_DATA = 'multipart/form-data;charset=UTF-8',
21
+ }
22
+
23
+ export { MethodEnum, ContentTypeEnum }
@@ -0,0 +1,215 @@
1
+ // 包含为 HTTP 定义的状态代码的值。
2
+ enum HttpStatusCode {
3
+ // 摘要:
4
+ // 等效于 HTTP 状态 100。 System.Net.HttpStatusCode.Continue 指示客户端可能继续其请求。
5
+ Continue = 100,
6
+ //
7
+ // 摘要:
8
+ // 等效于 HTTP 状态 101。 System.Net.HttpStatusCode.SwitchingProtocols 指示正在更改协议版本或协议。
9
+ SwitchingProtocols = 101,
10
+ //
11
+ // 摘要:
12
+ // 等效于 HTTP 状态 200。 System.Net.HttpStatusCode.OK 指示请求成功,且请求的信息包含在响应中。 这是最常接收的状态代码。
13
+ OK = 200,
14
+ //
15
+ // 摘要:
16
+ // 等效于 HTTP 状态 201。 System.Net.HttpStatusCode.Created 指示请求导致在响应被发送前创建新资源。
17
+ Created = 201,
18
+ //
19
+ // 摘要:
20
+ // 等效于 HTTP 状态 202。 System.Net.HttpStatusCode.Accepted 指示请求已被接受做进一步处理。
21
+ Accepted = 202,
22
+ //
23
+ // 摘要:
24
+ // 等效于 HTTP 状态 203。 System.Net.HttpStatusCode.NonAuthoritativeInformation 指示返回的元信息来自缓存副本而不是原始服务器,因此可能不正确。
25
+ NonAuthoritativeInformation = 203,
26
+ //
27
+ // 摘要:
28
+ // 等效于 HTTP 状态 204。 System.Net.HttpStatusCode.NoContent 指示已成功处理请求并且响应已被设定为无内容。
29
+ NoContent = 204,
30
+ //
31
+ // 摘要:
32
+ // 等效于 HTTP 状态 205。 System.Net.HttpStatusCode.ResetContent 指示客户端应重置(或重新加载)当前资源。
33
+ ResetContent = 205,
34
+ //
35
+ // 摘要:
36
+ // 等效于 HTTP 状态 206。 System.Net.HttpStatusCode.PartialContent 指示响应是包括字节范围的 GET
37
+ // 请求所请求的部分响应。
38
+ PartialContent = 206,
39
+ //
40
+ // 摘要:
41
+ // 等效于 HTTP 状态 300。 System.Net.HttpStatusCode.MultipleChoices 指示请求的信息有多种表示形式。
42
+ // 默认操作是将此状态视为重定向,并遵循与此响应关联的 Location 标头的内容。
43
+ MultipleChoices = 300,
44
+ //
45
+ // 摘要:
46
+ // 等效于 HTTP 状态 300。 System.Net.HttpStatusCode.Ambiguous 指示请求的信息有多种表示形式。 默认操作是将此状态视为重定向,并遵循与此响应关联的
47
+ // Location 标头的内容。
48
+ Ambiguous = 300,
49
+ //
50
+ // 摘要:
51
+ // 等效于 HTTP 状态 301。 System.Net.HttpStatusCode.MovedPermanently 指示请求的信息已移到 Location
52
+ // 头中指定的 URI 处。 接收到此状态时的默认操作为遵循与响应关联的 Location 头。
53
+ MovedPermanently = 301,
54
+ //
55
+ // 摘要:
56
+ // 等效于 HTTP 状态 301。 System.Net.HttpStatusCode.Moved 指示请求的信息已移到 Location 头中指定的
57
+ // URI 处。 接收到此状态时的默认操作为遵循与响应关联的 Location 头。 原始请求方法为 POST 时,重定向的请求将使用 GET 方法。
58
+ Moved = 301,
59
+ //
60
+ // 摘要:
61
+ // 等效于 HTTP 状态 302。 System.Net.HttpStatusCode.Found 指示请求的信息位于 Location 头中指定的
62
+ // URI 处。 接收到此状态时的默认操作为遵循与响应关联的 Location 头。 原始请求方法为 POST 时,重定向的请求将使用 GET 方法。
63
+ Found = 302,
64
+ //
65
+ // 摘要:
66
+ // 等效于 HTTP 状态 302。 System.Net.HttpStatusCode.Redirect 指示请求的信息位于 Location 头中指定的
67
+ // URI 处。 接收到此状态时的默认操作为遵循与响应关联的 Location 头。 原始请求方法为 POST 时,重定向的请求将使用 GET 方法。
68
+ Redirect = 302,
69
+ //
70
+ // 摘要:
71
+ // 等效于 HTTP 状态 303。 作为 POST 的结果,System.Net.HttpStatusCode.SeeOther 将客户端自动重定向到
72
+ // Location 头中指定的 URI。 用 GET 生成对 Location 标头所指定的资源的请求。
73
+ SeeOther = 303,
74
+ //
75
+ // 摘要:
76
+ // 等效于 HTTP 状态 303。 作为 POST 的结果,System.Net.HttpStatusCode.RedirectMethod 将客户端自动重定向到
77
+ // Location 头中指定的 URI。 用 GET 生成对 Location 标头所指定的资源的请求。
78
+ RedirectMethod = 303,
79
+ //
80
+ // 摘要:
81
+ // 等效于 HTTP 状态 304。 System.Net.HttpStatusCode.NotModified 指示客户端的缓存副本是最新的。 未传输此资源的内容。
82
+ NotModified = 304,
83
+ //
84
+ // 摘要:
85
+ // 等效于 HTTP 状态 305。 System.Net.HttpStatusCode.UseProxy 指示请求应使用位于 Location 头中指定的
86
+ // URI 的代理服务器。
87
+ UseProxy = 305,
88
+ //
89
+ // 摘要:
90
+ // 等效于 HTTP 状态 306。 System.Net.HttpStatusCode.Unused 是未完全指定的 HTTP/1.1 规范的建议扩展。
91
+ Unused = 306,
92
+ //
93
+ // 摘要:
94
+ // 等效于 HTTP 状态 307。 System.Net.HttpStatusCode.RedirectKeepVerb 指示请求信息位于 Location
95
+ // 头中指定的 URI 处。 接收到此状态时的默认操作为遵循与响应关联的 Location 头。 原始请求方法为 POST 时,重定向的请求还将使用
96
+ // POST 方法。
97
+ RedirectKeepVerb = 307,
98
+ //
99
+ // 摘要:
100
+ // 等效于 HTTP 状态 307。 System.Net.HttpStatusCode.TemporaryRedirect 指示请求信息位于 Location
101
+ // 头中指定的 URI 处。 接收到此状态时的默认操作为遵循与响应关联的 Location 头。 原始请求方法为 POST 时,重定向的请求还将使用
102
+ // POST 方法。
103
+ TemporaryRedirect = 307,
104
+ //
105
+ // 摘要:
106
+ // 等效于 HTTP 状态 400。 System.Net.HttpStatusCode.BadRequest 指示服务器未能识别请求。 如果没有其他适用的错误,或者不知道准确的错误或错误没有自己的错误代码,则发送
107
+ // System.Net.HttpStatusCode.BadRequest。
108
+ BadRequest = 400,
109
+ //
110
+ // 摘要:
111
+ // 等效于 HTTP 状态 401。 System.Net.HttpStatusCode.Unauthorized 指示请求的资源要求身份验证。 WWW-Authenticate
112
+ // 头包含如何执行身份验证的详细信息。
113
+ Unauthorized = 401,
114
+ //
115
+ // 摘要:
116
+ // 等效于 HTTP 状态 402。 保留 System.Net.HttpStatusCode.PaymentRequired 以供将来使用。
117
+ PaymentRequired = 402,
118
+ //
119
+ // 摘要:
120
+ // 等效于 HTTP 状态 403。 System.Net.HttpStatusCode.Forbidden 指示服务器拒绝满足请求。
121
+ Forbidden = 403,
122
+ //
123
+ // 摘要:
124
+ // 等效于 HTTP 状态 404。 System.Net.HttpStatusCode.NotFound 指示请求的资源不在服务器上。
125
+ NotFound = 404,
126
+ //
127
+ // 摘要:
128
+ // 等效于 HTTP 状态 405。 System.Net.HttpStatusCode.MethodNotAllowed 指示请求的资源上不允许请求方法(POST
129
+ // 或 GET)。
130
+ MethodNotAllowed = 405,
131
+ //
132
+ // 摘要:
133
+ // 等效于 HTTP 状态 406。 System.Net.HttpStatusCode.NotAcceptable 指示客户端已用 Accept 头指示将不接受资源的任何可用表示形式。
134
+ NotAcceptable = 406,
135
+ //
136
+ // 摘要:
137
+ // 等效于 HTTP 状态 407。 System.Net.HttpStatusCode.ProxyAuthenticationRequired 指示请求的代理要求身份验证。
138
+ // Proxy-authenticate 头包含如何执行身份验证的详细信息。
139
+ ProxyAuthenticationRequired = 407,
140
+ //
141
+ // 摘要:
142
+ // 等效于 HTTP 状态 408。 System.Net.HttpStatusCode.RequestTimeout 指示客户端没有在服务器期望请求的时间内发送请求。
143
+ RequestTimeout = 408,
144
+ //
145
+ // 摘要:
146
+ // 等效于 HTTP 状态 409。 System.Net.HttpStatusCode.Conflict 指示由于服务器上的冲突而未能执行请求。
147
+ Conflict = 409,
148
+ //
149
+ // 摘要:
150
+ // 等效于 HTTP 状态 410。 System.Net.HttpStatusCode.Gone 指示请求的资源不再可用。
151
+ Gone = 410,
152
+ //
153
+ // 摘要:
154
+ // 等效于 HTTP 状态 411。 System.Net.HttpStatusCode.LengthRequired 指示缺少必需的 Content-length
155
+ // 头。
156
+ LengthRequired = 411,
157
+ //
158
+ // 摘要:
159
+ // 等效于 HTTP 状态 412。 System.Net.HttpStatusCode.PreconditionFailed 指示为此请求设置的条件失败,且无法执行此请求。
160
+ // 条件是用条件请求标头(如 If-Match、If-None-Match 或 If-Unmodified-Since)设置的。
161
+ PreconditionFailed = 412,
162
+ //
163
+ // 摘要:
164
+ // 等效于 HTTP 状态 413。 System.Net.HttpStatusCode.RequestEntityTooLarge 指示请求太大,服务器无法处理。
165
+ RequestEntityTooLarge = 413,
166
+ //
167
+ // 摘要:
168
+ // 等效于 HTTP 状态 414。 System.Net.HttpStatusCode.RequestUriTooLong 指示 URI 太长。
169
+ RequestUriTooLong = 414,
170
+ //
171
+ // 摘要:
172
+ // 等效于 HTTP 状态 415。 System.Net.HttpStatusCode.UnsupportedMediaType 指示请求是不支持的类型。
173
+ UnsupportedMediaType = 415,
174
+ //
175
+ // 摘要:
176
+ // 等效于 HTTP 状态 416。 System.Net.HttpStatusCode.RequestedRangeNotSatisfiable 指示无法返回从资源请求的数据范围,因为范围的开头在资源的开头之前,或因为范围的结尾在资源的结尾之后。
177
+ RequestedRangeNotSatisfiable = 416,
178
+ //
179
+ // 摘要:
180
+ // 等效于 HTTP 状态 417。 System.Net.HttpStatusCode.ExpectationFailed 指示服务器未能符合 Expect
181
+ // 头中给定的预期值。
182
+ ExpectationFailed = 417,
183
+ //
184
+ // 摘要:
185
+ // 等效于 HTTP 状态 426。 System.Net.HttpStatusCode.UpgradeRequired 指示客户端应切换为诸如 TLS/1.0
186
+ // 之类的其他协议。
187
+ UpgradeRequired = 426,
188
+ //
189
+ // 摘要:
190
+ // 等效于 HTTP 状态 500。 System.Net.HttpStatusCode.InternalServerError 指示服务器上发生了一般错误。
191
+ InternalServerError = 500,
192
+ //
193
+ // 摘要:
194
+ // 等效于 HTTP 状态 501。 System.Net.HttpStatusCode.NotImplemented 指示服务器不支持请求的函数。
195
+ NotImplemented = 501,
196
+ //
197
+ // 摘要:
198
+ // 等效于 HTTP 状态 502。 System.Net.HttpStatusCode.BadGateway 指示中间代理服务器从另一代理或原始服务器接收到错误响应。
199
+ BadGateway = 502,
200
+ //
201
+ // 摘要:
202
+ // 等效于 HTTP 状态 503。 System.Net.HttpStatusCode.ServiceUnavailable 指示服务器暂时不可用,通常是由于过多加载或维护。
203
+ ServiceUnavailable = 503,
204
+ //
205
+ // 摘要:
206
+ // 等效于 HTTP 状态 504。 System.Net.HttpStatusCode.GatewayTimeout 指示中间代理服务器在等待来自另一个代理或原始服务器的响应时已超时。
207
+ GatewayTimeout = 504,
208
+ //
209
+ // 摘要:
210
+ // 等效于 HTTP 状态 505。 System.Net.HttpStatusCode.HttpVersionNotSupported 指示服务器不支持请求的
211
+ // HTTP 版本。
212
+ HttpVersionNotSupported = 505,
213
+ }
214
+
215
+ export { HttpStatusCode }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "mhy-http-ts",
3
+ "version": "1.0.0",
4
+ "description": "基于ts的axios二次封装",
5
+ "main": "./index.ts",
6
+ "repository": "https://gitee.com/long-leg-kirky/mhy-http-ts",
7
+ "keywords": [
8
+ "axios",
9
+ "typescript"
10
+ ],
11
+ "author": {
12
+ "name": "Fang WenBin",
13
+ "email": "lasskiss@sina.com",
14
+ "url": "https://gitee.com/long-leg-kirky"
15
+ },
16
+ "dependencies": {
17
+ "@types/qs": "^6.9.7",
18
+ "axios": "^1.4.0",
19
+ "lodash": "^4.17.21",
20
+ "qs": "^6.11.2"
21
+ },
22
+ "license": "MIT"
23
+ }