@spitx/helper 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/error/index.d.ts +4 -0
- package/flat/index.d.ts +5 -0
- package/index.d.ts +6 -0
- package/main.js +82 -0
- package/main.umd.cjs +1 -0
- package/onlyLast/index.d.ts +10 -0
- package/package.json +18 -0
- package/promise/index.d.ts +7 -0
- package/request/index.d.ts +1 -0
- package/request/loading.d.ts +48 -0
- package/service/index.d.ts +16 -0
- package/string/index.d.ts +1 -0
- package/useLoadingDelay/index.d.ts +8 -0
package/error/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type ErrorHandlerResult<T> = Promise<[Error, null] | [null, T]>;
|
|
2
|
+
export declare function handleError<T>(p: Promise<T>): ErrorHandlerResult<T>;
|
|
3
|
+
export type ErrorHandlerResultSync<T> = [Error, null] | [null, T];
|
|
4
|
+
export declare function handleErrorSync<T extends (...args: any[]) => any>(callback: T): ErrorHandlerResultSync<ReturnType<T>>;
|
package/flat/index.d.ts
ADDED
package/index.d.ts
ADDED
package/main.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
function d(e) {
|
|
2
|
+
let n;
|
|
3
|
+
const r = new Promise((t) => {
|
|
4
|
+
n = t;
|
|
5
|
+
});
|
|
6
|
+
return e.then((t) => {
|
|
7
|
+
n([null, t]);
|
|
8
|
+
}).catch((t) => {
|
|
9
|
+
n([t, null]);
|
|
10
|
+
}), r;
|
|
11
|
+
}
|
|
12
|
+
function y(e) {
|
|
13
|
+
try {
|
|
14
|
+
return [null, e()];
|
|
15
|
+
} catch (n) {
|
|
16
|
+
return [n, null];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function g(e, n) {
|
|
20
|
+
const r = [...e];
|
|
21
|
+
let t;
|
|
22
|
+
for (; r.length && (t = r.shift(), !!t); ) {
|
|
23
|
+
const o = n(t);
|
|
24
|
+
Array.isArray(o) && r.unshift(...o);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function a() {
|
|
28
|
+
let e;
|
|
29
|
+
return async function(n) {
|
|
30
|
+
const r = e = Math.random() * 1e7;
|
|
31
|
+
return {
|
|
32
|
+
data: await n,
|
|
33
|
+
isLast: r === e
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function h(e = () => {
|
|
38
|
+
}) {
|
|
39
|
+
let n = () => {
|
|
40
|
+
}, r = () => {
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
promise: new Promise((o, s) => {
|
|
44
|
+
e(o, s), n = o, r = s;
|
|
45
|
+
}),
|
|
46
|
+
resolve: n,
|
|
47
|
+
reject: r
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function w(e) {
|
|
51
|
+
return new Promise((n) => {
|
|
52
|
+
setTimeout(n, e);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const i = /* @__PURE__ */ new WeakMap();
|
|
56
|
+
async function p(e, n) {
|
|
57
|
+
const { least: r, signal: t = new AbortController() } = n;
|
|
58
|
+
i.has(t) || i.set(t, a());
|
|
59
|
+
const o = i.get(t) || a(), [s] = await Promise.all([o(e), w(r)]);
|
|
60
|
+
return s;
|
|
61
|
+
}
|
|
62
|
+
async function P(e, n) {
|
|
63
|
+
const {
|
|
64
|
+
delay: r,
|
|
65
|
+
signal: t = new AbortController(),
|
|
66
|
+
onSlow: o
|
|
67
|
+
} = n;
|
|
68
|
+
i.has(t) || i.set(t, a());
|
|
69
|
+
const s = i.get(t) || a(), { promise: u, resolve: f } = h(), l = Date.now();
|
|
70
|
+
setTimeout(() => f(l), r);
|
|
71
|
+
const c = s(e);
|
|
72
|
+
return await Promise.race([c, u]) === l && o(), c;
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
g as flat,
|
|
76
|
+
d as handleError,
|
|
77
|
+
y as handleErrorSync,
|
|
78
|
+
a as onlyLast,
|
|
79
|
+
p as withMinLoading,
|
|
80
|
+
h as withPromiseResolver,
|
|
81
|
+
P as withSlowLoading
|
|
82
|
+
};
|
package/main.umd.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r.main={}))})(this,function(r){"use strict";function a(t){let n;const o=new Promise(e=>{n=e});return t.then(e=>{n([null,e])}).catch(e=>{n([e,null])}),o}function h(t){try{return[null,t()]}catch(n){return[n,null]}}function w(t,n){const o=[...t];let e;for(;o.length&&(e=o.shift(),!!e);){const i=n(e);Array.isArray(i)&&o.unshift(...i)}}function c(){let t;return async function(n){const o=t=Math.random()*1e7;return{data:await n,isLast:o===t}}}function u(t=()=>{}){let n=()=>{},o=()=>{};return{promise:new Promise((i,l)=>{t(i,l),n=i,o=l}),resolve:n,reject:o}}function m(t){return new Promise(n=>{setTimeout(n,t)})}const s=new WeakMap;async function y(t,n){const{least:o,signal:e=new AbortController}=n;s.has(e)||s.set(e,c());const i=s.get(e)||c(),[l]=await Promise.all([i(t),m(o)]);return l}async function g(t,n){const{delay:o,signal:e=new AbortController,onSlow:i}=n;s.has(e)||s.set(e,c());const l=s.get(e)||c(),{promise:P,resolve:p}=u(),f=Date.now();setTimeout(()=>p(f),o);const d=l(t);return await Promise.race([d,P])===f&&i(),d}r.flat=w,r.handleError=a,r.handleErrorSync=h,r.onlyLast=c,r.withMinLoading=y,r.withPromiseResolver=u,r.withSlowLoading=g,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spitx/helper",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "helper module",
|
|
6
|
+
"author": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./main.js"
|
|
11
|
+
},
|
|
12
|
+
"main": "./main.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
15
|
+
"build": "vite build"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {}
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { withMinLoading, withSlowLoading, } from './loading';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对Promise做如下处理
|
|
3
|
+
* - 对请求都保持至少delay时长的loading
|
|
4
|
+
* - 处理竞态请求
|
|
5
|
+
* @param {AbortController} option.signal 用于标识竞态请求
|
|
6
|
+
* @example
|
|
7
|
+
* const signal = new AbortController()
|
|
8
|
+
* const { data, isLast} = leastLoading(request, {
|
|
9
|
+
* delay: 200,
|
|
10
|
+
* signal: signal,
|
|
11
|
+
* })
|
|
12
|
+
* if (!isLast) return
|
|
13
|
+
* doSomething(data)
|
|
14
|
+
*/
|
|
15
|
+
export declare function withMinLoading<T>(p: Promise<T>, option: {
|
|
16
|
+
least: number;
|
|
17
|
+
signal?: AbortController;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
data: Awaited<T>;
|
|
20
|
+
isLast: boolean;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* 对 Promise 做如下处理
|
|
24
|
+
* - 请求时长 < delay 时,不做任何处理
|
|
25
|
+
* - 请求时长 > delay 且 为竞态请求的最后一个时,触发最后一个请求的onSlow函数
|
|
26
|
+
* - 处理竞态请求
|
|
27
|
+
* @param {AbortController} option.signal 用于标识竞态请求
|
|
28
|
+
* @example
|
|
29
|
+
* const signal = new AbortController()
|
|
30
|
+
* const loading = ref(false)
|
|
31
|
+
* const { data, isLast} = slowLoading(request, {
|
|
32
|
+
* delay: 200,
|
|
33
|
+
* signal: signal,
|
|
34
|
+
* onSlow: () => {
|
|
35
|
+
* loading.value = true
|
|
36
|
+
* }
|
|
37
|
+
* })
|
|
38
|
+
* if (!isLast) return
|
|
39
|
+
* doSomething(data)
|
|
40
|
+
*/
|
|
41
|
+
export declare function withSlowLoading<T>(p: Promise<T>, option: {
|
|
42
|
+
delay: number;
|
|
43
|
+
signal?: AbortController;
|
|
44
|
+
onSlow: () => void;
|
|
45
|
+
}): Promise<{
|
|
46
|
+
data: Awaited<T>;
|
|
47
|
+
isLast: boolean;
|
|
48
|
+
}>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
import { ErrorHandlerResult } from '../error';
|
|
3
|
+
declare function createApiInstance(options?: CreateAxiosDefaults, callbacks?: {
|
|
4
|
+
onNotFound?: () => void;
|
|
5
|
+
onServiceFaild?: () => void;
|
|
6
|
+
onUnauthorized?: () => void;
|
|
7
|
+
beforeRequest?: (config: InternalAxiosRequestConfig<any>) => void;
|
|
8
|
+
beforeResponse?: (response: AxiosResponse<any, any>) => void;
|
|
9
|
+
}): {
|
|
10
|
+
instance: import('axios').AxiosInstance;
|
|
11
|
+
get<T>(url: string, config?: import('axios').AxiosRequestConfig<unknown> | undefined): ErrorHandlerResult<T>;
|
|
12
|
+
post<T>(url: string, data?: unknown, config?: import('axios').AxiosRequestConfig<unknown> | undefined): ErrorHandlerResult<T>;
|
|
13
|
+
delete<T>(url: string, config?: import('axios').AxiosRequestConfig<unknown> | undefined): ErrorHandlerResult<T>;
|
|
14
|
+
put<T>(url: string, data?: unknown, config?: import('axios').AxiosRequestConfig<unknown> | undefined): ErrorHandlerResult<T>;
|
|
15
|
+
};
|
|
16
|
+
export default createApiInstance;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function randomString(len?: number): string;
|