@varlet/axle 1.0.1 → 1.1.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/dist/use.mjs ADDED
@@ -0,0 +1,271 @@
1
+ import { isCancel } from "axios";
2
+ import { isFunction } from "rattail";
3
+ import { getCurrentInstance, onActivated, onDeactivated, onMounted, onUnmounted, ref, watch } from "vue";
4
+ //#region src/use.ts
5
+ function normalizeValueGetter(valueGetter) {
6
+ return isFunction(valueGetter) ? valueGetter() : valueGetter;
7
+ }
8
+ const cacheBuffer = /* @__PURE__ */ new Map();
9
+ function cleanupCacheBuffer() {
10
+ cacheBuffer.forEach((value, key) => {
11
+ if (value.expiredTime && Date.now() >= value.expiredTime) cacheBuffer.delete(key);
12
+ });
13
+ }
14
+ function createUseAxle(options) {
15
+ const { axle, onTransform: defaultOnTransform, immediate: defaultImmediate = true, abortOnUnmount: defaultAbortOnUnmount = true, abortPrevious: defaultAbortPrevious = false, pollingSilent: defaultPollingSilent = false, cacheTime: defaultCacheTime = Infinity } = options;
16
+ const useAxle = (options) => {
17
+ const { url: initialUrlOrGetter, method, immediate = defaultImmediate, abortOnUnmount = defaultAbortOnUnmount, abortPrevious = defaultAbortPrevious, cacheTime = defaultCacheTime, cacheKey, pollingInterval, pollingSilent = defaultPollingSilent, pollingOnHidden = true, pollingOnDeactivated = false, refreshOnWindowFocus = false, value: initialValue, resetValue: initialResetValue, cloneResetValue: initialCloneResetValue, params: initialParamsOrGetter, config: initialConfigOrGetter, watch: watchOptions, runnable = () => true, onBefore = () => {}, onAfter = () => {}, onTransform = defaultOnTransform ?? ((response) => response), onSuccess = () => {}, onError = () => {} } = options;
18
+ const value = ref(initialValue);
19
+ const loading = ref(false);
20
+ const error = ref();
21
+ const downloadProgress = ref(0);
22
+ const uploadProgress = ref(0);
23
+ const pollingCanceled = ref(false);
24
+ const refs = {
25
+ value,
26
+ loading,
27
+ error,
28
+ downloadProgress,
29
+ uploadProgress,
30
+ pollingCanceled
31
+ };
32
+ const extra = {
33
+ uploadProgress,
34
+ downloadProgress,
35
+ loading,
36
+ error,
37
+ pollingCanceled,
38
+ abort,
39
+ resetValue,
40
+ invalidateCache,
41
+ cancelPolling
42
+ };
43
+ let controller = new AbortController();
44
+ let latestRunId = 0;
45
+ let cachedRequest;
46
+ let pollingTimer = null;
47
+ let running = false;
48
+ let deactivated = false;
49
+ let hidden = false;
50
+ let unmounted = false;
51
+ const run = Object.assign(async (options = {}) => {
52
+ if (!runnable() || abortOnUnmount && unmounted) return;
53
+ if (abortPrevious && !isCachedRequestPending()) controller.abort();
54
+ if (abortPrevious) cachedRequest = void 0;
55
+ if (abortPrevious || controller.signal.aborted) controller = new AbortController();
56
+ const runId = ++latestRunId;
57
+ const runController = controller;
58
+ running = true;
59
+ pollingCanceled.value = false;
60
+ if ((options.resetValue ?? initialResetValue ?? false) === true) resetValue(options);
61
+ uploadProgress.value = 0;
62
+ downloadProgress.value = 0;
63
+ const url = options.url ?? normalizeValueGetter(initialUrlOrGetter);
64
+ const params = options.params ?? normalizeValueGetter(initialParamsOrGetter);
65
+ const config = options.config ?? normalizeValueGetter(initialConfigOrGetter);
66
+ const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
67
+ onBefore(refs);
68
+ loading.value = !options.silent;
69
+ try {
70
+ cleanupCacheBuffer();
71
+ const response = await getResponse();
72
+ if (shouldSetCacheResponse(normalizedCacheKey)) {
73
+ const cache = cacheBuffer.get(normalizedCacheKey);
74
+ cache.response = response;
75
+ cache.expiredTime = Date.now() + cacheTime;
76
+ }
77
+ if (!isLatestRun()) return response;
78
+ const transformedValue = await onTransform(response, refs);
79
+ if (!isLatestRun()) return response;
80
+ value.value = transformedValue;
81
+ error.value = void 0;
82
+ onSuccess(response, refs);
83
+ loading.value = false;
84
+ onAfter(refs);
85
+ return response;
86
+ } catch (responseError) {
87
+ if (!isLatestRun()) throw responseError;
88
+ error.value = responseError;
89
+ onError(responseError, refs);
90
+ loading.value = false;
91
+ onAfter(refs);
92
+ throw responseError;
93
+ } finally {
94
+ if (isLatestRun()) {
95
+ running = false;
96
+ clearPolling();
97
+ startPolling();
98
+ }
99
+ }
100
+ function isLatestRun() {
101
+ return !abortPrevious || runId === latestRunId;
102
+ }
103
+ function getResponse() {
104
+ if (shouldUseCache(normalizedCacheKey)) return JSON.parse(JSON.stringify(cacheBuffer.get(normalizedCacheKey).response));
105
+ if (shouldAwaitCachePromise(normalizedCacheKey)) return cacheBuffer.get(normalizedCacheKey).promise;
106
+ const promise = fetchResponse();
107
+ if (shouldSetCachePromise(normalizedCacheKey)) {
108
+ cacheBuffer.set(normalizedCacheKey, { promise });
109
+ cachedRequest = {
110
+ key: normalizedCacheKey,
111
+ promise
112
+ };
113
+ }
114
+ return promise;
115
+ }
116
+ function fetchResponse() {
117
+ return axle[method](url, params, {
118
+ signal: runController.signal,
119
+ onUploadProgress(event) {
120
+ if (isLatestRun()) uploadProgress.value = event.progress ?? 0;
121
+ },
122
+ onDownloadProgress(event) {
123
+ if (isLatestRun()) downloadProgress.value = event.progress ?? 0;
124
+ },
125
+ ...config
126
+ });
127
+ }
128
+ function shouldUseCache(key) {
129
+ if (!key || !cacheBuffer.has(key)) return false;
130
+ return cacheBuffer.get(key).response != null;
131
+ }
132
+ function shouldAwaitCachePromise(key) {
133
+ if (!key || !cacheBuffer.has(key)) return false;
134
+ return cacheBuffer.get(key).promise;
135
+ }
136
+ function shouldSetCachePromise(key) {
137
+ if (!key || cacheBuffer.has(key)) return false;
138
+ return true;
139
+ }
140
+ function shouldSetCacheResponse(key) {
141
+ if (!key || !cacheBuffer.has(key)) return false;
142
+ return cacheBuffer.get(key).response == null;
143
+ }
144
+ }, extra);
145
+ if (immediate) runWithInitialConfig();
146
+ if (getCurrentInstance()) {
147
+ onActivated(() => {
148
+ deactivated = false;
149
+ addVisibilityChangeListener();
150
+ addWindowFocusListener();
151
+ startPolling();
152
+ });
153
+ onMounted(() => {
154
+ addVisibilityChangeListener();
155
+ addWindowFocusListener();
156
+ });
157
+ onDeactivated(() => {
158
+ deactivated = true;
159
+ removeVisibilityChangeListener();
160
+ removeWindowFocusListener();
161
+ clearPolling();
162
+ });
163
+ onUnmounted(() => {
164
+ removeVisibilityChangeListener();
165
+ removeWindowFocusListener();
166
+ cancelPolling();
167
+ });
168
+ onUnmounted(() => {
169
+ unmounted = true;
170
+ if (abortOnUnmount) abort();
171
+ });
172
+ if (watchOptions) {
173
+ const normalizedWatchOptions = watchOptions === true ? {
174
+ params: true,
175
+ config: true
176
+ } : {
177
+ params: false,
178
+ config: false,
179
+ ...watchOptions
180
+ };
181
+ const enableWatchParams = isFunction(initialParamsOrGetter) && normalizedWatchOptions.params;
182
+ const enableWatchConfig = isFunction(initialConfigOrGetter) && normalizedWatchOptions.config;
183
+ watch(() => [
184
+ enableWatchParams ? normalizeValueGetter(initialParamsOrGetter) : void 0,
185
+ enableWatchConfig ? normalizeValueGetter(initialConfigOrGetter) : void 0,
186
+ normalizedWatchOptions._custom ? normalizedWatchOptions._custom() : void 0
187
+ ], () => {
188
+ runWithInitialConfig();
189
+ });
190
+ }
191
+ if (isFunction(pollingInterval)) watch(pollingInterval, () => {
192
+ clearPolling();
193
+ startPolling();
194
+ });
195
+ }
196
+ function runWithInitialConfig({ silent = false } = {}) {
197
+ return run({
198
+ url: normalizeValueGetter(initialUrlOrGetter),
199
+ params: normalizeValueGetter(initialParamsOrGetter),
200
+ config: normalizeValueGetter(initialConfigOrGetter),
201
+ silent
202
+ }).catch((runError) => {
203
+ if (isCancel(runError)) return;
204
+ throw runError;
205
+ });
206
+ }
207
+ function resetValue(options = {}) {
208
+ value.value = ((options.cloneResetValue ?? initialCloneResetValue ?? false) === true ? (v) => v == null ? null : JSON.parse(JSON.stringify(v)) : isFunction(initialCloneResetValue) ? initialCloneResetValue : (v) => v)(initialValue);
209
+ }
210
+ function abort() {
211
+ controller.abort();
212
+ if (isCachedRequestPending()) cacheBuffer.delete(cachedRequest.key);
213
+ cachedRequest = void 0;
214
+ }
215
+ function isCachedRequestPending() {
216
+ if (!cachedRequest) return false;
217
+ const buffer = cacheBuffer.get(cachedRequest.key);
218
+ return buffer?.promise === cachedRequest.promise && buffer.response == null;
219
+ }
220
+ function addWindowFocusListener() {
221
+ if (typeof window !== "undefined") window.addEventListener("focus", handleWindowFocus);
222
+ }
223
+ function removeWindowFocusListener() {
224
+ if (typeof window !== "undefined") window.removeEventListener("focus", handleWindowFocus);
225
+ }
226
+ function handleWindowFocus() {
227
+ if (refreshOnWindowFocus) runWithInitialConfig();
228
+ }
229
+ function addVisibilityChangeListener() {
230
+ if (typeof document !== "undefined") document.addEventListener("visibilitychange", handleVisibilityChange);
231
+ }
232
+ function removeVisibilityChangeListener() {
233
+ if (typeof document !== "undefined") document.removeEventListener("visibilitychange", handleVisibilityChange);
234
+ }
235
+ function handleVisibilityChange() {
236
+ hidden = document.visibilityState === "hidden";
237
+ if (pollingOnHidden) return;
238
+ hidden ? clearPolling() : startPolling();
239
+ }
240
+ function startPolling() {
241
+ const interval = normalizeValueGetter(pollingInterval);
242
+ if (interval == null || running || pollingCanceled.value || pollingTimer != null || deactivated && !pollingOnDeactivated || hidden && !pollingOnHidden) return;
243
+ pollingTimer = setTimeout(() => {
244
+ pollingTimer = null;
245
+ runWithInitialConfig({ silent: pollingSilent });
246
+ }, interval);
247
+ }
248
+ function clearPolling() {
249
+ if (pollingTimer == null) return;
250
+ clearTimeout(pollingTimer);
251
+ pollingTimer = null;
252
+ }
253
+ function cancelPolling() {
254
+ clearPolling();
255
+ pollingCanceled.value = true;
256
+ }
257
+ function invalidateCache() {
258
+ const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
259
+ if (!normalizedCacheKey || !cacheBuffer.has(normalizedCacheKey)) return;
260
+ cacheBuffer.delete(normalizedCacheKey);
261
+ }
262
+ return [
263
+ value,
264
+ run,
265
+ extra
266
+ ];
267
+ };
268
+ return useAxle;
269
+ }
270
+ //#endregion
271
+ export { cleanupCacheBuffer, createUseAxle, normalizeValueGetter };
package/package.json CHANGED
@@ -1,76 +1,89 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "keywords": [
6
- "axle",
7
6
  "axios",
7
+ "axle",
8
+ "composition api",
8
9
  "helpers",
9
- "vue3",
10
- "composition api"
10
+ "vue3"
11
11
  ],
12
12
  "bugs": {
13
13
  "url": "https://github.com/varletjs/axle/issues"
14
14
  },
15
+ "license": "MIT",
16
+ "author": "haoziqaq <357229046@qq.com>",
15
17
  "repository": {
16
18
  "type": "git",
17
19
  "url": "git+https://github.com/varletjs/axle.git"
18
20
  },
19
- "license": "MIT",
20
- "author": "haoziqaq <357229046@qq.com>",
21
- "sideEffects": false,
21
+ "files": [
22
+ "dist"
23
+ ],
22
24
  "type": "module",
25
+ "sideEffects": false,
26
+ "main": "dist/index.mjs",
27
+ "module": "dist/index.mjs",
28
+ "types": "dist/index.d.mts",
29
+ "typesVersions": {
30
+ "*": {
31
+ "use": [
32
+ "dist/use.d.mts"
33
+ ],
34
+ "api": [
35
+ "dist/api.d.mts"
36
+ ]
37
+ }
38
+ },
23
39
  "exports": {
24
40
  ".": {
25
- "import": "./lib/index.js",
26
- "require": "./lib/index.cjs",
27
- "types": "./lib/index.d.ts"
41
+ "types": "./dist/index.d.mts",
42
+ "import": "./dist/index.mjs"
28
43
  },
29
44
  "./use": {
30
- "import": "./lib/use.js",
31
- "require": "./lib/use.cjs",
32
- "types": "./lib/use.d.ts"
45
+ "types": "./dist/use.d.mts",
46
+ "import": "./dist/use.mjs"
33
47
  },
34
48
  "./api": {
35
- "import": "./lib/api.js",
36
- "require": "./lib/api.cjs",
37
- "types": "./lib/api.d.ts"
49
+ "types": "./dist/api.d.mts",
50
+ "import": "./dist/api.mjs"
38
51
  },
39
52
  "./*": "./*"
40
53
  },
41
- "main": "lib/index.cjs",
42
- "module": "lib/index.js",
43
- "types": "index.d.ts",
44
- "typesVersions": {
45
- "*": {
46
- "*": [
47
- "./lib/*"
48
- ]
49
- }
50
- },
51
- "files": [
52
- "lib"
53
- ],
54
54
  "dependencies": {
55
55
  "axios": "1.7.3",
56
56
  "crypto-js": "^4.2.0",
57
57
  "lodash": "4.17.21",
58
58
  "qs": "^6.11.0",
59
- "rattail": "1.0.0"
59
+ "rattail": "^2.0.3"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@types/crypto-js": "^4.2.1",
63
63
  "@types/lodash": "4.14.202",
64
64
  "@types/qs": "^6.9.7",
65
- "tsup": "7.2.0",
66
- "typescript": "^5.1.5",
67
- "vue": "3.3.4"
65
+ "oxc-minify": "^0.125.0",
66
+ "typescript": "5.3.3",
67
+ "vite-plus": "0.1.18",
68
+ "vitepress": "2.0.0-alpha.17",
69
+ "vue": "3.5.32"
68
70
  },
69
71
  "peerDependencies": {
70
72
  "vue": "^3.2.0"
71
73
  },
74
+ "engines": {
75
+ "pnpm": ">=10.0.0"
76
+ },
72
77
  "scripts": {
73
- "build": "tsup src/index.ts src/use.ts src/api.ts --format esm,cjs --out-dir=lib --dts --clean",
74
- "dev": "tsup src/index.ts src/use.ts src/api.ts --format esm --out-dir=lib --watch --dts"
78
+ "build": "vp pack",
79
+ "clean": "rt clean",
80
+ "dev": "vp pack --watch",
81
+ "docs:build": "vitepress build docs",
82
+ "docs:dev": "vitepress dev docs",
83
+ "docs:preview": "vitepress preview docs",
84
+ "lint": "vp lint --fix",
85
+ "format": "vp fmt",
86
+ "release": "pnpm build && rt release",
87
+ "test": "vp test run"
75
88
  }
76
89
  }
package/lib/api.cjs DELETED
@@ -1,89 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __spreadValues = (a, b) => {
12
- for (var prop in b || (b = {}))
13
- if (__hasOwnProp.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- if (__getOwnPropSymbols)
16
- for (var prop of __getOwnPropSymbols(b)) {
17
- if (__propIsEnum.call(b, prop))
18
- __defNormalProp(a, prop, b[prop]);
19
- }
20
- return a;
21
- };
22
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
- var __objRest = (source, exclude) => {
24
- var target = {};
25
- for (var prop in source)
26
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
27
- target[prop] = source[prop];
28
- if (source != null && __getOwnPropSymbols)
29
- for (var prop of __getOwnPropSymbols(source)) {
30
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
31
- target[prop] = source[prop];
32
- }
33
- return target;
34
- };
35
- var __export = (target, all) => {
36
- for (var name in all)
37
- __defProp(target, name, { get: all[name], enumerable: true });
38
- };
39
- var __copyProps = (to, from, except, desc) => {
40
- if (from && typeof from === "object" || typeof from === "function") {
41
- for (let key of __getOwnPropNames(from))
42
- if (!__hasOwnProp.call(to, key) && key !== except)
43
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
44
- }
45
- return to;
46
- };
47
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
48
-
49
- // src/api.ts
50
- var api_exports = {};
51
- __export(api_exports, {
52
- createApi: () => createApi
53
- });
54
- module.exports = __toCommonJS(api_exports);
55
- var import_rattail = require("rattail");
56
- function createApi(axle, useAxle) {
57
- return function api(url, method) {
58
- function load(params, pathParams, config) {
59
- return axle[method](patchUrl(url, pathParams != null ? pathParams : {}), params, config);
60
- }
61
- function use(options = {}) {
62
- const _a = options, { pathParams = {}, watch: watchOptions } = _a, rest = __objRest(_a, ["pathParams", "watch"]);
63
- const enableWatchPathParams = (0, import_rattail.isFunction)(pathParams) && ((watchOptions == null ? void 0 : watchOptions.pathParams) || watchOptions === true);
64
- const normalizedWatchOptions = watchOptions === true ? { params: true, config: true, _custom: enableWatchPathParams ? pathParams : void 0 } : watchOptions === false ? { params: false, config: false } : __spreadProps(__spreadValues({ params: false, config: false }, watchOptions), { _custom: enableWatchPathParams ? pathParams : void 0 });
65
- return useAxle(__spreadValues({
66
- url: () => patchUrl(url, pathParams),
67
- method,
68
- watch: normalizedWatchOptions
69
- }, rest));
70
- }
71
- function patchUrl(url2, pathParams) {
72
- const params = (0, import_rattail.isFunction)(pathParams) ? pathParams() : pathParams;
73
- Object.entries(params).forEach(([key, value]) => {
74
- url2 = url2.replace(`:${key}`, value);
75
- });
76
- return url2;
77
- }
78
- return {
79
- url,
80
- load,
81
- use,
82
- patchUrl
83
- };
84
- };
85
- }
86
- // Annotate the CommonJS export names for ESM import in node:
87
- 0 && (module.exports = {
88
- createApi
89
- });
package/lib/api.d.cts DELETED
@@ -1,28 +0,0 @@
1
- import { e as AxleInstance, d as RunnerMethod, A as AxleRequestConfig } from './instance-857fe07c.js';
2
- import { WatchOptions, UseAxleOptions, UseAxleOptionsWithRunnable, UseAxle, UseAxleInstance } from './use.cjs';
3
- import 'axios';
4
- import 'vue';
5
-
6
- type ApiPathParams = Record<string, any> | (() => Record<string, any>);
7
- type ApiWatchOptions = WatchOptions & {
8
- pathParams?: boolean;
9
- };
10
- type ApiUseOptions<V, R, P, D> = Partial<UseAxleOptions<V, R, P, D>> & {
11
- pathParams?: ApiPathParams;
12
- watch?: ApiWatchOptions;
13
- };
14
- type ApiUseOptionsWithRunnable<V, R, P, D> = Partial<Omit<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'>> & Pick<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'> & {
15
- pathParams?: ApiPathParams;
16
- watch?: ApiWatchOptions;
17
- };
18
- declare function createApi(axle: AxleInstance, useAxle: UseAxle): <R = any, P = Record<string, any>, D = Record<string, any>>(url: string, method: RunnerMethod) => {
19
- url: string;
20
- load: (params?: P | undefined, pathParams?: ApiPathParams, config?: AxleRequestConfig<D, Record<string, any>> | undefined) => Promise<R>;
21
- use: {
22
- <V = R>(options?: ApiUseOptionsWithRunnable<V, R, P, D> | undefined): UseAxleInstance<V, R | undefined, P, D>;
23
- <V_1 = R>(options?: ApiUseOptions<V_1, R, P, D> | undefined): UseAxleInstance<V_1, R, P, D>;
24
- };
25
- patchUrl: (url: string, pathParams: ApiPathParams) => string;
26
- };
27
-
28
- export { ApiPathParams, ApiUseOptions, ApiUseOptionsWithRunnable, ApiWatchOptions, createApi };
package/lib/api.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import { e as AxleInstance, d as RunnerMethod, A as AxleRequestConfig } from './instance-857fe07c.js';
2
- import { WatchOptions, UseAxleOptions, UseAxleOptionsWithRunnable, UseAxle, UseAxleInstance } from './use.js';
3
- import 'axios';
4
- import 'vue';
5
-
6
- type ApiPathParams = Record<string, any> | (() => Record<string, any>);
7
- type ApiWatchOptions = WatchOptions & {
8
- pathParams?: boolean;
9
- };
10
- type ApiUseOptions<V, R, P, D> = Partial<UseAxleOptions<V, R, P, D>> & {
11
- pathParams?: ApiPathParams;
12
- watch?: ApiWatchOptions;
13
- };
14
- type ApiUseOptionsWithRunnable<V, R, P, D> = Partial<Omit<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'>> & Pick<UseAxleOptionsWithRunnable<V, R, P, D>, 'runnable'> & {
15
- pathParams?: ApiPathParams;
16
- watch?: ApiWatchOptions;
17
- };
18
- declare function createApi(axle: AxleInstance, useAxle: UseAxle): <R = any, P = Record<string, any>, D = Record<string, any>>(url: string, method: RunnerMethod) => {
19
- url: string;
20
- load: (params?: P | undefined, pathParams?: ApiPathParams, config?: AxleRequestConfig<D, Record<string, any>> | undefined) => Promise<R>;
21
- use: {
22
- <V = R>(options?: ApiUseOptionsWithRunnable<V, R, P, D> | undefined): UseAxleInstance<V, R | undefined, P, D>;
23
- <V_1 = R>(options?: ApiUseOptions<V_1, R, P, D> | undefined): UseAxleInstance<V_1, R, P, D>;
24
- };
25
- patchUrl: (url: string, pathParams: ApiPathParams) => string;
26
- };
27
-
28
- export { ApiPathParams, ApiUseOptions, ApiUseOptionsWithRunnable, ApiWatchOptions, createApi };
package/lib/api.js DELETED
@@ -1,41 +0,0 @@
1
- import {
2
- __objRest,
3
- __spreadProps,
4
- __spreadValues
5
- } from "./chunk-G2ADBYYC.js";
6
-
7
- // src/api.ts
8
- import { isFunction } from "rattail";
9
- function createApi(axle, useAxle) {
10
- return function api(url, method) {
11
- function load(params, pathParams, config) {
12
- return axle[method](patchUrl(url, pathParams != null ? pathParams : {}), params, config);
13
- }
14
- function use(options = {}) {
15
- const _a = options, { pathParams = {}, watch: watchOptions } = _a, rest = __objRest(_a, ["pathParams", "watch"]);
16
- const enableWatchPathParams = isFunction(pathParams) && ((watchOptions == null ? void 0 : watchOptions.pathParams) || watchOptions === true);
17
- const normalizedWatchOptions = watchOptions === true ? { params: true, config: true, _custom: enableWatchPathParams ? pathParams : void 0 } : watchOptions === false ? { params: false, config: false } : __spreadProps(__spreadValues({ params: false, config: false }, watchOptions), { _custom: enableWatchPathParams ? pathParams : void 0 });
18
- return useAxle(__spreadValues({
19
- url: () => patchUrl(url, pathParams),
20
- method,
21
- watch: normalizedWatchOptions
22
- }, rest));
23
- }
24
- function patchUrl(url2, pathParams) {
25
- const params = isFunction(pathParams) ? pathParams() : pathParams;
26
- Object.entries(params).forEach(([key, value]) => {
27
- url2 = url2.replace(`:${key}`, value);
28
- });
29
- return url2;
30
- }
31
- return {
32
- url,
33
- load,
34
- use,
35
- patchUrl
36
- };
37
- };
38
- }
39
- export {
40
- createApi
41
- };
@@ -1,37 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __objRest = (source, exclude) => {
21
- var target = {};
22
- for (var prop in source)
23
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
- target[prop] = source[prop];
25
- if (source != null && __getOwnPropSymbols)
26
- for (var prop of __getOwnPropSymbols(source)) {
27
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
- target[prop] = source[prop];
29
- }
30
- return target;
31
- };
32
-
33
- export {
34
- __spreadValues,
35
- __spreadProps,
36
- __objRest
37
- };