@aliyun-obv/api 0.0.1
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/dist/api.d.ts +35 -0
- package/dist/api.es.js +148 -0
- package/package.json +14 -0
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
interface BaseFetchDefault {
|
|
2
|
+
baseURL?: string;
|
|
3
|
+
getDefaultInit: RequestInit;
|
|
4
|
+
postDefaultInit: RequestInit;
|
|
5
|
+
beforeRequest?: (url: string, init: RequestInit) => RequestInit;
|
|
6
|
+
afterResponse?: (response: Response, options: BaseFetchOptions) => any;
|
|
7
|
+
}
|
|
8
|
+
interface BaseFetchOptions {
|
|
9
|
+
forbidAlert?: boolean;
|
|
10
|
+
}
|
|
11
|
+
type IFetchAPI = (input: string, init?: RequestInit, options?: BaseFetchOptions) => Promise<any>;
|
|
12
|
+
declare class BaseFetch<R> {
|
|
13
|
+
private fetchApi;
|
|
14
|
+
defaults: BaseFetchDefault;
|
|
15
|
+
constructor(fetchApi?: IFetchAPI);
|
|
16
|
+
private callFetch;
|
|
17
|
+
get(api: string, params?: Record<string, any>, options?: BaseFetchOptions): Promise<R>;
|
|
18
|
+
post(api: string, config?: Record<string, any>, options?: BaseFetchOptions): Promise<R>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface IResponseData {
|
|
22
|
+
[x: string]: any;
|
|
23
|
+
res: any;
|
|
24
|
+
success: any;
|
|
25
|
+
data: any;
|
|
26
|
+
code: any;
|
|
27
|
+
message: any;
|
|
28
|
+
response: Response;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* For SLS EndPoint
|
|
32
|
+
*/
|
|
33
|
+
declare const easyFetch: BaseFetch<IResponseData>;
|
|
34
|
+
|
|
35
|
+
export { easyFetch };
|
package/dist/api.es.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
|
|
3
|
+
function getParams(obj = {}) {
|
|
4
|
+
return Object.keys(obj).filter((k) => obj[k] || +obj[k] === 0).map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`).join("&");
|
|
5
|
+
}
|
|
6
|
+
function getPostParam(obj = {}) {
|
|
7
|
+
return Object.keys(obj).filter((k) => obj[k] || +obj[k] === 0).map((k) => {
|
|
8
|
+
if (obj[k] instanceof Array) {
|
|
9
|
+
return obj[k].map(
|
|
10
|
+
(val) => `${encodeURIComponent(k)}=${encodeURIComponent(val)}`
|
|
11
|
+
).join("&");
|
|
12
|
+
}
|
|
13
|
+
return `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`;
|
|
14
|
+
}).join("&");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var __defProp$1 = Object.defineProperty;
|
|
18
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
19
|
+
var __publicField = (obj, key, value) => {
|
|
20
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __async$1 = (__this, __arguments, generator) => {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
var fulfilled = (value) => {
|
|
26
|
+
try {
|
|
27
|
+
step(generator.next(value));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var rejected = (value) => {
|
|
33
|
+
try {
|
|
34
|
+
step(generator.throw(value));
|
|
35
|
+
} catch (e) {
|
|
36
|
+
reject(e);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
40
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
class BaseFetch {
|
|
44
|
+
constructor(fetchApi) {
|
|
45
|
+
__publicField(this, "fetchApi");
|
|
46
|
+
__publicField(this, "defaults", {
|
|
47
|
+
baseURL: "",
|
|
48
|
+
getDefaultInit: {
|
|
49
|
+
method: "get",
|
|
50
|
+
credentials: "same-origin",
|
|
51
|
+
headers: {}
|
|
52
|
+
},
|
|
53
|
+
postDefaultInit: {
|
|
54
|
+
method: "post",
|
|
55
|
+
credentials: "same-origin",
|
|
56
|
+
headers: {}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
this.fetchApi = fetchApi;
|
|
60
|
+
}
|
|
61
|
+
callFetch(_0, _1) {
|
|
62
|
+
return __async$1(this, arguments, function* (api, init, options = {}) {
|
|
63
|
+
if (this.fetchApi != null) {
|
|
64
|
+
return this.fetchApi(api, init, options);
|
|
65
|
+
}
|
|
66
|
+
return fetch(api, init);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
get(_0) {
|
|
70
|
+
return __async$1(this, arguments, function* (api, params = {}, options = {}) {
|
|
71
|
+
const url = `${api}?${getParams(params)}`;
|
|
72
|
+
const fetchInit = this.defaults.getDefaultInit;
|
|
73
|
+
const res = yield this.callFetch(url, fetchInit, options);
|
|
74
|
+
if (this.defaults.afterResponse) {
|
|
75
|
+
return this.defaults.afterResponse(res, options);
|
|
76
|
+
}
|
|
77
|
+
return res;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
post(_0) {
|
|
81
|
+
return __async$1(this, arguments, function* (api, config = {}, options = {}) {
|
|
82
|
+
const fetchInit = _.merge({}, this.defaults.postDefaultInit, {
|
|
83
|
+
body: getPostParam(config)
|
|
84
|
+
});
|
|
85
|
+
const res = yield this.callFetch(api, fetchInit, options);
|
|
86
|
+
if (this.defaults.afterResponse) {
|
|
87
|
+
return this.defaults.afterResponse(res, options);
|
|
88
|
+
}
|
|
89
|
+
return res;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
var __defProp = Object.defineProperty;
|
|
95
|
+
var __defProps = Object.defineProperties;
|
|
96
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
97
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
98
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
99
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
100
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
101
|
+
var __spreadValues = (a, b) => {
|
|
102
|
+
for (var prop in b || (b = {}))
|
|
103
|
+
if (__hasOwnProp.call(b, prop))
|
|
104
|
+
__defNormalProp(a, prop, b[prop]);
|
|
105
|
+
if (__getOwnPropSymbols)
|
|
106
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
107
|
+
if (__propIsEnum.call(b, prop))
|
|
108
|
+
__defNormalProp(a, prop, b[prop]);
|
|
109
|
+
}
|
|
110
|
+
return a;
|
|
111
|
+
};
|
|
112
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
113
|
+
var __async = (__this, __arguments, generator) => {
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
var fulfilled = (value) => {
|
|
116
|
+
try {
|
|
117
|
+
step(generator.next(value));
|
|
118
|
+
} catch (e) {
|
|
119
|
+
reject(e);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
var rejected = (value) => {
|
|
123
|
+
try {
|
|
124
|
+
step(generator.throw(value));
|
|
125
|
+
} catch (e) {
|
|
126
|
+
reject(e);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
130
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
const easyFetch = new BaseFetch();
|
|
134
|
+
easyFetch.defaults.postDefaultInit.headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
135
|
+
easyFetch.defaults.postDefaultInit.headers["Accept"] = "application/json";
|
|
136
|
+
easyFetch.defaults.afterResponse = (response) => __async(void 0, null, function* () {
|
|
137
|
+
if (response.status >= 200 && response.status < 300) {
|
|
138
|
+
const data = yield response.json();
|
|
139
|
+
return __spreadProps(__spreadValues({}, data != null ? data : {}), {
|
|
140
|
+
response
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
throw new Error(response.statusText);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
export { easyFetch };
|
|
148
|
+
//# sourceMappingURL=api.es.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aliyun-obv/api",
|
|
3
|
+
"description": "",
|
|
4
|
+
"main": "dist/api.es.js",
|
|
5
|
+
"module": "dist/api.es.js",
|
|
6
|
+
"types": "dist/api.d.ts",
|
|
7
|
+
"scripts": {},
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"urijs": "^1.19.1"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {},
|
|
13
|
+
"version": "0.0.1"
|
|
14
|
+
}
|