@ramathibodi/nuxt-commons 4.0.4 → 4.0.6

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.
@@ -1,10 +1,9 @@
1
1
  import { computed, ref, watch } from "vue";
2
2
  import { useAlert } from "./alert.js";
3
- import { useApi } from "./api.js";
4
- import { buildApiEndpoint, stripModelByQualifier } from "./apiModel.js";
3
+ import { useApiModelOperation } from "./apiModelOperation.js";
5
4
  export function useApiModelItem(props) {
6
5
  const alert = useAlert();
7
- const api = useApi();
6
+ const ops = computed(() => useApiModelOperation(props.modelName));
8
7
  const modelBy = ref();
9
8
  const item = ref();
10
9
  const isLoading = ref(false);
@@ -20,11 +19,7 @@ export function useApiModelItem(props) {
20
19
  function reload() {
21
20
  isLoading.value = true;
22
21
  item.value = void 0;
23
- const endpoint = buildApiEndpoint(props.modelName);
24
- return api.postPromise(endpoint, {
25
- variables: computedModelBy.value,
26
- fields: fields.value
27
- }).then((result) => {
22
+ return ops.value.read(fields.value, computedModelBy.value).then((result) => {
28
23
  if (Array.isArray(result)) {
29
24
  alert?.addAlert({ alertType: "error", message: "Return result is not single item" });
30
25
  } else {
@@ -38,11 +33,7 @@ export function useApiModelItem(props) {
38
33
  }
39
34
  function createItem(createItem2, callback) {
40
35
  isLoading.value = true;
41
- const endpoint = buildApiEndpoint(stripModelByQualifier(props.modelName), "create");
42
- return api.postPromise(endpoint, {
43
- input: createItem2,
44
- fields: fields.value
45
- }).then((result) => {
36
+ return ops.value.create(createItem2, fields.value).then((result) => {
46
37
  item.value = result;
47
38
  }).catch((error) => {
48
39
  alert?.addAlert({ alertType: "error", message: error?.message ?? error });
@@ -54,11 +45,7 @@ export function useApiModelItem(props) {
54
45
  }
55
46
  function updateItem(updateItem2, callback) {
56
47
  isLoading.value = true;
57
- const endpoint = buildApiEndpoint(stripModelByQualifier(props.modelName), "update");
58
- return api.postPromise(endpoint, {
59
- input: updateItem2,
60
- fields: fields.value
61
- }).then((result) => {
48
+ return ops.value.update(updateItem2, fields.value).then((result) => {
62
49
  item.value = result;
63
50
  }).catch((error) => {
64
51
  alert?.addAlert({ alertType: "error", message: error?.message ?? error });
@@ -70,11 +57,7 @@ export function useApiModelItem(props) {
70
57
  }
71
58
  function deleteItem(deleteItem2, callback) {
72
59
  isLoading.value = true;
73
- const endpoint = buildApiEndpoint(stripModelByQualifier(props.modelName), "delete");
74
- return api.postPromise(endpoint, {
75
- input: deleteItem2,
76
- fields: fields.value
77
- }).catch((error) => {
60
+ return ops.value.delete(deleteItem2, fields.value).catch((error) => {
78
61
  alert?.addAlert({ alertType: "error", message: error?.message ?? error });
79
62
  }).finally(() => {
80
63
  isLoading.value = false;
@@ -0,0 +1,19 @@
1
+ export interface ApiModelPageable {
2
+ page?: number;
3
+ perPage?: number;
4
+ sortBy?: any;
5
+ }
6
+ export interface ApiModelOperationCallables<TItem = any, TInput = any> {
7
+ read: <T = TItem>(fields?: Array<string | object>, variables?: Record<string, any>, cache?: boolean | number) => Promise<T>;
8
+ readPageable: <T = TItem>(fields?: Array<string | object>, variables?: Record<string, any>, pageable?: ApiModelPageable) => Promise<{
9
+ data: T[];
10
+ meta: {
11
+ totalItems: number;
12
+ };
13
+ }>;
14
+ search: <T = TItem>(fields?: Array<string | object>, variables?: Record<string, any>, search?: any) => Promise<T[]>;
15
+ create: <T = TItem>(input: TInput | Record<string, any>, fields?: Array<string | object>) => Promise<T>;
16
+ update: <T = TItem>(input: TInput | Record<string, any>, fields?: Array<string | object>) => Promise<T>;
17
+ delete: <T = any>(input: TInput | Record<string, any>, fields?: Array<string | object>) => Promise<T>;
18
+ }
19
+ export declare function useApiModelOperation<TItem = any, TInput = any>(modelName: string): ApiModelOperationCallables<TItem, TInput>;
@@ -0,0 +1,56 @@
1
+ import { useApi } from "./api.js";
2
+ import { buildApiEndpoint, stripModelByQualifier } from "./apiModel.js";
3
+ import { buildFields } from "./graphqlOperation.js";
4
+ import { useGraphqlBridge } from "../bridges/graphql.js";
5
+ export function useApiModelOperation(modelName) {
6
+ const api = useApi();
7
+ function prepFields(fields) {
8
+ const { graphqlType } = useGraphqlBridge();
9
+ const schema = graphqlType?.[modelName];
10
+ if (!schema) return fields;
11
+ return buildFields(schema, fields, 0);
12
+ }
13
+ return {
14
+ read: (fields, variables, cache) => {
15
+ const endpoint = buildApiEndpoint(modelName);
16
+ return api.postPromise(
17
+ endpoint,
18
+ { variables, fields: prepFields(fields) },
19
+ void 0,
20
+ void 0,
21
+ cache
22
+ );
23
+ },
24
+ readPageable: (fields, variables, pageable) => {
25
+ const endpoint = buildApiEndpoint(modelName, "pageable");
26
+ return api.postPromise(endpoint, {
27
+ ...variables || {},
28
+ variables,
29
+ fields: prepFields(fields),
30
+ page: pageable?.page,
31
+ perPage: pageable?.perPage,
32
+ sortBy: pageable?.sortBy
33
+ });
34
+ },
35
+ search: (fields, variables, search) => {
36
+ const endpoint = buildApiEndpoint(modelName, "search");
37
+ return api.postPromise(endpoint, {
38
+ variables,
39
+ fields: prepFields(fields),
40
+ search
41
+ });
42
+ },
43
+ create: (input, fields) => {
44
+ const endpoint = buildApiEndpoint(stripModelByQualifier(modelName), "create");
45
+ return api.postPromise(endpoint, { input, fields: prepFields(fields) });
46
+ },
47
+ update: (input, fields) => {
48
+ const endpoint = buildApiEndpoint(stripModelByQualifier(modelName), "update");
49
+ return api.postPromise(endpoint, { input, fields: prepFields(fields) });
50
+ },
51
+ delete: (input, fields) => {
52
+ const endpoint = buildApiEndpoint(stripModelByQualifier(modelName), "delete");
53
+ return api.postPromise(endpoint, { input, fields: prepFields(fields) });
54
+ }
55
+ };
56
+ }
@@ -1,5 +1,6 @@
1
1
  import { type UseMutationReturn } from '@vue/apollo-composable';
2
2
  import { type ClassConstructor } from '../utils/object.js';
3
+ import { type CacheOption } from './api.js';
3
4
  declare type VariableOptions = {
4
5
  type?: string;
5
6
  name?: string;
@@ -11,7 +12,7 @@ declare type VariableOptions = {
11
12
  };
12
13
  export declare function useGraphQl(): {
13
14
  query: <T>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions, cache?: boolean) => import("@vue/apollo-composable").UseQueryReturn<T, Record<string, never>>;
14
- queryPromise: <T>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions, cache?: boolean | number) => Promise<T>;
15
+ queryPromise: <T>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions, cache?: CacheOption) => Promise<T>;
15
16
  mutation: <T>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions) => UseMutationReturn<any, any>;
16
17
  mutationPromise: <T>(operation: string, fields: Array<string | Object> | ClassConstructor<any>, variables?: VariableOptions) => Promise<T>;
17
18
  };
@@ -7,6 +7,7 @@ export function buildFields(operationFields, fields, depth = 0) {
7
7
  if (!operationFields) return [];
8
8
  if (isClassConstructor(fields)) fields = classAttributes(fields);
9
9
  if (!fields || fields.length == 0) fields = ["*"];
10
+ else fields = [...fields];
10
11
  if (fields.includes("*")) {
11
12
  operationFields.fields.forEach((field) => {
12
13
  if (!fields.includes(field) && !field.toLowerCase().endsWith("base64") && !field.toLowerCase().endsWith("base64string")) fields.push(field);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ramathibodi/nuxt-commons",
3
- "version": "4.0.4",
3
+ "version": "4.0.6",
4
4
  "description": "Ramathibodi Nuxt modules for common components",
5
5
  "repository": {
6
6
  "type": "git",