@anweb/nuxt-ancore 1.2.0 → 1.3.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/module.d.mts CHANGED
@@ -1,15 +1,8 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
- import { TApi } from '@anweb/nuxt-ancore';
3
2
 
4
3
  interface ModuleOptions {
5
4
  }
6
-
7
- declare module '#app' {
8
- interface NuxtApp {
9
- $api: TApi
10
- }
11
- }
12
-
13
5
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
14
6
 
15
7
  export { _default as default };
8
+ export type { ModuleOptions };
package/dist/module.d.ts CHANGED
@@ -1,15 +1,8 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
- import { TApi } from '@anweb/nuxt-ancore';
3
2
 
4
3
  interface ModuleOptions {
5
4
  }
6
-
7
- declare module '#app' {
8
- interface NuxtApp {
9
- $api: TApi
10
- }
11
- }
12
-
13
5
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
14
6
 
15
7
  export { _default as default };
8
+ export type { ModuleOptions };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "AnCore",
3
3
  "configKey": "ancore",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.1",
7
7
  "unbuild": "3.5.0"
package/dist/module.mjs CHANGED
@@ -9,6 +9,8 @@ const module = defineNuxtModule({
9
9
  async setup(_options, _nuxt) {
10
10
  const { resolve } = createResolver(import.meta.url);
11
11
  _nuxt.options.runtimeConfig.public.ancore = {};
12
+ _nuxt.options.alias["#ancore/utils"] = resolve("./runtime/utils");
13
+ _nuxt.options.alias["#ancore/types"] = resolve("./runtime/types");
12
14
  addImportsDir(resolve("./runtime/composables"));
13
15
  }
14
16
  });
@@ -8,8 +8,8 @@ interface TUseData<TData, TError> {
8
8
  init: () => Promise<void>;
9
9
  loading: ComputedRef<boolean>;
10
10
  request: Ref<NitroFetchRequest>;
11
- data: Ref<TData>;
12
- error: Ref<TError>;
11
+ data: ComputedRef<TData>;
12
+ error: ComputedRef<TError>;
13
13
  status: ReturnType<typeof useAsyncData>['status'];
14
14
  }
15
15
  export declare const useData: <TData = unknown, TError = unknown>(config: TConfig) => TUseData<TData, TError>;
@@ -1,6 +1,6 @@
1
1
  import { computed, ref } from "vue";
2
2
  import { useAsyncData } from "#app";
3
- import { userApi } from "../../utils";
3
+ import { userApi } from "#ancore/utils";
4
4
  export const useData = (config) => {
5
5
  const request = ref(config.request);
6
6
  const loading = computed(() => {
@@ -0,0 +1,20 @@
1
+ import type { Rules } from 'async-validator';
2
+ import { type UseAsyncValidatorExecuteReturn } from '@vueuse/integrations/useAsyncValidator';
3
+ interface TFormParams<T> {
4
+ rules?: Rules;
5
+ empty: Record<keyof T, unknown>;
6
+ data?: Partial<T> | null;
7
+ }
8
+ declare const _default: <T = unknown>(params: TFormParams<T>) => {
9
+ state: [T] extends [import("vue").Ref<any, any>] ? import("@vue/shared").IfAny<T, import("vue").Ref<T, T>, T> : import("vue").Ref<import("vue").UnwrapRef<T>, T | import("vue").UnwrapRef<T>>;
10
+ merge: (data: T) => void;
11
+ validator: {
12
+ check: () => Promise<UseAsyncValidatorExecuteReturn>;
13
+ errors: import("vue").Ref<Record<string, import("async-validator").ValidateError[]> | undefined, Record<string, import("async-validator").ValidateError[]> | undefined>;
14
+ };
15
+ history: {
16
+ isChanged: import("vue").ComputedRef<boolean>;
17
+ reset: () => void;
18
+ };
19
+ };
20
+ export default _default;
@@ -0,0 +1,68 @@
1
+ import {
2
+ useAsyncValidator
3
+ } from "@vueuse/integrations/useAsyncValidator";
4
+ import { useRefHistory } from "@vueuse/core";
5
+ import { computed, ref, nextTick, watchEffect, watch } from "vue";
6
+ export default (params) => {
7
+ const state = ref(params.empty ? { ...params.empty } : {});
8
+ const keys = Object.keys(params.empty);
9
+ let validatorExecute;
10
+ const validatorErrors = ref();
11
+ const History = useRefHistory(state, { deep: true });
12
+ const init = () => {
13
+ if (params.data) merge(params.data);
14
+ if (params.rules) initValidate();
15
+ void nextTick(History.clear);
16
+ };
17
+ const merge = (data) => {
18
+ for (const key of keys) {
19
+ state.value[key] = data[key];
20
+ }
21
+ };
22
+ const initValidate = () => {
23
+ if (!params.rules) return;
24
+ const validator = useAsyncValidator(
25
+ state,
26
+ params.rules,
27
+ { manual: true }
28
+ );
29
+ validatorExecute = validator.execute;
30
+ watchEffect(() => {
31
+ validatorErrors.value = validator.errorFields.value;
32
+ });
33
+ };
34
+ const first = computed(() => {
35
+ return History.history.value.at(-1)?.snapshot;
36
+ });
37
+ const isChanged = computed(() => {
38
+ if (History.history.value.length === 1) return false;
39
+ const firstOne = JSON.stringify(first.value);
40
+ const last = JSON.stringify(History.last.value.snapshot);
41
+ return firstOne !== last;
42
+ });
43
+ watch(state, () => {
44
+ if (validatorErrors.value) {
45
+ validatorErrors.value = void 0;
46
+ }
47
+ }, { deep: true });
48
+ init();
49
+ return {
50
+ state,
51
+ merge: (data) => {
52
+ merge(data);
53
+ },
54
+ validator: {
55
+ check: () => {
56
+ return validatorExecute();
57
+ },
58
+ errors: validatorErrors
59
+ },
60
+ history: {
61
+ isChanged,
62
+ reset: () => {
63
+ state.value = first.value;
64
+ void nextTick(History.clear);
65
+ }
66
+ }
67
+ };
68
+ };
@@ -0,0 +1,27 @@
1
+ import type { NitroFetchRequest } from 'nitropack';
2
+ import { type UseEventBusReturn } from '@vueuse/core';
3
+ import type { TWS as TWSDefault } from '#ancore/types';
4
+ interface TConfig<TData, TFilter, TWS extends TWSDefault> {
5
+ request: NitroFetchRequest;
6
+ filter?: TFilter;
7
+ reverse?: boolean;
8
+ events?: {
9
+ bus: UseEventBusReturn<any, any>;
10
+ callback: (list: TData[], count: number, data: any) => number | void;
11
+ }[];
12
+ ws?: {
13
+ type: TWS['type'];
14
+ callback: (list: TData[], count: number, data: any) => number | void;
15
+ }[];
16
+ }
17
+ declare const _default: <TData, TFilter = unknown, TWS extends TWSDefault = TWSDefault>(config: TConfig<TData, TFilter, TWS>) => {
18
+ init: (newFilter?: TFilter) => Promise<void>;
19
+ inited: import("vue").Ref<boolean, boolean>;
20
+ filter: [Partial<TFilter>] extends [import("vue").Ref<any, any>] ? import("@vue/shared").IfAny<import("vue").Ref<any, any> & Partial<TFilter>, import("vue").Ref<import("vue").Ref<any, any> & Partial<TFilter>, import("vue").Ref<any, any> & Partial<TFilter>>, import("vue").Ref<any, any> & Partial<TFilter>> : import("vue").Ref<import("vue").UnwrapRef<Partial<TFilter>>, Partial<TFilter> | import("vue").UnwrapRef<Partial<TFilter>>>;
21
+ loading: import("vue").ComputedRef<boolean>;
22
+ items: TData[];
23
+ count: import("vue").Ref<number | null, number | null>;
24
+ error: import("vue").Ref<import("#app").NuxtError<unknown> | undefined, import("#app").NuxtError<unknown> | undefined>;
25
+ status: import("vue").Ref<import("#app").AsyncDataRequestStatus, import("#app").AsyncDataRequestStatus>;
26
+ };
27
+ export default _default;
@@ -0,0 +1,82 @@
1
+ import { useEventBus } from "@vueuse/core";
2
+ import { computed, ref, reactive, watch } from "vue";
3
+ import { useAsyncData } from "#app";
4
+ import { userApi, toQuery } from "#ancore/utils";
5
+ export default (config) => {
6
+ const busWS = useEventBus("ws");
7
+ const inited = ref(false);
8
+ const filter = ref(config.filter || {});
9
+ const items = reactive([]);
10
+ const count = ref(null);
11
+ const init = async (newFilter) => {
12
+ if (newFilter) {
13
+ filter.value = { ...newFilter };
14
+ } else if (config.filter) {
15
+ filter.value = { ...config.filter };
16
+ }
17
+ await execute();
18
+ refresh();
19
+ if (config.events) {
20
+ for (const event of config.events) {
21
+ event.bus.on((data2) => {
22
+ const newCount = event.callback(items, count.value || 0, data2);
23
+ if (newCount || newCount === 0) {
24
+ count.value = newCount;
25
+ }
26
+ });
27
+ }
28
+ }
29
+ if (config.ws) {
30
+ busWS.on((data2) => {
31
+ const event = config.ws?.find((item) => item.type === data2.type);
32
+ if (event) {
33
+ const newCount = event.callback(items, count.value || 0, data2.data);
34
+ if (newCount || newCount === 0) {
35
+ count.value = newCount;
36
+ }
37
+ }
38
+ });
39
+ }
40
+ inited.value = true;
41
+ };
42
+ const refresh = () => {
43
+ if (!data.value) return;
44
+ if (!filter.value.skip) {
45
+ count.value = null;
46
+ items.length = 0;
47
+ }
48
+ if (config.reverse) {
49
+ items.unshift(...data.value.items);
50
+ } else {
51
+ items.push(...data.value.items);
52
+ }
53
+ count.value = data.value.count;
54
+ };
55
+ const key = computed(() => {
56
+ return config.request + "?" + toQuery(filter.value);
57
+ });
58
+ const loading = computed(() => {
59
+ return status.value === "pending";
60
+ });
61
+ const {
62
+ data,
63
+ error,
64
+ execute,
65
+ status
66
+ } = useAsyncData(
67
+ key,
68
+ () => userApi(config.request, { method: "GET", query: filter.value }),
69
+ { immediate: false }
70
+ );
71
+ watch(data, refresh);
72
+ return {
73
+ init,
74
+ inited,
75
+ filter,
76
+ loading,
77
+ items,
78
+ count,
79
+ error,
80
+ status
81
+ };
82
+ };
@@ -0,0 +1,3 @@
1
+ export * from './nuxt.js';
2
+ export * from './ws.js';
3
+ export * from './responseList.js';
@@ -0,0 +1,3 @@
1
+ export * from "./nuxt";
2
+ export * from "./ws.js";
3
+ export * from "./responseList.js";
@@ -0,0 +1,8 @@
1
+ import type { TApi } from '#ancore/utils'
2
+
3
+
4
+ declare module '#app' {
5
+ interface NuxtApp {
6
+ $api: TApi
7
+ }
8
+ }
@@ -0,0 +1,4 @@
1
+ export interface TResponseList<TData> {
2
+ items: TData[];
3
+ count: number;
4
+ }
File without changes
@@ -0,0 +1,4 @@
1
+ export interface TWS {
2
+ type: string;
3
+ data: unknown;
4
+ }
File without changes
@@ -0,0 +1 @@
1
+ export declare const asyncInit: (fInit: () => Promise<void>) => Promise<void>;
@@ -0,0 +1,9 @@
1
+ import { useNuxtApp } from "#app";
2
+ export const asyncInit = async (fInit) => {
3
+ const nuxtApp = useNuxtApp();
4
+ if (!!nuxtApp.vueApp._instance?.isMounted) {
5
+ void fInit();
6
+ } else {
7
+ await fInit();
8
+ }
9
+ };
@@ -1 +1,4 @@
1
1
  export * from './api.js';
2
+ export * from './asyncInit.js';
3
+ export * from './userApi.js';
4
+ export * from './toQuery.js';
@@ -1 +1,4 @@
1
1
  export * from "./api.js";
2
+ export * from "./asyncInit.js";
3
+ export * from "./userApi.js";
4
+ export * from "./toQuery.js";
@@ -0,0 +1,4 @@
1
+ type TValue = string | number | boolean | undefined | null;
2
+ export type TQueryObject = Record<string, TValue | TValue[]>;
3
+ export declare const toQuery: (params: TQueryObject) => string;
4
+ export {};
@@ -0,0 +1,3 @@
1
+ export const toQuery = (params) => {
2
+ return Object.entries(params).filter(([, value]) => value != null).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`).join("&");
3
+ };
@@ -0,0 +1,2 @@
1
+ import type { NitroFetchOptions, NitroFetchRequest } from 'nitropack';
2
+ export declare const userApi: <TData = unknown, TError = unknown>(request: NitroFetchRequest, opts: NitroFetchOptions<string>) => Promise<TData>;
@@ -0,0 +1,10 @@
1
+ import { useNuxtApp } from "#app";
2
+ import { api as coreApi } from "#ancore/utils";
3
+ export const userApi = (request, opts) => {
4
+ const { $api } = useNuxtApp();
5
+ if ($api) {
6
+ return $api(request, opts);
7
+ } else {
8
+ return coreApi(request, opts);
9
+ }
10
+ };
package/dist/types.d.mts CHANGED
@@ -1,7 +1,3 @@
1
- import type { NuxtModule } from '@nuxt/schema'
2
-
3
- import type { default as Module } from './module.mjs'
4
-
5
- export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
-
7
1
  export { default } from './module.mjs'
2
+
3
+ export { type ModuleOptions } from './module.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anweb/nuxt-ancore",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "AnCore Nuxt module",
5
5
  "repository": "https://github.com/ANLTD/ancore",
6
6
  "license": "MIT",
@@ -37,7 +37,10 @@
37
37
  "release:major": "npm run prepack && changelogen --bump major --release && npm publish --access public && git push --follow-tags"
38
38
  },
39
39
  "dependencies": {
40
- "@nuxt/kit": "^4.0.0"
40
+ "@nuxt/kit": "^4.0.0",
41
+ "@vueuse/core": "^13.5.0",
42
+ "@vueuse/integrations": "^13.5.0",
43
+ "async-validator": "^4.2.5"
41
44
  },
42
45
  "devDependencies": {
43
46
  "@nuxt/devtools": "^2.6.2",
@@ -48,4 +51,4 @@
48
51
  "nuxt": "^4.0.0",
49
52
  "typescript": "~5.8.3"
50
53
  }
51
- }
54
+ }