@ametie/vue-muza-use 0.0.1
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/README.md +267 -0
- package/dist/index.cjs +573 -0
- package/dist/index.d.cts +350 -0
- package/dist/index.d.ts +350 -0
- package/dist/index.mjs +521 -0
- package/package.json +48 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import { inject } from "vue";
|
|
3
|
+
var API_INJECTION_KEY = /* @__PURE__ */ Symbol("use-api-config");
|
|
4
|
+
function createApi(options) {
|
|
5
|
+
return {
|
|
6
|
+
install(app) {
|
|
7
|
+
app.provide(API_INJECTION_KEY, options);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function useApiConfig() {
|
|
12
|
+
const config = inject(API_INJECTION_KEY);
|
|
13
|
+
if (!config) {
|
|
14
|
+
throw new Error("API plugin not installed! Did you forget app.use(createApi(...))?");
|
|
15
|
+
}
|
|
16
|
+
return config;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/utils/debounce.ts
|
|
20
|
+
function debounceFn(fn, delay) {
|
|
21
|
+
let timeoutId = null;
|
|
22
|
+
return function(...args) {
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
if (timeoutId) {
|
|
25
|
+
clearTimeout(timeoutId);
|
|
26
|
+
}
|
|
27
|
+
timeoutId = setTimeout(async () => {
|
|
28
|
+
const result = await fn(...args);
|
|
29
|
+
resolve(result);
|
|
30
|
+
timeoutId = null;
|
|
31
|
+
}, delay);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/useApi.ts
|
|
37
|
+
import { ref as ref3, getCurrentScope, onScopeDispose, toValue } from "vue";
|
|
38
|
+
|
|
39
|
+
// src/utils/errorParser.ts
|
|
40
|
+
function parseApiError(error) {
|
|
41
|
+
if (error && typeof error === "object" && "isAxiosError" in error && error.isAxiosError) {
|
|
42
|
+
const axiosError = error;
|
|
43
|
+
if (axiosError.response) {
|
|
44
|
+
const { data, status } = axiosError.response;
|
|
45
|
+
const message = data?.message || data?.error || axiosError.message || "Unknown Error";
|
|
46
|
+
return {
|
|
47
|
+
message,
|
|
48
|
+
status,
|
|
49
|
+
code: data?.code,
|
|
50
|
+
errors: data?.errors,
|
|
51
|
+
details: data
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
message: error instanceof Error ? error.message : String(error),
|
|
57
|
+
status: 0
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/composables/useApiState.ts
|
|
62
|
+
import { ref } from "vue";
|
|
63
|
+
function useApiState(initialData = null, options = {}) {
|
|
64
|
+
const { initialLoading = false } = options;
|
|
65
|
+
const data = ref(initialData);
|
|
66
|
+
const loading = ref(initialLoading);
|
|
67
|
+
const error = ref(null);
|
|
68
|
+
const statusCode = ref(null);
|
|
69
|
+
const response = ref(null);
|
|
70
|
+
const setData = (newData, fullResponse) => {
|
|
71
|
+
data.value = newData;
|
|
72
|
+
error.value = null;
|
|
73
|
+
if (fullResponse) {
|
|
74
|
+
response.value = fullResponse;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const setError = (newError) => {
|
|
78
|
+
error.value = newError;
|
|
79
|
+
};
|
|
80
|
+
const setLoading = (isLoading) => {
|
|
81
|
+
loading.value = isLoading;
|
|
82
|
+
};
|
|
83
|
+
const setStatusCode = (code) => {
|
|
84
|
+
statusCode.value = code;
|
|
85
|
+
};
|
|
86
|
+
const reset = () => {
|
|
87
|
+
data.value = initialData;
|
|
88
|
+
loading.value = false;
|
|
89
|
+
error.value = null;
|
|
90
|
+
statusCode.value = null;
|
|
91
|
+
response.value = null;
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
data,
|
|
95
|
+
loading,
|
|
96
|
+
error,
|
|
97
|
+
statusCode,
|
|
98
|
+
response,
|
|
99
|
+
setData,
|
|
100
|
+
setError,
|
|
101
|
+
setLoading,
|
|
102
|
+
setStatusCode,
|
|
103
|
+
reset
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/composables/useAbortController.ts
|
|
108
|
+
import { ref as ref2, readonly } from "vue";
|
|
109
|
+
var abortController = new AbortController();
|
|
110
|
+
var abortCount = ref2(0);
|
|
111
|
+
function useAbortController() {
|
|
112
|
+
const signal = readonly(ref2(abortController.signal));
|
|
113
|
+
const abort = () => {
|
|
114
|
+
abortCount.value++;
|
|
115
|
+
abortController.abort();
|
|
116
|
+
abortController = new AbortController();
|
|
117
|
+
};
|
|
118
|
+
const getSignal = () => {
|
|
119
|
+
return abortController.signal;
|
|
120
|
+
};
|
|
121
|
+
const isAbortError = (error) => {
|
|
122
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
signal,
|
|
126
|
+
abort,
|
|
127
|
+
getSignal,
|
|
128
|
+
isAbortError,
|
|
129
|
+
abortCount: readonly(abortCount)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/useApi.ts
|
|
134
|
+
function useApi(url, options = {}) {
|
|
135
|
+
const { axios: axios2, onError: globalErrorHandler, globalOptions } = useApiConfig();
|
|
136
|
+
const {
|
|
137
|
+
method = "GET",
|
|
138
|
+
immediate = false,
|
|
139
|
+
onSuccess,
|
|
140
|
+
onError,
|
|
141
|
+
onBefore,
|
|
142
|
+
onFinish,
|
|
143
|
+
initialData = null,
|
|
144
|
+
debounce = 0,
|
|
145
|
+
skipErrorNotification = false,
|
|
146
|
+
retry = globalOptions?.retry ?? false,
|
|
147
|
+
retryDelay = globalOptions?.retryDelay ?? 1e3,
|
|
148
|
+
authMode = "default",
|
|
149
|
+
useGlobalAbort = globalOptions?.useGlobalAbort ?? true,
|
|
150
|
+
initialLoading = false,
|
|
151
|
+
...axiosConfig
|
|
152
|
+
} = options;
|
|
153
|
+
const startLoading = initialLoading ?? immediate;
|
|
154
|
+
const state = useApiState(initialData, { initialLoading: startLoading });
|
|
155
|
+
const abortController2 = ref3(null);
|
|
156
|
+
const globalAbort = useGlobalAbort ? useAbortController() : null;
|
|
157
|
+
const executeRequest = async (config) => {
|
|
158
|
+
const requestUrl = typeof url === "string" ? url : url.value;
|
|
159
|
+
if (abortController2.value) abortController2.value.abort("Cancelled by new request");
|
|
160
|
+
const controller = new AbortController();
|
|
161
|
+
abortController2.value = controller;
|
|
162
|
+
let globalAbortHandler = null;
|
|
163
|
+
let subscribedSignal = null;
|
|
164
|
+
if (globalAbort) {
|
|
165
|
+
const gs = globalAbort.getSignal();
|
|
166
|
+
if (!gs.aborted) {
|
|
167
|
+
subscribedSignal = gs;
|
|
168
|
+
const currentCount = globalAbort.abortCount.value;
|
|
169
|
+
globalAbortHandler = () => {
|
|
170
|
+
if (globalAbort.abortCount.value === currentCount) controller.abort("Global filter change");
|
|
171
|
+
};
|
|
172
|
+
gs.addEventListener("abort", globalAbortHandler);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
onBefore?.();
|
|
176
|
+
state.setLoading(true);
|
|
177
|
+
state.setError(null);
|
|
178
|
+
let wasCancelled = false;
|
|
179
|
+
try {
|
|
180
|
+
const rawData = config?.data !== void 0 ? config.data : axiosConfig.data;
|
|
181
|
+
const resolvedData = toValue(rawData);
|
|
182
|
+
const response = await axios2.request({
|
|
183
|
+
url: requestUrl,
|
|
184
|
+
method,
|
|
185
|
+
...axiosConfig,
|
|
186
|
+
...config,
|
|
187
|
+
data: resolvedData,
|
|
188
|
+
signal: controller.signal,
|
|
189
|
+
authMode: config?.authMode || authMode
|
|
190
|
+
});
|
|
191
|
+
state.setData(response.data, response);
|
|
192
|
+
state.setStatusCode(response.status);
|
|
193
|
+
onSuccess?.(response);
|
|
194
|
+
return response.data;
|
|
195
|
+
} catch (err) {
|
|
196
|
+
if (controller.signal.aborted || err?.code === "ERR_CANCELED") {
|
|
197
|
+
wasCancelled = true;
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
const apiError = parseApiError(err);
|
|
201
|
+
if (!skipErrorNotification && globalErrorHandler) {
|
|
202
|
+
globalErrorHandler(apiError, err);
|
|
203
|
+
}
|
|
204
|
+
state.setError(apiError);
|
|
205
|
+
state.setStatusCode(apiError.status);
|
|
206
|
+
onError?.(apiError);
|
|
207
|
+
return null;
|
|
208
|
+
} finally {
|
|
209
|
+
if (globalAbortHandler && subscribedSignal) subscribedSignal.removeEventListener("abort", globalAbortHandler);
|
|
210
|
+
if (!wasCancelled) {
|
|
211
|
+
state.setLoading(false);
|
|
212
|
+
onFinish?.();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const execute = debounce > 0 ? debounceFn(executeRequest, debounce) : executeRequest;
|
|
217
|
+
const abort = (msg) => {
|
|
218
|
+
abortController2.value?.abort(msg);
|
|
219
|
+
abortController2.value = null;
|
|
220
|
+
};
|
|
221
|
+
const reset = () => {
|
|
222
|
+
abort();
|
|
223
|
+
state.reset();
|
|
224
|
+
state.setLoading(false);
|
|
225
|
+
};
|
|
226
|
+
if (getCurrentScope()) {
|
|
227
|
+
onScopeDispose(() => abort("Scope disposed"));
|
|
228
|
+
}
|
|
229
|
+
if (immediate) execute();
|
|
230
|
+
return { ...state, execute, abort, reset };
|
|
231
|
+
}
|
|
232
|
+
function useApiGet(url, options) {
|
|
233
|
+
return useApi(url, { ...options, method: "GET" });
|
|
234
|
+
}
|
|
235
|
+
function useApiPost(url, options) {
|
|
236
|
+
return useApi(url, { ...options, method: "POST" });
|
|
237
|
+
}
|
|
238
|
+
function useApiPut(url, options) {
|
|
239
|
+
return useApi(url, { ...options, method: "PUT" });
|
|
240
|
+
}
|
|
241
|
+
function useApiPatch(url, options) {
|
|
242
|
+
return useApi(url, { ...options, method: "PATCH" });
|
|
243
|
+
}
|
|
244
|
+
function useApiDelete(url, options) {
|
|
245
|
+
return useApi(url, { ...options, method: "DELETE" });
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/features/createInstance.ts
|
|
249
|
+
import axios from "axios";
|
|
250
|
+
|
|
251
|
+
// src/features/monitor.ts
|
|
252
|
+
var AuthEventType = /* @__PURE__ */ ((AuthEventType2) => {
|
|
253
|
+
AuthEventType2["REFRESH_START"] = "AUTH_REFRESH_START";
|
|
254
|
+
AuthEventType2["REQUEST_QUEUED"] = "AUTH_REQUEST_QUEUED";
|
|
255
|
+
AuthEventType2["REFRESH_SUCCESS"] = "AUTH_REFRESH_SUCCESS";
|
|
256
|
+
AuthEventType2["REFRESH_ERROR"] = "AUTH_REFRESH_ERROR";
|
|
257
|
+
return AuthEventType2;
|
|
258
|
+
})(AuthEventType || {});
|
|
259
|
+
var defaultMonitor = (type, payload) => {
|
|
260
|
+
const isDev = typeof process !== "undefined" && process.env?.NODE_ENV === "development" || import.meta?.env?.DEV;
|
|
261
|
+
if (isDev) {
|
|
262
|
+
console.debug(`[AuthMonitor] ${type}`, payload);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
var currentMonitor = defaultMonitor;
|
|
266
|
+
function setAuthMonitor(fn) {
|
|
267
|
+
currentMonitor = fn;
|
|
268
|
+
}
|
|
269
|
+
function trackAuthEvent(type, payload = {}) {
|
|
270
|
+
currentMonitor(type, { ...payload, timestamp: (/* @__PURE__ */ new Date()).toISOString() });
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/features/tokenManager.ts
|
|
274
|
+
var TOKEN_TYPE = "Bearer";
|
|
275
|
+
var ACCESS_TOKEN_KEY = "accessToken";
|
|
276
|
+
var TOKEN_EXPIRES_KEY = "tokenExpiresAt";
|
|
277
|
+
var LocalStorageTokenStorage = class {
|
|
278
|
+
getAccessToken() {
|
|
279
|
+
return localStorage.getItem(ACCESS_TOKEN_KEY);
|
|
280
|
+
}
|
|
281
|
+
getRefreshToken() {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
setTokens(tokens) {
|
|
285
|
+
localStorage.setItem(ACCESS_TOKEN_KEY, tokens.accessToken);
|
|
286
|
+
if (tokens.expiresIn) {
|
|
287
|
+
const expiresAt = Date.now() + tokens.expiresIn * 1e3;
|
|
288
|
+
localStorage.setItem(TOKEN_EXPIRES_KEY, expiresAt.toString());
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
clearTokens() {
|
|
292
|
+
localStorage.removeItem(ACCESS_TOKEN_KEY);
|
|
293
|
+
localStorage.removeItem(TOKEN_EXPIRES_KEY);
|
|
294
|
+
}
|
|
295
|
+
getTokenExpiresAt() {
|
|
296
|
+
const expiresAt = localStorage.getItem(TOKEN_EXPIRES_KEY);
|
|
297
|
+
return expiresAt ? parseInt(expiresAt, 10) : null;
|
|
298
|
+
}
|
|
299
|
+
isTokenExpired() {
|
|
300
|
+
const expiresAt = this.getTokenExpiresAt();
|
|
301
|
+
if (!expiresAt) return false;
|
|
302
|
+
return Date.now() >= expiresAt - 5e3;
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
var TokenManager = class {
|
|
306
|
+
storage;
|
|
307
|
+
refreshPromise = null;
|
|
308
|
+
constructor(storage = new LocalStorageTokenStorage()) {
|
|
309
|
+
this.storage = storage;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Get access token
|
|
313
|
+
*/
|
|
314
|
+
getAccessToken() {
|
|
315
|
+
return this.storage.getAccessToken();
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Get refresh token
|
|
319
|
+
*/
|
|
320
|
+
getRefreshToken() {
|
|
321
|
+
return this.storage.getRefreshToken();
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Save tokens
|
|
325
|
+
*/
|
|
326
|
+
setTokens(tokens) {
|
|
327
|
+
this.storage.setTokens(tokens);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Clear tokens
|
|
331
|
+
*/
|
|
332
|
+
clearTokens() {
|
|
333
|
+
this.storage.clearTokens();
|
|
334
|
+
this.refreshPromise = null;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Check if token is expired
|
|
338
|
+
*/
|
|
339
|
+
isTokenExpired() {
|
|
340
|
+
return this.storage.isTokenExpired();
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Get token expiration time
|
|
344
|
+
*/
|
|
345
|
+
getTokenExpiresAt() {
|
|
346
|
+
return this.storage.getTokenExpiresAt();
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Check if tokens exist
|
|
350
|
+
*/
|
|
351
|
+
hasTokens() {
|
|
352
|
+
return !!this.getAccessToken();
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Get Authorization header
|
|
356
|
+
*/
|
|
357
|
+
getAuthHeader() {
|
|
358
|
+
const token = this.getAccessToken();
|
|
359
|
+
return token ? `${TOKEN_TYPE} ${token}` : null;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Set token refresh promise (to prevent race conditions)
|
|
363
|
+
*/
|
|
364
|
+
setRefreshPromise(promise) {
|
|
365
|
+
this.refreshPromise = promise;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Get token refresh promise
|
|
369
|
+
*/
|
|
370
|
+
getRefreshPromise() {
|
|
371
|
+
return this.refreshPromise;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Clear token refresh promise
|
|
375
|
+
*/
|
|
376
|
+
clearRefreshPromise() {
|
|
377
|
+
this.refreshPromise = null;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Set storage (useful for tests)
|
|
381
|
+
*/
|
|
382
|
+
setStorage(storage) {
|
|
383
|
+
this.storage = storage;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
var tokenManager = new TokenManager();
|
|
387
|
+
|
|
388
|
+
// src/features/interceptors.ts
|
|
389
|
+
var AUTH_HEADER = "Authorization";
|
|
390
|
+
var TOKEN_TYPE2 = "Bearer";
|
|
391
|
+
var failedQueue = [];
|
|
392
|
+
var isRefreshing = false;
|
|
393
|
+
function processQueue(error, token = null) {
|
|
394
|
+
failedQueue.forEach((promise) => {
|
|
395
|
+
if (error) promise.reject(error);
|
|
396
|
+
else if (token) promise.resolve(token);
|
|
397
|
+
});
|
|
398
|
+
failedQueue = [];
|
|
399
|
+
}
|
|
400
|
+
function setupInterceptors(axiosInstance, options = {}) {
|
|
401
|
+
const {
|
|
402
|
+
refreshUrl = "/auth/refresh",
|
|
403
|
+
onTokenRefreshFailed,
|
|
404
|
+
extractTokens
|
|
405
|
+
} = options;
|
|
406
|
+
axiosInstance.interceptors.request.use(
|
|
407
|
+
(config) => {
|
|
408
|
+
const extendedConfig = config;
|
|
409
|
+
if (extendedConfig.authMode === "public") return config;
|
|
410
|
+
const token = tokenManager.getAccessToken();
|
|
411
|
+
if (token) {
|
|
412
|
+
config.headers.set(AUTH_HEADER, `${TOKEN_TYPE2} ${token}`);
|
|
413
|
+
}
|
|
414
|
+
return config;
|
|
415
|
+
},
|
|
416
|
+
(error) => Promise.reject(error)
|
|
417
|
+
);
|
|
418
|
+
axiosInstance.interceptors.response.use(
|
|
419
|
+
(response) => response,
|
|
420
|
+
async (error) => {
|
|
421
|
+
const originalRequest = error.config;
|
|
422
|
+
if (!originalRequest || error.response?.status !== 401 || originalRequest._retry) {
|
|
423
|
+
return Promise.reject(error);
|
|
424
|
+
}
|
|
425
|
+
if (originalRequest.authMode === "public" || originalRequest.authMode === "optional") {
|
|
426
|
+
return Promise.reject(error);
|
|
427
|
+
}
|
|
428
|
+
if (originalRequest.url?.includes(refreshUrl)) {
|
|
429
|
+
isRefreshing = false;
|
|
430
|
+
processQueue(error, null);
|
|
431
|
+
tokenManager.clearTokens();
|
|
432
|
+
onTokenRefreshFailed?.();
|
|
433
|
+
return Promise.reject(error);
|
|
434
|
+
}
|
|
435
|
+
if (isRefreshing) {
|
|
436
|
+
trackAuthEvent("AUTH_REQUEST_QUEUED" /* REQUEST_QUEUED */, { url: originalRequest.url });
|
|
437
|
+
return new Promise((resolve, reject) => {
|
|
438
|
+
failedQueue.push({
|
|
439
|
+
resolve: (token) => {
|
|
440
|
+
originalRequest.headers.set(AUTH_HEADER, `${TOKEN_TYPE2} ${token}`);
|
|
441
|
+
resolve(axiosInstance(originalRequest));
|
|
442
|
+
},
|
|
443
|
+
reject: (err) => reject(err)
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
originalRequest._retry = true;
|
|
448
|
+
isRefreshing = true;
|
|
449
|
+
trackAuthEvent("AUTH_REFRESH_START" /* REFRESH_START */, { url: originalRequest.url });
|
|
450
|
+
try {
|
|
451
|
+
const response = await axiosInstance.post(
|
|
452
|
+
refreshUrl,
|
|
453
|
+
{},
|
|
454
|
+
{
|
|
455
|
+
// @ts-expect-error
|
|
456
|
+
authMode: "public",
|
|
457
|
+
withCredentials: true
|
|
458
|
+
}
|
|
459
|
+
);
|
|
460
|
+
let accessToken = "";
|
|
461
|
+
if (extractTokens) {
|
|
462
|
+
const tokens = extractTokens(response);
|
|
463
|
+
accessToken = tokens.accessToken;
|
|
464
|
+
} else {
|
|
465
|
+
const data = response.data;
|
|
466
|
+
accessToken = data.accessToken || data.access_token;
|
|
467
|
+
}
|
|
468
|
+
if (!accessToken) throw new Error("No access token in refresh response");
|
|
469
|
+
tokenManager.setTokens({ accessToken });
|
|
470
|
+
axiosInstance.defaults.headers.common[AUTH_HEADER] = `${TOKEN_TYPE2} ${accessToken}`;
|
|
471
|
+
trackAuthEvent("AUTH_REFRESH_SUCCESS" /* REFRESH_SUCCESS */);
|
|
472
|
+
processQueue(null, accessToken);
|
|
473
|
+
originalRequest.headers.set(AUTH_HEADER, `${TOKEN_TYPE2} ${accessToken}`);
|
|
474
|
+
return axiosInstance(originalRequest);
|
|
475
|
+
} catch (refreshError) {
|
|
476
|
+
trackAuthEvent("AUTH_REFRESH_ERROR" /* REFRESH_ERROR */, { error: refreshError });
|
|
477
|
+
processQueue(refreshError, null);
|
|
478
|
+
tokenManager.clearTokens();
|
|
479
|
+
onTokenRefreshFailed?.();
|
|
480
|
+
return Promise.reject(refreshError);
|
|
481
|
+
} finally {
|
|
482
|
+
isRefreshing = false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// src/features/createInstance.ts
|
|
489
|
+
function createApiClient(options = {}) {
|
|
490
|
+
const {
|
|
491
|
+
withAuth = true,
|
|
492
|
+
authOptions,
|
|
493
|
+
...axiosConfig
|
|
494
|
+
} = options;
|
|
495
|
+
const instance = axios.create({
|
|
496
|
+
timeout: 6e4,
|
|
497
|
+
headers: { "Content-Type": "application/json" },
|
|
498
|
+
...axiosConfig
|
|
499
|
+
});
|
|
500
|
+
if (withAuth) {
|
|
501
|
+
setupInterceptors(instance, authOptions);
|
|
502
|
+
}
|
|
503
|
+
return instance;
|
|
504
|
+
}
|
|
505
|
+
export {
|
|
506
|
+
AuthEventType,
|
|
507
|
+
createApi,
|
|
508
|
+
createApiClient,
|
|
509
|
+
setAuthMonitor,
|
|
510
|
+
setupInterceptors,
|
|
511
|
+
tokenManager,
|
|
512
|
+
useAbortController,
|
|
513
|
+
useApi,
|
|
514
|
+
useApiConfig,
|
|
515
|
+
useApiDelete,
|
|
516
|
+
useApiGet,
|
|
517
|
+
useApiPatch,
|
|
518
|
+
useApiPost,
|
|
519
|
+
useApiPut,
|
|
520
|
+
useApiState
|
|
521
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ametie/vue-muza-use",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Powerful Vue 3 API composable (Muza Kit) with Axios, Auto-Refresh & TypeScript",
|
|
5
|
+
"author": "MortyQ",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/MortyQ/vue-useApi.git"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
16
|
+
"keywords": ["vue", "axios", "composable", "api", "typescript", "request", "auth", "refresh", "muza"],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
|
+
"module": "./dist/index.mjs",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.cjs"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"dev": "tsup --watch"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"axios": "^1.0.0",
|
|
39
|
+
"vue": "^3.3.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^24.10.10",
|
|
43
|
+
"axios": "^1.13.4",
|
|
44
|
+
"tsup": "^8.5.1",
|
|
45
|
+
"typescript": "^5.9.3",
|
|
46
|
+
"vue": "^3.5.27"
|
|
47
|
+
}
|
|
48
|
+
}
|