@platforma-sdk/ui-vue 1.4.4 → 1.4.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platforma-sdk/ui-vue",
3
- "version": "1.4.4",
3
+ "version": "1.4.5",
4
4
  "type": "module",
5
5
  "main": "dist/lib.umd.cjs",
6
6
  "module": "dist/lib.js",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "vue": "^3.5.9",
22
- "@milaboratories/uikit": "^1.2.15",
22
+ "@milaboratories/uikit": "^1.2.16",
23
23
  "@platforma-sdk/model": "^1.2.30"
24
24
  },
25
25
  "devDependencies": {
@@ -50,7 +50,7 @@
50
50
  "vue-tsc": "^2.1.6",
51
51
  "zod": "^3.23.8",
52
52
  "yarpm": "^1.2.0",
53
- "@milaboratories/helpers": "^1.6.4"
53
+ "@milaboratories/helpers": "^1.6.5"
54
54
  },
55
55
  "scripts": {
56
56
  "test": "vitest run --passWithNoTests",
@@ -0,0 +1,63 @@
1
+ import type { WatchSource, WatchOptions } from 'vue';
2
+ import { reactive, watch, ref, computed } from 'vue';
3
+ import { exclusiveRequest } from '@milaboratories/helpers';
4
+ import type { FetchResult } from '../types';
5
+
6
+ // TODO Should we keep the old value while fetching the new value?
7
+
8
+ /**
9
+ * Watch any reactive source and perform an asynchronous operation
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const v = useWatchFetch(
14
+ * watchSource,
15
+ * async (sourceValue) => {
16
+ * return await fetchDataFromApi(sourceValue);
17
+ * }
18
+ * );
19
+ *
20
+ * // Usage in a template
21
+ * <template>
22
+ * <div v-if="v.loading">Loading...</div>
23
+ * <div v-else-if="v.error">Error: {{ v.error.message }}</div>
24
+ * <div v-else>Data: {{ v.value }}</div>
25
+ * </template>
26
+ * ```
27
+ */
28
+ export function useWatchFetch<S, V>(watchSource: WatchSource<S>, doFetch: (s: S) => Promise<V>, watchOptions?: WatchOptions): FetchResult<V> {
29
+ const loadingRef = ref(0);
30
+
31
+ const data = reactive({
32
+ loading: computed(() => loadingRef.value > 0),
33
+ loadingRef,
34
+ value: undefined as V,
35
+ error: undefined,
36
+ }) as FetchResult<V>;
37
+
38
+ const exclusive = exclusiveRequest(doFetch);
39
+
40
+ watch(
41
+ watchSource,
42
+ async (s) => {
43
+ data.error = undefined;
44
+ loadingRef.value++;
45
+ exclusive(s)
46
+ .then((res) => {
47
+ if (res.ok) {
48
+ data.value = res.value;
49
+ }
50
+ })
51
+ .catch((err) => {
52
+ data.value = undefined;
53
+ data.error = err;
54
+ })
55
+ .finally(() => {
56
+ loadingRef.value--;
57
+ });
58
+ },
59
+ Object.assign({ immediate: true, deep: true }, watchOptions ?? {}),
60
+ );
61
+
62
+ return data;
63
+ }
package/src/lib.ts CHANGED
@@ -19,7 +19,7 @@ export * from './utils';
19
19
 
20
20
  export * from './computedResult';
21
21
 
22
- export * from './composition/useWatchResult';
22
+ export * from './composition/useWatchFetch';
23
23
 
24
24
  export * from './composition/fileContent';
25
25
 
package/src/types.ts CHANGED
@@ -55,6 +55,12 @@ export type LocalState<Href extends `/${string}` = `/${string}`> = {
55
55
  routes: Routes<Href>;
56
56
  };
57
57
 
58
+ export type FetchResult<V, E = unknown> = {
59
+ loading: boolean;
60
+ value: V | undefined;
61
+ error: E;
62
+ };
63
+
58
64
  // Results (ValueOrErrors)
59
65
 
60
66
  export type UnwrapValueOrError<W> = W extends {
@@ -79,6 +85,17 @@ export type ModelResult<T, E = unknown> =
79
85
  error: E;
80
86
  };
81
87
 
88
+ export type OutputValues<Outputs extends BlockOutputsBase> = {
89
+ [P in keyof Outputs]?: UnwrapValueOrError<Outputs[P]>;
90
+ };
91
+
92
+ export type OutputErrors<Outputs extends BlockOutputsBase> = {
93
+ [P in keyof Outputs]?: Error;
94
+ };
95
+
96
+ /**
97
+ * @deprecated
98
+ */
82
99
  export type OptionalResult<T> =
83
100
  | {
84
101
  errors?: undefined;
@@ -89,19 +106,6 @@ export type OptionalResult<T> =
89
106
  errors: string[];
90
107
  };
91
108
 
92
- export type OutputValues<Outputs extends BlockOutputsBase> = {
93
- [P in keyof Outputs]?: UnwrapValueOrError<Outputs[P]>;
94
- };
95
-
96
- export type OutputErrors<Outputs extends BlockOutputsBase> = {
97
- [P in keyof Outputs]?: Error;
98
- };
99
-
100
- // declare global {
101
- // const platforma: Platforma | undefined;
102
- // interface Window {
103
- // platforma: Platforma | undefined;
104
- // }
105
- // }
109
+ // Static tests
106
110
 
107
111
  type _cases = [Expect<Equal<number, UnwrapValueOrError<ValueOrErrors<number>>>>];
@@ -1,7 +0,0 @@
1
- import type { WatchSource } from 'vue';
2
- import type { OptionalResult } from '../types';
3
- /**
4
- * Use for synchronous/asynchronous converters (wip)
5
- */
6
- export declare function useWatchResult<S, V>(watchSource: WatchSource<S>, load: (s: S) => Promise<V> | V): OptionalResult<V>;
7
- //# sourceMappingURL=useWatchResult.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useWatchResult.d.ts","sourceRoot":"","sources":["../../../src/composition/useWatchResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAEvC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAkCnH"}
@@ -1,42 +0,0 @@
1
- import type { WatchSource } from 'vue';
2
- import { reactive, watch } from 'vue';
3
- import type { OptionalResult } from '../types';
4
-
5
- /**
6
- * Use for synchronous/asynchronous converters (wip)
7
- */
8
- export function useWatchResult<S, V>(watchSource: WatchSource<S>, load: (s: S) => Promise<V> | V): OptionalResult<V> {
9
- const data = reactive({
10
- value: undefined as unknown,
11
- errors: undefined as unknown,
12
- });
13
-
14
- const state = {
15
- version: 0,
16
- };
17
-
18
- const resolve = async (s: S, version: number) => {
19
- const value = await load(s);
20
- return { value, version };
21
- };
22
-
23
- watch(
24
- watchSource,
25
- async (s) => {
26
- data.errors = undefined;
27
- data.value = undefined;
28
- try {
29
- const { value, version } = await resolve(s, ++state.version);
30
-
31
- if (version === state.version) {
32
- data.value = value;
33
- }
34
- } catch (error) {
35
- data.errors = [String(error)];
36
- }
37
- },
38
- { immediate: true, deep: true },
39
- );
40
-
41
- return data as OptionalResult<V>;
42
- }