@karta.io/utils 1.0.0-alpha.0 → 1.0.0-alpha.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/dist/index.cjs CHANGED
@@ -5,8 +5,8 @@ const commonData = require('@karta.io/common-data');
5
5
  const libphonenumberJs = require('libphonenumber-js');
6
6
  const snakeCase = require('lodash/snakeCase.js');
7
7
  const vue = require('vue');
8
- const vueQuery = require('@tanstack/vue-query');
9
8
  const core = require('@vueuse/core');
9
+ const vueQuery = require('@tanstack/vue-query');
10
10
  const get = require('lodash/get.js');
11
11
  const has = require('lodash/has.js');
12
12
  const logdown = require('logdown');
@@ -273,6 +273,50 @@ function useAbortController(callback) {
273
273
  };
274
274
  }
275
275
 
276
+ function useAsyncItems({
277
+ apiMethod,
278
+ queryKey,
279
+ params,
280
+ config = void 0
281
+ }) {
282
+ const getQueryKey = () => [queryKey, params];
283
+ const { call, abort } = useAbortController(apiMethod);
284
+ const { isLoading, isFetching, isError, data, error } = vueQuery.useQuery({
285
+ queryKey: getQueryKey(),
286
+ queryFn: () => call(core.toValue(params), config)
287
+ });
288
+ const items = vue.computed(() => data.value?.results || []);
289
+ const count = vue.ref(0);
290
+ vue.watch(
291
+ data,
292
+ (newValue) => {
293
+ if (newValue === void 0 || newValue.count === void 0)
294
+ return;
295
+ count.value = newValue.count;
296
+ },
297
+ { immediate: true }
298
+ );
299
+ const queryClient = vueQuery.useQueryClient();
300
+ const refetch = () => {
301
+ queryClient.invalidateQueries({ queryKey: getQueryKey() });
302
+ };
303
+ const reload = () => {
304
+ queryClient.resetQueries({ queryKey: getQueryKey() });
305
+ };
306
+ return {
307
+ isLoading,
308
+ isFetching,
309
+ isError,
310
+ data,
311
+ error,
312
+ items,
313
+ count,
314
+ refetch,
315
+ reload,
316
+ abort
317
+ };
318
+ }
319
+
276
320
  function isEnabled(options) {
277
321
  if (options?.debug !== void 0)
278
322
  return options.debug;
@@ -1127,6 +1171,7 @@ exports.parsePhone = parsePhone;
1127
1171
  exports.snakelizeKeys = snakelizeKeys;
1128
1172
  exports.unformatNumber = unformatNumber;
1129
1173
  exports.useAbortController = useAbortController;
1174
+ exports.useAsyncItems = useAsyncItems;
1130
1175
  exports.useAsyncMultiselect = useAsyncMultiselect;
1131
1176
  exports.useAsyncSelect = useAsyncSelect;
1132
1177
  exports.useBreakpoint = useBreakpoint;
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import * as vue from 'vue';
2
- import { Ref, Component, EmitsOptions, VNode } from 'vue';
3
- import { MaybeRef } from '@vueuse/core';
2
+ import { MaybeRef, Ref, Component, EmitsOptions, VNode } from 'vue';
3
+ import { AxiosRequestConfig } from 'axios';
4
+ export { AxiosError as HttpError, AxiosPromise as HttpPromise, AxiosRequestConfig as HttpRequestConfig, AxiosResponse as HttpResponse } from 'axios';
5
+ import { MaybeRef as MaybeRef$1 } from '@vueuse/core';
4
6
  import dayjs from 'dayjs';
5
7
  import { Browser } from 'detect-browser';
6
8
  import logdown from 'logdown';
7
9
  import { RuleItem } from 'async-validator';
8
- export { AxiosError as HttpError, AxiosPromise as HttpPromise, AxiosRequestConfig as HttpRequestConfig, AxiosResponse as HttpResponse } from 'axios';
9
10
 
10
11
  interface AppComponentsResolverOptions {
11
12
  prefix?: string;
@@ -106,6 +107,24 @@ declare function useAbortController<T extends (...args: any[]) => any>(callback:
106
107
  isAborted(error: ResponseError): boolean;
107
108
  };
108
109
 
110
+ declare function useAsyncItems<T extends (...args: any[]) => any>({ apiMethod, queryKey, params, config, }: {
111
+ apiMethod: T;
112
+ queryKey: string;
113
+ params: MaybeRef<Parameters<T>[0]>;
114
+ config?: AxiosRequestConfig;
115
+ }): {
116
+ isLoading: vue.Ref<false> | vue.Ref<true>;
117
+ isFetching: vue.Ref<boolean>;
118
+ isError: vue.Ref<false> | vue.Ref<true>;
119
+ data: vue.Ref<any>;
120
+ error: vue.Ref<unknown>;
121
+ items: vue.ComputedRef<Awaited<ReturnType<T>>["results"]>;
122
+ count: vue.Ref<number>;
123
+ refetch: () => void;
124
+ reload: () => void;
125
+ abort: () => void;
126
+ };
127
+
109
128
  interface ReactiveData$1 {
110
129
  search: string;
111
130
  page: number;
@@ -115,8 +134,8 @@ interface ReactiveData$1 {
115
134
  declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValues, params, queryKey, dataItemsKey, dataItemIdKey, dataCountKey, delay, onUpdated, }: {
116
135
  apiMethod: T;
117
136
  mapOptionCallback?: K;
118
- selectedValues?: MaybeRef<Parameters<T>[0]['includedIds']>;
119
- params?: MaybeRef<Partial<Parameters<T>[0]>>;
137
+ selectedValues?: MaybeRef$1<Parameters<T>[0]['includedIds']>;
138
+ params?: MaybeRef$1<Partial<Parameters<T>[0]>>;
120
139
  queryKey: string;
121
140
  dataItemsKey?: string;
122
141
  dataItemIdKey?: string;
@@ -143,8 +162,8 @@ interface ReactiveData {
143
162
  declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValue, params, queryKey, dataItemsKey, dataCountKey, delay, onUpdated, }: {
144
163
  apiMethod: T;
145
164
  mapOptionCallback?: K;
146
- selectedValue?: MaybeRef<Parameters<T>[0]['includedIds']>;
147
- params?: MaybeRef<Partial<Parameters<T>[0]>>;
165
+ selectedValue?: MaybeRef$1<Parameters<T>[0]['includedIds']>;
166
+ params?: MaybeRef$1<Partial<Parameters<T>[0]>>;
148
167
  queryKey: string;
149
168
  dataItemsKey?: string;
150
169
  dataCountKey?: string;
@@ -279,4 +298,4 @@ declare function isTypeUrl(message?: string): RuleItem;
279
298
  declare function isTypeNumber(value?: number, message?: string): RuleItem;
280
299
  declare function isStrictLength(value?: number, message?: string): RuleItem;
281
300
 
282
- export { AppComponentsResolver, BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, formatMoney, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
301
+ export { AppComponentsResolver, BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, formatMoney, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
package/dist/index.mjs CHANGED
@@ -2,9 +2,9 @@ import camelCase from 'lodash/camelCase.js';
2
2
  import { CURRENCY_DEFAULT, BREAKPOINTS } from '@karta.io/common-data';
3
3
  import { parsePhoneNumberFromString } from 'libphonenumber-js';
4
4
  import snakeCase from 'lodash/snakeCase.js';
5
- import { reactive, ref, watch, computed, isRef, nextTick, onBeforeUnmount, onMounted, getCurrentInstance, render, createVNode } from 'vue';
6
- import { useQuery } from '@tanstack/vue-query';
5
+ import { computed, ref, watch, reactive, isRef, nextTick, onBeforeUnmount, onMounted, getCurrentInstance, render, createVNode } from 'vue';
7
6
  import { toValue, useDebounceFn, useBreakpoints } from '@vueuse/core';
7
+ import { useQuery, useQueryClient } from '@tanstack/vue-query';
8
8
  import get from 'lodash/get.js';
9
9
  import has from 'lodash/has.js';
10
10
  import logdown from 'logdown';
@@ -256,6 +256,50 @@ function useAbortController(callback) {
256
256
  };
257
257
  }
258
258
 
259
+ function useAsyncItems({
260
+ apiMethod,
261
+ queryKey,
262
+ params,
263
+ config = void 0
264
+ }) {
265
+ const getQueryKey = () => [queryKey, params];
266
+ const { call, abort } = useAbortController(apiMethod);
267
+ const { isLoading, isFetching, isError, data, error } = useQuery({
268
+ queryKey: getQueryKey(),
269
+ queryFn: () => call(toValue(params), config)
270
+ });
271
+ const items = computed(() => data.value?.results || []);
272
+ const count = ref(0);
273
+ watch(
274
+ data,
275
+ (newValue) => {
276
+ if (newValue === void 0 || newValue.count === void 0)
277
+ return;
278
+ count.value = newValue.count;
279
+ },
280
+ { immediate: true }
281
+ );
282
+ const queryClient = useQueryClient();
283
+ const refetch = () => {
284
+ queryClient.invalidateQueries({ queryKey: getQueryKey() });
285
+ };
286
+ const reload = () => {
287
+ queryClient.resetQueries({ queryKey: getQueryKey() });
288
+ };
289
+ return {
290
+ isLoading,
291
+ isFetching,
292
+ isError,
293
+ data,
294
+ error,
295
+ items,
296
+ count,
297
+ refetch,
298
+ reload,
299
+ abort
300
+ };
301
+ }
302
+
259
303
  function isEnabled(options) {
260
304
  if (options?.debug !== void 0)
261
305
  return options.debug;
@@ -1085,4 +1129,4 @@ function isStrictLength(value = 3, message) {
1085
1129
  };
1086
1130
  }
1087
1131
 
1088
- export { AppComponentsResolver, UiComponentsResolver, camelizeKeys, formatMoney, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
1132
+ export { AppComponentsResolver, UiComponentsResolver, camelizeKeys, formatMoney, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karta.io/utils",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.1",
4
4
  "description": "karta.io utils for admin and application",
5
5
  "author": "Vitaly Blokhin <karambafe@gmail.com>",
6
6
  "homepage": "",
@@ -24,7 +24,7 @@
24
24
  "vue": "3.3.x"
25
25
  },
26
26
  "dependencies": {
27
- "@karta.io/common-data": "^1.0.0-alpha.0",
27
+ "@karta.io/common-data": "^1.0.0-alpha.1",
28
28
  "@tanstack/vue-query": "4.33.1",
29
29
  "@vueuse/core": "10.3.0",
30
30
  "async-validator": "4.2.5",