yunxi-request 0.3.5 → 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.
@@ -0,0 +1,104 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __publicField = (obj, key, value) => {
21
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ return value;
23
+ };
24
+ import axios from "axios";
25
+ const defaultConfig = {
26
+ baseURL: "",
27
+ method: "get",
28
+ timeout: 1e3 * 10,
29
+ withCredentials: false
30
+ };
31
+ const defaultCustomConfig = {
32
+ raw: false,
33
+ errorHandle: void 0,
34
+ handleRequest: void 0,
35
+ debugger: false
36
+ };
37
+ class HttpServer {
38
+ constructor(options, customOptions) {
39
+ __publicField(this, "instance");
40
+ __publicField(this, "debugger");
41
+ const config = Object.assign(defaultConfig, options);
42
+ const customConfig = Object.assign(defaultCustomConfig, customOptions);
43
+ this.instance = axios.create(config);
44
+ this.debugger = customOptions == null ? void 0 : customOptions.debugger;
45
+ if (customConfig.handleRequest) {
46
+ this.handleRequest(customConfig.handleRequest);
47
+ }
48
+ if (!customConfig.raw && customConfig.errorHandle) {
49
+ this.handleResponse(customConfig.errorHandle);
50
+ }
51
+ }
52
+ handleRequest(handle) {
53
+ this.instance.interceptors.request.use((config) => {
54
+ if (this.debugger) {
55
+ console.log("\u8BF7\u6C42 config \u6253\u5370", config);
56
+ }
57
+ return handle(config);
58
+ }, (error) => {
59
+ return Promise.reject(error);
60
+ });
61
+ }
62
+ handleResponse(errorHandle) {
63
+ this.instance.interceptors.response.use((response) => {
64
+ if (this.debugger) {
65
+ console.log("\u8BF7\u6C42 response \u6253\u5370", response);
66
+ }
67
+ if (response.request.responseType === "blob")
68
+ return response.data;
69
+ const data = response.data;
70
+ const code = data.code || data.statusCode;
71
+ if (code === 200)
72
+ return data;
73
+ errorHandle(data, response);
74
+ return Promise.reject(data);
75
+ }, (error) => {
76
+ const response = {
77
+ code: +error.message.slice(-3),
78
+ data: {},
79
+ msg: error.message,
80
+ total: 0,
81
+ trace: ""
82
+ };
83
+ errorHandle(response);
84
+ return Promise.reject(response);
85
+ });
86
+ }
87
+ async get(url, params, config) {
88
+ return await this.instance.get(url, __spreadProps(__spreadValues({}, config), {
89
+ params
90
+ }));
91
+ }
92
+ async delete(url, params, config) {
93
+ return await this.instance.delete(url, __spreadProps(__spreadValues({}, config), {
94
+ params
95
+ }));
96
+ }
97
+ async post(url, data, config) {
98
+ return await this.instance.post(url, data, config);
99
+ }
100
+ async put(url, data, config) {
101
+ return await this.instance.put(url, data, config);
102
+ }
103
+ }
104
+ export { HttpServer as default };
@@ -0,0 +1 @@
1
+ var b=Object.defineProperty,q=Object.defineProperties;var y=Object.getOwnPropertyDescriptors;var l=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,m=Object.prototype.propertyIsEnumerable;var o=(s,t,a)=>t in s?b(s,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):s[t]=a,d=(s,t)=>{for(var a in t||(t={}))R.call(t,a)&&o(s,a,t[a]);if(l)for(var a of l(t))m.call(t,a)&&o(s,a,t[a]);return s},c=(s,t)=>q(s,y(t));var f=(s,t,a)=>(o(s,typeof t!="symbol"?t+"":t,a),a);(function(s,t){typeof exports=="object"&&typeof module!="undefined"?module.exports=t(require("axios")):typeof define=="function"&&define.amd?define(["axios"],t):(s=typeof globalThis!="undefined"?globalThis:s||self,s.YxRequest=t(s.axios))})(this,function(s){"use strict";function t(r){return r&&typeof r=="object"&&"default"in r?r:{default:r}}var a=t(s);const g={baseURL:"",method:"get",timeout:1e3*10,withCredentials:!1},h={raw:!1,errorHandle:void 0,handleRequest:void 0,debugger:!1};class p{constructor(i,e){f(this,"instance");f(this,"debugger");const n=Object.assign(g,i),u=Object.assign(h,e);this.instance=a.default.create(n),this.debugger=e==null?void 0:e.debugger,u.handleRequest&&this.handleRequest(u.handleRequest),!u.raw&&u.errorHandle&&this.handleResponse(u.errorHandle)}handleRequest(i){this.instance.interceptors.request.use(e=>(this.debugger&&console.log("\u8BF7\u6C42 config \u6253\u5370",e),i(e)),e=>Promise.reject(e))}handleResponse(i){this.instance.interceptors.response.use(e=>{if(this.debugger&&console.log("\u8BF7\u6C42 response \u6253\u5370",e),e.request.responseType==="blob")return e.data;const n=e.data;return(n.code||n.statusCode)===200?n:(i(n,e),Promise.reject(n))},e=>{const n={code:+e.message.slice(-3),data:{},msg:e.message,total:0,trace:""};return i(n),Promise.reject(n)})}async get(i,e,n){return await this.instance.get(i,c(d({},n),{params:e}))}async delete(i,e,n){return await this.instance.delete(i,c(d({},n),{params:e}))}async post(i,e,n){return await this.instance.post(i,e,n)}async put(i,e,n){return await this.instance.put(i,e,n)}}return p});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yunxi-request",
3
- "version": "0.3.5",
3
+ "version": "1.0.0",
4
4
  "description": "基于axios的请求库",
5
5
  "author": "xushaoping <xusp@yunxi.tv>",
6
6
  "license": "ISC",
package/types/index.d.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  import { AxiosRequestConfig } from 'axios';
2
2
  import { IResponseData, ICustomConfig } from './types';
3
3
  export default class HttpServer {
4
- private instance;
5
- private readonly debugger;
6
- constructor(options?: AxiosRequestConfig, customOptions?: ICustomConfig);
7
- private handleRequest;
8
- private handleResponse;
9
- get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
10
- delete<T = any>(
11
- url: string,
12
- params?: any,
13
- config?: AxiosRequestConfig,
14
- ): Promise<IResponseData<T>>;
15
- post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
16
- put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
4
+ private instance;
5
+ private readonly debugger;
6
+ constructor(options?: AxiosRequestConfig, customOptions?: ICustomConfig);
7
+ private handleRequest;
8
+ private handleResponse;
9
+ get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
10
+ delete<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
11
+ post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
12
+ put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<IResponseData<T>>;
17
13
  }
package/types/index.js CHANGED
@@ -1,89 +1,85 @@
1
1
  import axios from 'axios';
2
2
  const defaultConfig = {
3
- baseURL: '',
4
- method: 'get',
5
- timeout: 1000 * 10,
6
- withCredentials: false,
3
+ baseURL: '',
4
+ method: 'get',
5
+ timeout: 1000 * 10,
6
+ withCredentials: false,
7
7
  };
8
8
  const defaultCustomConfig = {
9
- raw: false,
10
- errorHandle: undefined,
11
- handleRequest: undefined,
12
- debugger: false,
9
+ raw: false,
10
+ errorHandle: undefined,
11
+ handleRequest: undefined,
12
+ debugger: false,
13
13
  };
14
14
  export default class HttpServer {
15
- instance;
16
- debugger;
17
- constructor(options, customOptions) {
18
- const config = Object.assign(defaultConfig, options);
19
- const customConfig = Object.assign(defaultCustomConfig, customOptions);
20
- this.instance = axios.create(config);
21
- this.debugger = customOptions?.debugger;
22
- if (customConfig.handleRequest) {
23
- this.handleRequest(customConfig.handleRequest);
24
- }
25
- if (!customConfig.raw && customConfig.errorHandle) {
26
- this.handleResponse(customConfig.errorHandle);
27
- }
28
- }
29
- handleRequest(handle) {
30
- this.instance.interceptors.request.use(
31
- config => {
32
- if (this.debugger) {
33
- console.log('请求 config 打印', config);
15
+ instance;
16
+ debugger;
17
+ constructor(options, customOptions) {
18
+ const config = Object.assign(defaultConfig, options);
19
+ const customConfig = Object.assign(defaultCustomConfig, customOptions);
20
+ this.instance = axios.create(config);
21
+ this.debugger = customOptions?.debugger;
22
+ if (customConfig.handleRequest) {
23
+ this.handleRequest(customConfig.handleRequest);
34
24
  }
35
- return handle(config);
36
- },
37
- error => {
38
- return Promise.reject(error);
39
- },
40
- );
41
- }
42
- handleResponse(errorHandle) {
43
- // 响应请求的拦截器
44
- this.instance.interceptors.response.use(
45
- response => {
46
- if (this.debugger) {
47
- console.log('请求 response 打印', response);
25
+ if (!customConfig.raw && customConfig.errorHandle) {
26
+ this.handleResponse(customConfig.errorHandle);
48
27
  }
49
- // 请求 blob 时直接返回数据 ,不做拦截处理
50
- if (response.request.responseType === 'blob') return response.data;
51
- const data = response.data;
52
- const code = data.code || data.statusCode;
53
- if (code === 200) return data;
54
- errorHandle(data, response);
55
- return Promise.reject(data);
56
- },
57
- error => {
58
- const response = {
59
- code: +error.message.slice(-3),
60
- data: {},
61
- msg: error.message,
62
- total: 0,
63
- trace: '',
64
- };
65
- errorHandle(response);
66
- return Promise.reject(response);
67
- },
68
- );
69
- }
70
- async get(url, params, config) {
71
- return await this.instance.get(url, {
72
- ...config,
73
- params: params,
74
- });
75
- }
76
- async delete(url, params, config) {
77
- return await this.instance.delete(url, {
78
- ...config,
79
- params: params,
80
- });
81
- }
82
- async post(url, data, config) {
83
- return await this.instance.post(url, data, config);
84
- }
85
- async put(url, data, config) {
86
- return await this.instance.put(url, data, config);
87
- }
28
+ }
29
+ handleRequest(handle) {
30
+ this.instance.interceptors.request.use(config => {
31
+ if (this.debugger) {
32
+ console.log('请求 config 打印', config);
33
+ }
34
+ return handle(config);
35
+ }, error => {
36
+ return Promise.reject(error);
37
+ });
38
+ }
39
+ handleResponse(errorHandle) {
40
+ // 响应请求的拦截器
41
+ this.instance.interceptors.response.use(response => {
42
+ if (this.debugger) {
43
+ console.log('请求 response 打印', response);
44
+ }
45
+ // 请求 blob 时直接返回数据 ,不做拦截处理
46
+ if (response.request.responseType === 'blob')
47
+ return response.data;
48
+ const data = response.data;
49
+ const code = data.code || data.statusCode;
50
+ if (code === 200)
51
+ return data;
52
+ errorHandle(data, response);
53
+ return Promise.reject(data);
54
+ }, (error) => {
55
+ const response = {
56
+ code: +error.message.slice(-3),
57
+ data: {},
58
+ msg: error.message,
59
+ total: 0,
60
+ trace: '',
61
+ };
62
+ errorHandle(response);
63
+ return Promise.reject(response);
64
+ });
65
+ }
66
+ async get(url, params, config) {
67
+ return await this.instance.get(url, {
68
+ ...config,
69
+ params: params,
70
+ });
71
+ }
72
+ async delete(url, params, config) {
73
+ return await this.instance.delete(url, {
74
+ ...config,
75
+ params: params,
76
+ });
77
+ }
78
+ async post(url, data, config) {
79
+ return await this.instance.post(url, data, config);
80
+ }
81
+ async put(url, data, config) {
82
+ return await this.instance.put(url, data, config);
83
+ }
88
84
  }
89
- //# sourceMappingURL=index.js.map
85
+ //# sourceMappingURL=index.js.map
package/types/types.d.ts CHANGED
@@ -2,17 +2,17 @@ import { AxiosRequestConfig, AxiosResponse } from 'axios';
2
2
  export declare type IErrorHandle = (args: IResponseData, response?: AxiosResponse) => void;
3
3
  export declare type IHandleRequest = (config: AxiosRequestConfig) => void;
4
4
  export interface IResponseData<T = any> {
5
- code: number;
6
- data: T;
7
- msg: string;
8
- total: number;
9
- trace: string;
10
- statusCode?: number;
11
- message?: string;
5
+ code: number;
6
+ data: T;
7
+ msg: string;
8
+ total: number;
9
+ trace: string;
10
+ statusCode?: number;
11
+ message?: string;
12
12
  }
13
13
  export interface ICustomConfig {
14
- raw?: boolean;
15
- debugger?: boolean;
16
- errorHandle?: IErrorHandle;
17
- handleRequest?: IHandleRequest;
14
+ raw?: boolean;
15
+ debugger?: boolean;
16
+ errorHandle?: IErrorHandle;
17
+ handleRequest?: IHandleRequest;
18
18
  }
package/types/types.js CHANGED
@@ -1,2 +1,2 @@
1
1
  export {};
2
- //# sourceMappingURL=types.js.map
2
+ //# sourceMappingURL=types.js.map