@axios-use/vue
Advanced tools
Comparing version 0.2.5 to 0.3.0-alpha.0
@@ -1,2 +0,2 @@ | ||
(function(s,c){typeof exports=="object"&&typeof module!="undefined"?c(exports,require("vue"),require("axios")):typeof define=="function"&&define.amd?define(["exports","vue","axios"],c):(s=typeof globalThis!="undefined"?globalThis:s||self,c(s.AxiosUseVue={},s.Vue,s.Axios))})(this,function(s,c,f){"use strict";var K=Object.defineProperty,Q=Object.defineProperties;var W=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var X=Object.prototype.hasOwnProperty,Y=Object.prototype.propertyIsEnumerable;var v=(s,c,f)=>c in s?K(s,c,{enumerable:!0,configurable:!0,writable:!0,value:f}):s[c]=f,I=(s,c)=>{for(var f in c||(c={}))X.call(c,f)&&v(s,f,c[f]);if(b)for(var f of b(c))Y.call(c,f)&&v(s,f,c[f]);return s},U=(s,c)=>Q(s,W(c));const k="[@axios-use/vue warn]: getUseRequestConfig() can only be used inside setup() or functional components.",S=Symbol("axios_use_vue_config"),w=(n,e)=>{Number(n.version.split(".")[0])===2?n.mixin({beforeCreate(){if(!this._provided){const a={};Object.defineProperty(this,"_provided",{get:()=>a,set:u=>Object.assign(a,u)})}this._provided[S]=e}}):n.provide(S,e)},j=n=>n==null?void 0:n.data,D=()=>{const n=!!c.getCurrentInstance();n||console.warn(k);const{instance:e=f,getResponseItem:t=j}=n?c.inject(S,{}):{};return{instance:e,getResponseItem:t}},E={install(n,e){w(n,e)}};function N(n){return n}const O=n=>n;function q(n){var l,g,h,y,R,i,r;const e=n,t=n,a=(g=(l=e==null?void 0:e.response)==null?void 0:l.data)!=null?g:t==null?void 0:t.data,u=(i=(R=(h=a==null?void 0:a.code)!=null?h:e==null?void 0:e.code)!=null?R:(y=e==null?void 0:e.response)==null?void 0:y.status)!=null?i:t==null?void 0:t.status,m=(e==null?void 0:e.message)||((r=e==null?void 0:e.response)==null?void 0:r.statusText)||(t==null?void 0:t.statusText);return{code:u,data:a,message:m,isCancel:f.isCancel(n),original:n}}function A(n,e){const t=D(),a=(e==null?void 0:e.instance)||t.instance,u=c.shallowRef([]),m=c.computed(()=>c.unref(u).length>0),{onCompleted:l,onError:g}=e||{},h=i=>{const r=c.unref(u);i&&r.length>0&&(u.value=r.filter(d=>d.token!==i))},y=i=>{const r=c.unref(u);r.length>0&&(r.map(d=>d.cancel(i)),u.value=[])};return c.onUnmounted(()=>{y()}),[(...i)=>{const r=n(...i),d=f.CancelToken.source();return{ready:()=>(u.value=[...c.unref(u),d],a(U(I({},r),{cancelToken:d.token})).then(o=>{h(d.token);const _=e!=null&&e.getResponseItem?e.getResponseItem(o):t.getResponseItem(o);return l==null||l(_,o),[_,o]}).catch(o=>{h(d.token);const _=q(o);throw g==null||g(_),_})),cancel:d.cancel}},{clear:y,hasPending:m}]}function L(n,e){const t=c.ref(e),a=u=>{t.value=n(t.value,u)};return[c.readonly(t),a]}function C(n){return n&&Array.isArray(n)?n.map(e=>c.unref(e)):[]}function M(n){return n&&Array.isArray(n)?n.some(c.isReactive):!1}const V="A new request has been made before completing the last one";function G(n,e){if(n)return e&&typeof e=="function"?e(...n):!0}function B(n,e){const t=e.type==="success"?e.response:n.response;return{data:e.type==="success"?e.data:n.data,response:t,error:e.type==="error"?e.error:void 0,isLoading:e.type==="start"}}function J(n,e,t){const[a,{clear:u}]=A(n,{onCompleted:t==null?void 0:t.onCompleted,onError:t==null?void 0:t.onError,instance:t==null?void 0:t.instance,getResponseItem:t==null?void 0:t.getResponseItem}),[m,l]=L(B,I({isLoading:G(e&&C(e),t==null?void 0:t.filter)},t==null?void 0:t.defaultState)),g=(...r)=>{u(V);const{ready:d,cancel:T}=a(...r);return l({type:"start"}),d().then(([o,_])=>{l({type:"success",data:o,response:_})}).catch(o=>{const _=o;_.isCancel||l({type:"error",error:_})}),T},h=()=>{const r=C(e||[]);if(typeof(t==null?void 0:t.filter)=="function"?t.filter(...r):!0)return g(...r)},y=r=>{l({type:"reset"}),u(r)},R=c.computed(()=>e&&C(e));return c.watch(R,r=>{r&&h()},{immediate:!0,deep:M(e)}),[c.computed(()=>c.unref(m)),g,h,y]}s._request=N,s.createRequestError=q,s.default=E,s.request=O,s.useRequest=A,s.useResource=J,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
(function(r,c){typeof exports=="object"&&typeof module!="undefined"?c(exports,require("vue"),require("axios")):typeof define=="function"&&define.amd?define(["exports","vue","axios"],c):(r=typeof globalThis!="undefined"?globalThis:r||self,c(r.AxiosUseVue={},r.Vue,r.Axios))})(this,function(r,c,s){"use strict";var X=Object.defineProperty,Y=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var O=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var L=(r,c,s)=>c in r?X(r,c,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[c]=s,D=(r,c)=>{for(var s in c||(c={}))F.call(c,s)&&L(r,s,c[s]);if(O)for(var s of O(c))H.call(c,s)&&L(r,s,c[s]);return r},M=(r,c)=>Y(r,z(c));var V=(r,c,s)=>new Promise((v,S)=>{var w=g=>{try{I(s.next(g))}catch(A){S(A)}},U=g=>{try{I(s.throw(g))}catch(A){S(A)}},I=g=>g.done?v(g.value):Promise.resolve(g.value).then(w,U);I((s=s.apply(r,c)).next())});const v="[@axios-use/vue warn]: getUseRequestConfig() can only be used inside setup() or functional components.",S=Symbol("axios_use_vue_config"),w=(n,e)=>{Number(n.version.split(".")[0])===2?n.mixin({beforeCreate(){if(!this._provided){const a={};Object.defineProperty(this,"_provided",{get:()=>a,set:u=>Object.assign(a,u)})}this._provided[S]=e}}):n.provide(S,e)},U=n=>n==null?void 0:n.data,I=()=>{const n=!!c.getCurrentInstance();n||console.warn(v);const{instance:e=s,getResponseItem:t=U}=n?c.inject(S,{}):{};return{instance:e,getResponseItem:t}},g={install(n,e){w(n,e)}};function A(n){return n}const G=n=>n;function j(n){var f,h,R,C,q,o,i;const e=n,t=n,a=(h=(f=e==null?void 0:e.response)==null?void 0:f.data)!=null?h:t==null?void 0:t.data,u=(o=(q=(R=a==null?void 0:a.code)!=null?R:e==null?void 0:e.code)!=null?q:(C=e==null?void 0:e.response)==null?void 0:C.status)!=null?o:t==null?void 0:t.status,T=(e==null?void 0:e.message)||((i=e==null?void 0:e.response)==null?void 0:i.statusText)||(t==null?void 0:t.statusText);return{code:u,data:a,message:T,isCancel:s.isCancel(n),original:n}}function E(n,e){const t=I(),a=(e==null?void 0:e.instance)||t.instance,u=c.shallowRef([]),T=c.computed(()=>c.unref(u).length>0),{onCompleted:f,onError:h}=e||{},R=o=>{const i=c.unref(u);o&&i.length>0&&(u.value=i.filter(y=>y.token!==o))},C=o=>{const i=c.unref(u);i.length>0&&(i.map(y=>y.cancel(o)),u.value=[])};return c.onUnmounted(()=>{C()}),[(...o)=>{const i=n(...o),y=s.CancelToken.source();return{ready:()=>(u.value=[...c.unref(u),y],a(M(D({},i),{cancelToken:y.token})).then(_=>{R(y.token);const l=e!=null&&e.getResponseItem?e.getResponseItem(_):t.getResponseItem(_);return f==null||f(l,_),[l,_]}).catch(_=>{R(y.token);const l=j(_);throw h==null||h(l),l})),cancel:y.cancel}},{clear:C,hasPending:T}]}function B(n,e){const t=c.ref(e),a=u=>{t.value=n(t.value,u)};return[c.readonly(t),a]}function k(n){return n&&Array.isArray(n)?n.map(e=>c.unref(e)):[]}function J(n){return n&&Array.isArray(n)?n.some(c.isReactive):!1}const N="A new request has been made before completing the last one";function K(n,e){if(n)return e&&typeof e=="function"?e(...n):!0}function Q(n,e){const t=e.type==="success"?e.response:n.response;return{data:e.type==="success"?e.data:n.data,response:t,error:e.type==="error"?e.error:void 0,isLoading:e.type==="start"}}function W(n,e,t){const[a,{clear:u}]=E(n,{onCompleted:t==null?void 0:t.onCompleted,onError:t==null?void 0:t.onError,instance:t==null?void 0:t.instance,getResponseItem:t==null?void 0:t.getResponseItem}),[T,f]=B(Q,D({isLoading:K(e&&k(e),t==null?void 0:t.filter)},t==null?void 0:t.defaultState)),h=(...d)=>{u(N);const{ready:_,cancel:l}=a(...d);return f({type:"start"}),_().then(([m,b])=>{f({type:"success",data:m,response:b})}).catch(m=>{const b=m;b.isCancel||f({type:"error",error:b})}),l},R=(...d)=>V(this,null,function*(){u(N);const{ready:_}=a(...d);try{f({type:"start"});const[l,m]=yield _();return f({type:"success",data:l,response:m}),[l,m]}catch(l){const m=l;throw m.isCancel||f({type:"error",error:m}),l}}),C=(...d)=>t!=null&&t.asyncReq?R(...d):h(...d),q=()=>{const d=k(e||[]);if(typeof(t==null?void 0:t.filter)=="function"?t.filter(...d):!0)return h(...d)},o=d=>{f({type:"reset"}),u(d)},i=c.computed(()=>e&&k(e));return c.watch(i,d=>{d&&q()},{immediate:!0,deep:J(e)}),[c.computed(()=>c.unref(T)),C,q,o]}r._request=A,r.createRequestError=j,r.default=g,r.request=G,r.useRequest=E,r.useResource=W,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
//# sourceMappingURL=axios-use-vue.umd.js.map |
@@ -17,2 +17,3 @@ import type { AxiosRequestConfig, AxiosResponse, AxiosError, Canceler } from "axios"; | ||
export type RequestDispatcher<T extends Request> = (...args: Parameters<T>) => Canceler; | ||
export type RequestAsyncFunc<T extends Request> = (...args: Parameters<T>) => Promise<readonly [Payload<T, true>, Payload<T>]>; | ||
/** | ||
@@ -19,0 +20,0 @@ * Normalize the error response returned from `@axios-use/vue` |
import type { ComputedRef } from "vue"; | ||
import type { Canceler } from "axios"; | ||
import type { RequestConfigType } from "./context"; | ||
import type { Payload, BodyData, RequestError, Request, RequestDispatcher, RequestCallbackFn } from "./request"; | ||
import type { Payload, BodyData, RequestError, Request, RequestAsyncFunc, RequestDispatcher, RequestCallbackFn } from "./request"; | ||
import type { FullRefArrayItem } from "./utils"; | ||
@@ -13,7 +13,7 @@ export type RequestState<T extends Request> = { | ||
export type UseResourceResultState<T extends Request> = ComputedRef<RequestState<T>>; | ||
export type UseResourceResult<T extends Request> = [ | ||
export type UseResourceResult<T extends Request, A extends boolean = false> = [ | ||
/** Response data group */ | ||
UseResourceResultState<T>, | ||
/** A function that enables you to re-execute the request. And pass in new variables */ | ||
RequestDispatcher<T>, | ||
A extends true ? RequestAsyncFunc<T> : RequestDispatcher<T>, | ||
/** A function that enables you to re-execute the request. Keep the latest variables */ | ||
@@ -24,8 +24,10 @@ () => Canceler | undefined, | ||
]; | ||
export type UseResourceOptions<T extends Request> = Pick<RequestConfigType, "instance" | "getResponseItem"> & RequestCallbackFn<T> & { | ||
export type UseResourceOptions<T extends Request, A extends boolean = false> = Pick<RequestConfigType, "instance" | "getResponseItem"> & RequestCallbackFn<T> & { | ||
/** Conditional Fetching */ | ||
filter?: (...args: Parameters<T>) => boolean; | ||
defaultState?: RequestState<T>; | ||
/** Control the return value of the request */ | ||
asyncReq?: A; | ||
}; | ||
export type RequestDepsParameters<T extends Request> = Parameters<T> | FullRefArrayItem<Parameters<T>> | ComputedRef<Parameters<T>>; | ||
export declare function useResource<T extends Request>(fn: T, requestParams?: RequestDepsParameters<T> | false, options?: UseResourceOptions<T>): UseResourceResult<T>; | ||
export declare function useResource<T extends Request, A extends boolean = false>(fn: T, requestParams?: RequestDepsParameters<T> | false, options?: UseResourceOptions<T, A>): UseResourceResult<T, A>; |
@@ -1,21 +0,37 @@ | ||
var L = Object.defineProperty; | ||
var R = Object.getOwnPropertySymbols; | ||
var A = Object.prototype.hasOwnProperty, b = Object.prototype.propertyIsEnumerable; | ||
var h = (t, r, e) => r in t ? L(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, m = (t, r) => { | ||
var b = Object.defineProperty; | ||
var _ = Object.getOwnPropertySymbols; | ||
var x = Object.prototype.hasOwnProperty, I = Object.prototype.propertyIsEnumerable; | ||
var E = (t, r, e) => r in t ? b(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, C = (t, r) => { | ||
for (var e in r || (r = {})) | ||
A.call(r, e) && h(t, e, r[e]); | ||
if (R) | ||
for (var e of R(r)) | ||
b.call(r, e) && h(t, e, r[e]); | ||
x.call(r, e) && E(t, e, r[e]); | ||
if (_) | ||
for (var e of _(r)) | ||
I.call(r, e) && E(t, e, r[e]); | ||
return t; | ||
}; | ||
import { computed as S, watch as w, unref as x } from "vue"; | ||
import { useRequest as D } from "./useRequest.js"; | ||
import { useReducer as I, unrefs as n, hasReactive as G } from "./utils.js"; | ||
const M = "A new request has been made before completing the last one"; | ||
function N(t, r) { | ||
var v = (t, r, e) => new Promise((l, d) => { | ||
var g = (u) => { | ||
try { | ||
y(e.next(u)); | ||
} catch (s) { | ||
d(s); | ||
} | ||
}, a = (u) => { | ||
try { | ||
y(e.throw(u)); | ||
} catch (s) { | ||
d(s); | ||
} | ||
}, y = (u) => u.done ? l(u.value) : Promise.resolve(u.value).then(g, a); | ||
y((e = e.apply(t, r)).next()); | ||
}); | ||
import { computed as w, watch as G, unref as M } from "vue"; | ||
import { useRequest as N } from "./useRequest.js"; | ||
import { useReducer as Q, unrefs as m, hasReactive as T } from "./utils.js"; | ||
const A = "A new request has been made before completing the last one"; | ||
function U(t, r) { | ||
if (t) | ||
return r && typeof r == "function" ? r(...t) : !0; | ||
} | ||
function Q(t, r) { | ||
function j(t, r) { | ||
const e = r.type === "success" ? r.response : t.response; | ||
@@ -29,4 +45,4 @@ return { | ||
} | ||
function B(t, r, e) { | ||
const [_, { clear: a }] = D(t, { | ||
function J(t, r, e) { | ||
const [l, { clear: d }] = N(t, { | ||
onCompleted: e == null ? void 0 : e.onCompleted, | ||
@@ -36,38 +52,49 @@ onError: e == null ? void 0 : e.onError, | ||
getResponseItem: e == null ? void 0 : e.getResponseItem | ||
}), [E, u] = I(Q, m({ | ||
isLoading: N( | ||
r && n(r), | ||
}), [g, a] = Q(j, C({ | ||
isLoading: U( | ||
r && m(r), | ||
e == null ? void 0 : e.filter | ||
) | ||
}, e == null ? void 0 : e.defaultState)), l = (...c) => { | ||
a(M); | ||
const { ready: g, cancel: C } = _(...c); | ||
return u({ type: "start" }), g().then(([d, f]) => { | ||
u({ type: "success", data: d, response: f }); | ||
}).catch((d) => { | ||
const f = d; | ||
f.isCancel || u({ type: "error", error: f }); | ||
}), C; | ||
}, y = () => { | ||
const c = n(r || []); | ||
}, e == null ? void 0 : e.defaultState)), y = (...c) => { | ||
d(A); | ||
const { ready: h, cancel: n } = l(...c); | ||
return a({ type: "start" }), h().then(([f, R]) => { | ||
a({ type: "success", data: f, response: R }); | ||
}).catch((f) => { | ||
const R = f; | ||
R.isCancel || a({ type: "error", error: R }); | ||
}), n; | ||
}, u = (...c) => v(this, null, function* () { | ||
d(A); | ||
const { ready: h } = l(...c); | ||
try { | ||
a({ type: "start" }); | ||
const [n, f] = yield h(); | ||
return a({ type: "success", data: n, response: f }), [n, f]; | ||
} catch (n) { | ||
const f = n; | ||
throw f.isCancel || a({ type: "error", error: f }), n; | ||
} | ||
}), s = (...c) => e != null && e.asyncReq ? u(...c) : y(...c), S = () => { | ||
const c = m(r || []); | ||
if (typeof (e == null ? void 0 : e.filter) == "function" ? e.filter(...c) : !0) | ||
return l(...c); | ||
}, s = (c) => { | ||
u({ type: "reset" }), a(c); | ||
}, v = S( | ||
() => r && n(r) | ||
return y(...c); | ||
}, L = (c) => { | ||
a({ type: "reset" }), d(c); | ||
}, D = w( | ||
() => r && m(r) | ||
); | ||
return w( | ||
v, | ||
return G( | ||
D, | ||
(c) => { | ||
c && y(); | ||
c && S(); | ||
}, | ||
{ | ||
immediate: !0, | ||
deep: G(r) | ||
deep: T(r) | ||
} | ||
), [S(() => x(E)), l, y, s]; | ||
), [w(() => M(g)), s, S, L]; | ||
} | ||
export { | ||
B as useResource | ||
J as useResource | ||
}; |
{ | ||
"name": "@axios-use/vue", | ||
"version": "0.2.5", | ||
"version": "0.3.0-alpha.0", | ||
"description": "A Vue composition utilities for Axios.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
64281
445