@bddh/starling-fetch 1.0.10
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/cjs/index.d.ts +8 -0
- package/cjs/index.js +1 -0
- package/cjs/interface.d.ts +18 -0
- package/cjs/useFetch.d.ts +8 -0
- package/cjs/useFetch.js +1 -0
- package/cjs/useFetchPolling.d.ts +15 -0
- package/cjs/useFetchPolling.js +1 -0
- package/es/index.d.ts +8 -0
- package/es/index.js +1 -0
- package/es/interface.d.ts +18 -0
- package/es/useFetch.d.ts +8 -0
- package/es/useFetch.js +1 -0
- package/es/useFetchPolling.d.ts +15 -0
- package/es/useFetchPolling.js +1 -0
- package/package.json +40 -0
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("./useFetch.js"),s=require("./useFetchPolling.js");exports.useFetch=e.default,exports.useFetchPolling=s.default;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file fetch/interface
|
|
3
|
+
* @author zhangyibo02
|
|
4
|
+
*/
|
|
5
|
+
export interface FetchResult<TParam, TData> {
|
|
6
|
+
loading: boolean;
|
|
7
|
+
reload: (needSetLoading?: boolean) => Promise<any>;
|
|
8
|
+
data?: TData;
|
|
9
|
+
error?: Error;
|
|
10
|
+
latestPayload?: TParam;
|
|
11
|
+
}
|
|
12
|
+
export interface FetchOptions<TParam, TData> {
|
|
13
|
+
abort?: boolean;
|
|
14
|
+
onRequest?: (payload: TParam) => TParam | void;
|
|
15
|
+
onResponse?: (result: TData, params?: TParam) => TData | void | Promise<void | TData>;
|
|
16
|
+
onError?: (ex: any) => void;
|
|
17
|
+
}
|
|
18
|
+
export type FetchAction<TParam, TData> = (params: TParam) => Promise<TData>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file useFetch
|
|
3
|
+
* @author zhangzhe(zhangzhe@baidu.com)
|
|
4
|
+
* @modify zhangyibo02
|
|
5
|
+
*/
|
|
6
|
+
import type { FetchOptions, FetchResult, FetchAction } from './interface';
|
|
7
|
+
declare const useFetch: <TParam, TData>(action: FetchAction<TParam, TData>, params: TParam, options?: FetchOptions<TParam, TData> | undefined) => FetchResult<TParam, TData>;
|
|
8
|
+
export default useFetch;
|
package/cjs/useFetch.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("react-redux"),t=require("lodash"),n=require("@huse/previous-value");exports.default=(u,o,s)=>{const{abort:a,onRequest:c,onResponse:i,onError:l}=s||{},[d,p]=e.useState(!1),[q,R]=e.useState(),[f,h]=e.useState(),[y,v]=e.useState(),E=n.useOriginalDeepCopy(o),S=e.useRef();S.current={onRequest:c,onResponse:i,onError:l};const b=(()=>{try{return r.useDispatch()}catch(e){return null}})(),g=e.useCallback((async(e=!0)=>{try{if(a)return;e&&p(!0),R(void 0);const r=t.isFunction(S.current?.onRequest)&&S.current?.onRequest(E)||E;v(r);let n=null;n=b&&u.toString().includes("dispatch(")?await b(u(r)):await u(r);const o=await(S.current?.onResponse?.(n,r))||n;return h(o),p(!1),n}catch(e){t.isFunction(S.current?.onError)&&S.current?.onError(e),p(!1),R(e)}}),[u,E,a,b]);return e.useEffect((()=>{g()}),[g]),{loading:d,data:f,error:q,reload:g,latestPayload:y}};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file utils/fetchPolling
|
|
3
|
+
* @description 轮训函数
|
|
4
|
+
* @author zhangyibo02
|
|
5
|
+
*/
|
|
6
|
+
import type { FetchOptions, FetchResult, FetchAction } from './interface';
|
|
7
|
+
export declare enum ErrorCode {
|
|
8
|
+
MAX_RELOAD = 150000
|
|
9
|
+
}
|
|
10
|
+
export interface FetchPollingOptions<TParam, TData> extends FetchOptions<TParam, TData> {
|
|
11
|
+
interval?: number;
|
|
12
|
+
reloadCount?: number;
|
|
13
|
+
}
|
|
14
|
+
declare const useFetchPolling: <TParam, TData>(action: FetchAction<TParam, TData>, params: TParam, options?: FetchPollingOptions<TParam, TData> | undefined) => FetchResult<TParam, TData>;
|
|
15
|
+
export default useFetchPolling;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,r=require("lodash"),t=require("react"),o=require("@baidu/starling-interval"),u=require("./useFetch.js");exports.ErrorCode=void 0,(e=exports.ErrorCode||(exports.ErrorCode={}))[e.MAX_RELOAD=15e4]="MAX_RELOAD";const s={[exports.ErrorCode.MAX_RELOAD]:{code:exports.ErrorCode.MAX_RELOAD,message:"超过最大重连次数"}};exports.default=(e,n,c)=>{const{abort:a,interval:d,reloadCount:i,onRequest:E,onResponse:l,onError:R}=c||{},f=t.useRef(!1),p=t.useRef({}),A=t.useRef(0),x=t.useRef(),C=t.useCallback((()=>{A.current=0}),[]),_=u.default(e,n,{onRequest:E,onResponse:e=>{C(),r.isFunction(l)&&l(e),f.current||a||(x.current=o.createTimeout(p.current.reload,d||0))},onError(e){if(200!==r.get(e,"response.status")&&!f.current&&!a)return r.isNumber(i)&&A.current>=i&&R&&R(s[exports.ErrorCode.MAX_RELOAD]),A.current+=1,R&&R(e),void(x.current=o.createTimeout(p.current.reload,d||0));C(),R&&R(e)},abort:a});return p.current=_,t.useEffect((()=>{x?.current&&a&&x.current?.()}),[a]),t.useEffect((()=>()=>{f.current=!0}),[]),_};
|
package/es/index.d.ts
ADDED
package/es/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{default as useFetch}from"./useFetch.js";export{default as useFetchPolling}from"./useFetchPolling.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file fetch/interface
|
|
3
|
+
* @author zhangyibo02
|
|
4
|
+
*/
|
|
5
|
+
export interface FetchResult<TParam, TData> {
|
|
6
|
+
loading: boolean;
|
|
7
|
+
reload: (needSetLoading?: boolean) => Promise<any>;
|
|
8
|
+
data?: TData;
|
|
9
|
+
error?: Error;
|
|
10
|
+
latestPayload?: TParam;
|
|
11
|
+
}
|
|
12
|
+
export interface FetchOptions<TParam, TData> {
|
|
13
|
+
abort?: boolean;
|
|
14
|
+
onRequest?: (payload: TParam) => TParam | void;
|
|
15
|
+
onResponse?: (result: TData, params?: TParam) => TData | void | Promise<void | TData>;
|
|
16
|
+
onError?: (ex: any) => void;
|
|
17
|
+
}
|
|
18
|
+
export type FetchAction<TParam, TData> = (params: TParam) => Promise<TData>;
|
package/es/useFetch.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file useFetch
|
|
3
|
+
* @author zhangzhe(zhangzhe@baidu.com)
|
|
4
|
+
* @modify zhangyibo02
|
|
5
|
+
*/
|
|
6
|
+
import type { FetchOptions, FetchResult, FetchAction } from './interface';
|
|
7
|
+
declare const useFetch: <TParam, TData>(action: FetchAction<TParam, TData>, params: TParam, options?: FetchOptions<TParam, TData> | undefined) => FetchResult<TParam, TData>;
|
|
8
|
+
export default useFetch;
|
package/es/useFetch.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useState as r,useRef as t,useCallback as o,useEffect as e}from"react";import{useDispatch as n}from"react-redux";import{isFunction as a}from"lodash";import{useOriginalDeepCopy as s}from"@huse/previous-value";const u=(u,c,i)=>{const{abort:l,onRequest:d,onResponse:p,onError:m}=i||{},[R,f]=r(!1),[h,q]=r(),[y,E]=r(),[v,w]=r(),g=s(c),x=t();x.current={onRequest:d,onResponse:p,onError:m};const b=(()=>{try{return n()}catch(r){return null}})(),P=o((async(r=!0)=>{try{if(l)return;r&&f(!0),q(void 0);const t=a(x.current?.onRequest)&&x.current?.onRequest(g)||g;w(t);let o=null;o=b&&u.toString().includes("dispatch(")?await b(u(t)):await u(t);const e=await(x.current?.onResponse?.(o,t))||o;return E(e),f(!1),o}catch(r){a(x.current?.onError)&&x.current?.onError(r),f(!1),q(r)}}),[u,g,l,b]);return e((()=>{P()}),[P]),{loading:R,data:y,error:h,reload:P,latestPayload:v}};export{u as default};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file utils/fetchPolling
|
|
3
|
+
* @description 轮训函数
|
|
4
|
+
* @author zhangyibo02
|
|
5
|
+
*/
|
|
6
|
+
import type { FetchOptions, FetchResult, FetchAction } from './interface';
|
|
7
|
+
export declare enum ErrorCode {
|
|
8
|
+
MAX_RELOAD = 150000
|
|
9
|
+
}
|
|
10
|
+
export interface FetchPollingOptions<TParam, TData> extends FetchOptions<TParam, TData> {
|
|
11
|
+
interval?: number;
|
|
12
|
+
reloadCount?: number;
|
|
13
|
+
}
|
|
14
|
+
declare const useFetchPolling: <TParam, TData>(action: FetchAction<TParam, TData>, params: TParam, options?: FetchPollingOptions<TParam, TData> | undefined) => FetchResult<TParam, TData>;
|
|
15
|
+
export default useFetchPolling;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isFunction as r,get as e,isNumber as t}from"lodash";import{useRef as n,useCallback as o,useEffect as u}from"react";import{createTimeout as c}from"@baidu/starling-interval";import s from"./useFetch.js";var a;!function(r){r[r.MAX_RELOAD=15e4]="MAX_RELOAD"}(a||(a={}));const i={[a.MAX_RELOAD]:{code:a.MAX_RELOAD,message:"超过最大重连次数"}},A=(A,m,R)=>{const{abort:d,interval:l,reloadCount:p,onRequest:f,onResponse:E,onError:D}=R||{},L=n(!1),M=n({}),O=n(0),X=n(),_=o((()=>{O.current=0}),[]),v=s(A,m,{onRequest:f,onResponse:e=>{_(),r(E)&&E(e),L.current||d||(X.current=c(M.current.reload,l||0))},onError(r){if(200!==e(r,"response.status")&&!L.current&&!d)return t(p)&&O.current>=p&&D&&D(i[a.MAX_RELOAD]),O.current+=1,D&&D(r),void(X.current=c(M.current.reload,l||0));_(),D&&D(r)},abort:d});return M.current=v,u((()=>{X?.current&&d&&X.current?.()}),[d]),u((()=>()=>{L.current=!0}),[]),v};export{a as ErrorCode,A as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bddh/starling-fetch",
|
|
3
|
+
"version": "1.0.10",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"react",
|
|
6
|
+
"hooks",
|
|
7
|
+
"useFetch"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"module": "es/index.js",
|
|
12
|
+
"main": "cjs/index.js",
|
|
13
|
+
"typings": "es/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"cjs",
|
|
16
|
+
"es"
|
|
17
|
+
],
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"react": ">=17",
|
|
20
|
+
"react-redux": ">=7",
|
|
21
|
+
"lodash": ">=4",
|
|
22
|
+
"@huse/previous-value":"^1.1.1",
|
|
23
|
+
"@baidu/starling-interval": "^1.0.4"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@huse/previous-value": "^1.1.1",
|
|
27
|
+
"@baidu/starling-interval": "^1.0.4"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@reduxjs/toolkit": "^2.6.1"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rm -rf cjs es && rollup -c --bundleConfigAsCjs",
|
|
34
|
+
"test": "npx jest --passWithNoTests",
|
|
35
|
+
"deploy": "npm publish --registry=https://registry.npmjs.org --access public"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"registry": "https://registry.npmjs.org"
|
|
39
|
+
}
|
|
40
|
+
}
|