@axios-use/vue
Advanced tools
Comparing version 0.2.1 to 0.2.2-alpha.0
@@ -1,34 +0,28 @@ | ||
import { getCurrentInstance, inject } from "vue"; | ||
import axios from "axios"; | ||
var INJECT_INSIDE_WARN_MSG = "[@axios-use/vue warn]: getUseRequestConfig() can only be used inside setup() or functional components."; | ||
export var AXIOS_USE_VUE_PROVIDE_KEY = Symbol("axios_use_vue_config"); | ||
export var setUseRequestConfig = function (app, options) { | ||
var _version = Number(app.version.split(".")[0]); | ||
// for vue2 | ||
if (_version === 2) { | ||
app.mixin({ | ||
beforeCreate: function () { | ||
if (!this._provided) { | ||
var _cache_1 = {}; | ||
Object.defineProperty(this, "_provided", { | ||
get: function () { return _cache_1; }, | ||
set: function (v) { return Object.assign(_cache_1, v); }, | ||
}); | ||
} | ||
this._provided[AXIOS_USE_VUE_PROVIDE_KEY] = options; | ||
}, | ||
import { getCurrentInstance as c, inject as r } from "vue"; | ||
import _ from "axios"; | ||
const a = "[@axios-use/vue warn]: getUseRequestConfig() can only be used inside setup() or functional components.", o = Symbol( | ||
"axios_use_vue_config" | ||
), g = (e, t) => { | ||
Number(e.version.split(".")[0]) === 2 ? e.mixin({ | ||
beforeCreate() { | ||
if (!this._provided) { | ||
const n = {}; | ||
Object.defineProperty(this, "_provided", { | ||
get: () => n, | ||
set: (i) => Object.assign(n, i) | ||
}); | ||
} | ||
this._provided[o] = t; | ||
} | ||
else { | ||
app.provide(AXIOS_USE_VUE_PROVIDE_KEY, options); | ||
} | ||
}) : e.provide(o, t); | ||
}, u = (e) => e == null ? void 0 : e.data, m = () => { | ||
const e = !!c(); | ||
e || console.warn(a); | ||
const { instance: t = _, getResponseItem: s = u } = e ? r(o, {}) : {}; | ||
return { instance: t, getResponseItem: s }; | ||
}; | ||
var defaultGetResponseData = function (res) { return res === null || res === void 0 ? void 0 : res.data; }; | ||
export var getUseRequestConfig = function () { | ||
var _isInside = Boolean(getCurrentInstance()); | ||
if (!_isInside) { | ||
console.warn(INJECT_INSIDE_WARN_MSG); | ||
} | ||
var _a = _isInside ? inject(AXIOS_USE_VUE_PROVIDE_KEY, {}) : {}, _b = _a.instance, instance = _b === void 0 ? axios : _b, _c = _a.getResponseItem, getResponseItem = _c === void 0 ? defaultGetResponseData : _c; | ||
return { instance: instance, getResponseItem: getResponseItem }; | ||
export { | ||
o as AXIOS_USE_VUE_PROVIDE_KEY, | ||
m as getUseRequestConfig, | ||
g as setUseRequestConfig | ||
}; |
@@ -1,5 +0,12 @@ | ||
import plugin from "./plugin"; | ||
export * from "./request"; | ||
export * from "./useRequest"; | ||
export * from "./useResource"; | ||
export default plugin; | ||
import r from "./plugin.js"; | ||
import { _request as u, createRequestError as s, request as p } from "./request.js"; | ||
import { useRequest as m } from "./useRequest.js"; | ||
import { useResource as x } from "./useResource.js"; | ||
export { | ||
u as _request, | ||
s as createRequestError, | ||
r as default, | ||
p as request, | ||
m as useRequest, | ||
x as useResource | ||
}; |
@@ -1,7 +0,9 @@ | ||
import { setUseRequestConfig } from "./context"; | ||
var _plugin = { | ||
install: function (app, options) { | ||
setUseRequestConfig(app, options); | ||
}, | ||
import { setUseRequestConfig as s } from "./context.js"; | ||
const i = { | ||
install(t, e) { | ||
s(t, e); | ||
} | ||
}; | ||
export default _plugin; | ||
export { | ||
i as default | ||
}; |
@@ -1,30 +0,24 @@ | ||
import axios from "axios"; | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export function _request(config) { | ||
return config; | ||
import q from "axios"; | ||
function T(n) { | ||
return n; | ||
} | ||
/** | ||
* For TypeScript type deduction | ||
*/ | ||
export var request = function (config) { | ||
return _request(config); | ||
}; | ||
export function createRequestError(error) { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
var axiosErr = error; | ||
var axiosRes = error; | ||
var data = (_b = (_a = axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.response) === null || _a === void 0 ? void 0 : _a.data) !== null && _b !== void 0 ? _b : axiosRes === null || axiosRes === void 0 ? void 0 : axiosRes.data; | ||
var code = | ||
const _ = (n) => n; | ||
function b(n) { | ||
var s, u, p, d, m, f, g; | ||
const t = n, e = n, c = (u = (s = t == null ? void 0 : t.response) == null ? void 0 : s.data) != null ? u : e == null ? void 0 : e.data, l = ( | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
(_f = (_d = (_c = data === null || data === void 0 ? void 0 : data.code) !== null && _c !== void 0 ? _c : axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.code) !== null && _d !== void 0 ? _d : (_e = axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.response) === null || _e === void 0 ? void 0 : _e.status) !== null && _f !== void 0 ? _f : axiosRes === null || axiosRes === void 0 ? void 0 : axiosRes.status; | ||
var message = (axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.message) || ((_g = axiosErr === null || axiosErr === void 0 ? void 0 : axiosErr.response) === null || _g === void 0 ? void 0 : _g.statusText) || (axiosRes === null || axiosRes === void 0 ? void 0 : axiosRes.statusText); | ||
return { | ||
code: code, | ||
data: data, | ||
message: message, | ||
isCancel: axios.isCancel(error), | ||
original: error, | ||
}; | ||
(f = (m = (p = c == null ? void 0 : c.code) != null ? p : t == null ? void 0 : t.code) != null ? m : (d = t == null ? void 0 : t.response) == null ? void 0 : d.status) != null ? f : e == null ? void 0 : e.status | ||
), o = (t == null ? void 0 : t.message) || ((g = t == null ? void 0 : t.response) == null ? void 0 : g.statusText) || (e == null ? void 0 : e.statusText); | ||
return { | ||
code: l, | ||
data: c, | ||
message: o, | ||
isCancel: q.isCancel(n), | ||
original: n | ||
}; | ||
} | ||
export { | ||
T as _request, | ||
b as createRequestError, | ||
_ as request | ||
}; |
@@ -1,76 +0,42 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
var v = Object.defineProperty, I = Object.defineProperties; | ||
var x = Object.getOwnPropertyDescriptors; | ||
var _ = Object.getOwnPropertySymbols; | ||
var C = Object.prototype.hasOwnProperty, T = Object.prototype.propertyIsEnumerable; | ||
var k = (n, e, t) => e in n ? v(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t, q = (n, e) => { | ||
for (var t in e || (e = {})) | ||
C.call(e, t) && k(n, t, e[t]); | ||
if (_) | ||
for (var t of _(e)) | ||
T.call(e, t) && k(n, t, e[t]); | ||
return n; | ||
}, R = (n, e) => I(n, x(e)); | ||
import { ref as y, computed as U, unref as m, onUnmounted as w } from "vue"; | ||
import P from "axios"; | ||
import { getUseRequestConfig as b } from "./context.js"; | ||
import { createRequestError as j } from "./request.js"; | ||
function H(n, e) { | ||
const t = b(), d = (e == null ? void 0 : e.instance) || t.instance, r = y([]), i = U(() => m(r).length > 0), { onCompleted: f, onError: l } = e || {}, g = (o) => { | ||
const s = m(r); | ||
o && s.length > 0 && (r.value = s.filter((c) => c.token !== o)); | ||
}, h = (o) => { | ||
const s = m(r); | ||
s.length > 0 && (s.map((c) => c.cancel(o)), r.value = []); | ||
}; | ||
return w(() => { | ||
h(); | ||
}), [(...o) => { | ||
const s = n(...o), c = P.CancelToken.source(); | ||
return { ready: () => (r.value = [...m(r), c], d(R(q({}, s), { cancelToken: c.token })).then((u) => { | ||
g(c.token); | ||
const a = e != null && e.getResponseItem ? e.getResponseItem(u) : t.getResponseItem(u); | ||
return f == null || f(a, u), [a, u]; | ||
}).catch((u) => { | ||
g(c.token); | ||
const a = j(u); | ||
throw l == null || l(a), a; | ||
})), cancel: c.cancel }; | ||
}, { clear: h, hasPending: i }]; | ||
} | ||
export { | ||
H as useRequest | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
import { computed, ref, unref, onUnmounted } from "vue"; | ||
import axios from "axios"; | ||
import { getUseRequestConfig } from "./context"; | ||
import { createRequestError } from "./request"; | ||
export function useRequest(fn, options) { | ||
var requestConfig = getUseRequestConfig(); | ||
var _axiosIns = (options === null || options === void 0 ? void 0 : options.instance) || requestConfig.instance; | ||
var sources = ref([]); | ||
var hasPending = computed(function () { return unref(sources).length > 0; }); | ||
var _a = options || {}, onCompleted = _a.onCompleted, onError = _a.onError; | ||
var removeCancelToken = function (token) { | ||
var _sources = unref(sources); | ||
if (token && _sources.length > 0) { | ||
sources.value = _sources.filter(function (s) { return s.token !== token; }); | ||
} | ||
}; | ||
var clear = function (message) { | ||
var _sources = unref(sources); | ||
if (_sources.length > 0) { | ||
_sources.map(function (s) { return s.cancel(message); }); | ||
sources.value = []; | ||
} | ||
}; | ||
onUnmounted(function () { | ||
clear(); | ||
}); | ||
var request = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _config = fn.apply(void 0, args); | ||
// for browser compatibility | ||
var _source = axios.CancelToken.source(); | ||
var ready = function () { | ||
sources.value = __spreadArray(__spreadArray([], unref(sources), true), [_source], false); | ||
return _axiosIns(__assign(__assign({}, _config), { cancelToken: _source.token })) | ||
.then(function (res) { | ||
removeCancelToken(_source.token); | ||
var _data = ((options === null || options === void 0 ? void 0 : options.getResponseItem) | ||
? options.getResponseItem(res) | ||
: requestConfig.getResponseItem(res)); | ||
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(_data, res); | ||
return [_data, res]; | ||
}) | ||
.catch(function (err) { | ||
removeCancelToken(_source.token); | ||
var _error = createRequestError(err); | ||
onError === null || onError === void 0 ? void 0 : onError(_error); | ||
throw _error; | ||
}); | ||
}; | ||
return { ready: ready, cancel: _source.cancel }; | ||
}; | ||
return [request, { clear: clear, hasPending: hasPending }]; | ||
} |
@@ -1,88 +0,71 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
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) => { | ||
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]); | ||
return t; | ||
}; | ||
import { computed, unref, watch } from "vue"; | ||
import { useRequest } from "./useRequest"; | ||
import { hasReactive, useReducer, unrefs } from "./utils"; | ||
var REQUEST_CLEAR_MESSAGE = "A new request has been made before completing the last one"; | ||
function getDefaultStateLoading(requestParams, filter) { | ||
if (requestParams) { | ||
if (filter && typeof filter === "function") { | ||
return filter.apply(void 0, requestParams); | ||
} | ||
return true; | ||
} | ||
return undefined; | ||
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) { | ||
if (t) | ||
return r && typeof r == "function" ? r(...t) : !0; | ||
} | ||
function getNextState(state, action) { | ||
var response = action.type === "success" ? action.response : state.response; | ||
return { | ||
data: action.type === "success" ? action.data : state.data, | ||
response: response, | ||
error: action.type === "error" ? action.error : undefined, | ||
isLoading: action.type === "start" ? true : false, | ||
}; | ||
function Q(t, r) { | ||
const e = r.type === "success" ? r.response : t.response; | ||
return { | ||
data: r.type === "success" ? r.data : t.data, | ||
response: e, | ||
error: r.type === "error" ? r.error : void 0, | ||
isLoading: r.type === "start" | ||
}; | ||
} | ||
export function useResource(fn, requestParams, options) { | ||
var _a = useRequest(fn, { | ||
onCompleted: options === null || options === void 0 ? void 0 : options.onCompleted, | ||
onError: options === null || options === void 0 ? void 0 : options.onError, | ||
instance: options === null || options === void 0 ? void 0 : options.instance, | ||
getResponseItem: options === null || options === void 0 ? void 0 : options.getResponseItem, | ||
}), createRequest = _a[0], clear = _a[1].clear; | ||
var _b = useReducer(getNextState, __assign({ isLoading: getDefaultStateLoading(requestParams ? unrefs(requestParams) : requestParams, options === null || options === void 0 ? void 0 : options.filter) }, options === null || options === void 0 ? void 0 : options.defaultState)), state = _b[0], dispatch = _b[1]; | ||
var request = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
clear(REQUEST_CLEAR_MESSAGE); | ||
var _a = createRequest.apply(void 0, args), ready = _a.ready, cancel = _a.cancel; | ||
dispatch({ type: "start" }); | ||
ready() | ||
.then(function (_a) { | ||
var data = _a[0], response = _a[1]; | ||
dispatch({ type: "success", data: data, response: response }); | ||
}) | ||
.catch(function (e) { | ||
var error = e; | ||
if (!error.isCancel) { | ||
dispatch({ type: "error", error: error }); | ||
} | ||
}); | ||
return cancel; | ||
}; | ||
var refresh = function () { | ||
var _args = unrefs(requestParams || []); | ||
var _filter = typeof (options === null || options === void 0 ? void 0 : options.filter) === "function" ? options.filter.apply(options, _args) : true; | ||
if (_filter) { | ||
return request.apply(void 0, _args); | ||
} | ||
return undefined; | ||
}; | ||
var cancel = function (message) { | ||
dispatch({ type: "reset" }); | ||
clear(message); | ||
}; | ||
var _paramsDeps = computed(function () { | ||
return requestParams ? unrefs(requestParams) : requestParams; | ||
}); | ||
watch(_paramsDeps, function (params) { | ||
if (params) { | ||
refresh(); | ||
} | ||
}, { | ||
immediate: true, | ||
deep: hasReactive(requestParams), | ||
}); | ||
var _rtnState = computed(function () { return unref(state); }); | ||
return [_rtnState, request, refresh, cancel]; | ||
function B(t, r, e) { | ||
const [_, { clear: a }] = D(t, { | ||
onCompleted: e == null ? void 0 : e.onCompleted, | ||
onError: e == null ? void 0 : e.onError, | ||
instance: e == null ? void 0 : e.instance, | ||
getResponseItem: e == null ? void 0 : e.getResponseItem | ||
}), [s, u] = I(Q, m({ | ||
isLoading: N( | ||
r && n(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 || []); | ||
if (typeof (e == null ? void 0 : e.filter) == "function" ? e.filter(...c) : !0) | ||
return l(...c); | ||
}, E = (c) => { | ||
u({ type: "reset" }), a(c); | ||
}, v = S( | ||
() => r && n(r) | ||
); | ||
return w( | ||
v, | ||
(c) => { | ||
c && y(); | ||
}, | ||
{ | ||
immediate: !0, | ||
deep: G(r) | ||
} | ||
), [S(() => x(s)), l, y, E]; | ||
} | ||
export { | ||
B as useResource | ||
}; |
@@ -1,32 +0,18 @@ | ||
import { isReactive, readonly, ref, unref } from "vue"; | ||
/** | ||
* | ||
* @param reducer | ||
* @param initialArg | ||
*/ | ||
export function useReducer(reducer, initialArg) { | ||
var state = ref(initialArg); | ||
var dispatch = function (action) { | ||
state.value = reducer(state.value, action); | ||
}; | ||
return [readonly(state), dispatch]; | ||
import { ref as i, readonly as a, unref as f, isReactive as o } from "vue"; | ||
function c(e, r) { | ||
const t = i(r), n = (u) => { | ||
t.value = e(t.value, u); | ||
}; | ||
return [a(t), n]; | ||
} | ||
/** | ||
* `unref` for ref group | ||
* @param arr | ||
*/ | ||
export function unrefs(arr) { | ||
if (arr && Array.isArray(arr)) { | ||
return arr.map(function (a) { return unref(a); }); | ||
} | ||
return []; | ||
function y(e) { | ||
return e && Array.isArray(e) ? e.map((r) => f(r)) : []; | ||
} | ||
/** | ||
* check whether `reactive` value exists | ||
*/ | ||
export function hasReactive(args) { | ||
if (args && Array.isArray(args)) { | ||
return args.some(isReactive); | ||
} | ||
return false; | ||
function l(e) { | ||
return e && Array.isArray(e) ? e.some(o) : !1; | ||
} | ||
export { | ||
l as hasReactive, | ||
y as unrefs, | ||
c as useReducer | ||
}; |
{ | ||
"name": "@axios-use/vue", | ||
"version": "0.2.1", | ||
"version": "0.2.2-alpha.0", | ||
"description": "A Vue composition utilities for Axios.", | ||
"main": "lib/index.js", | ||
"type": "module", | ||
"main": "lib/index.cjs", | ||
"module": "esm/index.js", | ||
"types": "lib/index.d.ts", | ||
"unpkg": "dist/axios-use-vue.umd.js", | ||
"scripts": { | ||
"build": "rm -rf ./lib/* ./esm/* && yarn build:cjs && yarn build:es", | ||
"build:cjs": "tsc", | ||
"build:es": "tsc -m ESNext --outDir ./esm", | ||
"build": "rimraf lib esm dist; npm run build:js; npm run build:type", | ||
"build:js": "vite build", | ||
"build:type": "tsc --outDir ./esm --emitDeclarationOnly", | ||
"test": "vitest", | ||
"test:coverage": "vitest run --coverage", | ||
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --ignore-path .gitignore", | ||
"lint:fix": "yarn lint --fix", | ||
"lint:fix": "npm run lint --fix", | ||
"format": "prettier --write '**/*' --config ./.prettierrc", | ||
"postinstall": "node ./demi/postinstall.js", | ||
"prepare": "yarn build" | ||
"prepare": "npm run build" | ||
}, | ||
@@ -42,2 +44,3 @@ "author": "wangcch <wangcch.cc@gmail.com>", | ||
"@rushstack/eslint-patch": "^1.2.0", | ||
"@vitejs/plugin-vue": "^4.4.1", | ||
"@vitest/coverage-c8": "^0.29.8", | ||
@@ -55,3 +58,5 @@ "@vue/compiler-dom": "^3.2.47", | ||
"prettier": "^2.8.4", | ||
"rimraf": "^5.0.5", | ||
"typescript": "^4.9.5", | ||
"vite": "^4.5.0", | ||
"vitest": "^0.29.8", | ||
@@ -58,0 +63,0 @@ "vue": "^3.2.47", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 4 instances in 1 package
60053
Yes
20
31
455
9
1