@rdyl/request 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/CHANGELOG.md +4 -0
- package/README.md +9 -0
- package/dist/index.js +41 -0
- package/dist/taro/index.js +49 -0
- package/dist/taro/url.js +148 -0
- package/dist/type.js +15 -0
- package/dist/utils.js +41 -0
- package/package.json +33 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.CreateRequest = CreateRequest;
|
|
21
|
+
var axios_1 = __importDefault(require("axios"));
|
|
22
|
+
var utils_1 = require("./utils");
|
|
23
|
+
function CreateRequest(baseURL, opts) {
|
|
24
|
+
var beforeRequest = opts.beforeRequest, success = opts.success, fail = opts.fail, adapter = opts.adapter;
|
|
25
|
+
var app = axios_1.default.create({
|
|
26
|
+
baseURL: baseURL,
|
|
27
|
+
adapter: adapter,
|
|
28
|
+
});
|
|
29
|
+
app.interceptors.request.use(beforeRequest, function (e) {
|
|
30
|
+
return Promise.reject(e);
|
|
31
|
+
});
|
|
32
|
+
app.interceptors.response.use(success, fail);
|
|
33
|
+
return {
|
|
34
|
+
request: app,
|
|
35
|
+
toRestful: function (path) {
|
|
36
|
+
return (0, utils_1.ParserRestful)(app, path);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
__exportStar(require("./utils"), exports);
|
|
41
|
+
__exportStar(require("./type"), exports);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.AxiosAdapter = AxiosAdapter;
|
|
18
|
+
var taro_1 = __importDefault(require("@tarojs/taro"));
|
|
19
|
+
var url_1 = require("./url");
|
|
20
|
+
function AxiosAdapter(config) {
|
|
21
|
+
return new Promise(function (resolve, reject) {
|
|
22
|
+
var upload = config.upload, _a = config.baseURL, baseURL = _a === void 0 ? "" : _a, _b = config.params, params = _b === void 0 ? {} : _b, data = config.data, headers = config.headers, path = config.url;
|
|
23
|
+
var uri = new url_1.URL(baseURL + path);
|
|
24
|
+
uri.searchParams.assign(params);
|
|
25
|
+
var url = uri.toString();
|
|
26
|
+
var complete = function (res) {
|
|
27
|
+
var response = __assign(__assign({}, res), { status: res.statusCode, statusText: res.errMsg, headers: res.header, config: config, request: null });
|
|
28
|
+
if (res.statusCode === 200) {
|
|
29
|
+
resolve(response);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
reject(response);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
if (upload) {
|
|
36
|
+
taro_1.default.uploadFile({
|
|
37
|
+
url: url,
|
|
38
|
+
header: __assign({}, headers),
|
|
39
|
+
filePath: data || "",
|
|
40
|
+
name: "file",
|
|
41
|
+
complete: complete,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
taro_1.default.request(__assign(__assign({}, config), { url: url, header: __assign({}, headers), complete: complete }));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
package/dist/taro/url.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.URL = exports.URLSearchParams = void 0;
|
|
13
|
+
var URLSearchParams = /** @class */ (function () {
|
|
14
|
+
function URLSearchParams(str) {
|
|
15
|
+
this.values = {};
|
|
16
|
+
var q = str.split("?")[1];
|
|
17
|
+
if (q) {
|
|
18
|
+
var keys = q.split("&");
|
|
19
|
+
this.values = Object.fromEntries(keys.map(function (s) {
|
|
20
|
+
var arr = s.split("=");
|
|
21
|
+
return [arr[0], arr[1]];
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(URLSearchParams.prototype, "keys", {
|
|
26
|
+
get: function () {
|
|
27
|
+
return Object.keys(this.values);
|
|
28
|
+
},
|
|
29
|
+
enumerable: false,
|
|
30
|
+
configurable: true
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(URLSearchParams.prototype, "size", {
|
|
33
|
+
get: function () {
|
|
34
|
+
return this.keys.length;
|
|
35
|
+
},
|
|
36
|
+
enumerable: false,
|
|
37
|
+
configurable: true
|
|
38
|
+
});
|
|
39
|
+
URLSearchParams.prototype.append = function (k, n) {
|
|
40
|
+
if (this.values[k]) {
|
|
41
|
+
if (Array.isArray(this.values[k])) {
|
|
42
|
+
this.values[k] = __spreadArray(__spreadArray([], this.values[k], true), ["".concat(n)], false);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
this.values[k] = [this.values[k], "".concat(n)];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.values[k] = "".concat(n);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
URLSearchParams.prototype.assign = function (pay) {
|
|
53
|
+
var _this = this;
|
|
54
|
+
Object.keys(pay).forEach(function (k) {
|
|
55
|
+
if (Array.isArray(pay[k])) {
|
|
56
|
+
pay[k].forEach(function (v) {
|
|
57
|
+
_this.append(k, v);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
_this.append(k, pay[k]);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
URLSearchParams.prototype.get = function (k) {
|
|
66
|
+
return this.values[k];
|
|
67
|
+
};
|
|
68
|
+
URLSearchParams.prototype.delete = function () {
|
|
69
|
+
var _this = this;
|
|
70
|
+
var arr = [];
|
|
71
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
72
|
+
arr[_i] = arguments[_i];
|
|
73
|
+
}
|
|
74
|
+
this.values = Object.fromEntries(Object.keys(this.values)
|
|
75
|
+
.filter(function (e) { return !arr.includes(e); })
|
|
76
|
+
.map(function (k) { return [k, _this.values[k]]; }));
|
|
77
|
+
};
|
|
78
|
+
URLSearchParams.prototype.forEach = function (fn) {
|
|
79
|
+
var _a = this, values = _a.values, keys = _a.keys;
|
|
80
|
+
keys.forEach(function (k) {
|
|
81
|
+
fn(k, values[k]);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
URLSearchParams.prototype.toString = function () {
|
|
85
|
+
if (this.size) {
|
|
86
|
+
var uList_1 = [];
|
|
87
|
+
var _a = this, values_1 = _a.values, keys = _a.keys;
|
|
88
|
+
keys.forEach(function (k) {
|
|
89
|
+
if (Array.isArray(values_1[k])) {
|
|
90
|
+
values_1[k].forEach(function (v) {
|
|
91
|
+
uList_1.push("".concat(k, "=").concat(v));
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
uList_1.push("".concat(k, "=").concat(values_1[k]));
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return "?".concat(uList_1.join("&"));
|
|
99
|
+
}
|
|
100
|
+
return "";
|
|
101
|
+
};
|
|
102
|
+
return URLSearchParams;
|
|
103
|
+
}());
|
|
104
|
+
exports.URLSearchParams = URLSearchParams;
|
|
105
|
+
var URL = /** @class */ (function () {
|
|
106
|
+
function URL(href) {
|
|
107
|
+
this.href = href;
|
|
108
|
+
this.searchParams = new URLSearchParams("");
|
|
109
|
+
this.host = "";
|
|
110
|
+
this.port = ""; //端口;
|
|
111
|
+
this.protocol = ""; // 协议
|
|
112
|
+
this.pathname = ""; // 地址
|
|
113
|
+
this.hash = ""; //hash
|
|
114
|
+
var match = URL.pattern.exec(href);
|
|
115
|
+
if (match) {
|
|
116
|
+
this.protocol = match[1];
|
|
117
|
+
this.host = match[2];
|
|
118
|
+
this.port = match[3] ? Number(match[3]) : "";
|
|
119
|
+
}
|
|
120
|
+
var sq = href.replace(URL.pattern, "");
|
|
121
|
+
var p = sq.split("?")[0] || "";
|
|
122
|
+
this.pathname = p.split("/#/")[0] || "";
|
|
123
|
+
this.hash = p.replace(this.pathname, "");
|
|
124
|
+
this.searchParams = new URLSearchParams(sq.replace(p, ""));
|
|
125
|
+
}
|
|
126
|
+
Object.defineProperty(URL.prototype, "origin", {
|
|
127
|
+
get: function () {
|
|
128
|
+
var _a = this, protocol = _a.protocol, host = _a.host, port = _a.port;
|
|
129
|
+
var o = protocol;
|
|
130
|
+
if (host) {
|
|
131
|
+
o += "://".concat(host);
|
|
132
|
+
}
|
|
133
|
+
if (port) {
|
|
134
|
+
o += ":".concat(port);
|
|
135
|
+
}
|
|
136
|
+
return o;
|
|
137
|
+
},
|
|
138
|
+
enumerable: false,
|
|
139
|
+
configurable: true
|
|
140
|
+
});
|
|
141
|
+
URL.prototype.toString = function () {
|
|
142
|
+
var _a = this, origin = _a.origin, pathname = _a.pathname, hash = _a.hash, searchParams = _a.searchParams;
|
|
143
|
+
return "".concat(origin).concat(pathname).concat(hash).concat(searchParams.toString());
|
|
144
|
+
};
|
|
145
|
+
URL.pattern = /(https?):\/\/(?:www\.)?([a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+|localhost):(\d+)?/g;
|
|
146
|
+
return URL;
|
|
147
|
+
}());
|
|
148
|
+
exports.URL = URL;
|
package/dist/type.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpStatus = void 0;
|
|
4
|
+
var HttpStatus;
|
|
5
|
+
(function (HttpStatus) {
|
|
6
|
+
HttpStatus[HttpStatus["ok"] = 200] = "ok";
|
|
7
|
+
HttpStatus[HttpStatus["okCreated"] = 201] = "okCreated";
|
|
8
|
+
HttpStatus[HttpStatus["okAccept"] = 202] = "okAccept";
|
|
9
|
+
HttpStatus[HttpStatus["notFound"] = 404] = "notFound";
|
|
10
|
+
HttpStatus[HttpStatus["parameterError"] = 400] = "parameterError";
|
|
11
|
+
HttpStatus[HttpStatus["unauthorized"] = 401] = "unauthorized";
|
|
12
|
+
HttpStatus[HttpStatus["inaccessible"] = 403] = "inaccessible";
|
|
13
|
+
HttpStatus[HttpStatus["exception"] = 500] = "exception";
|
|
14
|
+
HttpStatus[HttpStatus["unavailable"] = 503] = "unavailable";
|
|
15
|
+
})(HttpStatus || (exports.HttpStatus = HttpStatus = {}));
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.ParserRestful = ParserRestful;
|
|
15
|
+
function ParserRestful(request, url) {
|
|
16
|
+
return {
|
|
17
|
+
getList: function (params, c) {
|
|
18
|
+
if (params === void 0) { params = {}; }
|
|
19
|
+
if (c === void 0) { c = {}; }
|
|
20
|
+
return request(__assign({ url: url, params: params }, c));
|
|
21
|
+
},
|
|
22
|
+
get: function (id, c) {
|
|
23
|
+
if (c === void 0) { c = {}; }
|
|
24
|
+
return request(__assign({ url: "".concat(url, "/").concat(id) }, c));
|
|
25
|
+
},
|
|
26
|
+
update: function (id, data, c) {
|
|
27
|
+
if (c === void 0) { c = {}; }
|
|
28
|
+
return request(__assign({ method: "patch", url: "".concat(url, "/").concat(id), data: data }, c));
|
|
29
|
+
},
|
|
30
|
+
insert: function (data, c) {
|
|
31
|
+
if (c === void 0) { c = {}; }
|
|
32
|
+
return request(__assign({ method: "post", url: url, data: data }, c));
|
|
33
|
+
},
|
|
34
|
+
delete: function (ids, c) {
|
|
35
|
+
if (c === void 0) { c = {}; }
|
|
36
|
+
return request(__assign({ method: "delete", url: url, params: {
|
|
37
|
+
ids: ids.join(","),
|
|
38
|
+
} }, c));
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rdyl/request",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"types",
|
|
11
|
+
"CHANGELOG.md",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./dist/index.js",
|
|
16
|
+
"./taro": "./dist/taro/index.js"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
|
|
20
|
+
"build": "tsc && copyfiles -u 1 src/**/*.d.ts src/*.d.ts types && npm run changelog && git add CHANGELOG.md"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@tarojs/taro": "^4.0.7",
|
|
24
|
+
"copyfiles": "^2.4.1",
|
|
25
|
+
"typescript": "^5.6.3",
|
|
26
|
+
"@commitlint/cli": "^19.6.1",
|
|
27
|
+
"@commitlint/config-conventional": "^19.6.0",
|
|
28
|
+
"conventional-changelog-cli": "^5.0.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"axios": "^1.7.7"
|
|
32
|
+
}
|
|
33
|
+
}
|