@varlet/axle 0.2.6 → 0.2.7-alpha.1701536208987

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/es/instance.d.ts DELETED
@@ -1,61 +0,0 @@
1
- import type { AxiosInstance, AxiosInterceptorOptions, AxiosRequestConfig, AxiosResponse, HeadersDefaults, InternalAxiosRequestConfig, ResponseType } from 'axios';
2
- export type AxleRequestConfig = AxiosRequestConfig & {
3
- exceptionMessage?: any;
4
- };
5
- export type FetchRunner = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
6
- export type ModifyRunner = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
7
- export type FetchMethod = 'get' | 'delete' | 'options' | 'head';
8
- export type ModifyMethod = 'post' | 'put' | 'patch';
9
- export type RunnerMethod = keyof Omit<AxleInstance, 'axios' | 'getHeaders' | 'setHeader' | 'removeHeader' | 'useRequestInterceptor' | 'useResponseInterceptor'>;
10
- export interface Interceptor<V> {
11
- onFulfilled?: ((value: V) => any) | null;
12
- onRejected?: ((error: any) => any) | null;
13
- options?: AxiosInterceptorOptions;
14
- }
15
- export type RequestInterceptor = Interceptor<InternalAxiosRequestConfig>;
16
- export type ResponseInterceptor<V = AxiosResponse<any, any>> = Interceptor<V>;
17
- export type AxleInstance = {
18
- get: FetchRunner;
19
- getBlob: FetchRunner;
20
- getDocument: FetchRunner;
21
- getText: FetchRunner;
22
- getArrayBuffer: FetchRunner;
23
- getStream: FetchRunner;
24
- head: FetchRunner;
25
- headBlob: FetchRunner;
26
- headDocument: FetchRunner;
27
- headText: FetchRunner;
28
- headArrayBuffer: FetchRunner;
29
- headStream: FetchRunner;
30
- options: FetchRunner;
31
- optionsBlob: FetchRunner;
32
- optionsDocument: FetchRunner;
33
- optionsText: FetchRunner;
34
- optionsArrayBuffer: FetchRunner;
35
- optionsStream: FetchRunner;
36
- delete: FetchRunner;
37
- deleteBlob: FetchRunner;
38
- deleteDocument: FetchRunner;
39
- deleteText: FetchRunner;
40
- deleteArrayBuffer: FetchRunner;
41
- deleteStream: FetchRunner;
42
- post: ModifyRunner;
43
- postUrlEncode: ModifyRunner;
44
- postMultipart: ModifyRunner;
45
- put: ModifyRunner;
46
- putUrlEncode: ModifyRunner;
47
- putMultipart: ModifyRunner;
48
- patch: ModifyRunner;
49
- patchUrlEncode: ModifyRunner;
50
- patchMultipart: ModifyRunner;
51
- getHeaders(): HeadersDefaults['common'];
52
- setHeader(key: string, value: string): void;
53
- removeHeader(key: string | string[]): void;
54
- useRequestInterceptor(...interceptors: RequestInterceptor[]): void;
55
- useResponseInterceptor(...interceptors: ResponseInterceptor[]): void;
56
- axios: AxiosInstance;
57
- };
58
- export declare function createFetchRunner(service: AxiosInstance, method: FetchMethod, responseType: ResponseType): <T, R = AxiosResponse<T, any>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
59
- export declare function createModifyRunner(service: AxiosInstance, method: ModifyMethod, contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded'): <T, R = AxiosResponse<T, any>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
60
- export declare function download(url: string | Blob, filename: string): void;
61
- export declare function createAxle(config?: AxiosRequestConfig): AxleInstance;
package/es/instance.js DELETED
@@ -1,105 +0,0 @@
1
- import axios from 'axios';
2
- import qs from 'qs';
3
- import { inBrowser } from '@varlet/shared';
4
- export function createFetchRunner(service, method, responseType) {
5
- return function (url, params, config) {
6
- return service[method](url, Object.assign({ params,
7
- responseType }, config));
8
- };
9
- }
10
- export function createModifyRunner(service, method, contentType) {
11
- return function (url, params, config) {
12
- if (contentType === 'application/x-www-form-urlencoded') {
13
- params = qs.stringify(params);
14
- }
15
- if (contentType === 'multipart/form-data') {
16
- const formData = new FormData();
17
- Object.keys(params).forEach((key) => formData.append(key, params[key]));
18
- params = formData;
19
- }
20
- return service[method](url, params, Object.assign({ headers: {
21
- 'Content-Type': contentType,
22
- } }, config));
23
- };
24
- }
25
- export function download(url, filename) {
26
- if (!inBrowser()) {
27
- return;
28
- }
29
- const a = document.createElement('a');
30
- a.download = filename;
31
- a.style.display = 'none';
32
- a.href = typeof url === 'string' ? url : URL.createObjectURL(url);
33
- document.body.appendChild(a);
34
- a.click();
35
- URL.revokeObjectURL(a.href);
36
- document.body.removeChild(a);
37
- }
38
- export function createAxle(config = {}) {
39
- const service = axios.create(config);
40
- function getHeaders() {
41
- return service.defaults.headers['common'];
42
- }
43
- function setHeader(key, value) {
44
- ;
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
- function useRequestInterceptor(...interceptors) {
55
- interceptors.forEach((interceptor) => {
56
- service.interceptors.request.use(interceptor.onFulfilled, interceptor.onRejected, interceptor.options);
57
- });
58
- }
59
- function useResponseInterceptor(...interceptors) {
60
- interceptors.forEach((interceptor) => {
61
- service.interceptors.response.use(interceptor.onFulfilled, interceptor.onRejected, interceptor.options);
62
- });
63
- }
64
- return {
65
- get: createFetchRunner(service, 'get', 'json'),
66
- getBlob: createFetchRunner(service, 'get', 'blob'),
67
- getDocument: createFetchRunner(service, 'get', 'document'),
68
- getArrayBuffer: createFetchRunner(service, 'get', 'arraybuffer'),
69
- getText: createFetchRunner(service, 'get', 'text'),
70
- getStream: createFetchRunner(service, 'get', 'stream'),
71
- head: createFetchRunner(service, 'head', 'json'),
72
- headBlob: createFetchRunner(service, 'head', 'blob'),
73
- headDocument: createFetchRunner(service, 'head', 'document'),
74
- headArrayBuffer: createFetchRunner(service, 'head', 'arraybuffer'),
75
- headText: createFetchRunner(service, 'head', 'text'),
76
- headStream: createFetchRunner(service, 'head', 'stream'),
77
- options: createFetchRunner(service, 'options', 'json'),
78
- optionsBlob: createFetchRunner(service, 'options', 'blob'),
79
- optionsDocument: createFetchRunner(service, 'options', 'document'),
80
- optionsArrayBuffer: createFetchRunner(service, 'options', 'arraybuffer'),
81
- optionsText: createFetchRunner(service, 'options', 'text'),
82
- optionsStream: createFetchRunner(service, 'options', 'stream'),
83
- delete: createFetchRunner(service, 'delete', 'json'),
84
- deleteBlob: createFetchRunner(service, 'delete', 'blob'),
85
- deleteDocument: createFetchRunner(service, 'delete', 'document'),
86
- deleteArrayBuffer: createFetchRunner(service, 'delete', 'arraybuffer'),
87
- deleteText: createFetchRunner(service, 'delete', 'text'),
88
- deleteStream: createFetchRunner(service, 'delete', 'stream'),
89
- post: createModifyRunner(service, 'post', 'application/json'),
90
- postUrlEncode: createModifyRunner(service, 'post', 'application/x-www-form-urlencoded'),
91
- postMultipart: createModifyRunner(service, 'post', 'multipart/form-data'),
92
- put: createModifyRunner(service, 'put', 'application/json'),
93
- putUrlEncode: createModifyRunner(service, 'put', 'application/x-www-form-urlencoded'),
94
- putMultipart: createModifyRunner(service, 'put', 'multipart/form-data'),
95
- patch: createModifyRunner(service, 'patch', 'application/json'),
96
- patchUrlEncode: createModifyRunner(service, 'patch', 'application/x-www-form-urlencoded'),
97
- patchMultipart: createModifyRunner(service, 'patch', 'multipart/form-data'),
98
- getHeaders,
99
- setHeader,
100
- removeHeader,
101
- useRequestInterceptor,
102
- useResponseInterceptor,
103
- axios: service,
104
- };
105
- }
@@ -1,3 +0,0 @@
1
- export * from './requestHeadersInterceptor';
2
- export * from './responseBlobInterceptor';
3
- export * from './responseTimeoutInterceptor';
@@ -1,3 +0,0 @@
1
- export * from './requestHeadersInterceptor';
2
- export * from './responseBlobInterceptor';
3
- export * from './responseTimeoutInterceptor';
@@ -1,7 +0,0 @@
1
- import type { RequestInterceptor } from '../instance';
2
- import type { AxiosInterceptorOptions } from 'axios';
3
- export interface RequestHeadersInterceptorOptions {
4
- headers?: Record<string, string> | (() => Record<string, string>);
5
- axiosInterceptorOptions?: AxiosInterceptorOptions;
6
- }
7
- export declare function requestHeadersInterceptor(options?: RequestHeadersInterceptorOptions): RequestInterceptor;
@@ -1,18 +0,0 @@
1
- import { isFunction } from '@varlet/shared';
2
- export function requestHeadersInterceptor(options = {}) {
3
- const { headers: headersOrGetter } = options;
4
- return {
5
- onFulfilled(config) {
6
- var _a;
7
- const headers = (_a = (isFunction(headersOrGetter) ? headersOrGetter() : headersOrGetter)) !== null && _a !== void 0 ? _a : {};
8
- Object.entries(headers).forEach(([key, value]) => {
9
- config.headers[key] = value;
10
- });
11
- return config;
12
- },
13
- onRejected(error) {
14
- return Promise.reject(error);
15
- },
16
- options: options.axiosInterceptorOptions,
17
- };
18
- }
@@ -1,7 +0,0 @@
1
- import type { AxiosInterceptorOptions, AxiosResponse } from 'axios';
2
- import type { ResponseInterceptor } from '../instance';
3
- export interface ResponseBlobInterceptorOptions {
4
- onResponse?: (response: AxiosResponse<any, any>) => any;
5
- axiosInterceptorOptions?: AxiosInterceptorOptions;
6
- }
7
- export declare function responseBlobInterceptor(options?: ResponseBlobInterceptorOptions): ResponseInterceptor;
@@ -1,15 +0,0 @@
1
- export function responseBlobInterceptor(options = {}) {
2
- return {
3
- onFulfilled(response) {
4
- var _a, _b;
5
- if (response.request.responseType === 'blob') {
6
- return (_b = (_a = options.onResponse) === null || _a === void 0 ? void 0 : _a.call(options, response)) !== null && _b !== void 0 ? _b : response;
7
- }
8
- return response;
9
- },
10
- onRejected(error) {
11
- return Promise.reject(error);
12
- },
13
- options: options.axiosInterceptorOptions,
14
- };
15
- }
@@ -1,7 +0,0 @@
1
- import type { AxiosInterceptorOptions } from 'axios';
2
- import type { ResponseInterceptor } from '../instance';
3
- export interface ResponseTimeoutInterceptorOptions {
4
- normalizeErrorCode?: string;
5
- axiosInterceptorOptions?: AxiosInterceptorOptions;
6
- }
7
- export declare function responseTimeoutInterceptor(options?: ResponseTimeoutInterceptorOptions): ResponseInterceptor;
@@ -1,15 +0,0 @@
1
- export function responseTimeoutInterceptor(options = {}) {
2
- return {
3
- onFulfilled(response) {
4
- return response;
5
- },
6
- onRejected(error) {
7
- var _a;
8
- if ((error.code == 'ECONNABORTED' && error.message.includes('timeout')) || error.code === 'ETIMEDOUT') {
9
- error.code = (_a = options === null || options === void 0 ? void 0 : options.normalizeErrorCode) !== null && _a !== void 0 ? _a : 'TIMEOUT';
10
- }
11
- return Promise.reject(error);
12
- },
13
- options: options.axiosInterceptorOptions,
14
- };
15
- }
@@ -1,2 +0,0 @@
1
- import { type Ref } from "vue";
2
- export declare function useAverageProgress(...progressCollection: Ref<number>[]): Ref<number>;
@@ -1,9 +0,0 @@
1
- import { ref, watch } from "vue";
2
- export function useAverageProgress(...progressCollection) {
3
- const averageProgress = ref(0);
4
- watch(() => progressCollection.map(progress => progress.value), (newProgressCollection) => {
5
- const total = newProgressCollection.reduce((total, progress) => total + progress, 0);
6
- averageProgress.value = total / progressCollection.length;
7
- }, { immediate: true });
8
- return averageProgress;
9
- }
@@ -1,2 +0,0 @@
1
- import { type Ref } from "vue";
2
- export declare function useHasLoading(...loadings: Ref<boolean>[]): Ref<boolean>;
@@ -1,8 +0,0 @@
1
- import { ref, watch } from "vue";
2
- export function useHasLoading(...loadings) {
3
- const hasLoading = ref(false);
4
- watch(() => loadings.map(loading => loading.value), (newLoadings) => {
5
- hasLoading.value = newLoadings.some(Boolean);
6
- }, { immediate: true });
7
- return hasLoading;
8
- }
package/es/useValues.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { type Ref, type WritableComputedRef } from 'vue';
2
- export declare function useValues<T extends Ref<any>[]>(...valueCollection: T): WritableComputedRef<T[number]['value'][]>;
package/es/useValues.js DELETED
@@ -1,11 +0,0 @@
1
- import { computed } from 'vue';
2
- export function useValues(...valueCollection) {
3
- return computed({
4
- get: () => valueCollection.map((value) => value.value),
5
- set(values) {
6
- valueCollection.forEach((value, index) => {
7
- value.value = values[index];
8
- });
9
- },
10
- });
11
- }