@varlet/axle 0.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 +21 -0
- package/README.md +146 -0
- package/lib/index.d.ts +53 -0
- package/lib/index.js +94 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Joker
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://github.com/varletjs/axle">
|
|
3
|
+
<img src="https://github.com/varletjs/axle/blob/main/public/logo.svg" width="200">
|
|
4
|
+
</a>
|
|
5
|
+
<h1>axle</h1>
|
|
6
|
+
<p>A tool library based on axios to simplify the development process</p>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
### Quickstart
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
# use npm or yarn or pnpm
|
|
13
|
+
|
|
14
|
+
# npm
|
|
15
|
+
npm i @varlet/axle -S
|
|
16
|
+
|
|
17
|
+
# yarn
|
|
18
|
+
yarn add @varlet/axle
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add @varlet/axle
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Function Signature
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
// 'get' | 'head' | 'options' | 'delete'
|
|
28
|
+
export type FetchHelper = <T = any, R = AxiosResponse<T>>(
|
|
29
|
+
url: string,
|
|
30
|
+
params?: any,
|
|
31
|
+
config?: AxiosRequestConfig
|
|
32
|
+
) => Promise<R>
|
|
33
|
+
// 'post' | 'put' | 'patch'
|
|
34
|
+
export type ModifyHelper = <T = any, R = AxiosResponse<T>>(
|
|
35
|
+
url: string,
|
|
36
|
+
data?: any,
|
|
37
|
+
config?: AxiosRequestConfig
|
|
38
|
+
) => Promise<R>
|
|
39
|
+
|
|
40
|
+
export interface AxleHelpers {
|
|
41
|
+
// get data in json format
|
|
42
|
+
get: FetchHelper
|
|
43
|
+
// get data in blob format, which can be used to construct URL download under browser
|
|
44
|
+
getBlob: FetchHelper
|
|
45
|
+
// get the html document, return a dom node, can directly embed the document
|
|
46
|
+
getDocument: FetchHelper
|
|
47
|
+
// get text
|
|
48
|
+
getText: FetchHelper
|
|
49
|
+
// get Arraybuffer, which can be used for segmented download
|
|
50
|
+
getArraybuffer: FetchHelper
|
|
51
|
+
// get a readable stream, which can be used for browser caching
|
|
52
|
+
getStream: FetchHelper
|
|
53
|
+
|
|
54
|
+
head: FetchHelper
|
|
55
|
+
headBlob: FetchHelper
|
|
56
|
+
headDocument: FetchHelper
|
|
57
|
+
headText: FetchHelper
|
|
58
|
+
headArraybuffer: FetchHelper
|
|
59
|
+
headStream: FetchHelper
|
|
60
|
+
|
|
61
|
+
options: FetchHelper
|
|
62
|
+
optionsBlob: FetchHelper
|
|
63
|
+
optionsDocument: FetchHelper
|
|
64
|
+
optionsText: FetchHelper
|
|
65
|
+
optionsArraybuffer: FetchHelper
|
|
66
|
+
optionsStream: FetchHelper
|
|
67
|
+
|
|
68
|
+
delete: FetchHelper
|
|
69
|
+
deleteBlob: FetchHelper
|
|
70
|
+
deleteDocument: FetchHelper
|
|
71
|
+
deleteText: FetchHelper
|
|
72
|
+
deleteArrayBuffer: FetchHelper
|
|
73
|
+
deleteStream: FetchHelper
|
|
74
|
+
|
|
75
|
+
// submit application/x-www-form-urlencoded encoded data
|
|
76
|
+
post: ModifyHelper
|
|
77
|
+
// submit application/json encoded data
|
|
78
|
+
postJSON: ModifyHelper
|
|
79
|
+
// submit multipart/form-data encoded data
|
|
80
|
+
postMultipart: ModifyHelper
|
|
81
|
+
|
|
82
|
+
put: ModifyHelper
|
|
83
|
+
putJSON: ModifyHelper
|
|
84
|
+
putMultipart: ModifyHelper
|
|
85
|
+
|
|
86
|
+
patch: ModifyHelper
|
|
87
|
+
patchJSON: ModifyHelper
|
|
88
|
+
patchMultipart: ModifyHelper
|
|
89
|
+
|
|
90
|
+
// browser download file
|
|
91
|
+
download(blob: string | Blob, filename: string): void
|
|
92
|
+
|
|
93
|
+
// set global header
|
|
94
|
+
setHeader(key: string, value: string): void
|
|
95
|
+
// remove global header
|
|
96
|
+
removeHeader(key: string | string[]): void
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Normalize Api Arguments
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
// axios
|
|
104
|
+
import { createAxios } from 'axios'
|
|
105
|
+
|
|
106
|
+
const axios = createAxios({})
|
|
107
|
+
|
|
108
|
+
function getUser() {
|
|
109
|
+
return axios.get('/user', {
|
|
110
|
+
params: {
|
|
111
|
+
id: 1,
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function uploadFile() {
|
|
117
|
+
const formData = new FormData()
|
|
118
|
+
formData.append('name', 'axios')
|
|
119
|
+
formData.append('file', new File())
|
|
120
|
+
|
|
121
|
+
return axios.post('/user', formData, {
|
|
122
|
+
headers: {
|
|
123
|
+
'Ccontent-Type': 'multipart/form-data',
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
// axle
|
|
131
|
+
import { createAxle } from '@varlet/axle'
|
|
132
|
+
|
|
133
|
+
// create axle instance , axle has the same abilities as axios
|
|
134
|
+
const axle = createAxle(/** @see http://www.axios-js.com/zh-cn/docs/#axios-create-config **/)
|
|
135
|
+
|
|
136
|
+
// Some helper functions of axle
|
|
137
|
+
const axleHelpers = axle.helpers
|
|
138
|
+
|
|
139
|
+
function getUser() {
|
|
140
|
+
return axleHelpers.get('/user', { id: 1 })
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function addUser() {
|
|
144
|
+
return axleHelpers.postMultipart('/user', { name: 'axle', file: new File() })
|
|
145
|
+
}
|
|
146
|
+
```
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType } from "axios";
|
|
2
|
+
export declare type AxleRequestConfig = AxiosRequestConfig & {
|
|
3
|
+
exceptionMessage?: any;
|
|
4
|
+
};
|
|
5
|
+
export declare type FetchHelper = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
6
|
+
export declare type ModifyHelper = <T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
7
|
+
export interface AxiosHelpers {
|
|
8
|
+
get: FetchHelper;
|
|
9
|
+
getBlob: FetchHelper;
|
|
10
|
+
getDocument: FetchHelper;
|
|
11
|
+
getText: FetchHelper;
|
|
12
|
+
getArraybuffer: FetchHelper;
|
|
13
|
+
getStream: FetchHelper;
|
|
14
|
+
head: FetchHelper;
|
|
15
|
+
headBlob: FetchHelper;
|
|
16
|
+
headDocument: FetchHelper;
|
|
17
|
+
headText: FetchHelper;
|
|
18
|
+
headArraybuffer: FetchHelper;
|
|
19
|
+
headStream: FetchHelper;
|
|
20
|
+
options: FetchHelper;
|
|
21
|
+
optionsBlob: FetchHelper;
|
|
22
|
+
optionsDocument: FetchHelper;
|
|
23
|
+
optionsText: FetchHelper;
|
|
24
|
+
optionsArraybuffer: FetchHelper;
|
|
25
|
+
optionsStream: FetchHelper;
|
|
26
|
+
delete: FetchHelper;
|
|
27
|
+
deleteBlob: FetchHelper;
|
|
28
|
+
deleteDocument: FetchHelper;
|
|
29
|
+
deleteText: FetchHelper;
|
|
30
|
+
deleteArrayBuffer: FetchHelper;
|
|
31
|
+
deleteStream: FetchHelper;
|
|
32
|
+
post: ModifyHelper;
|
|
33
|
+
postJSON: ModifyHelper;
|
|
34
|
+
postMultipart: ModifyHelper;
|
|
35
|
+
put: ModifyHelper;
|
|
36
|
+
putJSON: ModifyHelper;
|
|
37
|
+
putMultipart: ModifyHelper;
|
|
38
|
+
patch: ModifyHelper;
|
|
39
|
+
patchJSON: ModifyHelper;
|
|
40
|
+
patchMultipart: ModifyHelper;
|
|
41
|
+
download(blob: string | Blob, filename: string): void;
|
|
42
|
+
setHeader(key: string, value: string): void;
|
|
43
|
+
removeHeader(key: string | string[]): void;
|
|
44
|
+
}
|
|
45
|
+
export declare type FetchMethods = "get" | "delete" | "options" | "head";
|
|
46
|
+
export declare type ModifyMethods = "post" | "put" | "patch";
|
|
47
|
+
export declare type AxleInstance = AxiosInstance & {
|
|
48
|
+
helpers: AxiosHelpers;
|
|
49
|
+
};
|
|
50
|
+
export declare function createFetchHelper(service: AxleInstance, method: FetchMethods, responseType: ResponseType): <T, R = AxiosResponse<T, any>>(url: string, params?: any, config?: AxleRequestConfig | undefined) => Promise<R>;
|
|
51
|
+
export declare function createModifyHelper(service: AxleInstance, method: ModifyMethods, contentType: "application/json" | "multipart/form-data" | "application/x-www-form-urlencoded"): <T, R = AxiosResponse<T, any>>(url: string, data?: any, config?: AxleRequestConfig | undefined) => Promise<R>;
|
|
52
|
+
export declare function download(url: string | Blob, filename: string): void;
|
|
53
|
+
export declare function createAxle(config: AxiosRequestConfig): AxleInstance;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createAxle = exports.download = exports.createModifyHelper = exports.createFetchHelper = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const qs_1 = __importDefault(require("qs"));
|
|
9
|
+
function createFetchHelper(service, method, responseType) {
|
|
10
|
+
return function (url, params, config) {
|
|
11
|
+
return service[method](url, Object.assign({ params: Object.assign({ _t: Date.now() }, params), responseType }, config));
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.createFetchHelper = createFetchHelper;
|
|
15
|
+
function createModifyHelper(service, method, contentType) {
|
|
16
|
+
return function (url, data, config) {
|
|
17
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
18
|
+
data = qs_1.default.stringify(data);
|
|
19
|
+
}
|
|
20
|
+
if (contentType === "multipart/form-data") {
|
|
21
|
+
const formData = new FormData();
|
|
22
|
+
Object.keys(data).forEach((key) => formData.append(key, data[key]));
|
|
23
|
+
data = formData;
|
|
24
|
+
}
|
|
25
|
+
return service[method](url, data, Object.assign({ headers: {
|
|
26
|
+
"Content-Type": contentType,
|
|
27
|
+
} }, config));
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.createModifyHelper = createModifyHelper;
|
|
31
|
+
function download(url, filename) {
|
|
32
|
+
const a = document.createElement("a");
|
|
33
|
+
a.download = filename;
|
|
34
|
+
a.style.display = "none";
|
|
35
|
+
a.href = typeof url === "string" ? url : URL.createObjectURL(url);
|
|
36
|
+
document.body.appendChild(a);
|
|
37
|
+
a.click();
|
|
38
|
+
URL.revokeObjectURL(a.href);
|
|
39
|
+
document.body.removeChild(a);
|
|
40
|
+
}
|
|
41
|
+
exports.download = download;
|
|
42
|
+
function createAxle(config) {
|
|
43
|
+
const service = axios_1.default.create(config);
|
|
44
|
+
function setHeader(key, value) {
|
|
45
|
+
service.defaults.headers["common"][key] = value;
|
|
46
|
+
}
|
|
47
|
+
function removeHeader(key) {
|
|
48
|
+
if (typeof key === "string") {
|
|
49
|
+
Reflect.deleteProperty(service.defaults.headers["common"], key);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
key.forEach((k) => Reflect.deleteProperty(service.defaults.headers["common"], k));
|
|
53
|
+
}
|
|
54
|
+
service.helpers = {
|
|
55
|
+
get: createFetchHelper(service, "get", "json"),
|
|
56
|
+
getBlob: createFetchHelper(service, "get", "blob"),
|
|
57
|
+
getDocument: createFetchHelper(service, "get", "document"),
|
|
58
|
+
getArraybuffer: createFetchHelper(service, "get", "arraybuffer"),
|
|
59
|
+
getText: createFetchHelper(service, "get", "text"),
|
|
60
|
+
getStream: createFetchHelper(service, "get", "stream"),
|
|
61
|
+
head: createFetchHelper(service, "head", "json"),
|
|
62
|
+
headBlob: createFetchHelper(service, "head", "blob"),
|
|
63
|
+
headDocument: createFetchHelper(service, "head", "document"),
|
|
64
|
+
headArraybuffer: createFetchHelper(service, "head", "arraybuffer"),
|
|
65
|
+
headText: createFetchHelper(service, "head", "text"),
|
|
66
|
+
headStream: createFetchHelper(service, "head", "stream"),
|
|
67
|
+
options: createFetchHelper(service, "options", "json"),
|
|
68
|
+
optionsBlob: createFetchHelper(service, "options", "blob"),
|
|
69
|
+
optionsDocument: createFetchHelper(service, "options", "document"),
|
|
70
|
+
optionsArraybuffer: createFetchHelper(service, "options", "arraybuffer"),
|
|
71
|
+
optionsText: createFetchHelper(service, "options", "text"),
|
|
72
|
+
optionsStream: createFetchHelper(service, "options", "stream"),
|
|
73
|
+
delete: createFetchHelper(service, "delete", "json"),
|
|
74
|
+
deleteBlob: createFetchHelper(service, "delete", "blob"),
|
|
75
|
+
deleteDocument: createFetchHelper(service, "delete", "document"),
|
|
76
|
+
deleteArrayBuffer: createFetchHelper(service, "delete", "arraybuffer"),
|
|
77
|
+
deleteText: createFetchHelper(service, "delete", "text"),
|
|
78
|
+
deleteStream: createFetchHelper(service, "delete", "stream"),
|
|
79
|
+
post: createModifyHelper(service, "post", "application/x-www-form-urlencoded"),
|
|
80
|
+
postJSON: createModifyHelper(service, "post", "application/json"),
|
|
81
|
+
postMultipart: createModifyHelper(service, "post", "multipart/form-data"),
|
|
82
|
+
put: createModifyHelper(service, "put", "application/x-www-form-urlencoded"),
|
|
83
|
+
putJSON: createModifyHelper(service, "put", "application/json"),
|
|
84
|
+
putMultipart: createModifyHelper(service, "put", "multipart/form-data"),
|
|
85
|
+
patch: createModifyHelper(service, "patch", "application/x-www-form-urlencoded"),
|
|
86
|
+
patchJSON: createModifyHelper(service, "patch", "application/json"),
|
|
87
|
+
patchMultipart: createModifyHelper(service, "patch", "multipart/form-data"),
|
|
88
|
+
download,
|
|
89
|
+
setHeader,
|
|
90
|
+
removeHeader,
|
|
91
|
+
};
|
|
92
|
+
return service;
|
|
93
|
+
}
|
|
94
|
+
exports.createAxle = createAxle;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@varlet/axle",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A useful axios helper",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "es/index.js",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"axle",
|
|
9
|
+
"axios",
|
|
10
|
+
"helpers"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"preinstall": "npx only-allow pnpm",
|
|
15
|
+
"bootstrap": "pnpm i && pnpm --filter ./playground i",
|
|
16
|
+
"dev": "varlet-cli dev",
|
|
17
|
+
"build": "varlet-cli build",
|
|
18
|
+
"preview": "varlet-cli preview",
|
|
19
|
+
"compile": "rm -rf lib es && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
|
|
20
|
+
"release": "pnpm compile && varlet-cli release"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/qs": "^6.9.7",
|
|
24
|
+
"@varlet/cli": "^1.26.7",
|
|
25
|
+
"@varlet/icons": "^1.26.7",
|
|
26
|
+
"@varlet/touch-emulator": "^1.26.7",
|
|
27
|
+
"clipboard": "^2.0.6",
|
|
28
|
+
"live-server": "^1.2.1",
|
|
29
|
+
"lodash-es": "^4.17.21",
|
|
30
|
+
"typescript": "^4.4.4",
|
|
31
|
+
"vue": "3.2.16",
|
|
32
|
+
"vue-router": "4.0.12"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://varlet.gitee.io/axle/",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/varletjs/axle.git"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/varletjs/axle/issues"
|
|
41
|
+
},
|
|
42
|
+
"browserslist": [
|
|
43
|
+
"Chrome >= 51",
|
|
44
|
+
"iOS >= 10"
|
|
45
|
+
],
|
|
46
|
+
"packageManager": "pnpm@6.24.3",
|
|
47
|
+
"files": [
|
|
48
|
+
"lib"
|
|
49
|
+
],
|
|
50
|
+
"author": "Joker <13255238145@163.com>",
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"axios": "^0.26.1",
|
|
53
|
+
"qs": "^6.10.3"
|
|
54
|
+
}
|
|
55
|
+
}
|