@arrai-innovations/reactive-helpers 18.0.2 → 19.0.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.
Files changed (49) hide show
  1. package/README.md +7 -1
  2. package/config/commonCrud.js +75 -0
  3. package/config/listCrud.js +57 -56
  4. package/config/objectCrud.js +110 -77
  5. package/index.js +6 -1
  6. package/package.json +3 -2
  7. package/types/config/commonCrud.d.ts +19 -0
  8. package/types/config/commonCrud.d.ts.map +1 -0
  9. package/types/config/listCrud.d.ts +44 -39
  10. package/types/config/listCrud.d.ts.map +1 -1
  11. package/types/config/objectCrud.d.ts +102 -40
  12. package/types/config/objectCrud.d.ts.map +1 -1
  13. package/types/index.d.ts +6 -1
  14. package/types/use/cancellableIntent.d.ts +2 -13
  15. package/types/use/cancellableIntent.d.ts.map +1 -1
  16. package/types/use/listInstance.d.ts +10 -10
  17. package/types/use/listInstance.d.ts.map +1 -1
  18. package/types/use/loadingError.d.ts +30 -6
  19. package/types/use/loadingError.d.ts.map +1 -1
  20. package/types/use/objectInstance.d.ts +83 -31
  21. package/types/use/objectInstance.d.ts.map +1 -1
  22. package/types/use/objectSubscription.d.ts +33 -29
  23. package/types/use/objectSubscription.d.ts.map +1 -1
  24. package/types/use/proxyLoadingError.d.ts +8 -23
  25. package/types/use/proxyLoadingError.d.ts.map +1 -1
  26. package/types/utils/cancellableFetch.d.ts +12 -0
  27. package/types/utils/cancellableFetch.d.ts.map +1 -0
  28. package/types/utils/cancellablePromise.d.ts +40 -0
  29. package/types/utils/cancellablePromise.d.ts.map +1 -0
  30. package/types/utils/deepUnref.d.ts +26 -0
  31. package/types/utils/deepUnref.d.ts.map +1 -0
  32. package/types/utils/refIfReactive.d.ts +2 -0
  33. package/types/utils/refIfReactive.d.ts.map +1 -0
  34. package/types/utils/unwrapNested.d.ts +2 -0
  35. package/types/utils/unwrapNested.d.ts.map +1 -0
  36. package/use/cancellableIntent.js +8 -13
  37. package/use/listInstance.js +67 -68
  38. package/use/listSearch.js +1 -1
  39. package/use/loadingError.js +30 -6
  40. package/use/objectInstance.js +228 -128
  41. package/use/objectSubscription.js +81 -82
  42. package/use/proxyLoadingError.js +14 -27
  43. package/use/search.js +1 -1
  44. package/utils/cancellableFetch.js +22 -0
  45. package/utils/cancellablePromise.js +70 -0
  46. package/utils/classes.js +5 -5
  47. package/utils/deepUnref.js +28 -0
  48. package/utils/refIfReactive.js +18 -0
  49. package/utils/unwrapNested.js +25 -0
package/README.md CHANGED
@@ -948,11 +948,17 @@ await awaitNot3.promise;
948
948
  ```bash
949
949
  $ npm ci
950
950
  ```
951
- 3. Run tests via jest:
951
+ 3. Run tests via vitest:
952
952
  ```bash
953
953
  $ npm test
954
954
  ```
955
955
  4. Run tests with coverage output:
956
+
956
957
  ```bash
957
958
  $ npm run coverage
958
959
  ```
960
+
961
+ 5. Generate types and typedocs:
962
+ ```bash
963
+ $ npm run docs
964
+ ```
@@ -0,0 +1,75 @@
1
+ import { CancellablePromise } from "../utils/cancellablePromise.js";
2
+ import cloneDeep from "lodash-es/cloneDeep.js";
3
+ import { addOrUpdateReactiveObject } from "../utils/assignReactiveObject.js";
4
+ import isFunction from "lodash-es/isFunction.js";
5
+ import { refIfReactive } from "../utils/refIfReactive.js";
6
+
7
+ /**
8
+ * @param {string} name - The name of the method.
9
+ * @returns {(...args: any[]) => import('../utils/cancellablePromise.js').MaybeCancellablePromise<any>} - A function that returns a rejected promise with an error message.
10
+ */
11
+ export const missingMethod = (name) => () =>
12
+ CancellablePromise.reject(new Error(`Crud method "${name}" is not implemented.`));
13
+
14
+ // HACK: eslint, tsc, webstorm all can't agree on how to do this right
15
+ // noinspection JSValidateTypes,JSUnusedLocalSymbols
16
+ /**
17
+ * @param {string} name - The name of the method.
18
+ * @returns {(
19
+ * (..._args: any[]) => import('../utils/cancellablePromise.js').CancellablePromise<void>
20
+ * )} - A function that returns a rejected promise with an error message.
21
+ */
22
+ export const requiredCancelMissingMethod =
23
+ (name) =>
24
+ /* eslint-disable no-unused-vars */
25
+ // @ts-ignore - refuses to accept returned CancellablePromise<void> = imported CancellablePromise<void>
26
+ (..._args) =>
27
+ CancellablePromise(Promise.reject(new Error(`Crud method "${name}" is not implemented.`)), () => {});
28
+ /* eslint-enable no-unused-vars */
29
+
30
+ /**
31
+ * Creates a default CRUD object with the given keys.
32
+ *
33
+ * @param {string[]} keys - The CRUD function keys.
34
+ * @param {Set<string>} cancellableKeys - Which ones need required cancellation.
35
+ * @returns {object} - The default CRUD object.
36
+ */
37
+ export const createDefaultCrud = (keys, cancellableKeys = new Set()) => {
38
+ const result = { args: {} };
39
+ for (const key of keys) {
40
+ result[key] = cancellableKeys.has(key) ? requiredCancelMissingMethod(key) : missingMethod(key);
41
+ }
42
+ return result;
43
+ };
44
+
45
+ /**
46
+ * Assigns the default CRUD functions to the target object.
47
+ *
48
+ * @param {object} target - The reactive object to assign to.
49
+ * @param {object} defaultCrud - The default CRUD definition (usually created by `createDefaultCrud`).
50
+ * @param {object} [options] - The options object.
51
+ * @param {object} [options.props] - The props object.
52
+ * @param {object} [options.functions] - The functions to assign.
53
+ * @param {Set<string>} [options.validKeys] - The valid keys for the functions.
54
+ */
55
+ export function assignCrud(
56
+ target,
57
+ defaultCrud,
58
+ { props, functions, validKeys = new Set(Object.keys(defaultCrud).filter((k) => k !== "args")) } = {}
59
+ ) {
60
+ Object.assign(target, cloneDeep(defaultCrud));
61
+ if (props?.crudArgs) {
62
+ addOrUpdateReactiveObject(target.args, props.crudArgs);
63
+ }
64
+ if (functions) {
65
+ for (const [key, fn] of Object.entries(functions)) {
66
+ if (!validKeys.has(key)) {
67
+ throw new Error(`Invalid function key "${key}" passed to assignCrud`);
68
+ }
69
+ if (!isFunction(fn)) {
70
+ throw new Error(`Function "${key}" is not actually a function`);
71
+ }
72
+ target[key] = refIfReactive(functions, key, fn);
73
+ }
74
+ }
75
+ }
@@ -1,7 +1,6 @@
1
- import { addOrUpdateReactiveObject } from "../utils/assignReactiveObject.js";
1
+ import { assignCrud, createDefaultCrud } from "./commonCrud.js";
2
2
  import cloneDeep from "lodash-es/cloneDeep.js";
3
- import isFunction from "lodash-es/isFunction.js";
4
- import { isReactive, toRef } from "vue";
3
+ import { readonly } from "vue";
5
4
 
6
5
  /**
7
6
  * Configuration for the default list crud functions.
@@ -9,14 +8,6 @@ import { isReactive, toRef } from "vue";
9
8
  * @module config/listCrud.js
10
9
  */
11
10
 
12
- const defaultCrud = {
13
- args: {},
14
- list: undefined,
15
- bulkDelete: undefined,
16
- subscribe: undefined,
17
- executeAction: undefined,
18
- };
19
-
20
11
  /**
21
12
  * @typedef {object} PaginateInfo
22
13
  * @property {number} totalRecords - The total records.
@@ -32,7 +23,7 @@ const defaultCrud = {
32
23
  */
33
24
 
34
25
  /**
35
- * @typedef {object} ListFnArgs
26
+ * @typedef {object} ListArgs
36
27
  * @property {object} crudArgs - The arguments to be passed to the crud functions.
37
28
  * @property {string} pkKey - The key name of the primary key.
38
29
  * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
@@ -43,10 +34,12 @@ const defaultCrud = {
43
34
  */
44
35
 
45
36
  /**
46
- * @typedef {object} DeleteFnArgs
37
+ * @typedef {object} BulkDeleteArgs
47
38
  * @property {object} crudArgs - The arguments to be passed to the crud functions.
48
39
  * @property {string[]} pks - The ids of the objects to be deleted.
49
40
  * @property {string} pkKey - The key name of the primary key.
41
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to a boolean indicating whether the request has
42
+ * been cancelled.
50
43
  */
51
44
 
52
45
  /**
@@ -57,51 +50,77 @@ const defaultCrud = {
57
50
  */
58
51
 
59
52
  /**
60
- * @typedef {object} SubscribeFnArgs
53
+ * @typedef {object} ListSubscribeArgs
61
54
  * @property {object} crudArgs - The arguments to be passed to the crud functions.
62
55
  * @property {string} pkKey - The key name of the primary key.
63
56
  * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
64
57
  * @property {object} listArgs - The arguments to be passed for list crud functions.
65
58
  * @property {SubscriptionEventCallback} subscriptionEventCallback - The method to call when new data is received.
59
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to a boolean indicating whether the request has
60
+ * been cancelled.
66
61
  */
67
62
 
68
63
  /**
69
- * @typedef {object} ExecuteActionFnArgs
64
+ * @typedef {object} ExecuteActionArgs
70
65
  * @property {object} crudArgs - The arguments to be passed to the crud functions.
71
66
  * @property {string[]} pks - The ids of the objects to be acted upon.
72
67
  * @property {string} pkKey - The key name of the primary key.
73
68
  * @property {string} action - The action to execute.
69
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to a boolean indicating whether the request has
70
+ * been cancelled.
74
71
  */
75
72
 
76
73
  /**
77
- * @typedef {(ListFnArgs)=>Promise<boolean> & { cancel: () => Promise<void>|void }} ListFn - The list function to get a list of items, returning a boolean indicating success.
74
+ * @callback CrudListFn
75
+ * @param {ListArgs} args - The arguments to be passed to the crud functions.
76
+ * @returns {import('../utils/cancellablePromise.js').MaybeCancellablePromise<void>} - A promise that resolves to a boolean indicating success.
78
77
  */
79
78
 
80
79
  /**
81
- * @typedef {(DeleteFnArgs)=>Promise<boolean> & { cancel: () => Promise<void>|void }} BulkDeleteFn - The delete function to bulk delete a list of items.
80
+ * @callback CrudBulkDeleteFn
81
+ * @param {BulkDeleteArgs} args - The arguments to be passed to the crud functions.
82
+ * @returns {import('../utils/cancellablePromise.js').MaybeCancellablePromise<void>} - A promise that resolves to a boolean indicating success.
82
83
  */
83
84
 
84
85
  /**
85
- * @typedef {(SubscribeFnArgs)=>Promise<boolean> & { cancel: () => Promise<void>|void }} SubscribeFn - The subscribe function to set up a subscription to a list of items.
86
+ * @callback CrudListSubscribeFn
87
+ * @param {ListSubscribeArgs} args - The arguments to be passed to the crud functions.
88
+ * @returns {import('../utils/cancellablePromise.js').CancellablePromise<void>} - A promise that resolves to a boolean indicating success.
86
89
  */
87
90
 
88
91
  /**
89
- * @typedef {(ExecuteActionFnArgs)=>Promise<import('./objectCrud.js').ResponseData|false> & { cancel: () => Promise<void>|void }} ExecuteActionFn - The function to execute a certain action on a list of items, returning the response data or false.
92
+ * @callback CrudExecuteActionFn
93
+ * @param {ExecuteActionArgs} args - The arguments to be passed to the crud functions.
94
+ * @returns {import('../utils/cancellablePromise.js').MaybeCancellablePromise<object|string|null>} - A promise that resolves the result of the action, returned to the executor.
90
95
  */
91
96
 
92
97
  /**
93
98
  * @typedef {object} ListCrudFunctions
94
- * @property {ListFn} [list] - The list function to get a list of items.
95
- * @property {BulkDeleteFn} [bulkDelete] - The delete function to bulk delete a list of items.
96
- * @property {ExecuteActionFn} [executeAction] - The function to execute a certain action on a list of items.
97
- * @property {SubscribeFn} [subscribe] - The subscribe function to get a subscription to a list of items.
99
+ * @property {CrudListFn} [list] - The list function to get a list of items.
100
+ * @property {CrudBulkDeleteFn} [bulkDelete] - The delete function to bulk delete a list of items.
101
+ * @property {CrudExecuteActionFn} [executeAction] - The function to execute a certain action on a list of items.
102
+ * @property {CrudListSubscribeFn} [subscribe] - The subscribe function to get a subscription to a list of items.
98
103
  */
99
104
 
100
105
  /**
101
106
  * @typedef {object} ListCrudArgs
102
- * @property {object} [args={}] - The default arguments for the crud functions.
107
+ * @property {object} args - The default arguments for the crud functions.
103
108
  */
104
109
 
110
+ /**
111
+ * @typedef {object} ListCrudArgsOption
112
+ * @property {object} [crudArgs={}] - The default arguments for the crud functions.
113
+ */
114
+
115
+ const _defaultCrud = createDefaultCrud(["list", "bulkDelete", "executeAction", "subscribe"], new Set(["subscribe"]));
116
+
117
+ /**
118
+ * The default list crud functions.
119
+ *
120
+ * @type {Readonly<ListCrudFunctions>}
121
+ */
122
+ export const defaultListCrud = readonly(_defaultCrud);
123
+
105
124
  /**
106
125
  * Set the list and subscribe functions for the default crud.
107
126
  *
@@ -109,43 +128,25 @@ const defaultCrud = {
109
128
  * @throws {Error} - If unknown keys are passed.
110
129
  * @returns {void}
111
130
  */
112
- export const setListCrud = ({ list, bulkDelete, subscribe, executeAction, args = {}, ...rest }) => {
113
- defaultCrud.list = list;
114
- defaultCrud.subscribe = subscribe;
115
- defaultCrud.bulkDelete = bulkDelete;
116
- defaultCrud.executeAction = executeAction;
117
- Object.assign(defaultCrud.args, cloneDeep(args));
118
- if (Object.keys(rest).length) {
119
- throw new Error(`Unknown key(s) passed to setListCrud: ${Object.keys(rest).join(", ")}`);
131
+ export const setListCrud = ({ args = {}, ...rest }) => {
132
+ Object.assign(_defaultCrud.args, cloneDeep(args));
133
+ for (const [key, value] of Object.entries(rest)) {
134
+ if (!(key in _defaultCrud)) {
135
+ throw new Error(`Unknown key "${key}" passed to setListCrud`);
136
+ }
137
+ _defaultCrud[key] = value ?? _defaultCrud[key];
120
138
  }
121
139
  };
122
140
 
123
141
  /**
124
142
  * Get the previously set list and subscribe functions for the default crud.
125
143
  *
126
- * @param {import("vue").UnwrapNestedRefs<ListCrudFunctions & ListCrudArgs>} reactiveCrud - The reactive crud object, which will be mutated.
144
+ * @param {import("vue").UnwrapNestedRefs<ListCrudFunctions & ListCrudArgs>} target - The reactive crud object, which will be mutated.
127
145
  * @param {object} options - The options for the default crud.
128
- * @param {import("vue").UnwrapNestedRefs<{
129
- * crudArgs: object|undefined,
130
- * }>} [options.props] - The props to set for the crud.
131
- * @param {ListCrudFunctions & ListCrudArgs} [options.functions] - The functions to set for the crud.
132
- */
133
- export const getListCrud = (reactiveCrud, { props, functions } = {}) => {
134
- // don't mutate the default crud
135
- Object.assign(reactiveCrud, cloneDeep(defaultCrud));
136
- if (props?.crudArgs) {
137
- addOrUpdateReactiveObject(reactiveCrud.args, props.crudArgs);
138
- }
139
- if (functions) {
140
- for (const [key, value] of Object.entries(functions)) {
141
- if (isFunction(value) && key in reactiveCrud) {
142
- reactiveCrud[key] = isReactive(functions)
143
- ? // @ts-ignore - this is a valid key...
144
- toRef(functions, key)
145
- : value;
146
- } else {
147
- throw Error(`Invalid function "${key}" for getListCrud: invalid key or not a function.`);
148
- }
149
- }
150
- }
146
+ * @param {import("vue").UnwrapNestedRefs<ListCrudArgsOption>} [options.props] - The props to set for the crud.
147
+ * @param {ListCrudFunctions} [options.functions] - The functions to set for the crud.
148
+ * @throws {Error} - If an invalid function is passed, or if the function is not a function.
149
+ */
150
+ export const getListCrud = (target, options) => {
151
+ assignCrud(target, _defaultCrud, options);
151
152
  };
@@ -1,7 +1,6 @@
1
- import { addOrUpdateReactiveObject } from "../utils/assignReactiveObject.js";
1
+ import { assignCrud, createDefaultCrud } from "./commonCrud.js";
2
2
  import cloneDeep from "lodash-es/cloneDeep.js";
3
- import isFunction from "lodash-es/isFunction.js";
4
- import { isReactive, toRef } from "vue";
3
+ import { readonly } from "vue";
5
4
 
6
5
  /**
7
6
  * Configuration for the default object crud functions.
@@ -9,89 +8,133 @@ import { isReactive, toRef } from "vue";
9
8
  * @module config/objectCrud.js
10
9
  */
11
10
 
12
- const defaultCrud = {
13
- args: {},
14
- retrieve: undefined,
15
- create: undefined,
16
- update: undefined,
17
- patch: undefined,
18
- delete: undefined,
19
- subscribe: undefined,
20
- };
11
+ /**
12
+ * @typedef {{[key:string]: any}} ObjectCrudArgsArgs
13
+ */
21
14
 
22
15
  /**
23
16
  * Defines the CRUD-related functions and additional utilities provided by the object instance.
24
17
  *
25
18
  * @typedef {object} ObjectCrudArgsProperties
26
- * @property {object} [args={}] - The arguments to be passed to the crud functions.
19
+ * @property {ObjectCrudArgsArgs} args - The arguments to be passed to the crud functions.
27
20
  */
28
21
 
29
22
  /**
30
- * @typedef {object} CreateDetailArgs
31
- * @property {object} crudArgs - The arguments to be passed to the crud functions.
32
- * @property {object} object - The data to be acted upon.
33
- * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
23
+ * @typedef {object} ObjectCrudArgsOption
24
+ * @property {ObjectCrudArgsArgs} [crudArgs={}] - The arguments to be passed to the crud functions.
25
+ */
26
+
27
+ /**
28
+ * @typedef {object} CreateArgs
29
+ * @property {{[key:string]: any}} crudArgs - The arguments to be passed to the crud functions.
30
+ * @property {{[key:string]: any}} object - The data to be acted upon.
31
+ * @property {{[key:string]: any}} retrieveArgs - The arguments to be passed to the retrieve function.
34
32
  * @property {string} pkKey - The key name of the primary key.
33
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to indicate if the request was cancelled.
35
34
  */
36
35
 
37
36
  /**
38
- * @typedef {object} RetrieveDetailArgs
39
- * @property {object} crudArgs - The arguments to be passed to the crud functions.
37
+ * @typedef {object} RetrieveArgs
38
+ * @property {{[key:string]: any}} crudArgs - The arguments to be passed to the crud functions.
40
39
  * @property {string} pk - The pk of the object to be acted upon.
41
40
  * @property {string} pkKey - The key name of the primary key.
42
- * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
41
+ * @property {{[key:string]: any}} retrieveArgs - The arguments to be passed to the retrieve function.
42
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to indicate if the request was cancelled.
43
43
  */
44
44
 
45
45
  /**
46
- * @typedef {object} UpdateDetailArgs
47
- * @property {object} crudArgs - The arguments to be passed to the crud functions.
48
- * @property {import('../use/objectInstance.js').CrudObject} object - The data to be acted upon.
49
- * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
46
+ * @typedef {object} UpdateArgs
47
+ * @property {{[key:string]: any}} crudArgs - The arguments to be passed to the crud functions.
48
+ * @property {import('../use/objectInstance.js').ExistingCrudObject} object - The data to be acted upon.
49
+ * @property {{[key:string]: any}} retrieveArgs - The arguments to be passed to the retrieve function.
50
50
  * @property {string} pkKey - The key name of the primary key.
51
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to indicate if the request was cancelled.
51
52
  */
52
53
 
53
54
  /**
54
- * @typedef {object} DeleteDetailArgs
55
- * @property {object} crudArgs - The arguments to be passed to the crud functions.
55
+ * @typedef {object} DeleteArgs
56
+ * @property {{[key:string]: any}} crudArgs - The arguments to be passed to the crud functions.
56
57
  * @property {string} pk - The pk of the object to be acted upon.
57
58
  * @property {string} pkKey - The key name of the primary key.
58
- * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
59
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to indicate if the request was cancelled.
59
60
  */
60
61
 
61
62
  /**
62
- * @typedef {object} PartialDetailArgs
63
- * @property {object} crudArgs - The arguments to be passed to the crud functions.
63
+ * @typedef {object} PartialArgs
64
+ * @property {{[key:string]: any}} crudArgs - The arguments to be passed to the crud functions.
64
65
  * @property {string} pk - The pk of the object to be acted upon.
65
66
  * @property {string} pkKey - The key name of the primary key.
66
- * @property {object} partialObject - The data to be acted upon.
67
- * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
67
+ * @property {{[key:string]: any}} partialObject - The data to be acted upon.
68
+ * @property {{[key:string]: any}} retrieveArgs - The arguments to be passed to the retrieve function.
69
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to indicate if the request was cancelled.
70
+ */
71
+
72
+ /**
73
+ * @callback CrudSubscribeCallback
74
+ * @param {import('../use/objectInstance.js').ExistingCrudObject} data - The data to be passed to the callback.
75
+ * @param {string} action - The action that was performed.
68
76
  */
69
77
 
70
78
  /**
71
- * @typedef {object} SubscribeArgs
72
- * @property {object} crudArgs - The arguments to be passed to the crud functions.
79
+ * @typedef {object} ObjectSubscribeArgs
80
+ * @property {{[key:string]: any}} crudArgs - The arguments to be passed to the crud functions.
73
81
  * @property {string} pk - The pk of the object to be acted upon.
74
82
  * @property {string} pkKey - The key name of the primary key.
75
- * @property {object} retrieveArgs - The arguments to be passed to the retrieve function.
76
- * @property {(
77
- * data:import('../use/objectInstance.js').CrudObject, action:string
78
- * ) => void} callback - The callback to be called when the object is updated.
83
+ * @property {{[key:string]: any}} retrieveArgs - The arguments to be passed to the retrieve function.
84
+ * @property {CrudSubscribeCallback} callback - The callback to be called when the object is updated.
85
+ * @property {Readonly<import('vue').Ref<boolean>>} isCancelled - A ref to indicate if the request was cancelled.
86
+ */
87
+
88
+ /**
89
+ * @typedef {import('../utils/cancellablePromise.js').MaybeCancellablePromise<object|string>} CrudResponse
90
+ */
91
+
92
+ /**
93
+ * @callback CrudCreateFn
94
+ * @param {CreateArgs} args - The arguments to be passed to the create function.
95
+ * @returns {CrudResponse} - The response data from the create function.
96
+ */
97
+
98
+ /**
99
+ * @callback CrudRetrieveFn
100
+ * @param {RetrieveArgs} args - The arguments to be passed to the retrieve function.
101
+ * @returns {CrudResponse} - The response data from the retrieve function.
79
102
  */
80
103
 
81
104
  /**
82
- * @typedef {Promise<object|string> & { cancel: () => Promise<void>|void }} ResponseData
105
+ * @callback CrudUpdateFn
106
+ * @param {UpdateArgs} args - The arguments to be passed to the update function.
107
+ * @returns {CrudResponse} - The response data from the update function.
108
+ */
109
+
110
+ /**
111
+ * @callback CrudPatchFn
112
+ * @param {PartialArgs} args - The arguments to be passed to the patch function.
113
+ * @returns {CrudResponse} - The response data from the patch function.
114
+ */
115
+
116
+ /**
117
+ * @callback CrudDeleteFn
118
+ * @param {DeleteArgs} args - The arguments to be passed to the delete function.
119
+ * @returns {CrudResponse} - The response data from the delete function.
120
+ */
121
+
122
+ /**
123
+ * @callback CrudObjectSubscribeFn
124
+ * @param {ObjectSubscribeArgs} args - The arguments to be passed to the subscribe function.
125
+ * @returns {import('../utils/cancellablePromise.js').CancellablePromise<void>} - The cancellable promise.
83
126
  */
84
127
 
85
128
  /**
86
129
  * Defines the CRUD-related functions and additional utilities provided by the object instance.
87
130
  *
88
131
  * @typedef {object} ObjectCrudFunctions
89
- * @property {(CreateDetailArgs)=>ResponseData} [create] - A function to be used instead of the default crud create function.
90
- * @property {(RetrieveDetailArgs)=>ResponseData} [retrieve] - A function to be used instead of the default crud retrieve function.
91
- * @property {(UpdateDetailArgs)=>ResponseData} [update] - A function to be used instead of the default crud update function.
92
- * @property {(DeleteDetailArgs)=>ResponseData} [delete] - A function to be used instead of the default crud delete function.
93
- * @property {(PartialDetailArgs)=>ResponseData} [patch] - A function to be used instead of the default crud patch function.
94
- * @property {(SubscribeArgs)=>void & { cancel: () => Promise<void>|void }} [subscribe] - A function to be used instead of the default crud subscribe function.
132
+ * @property {CrudCreateFn} [create] - A function to be used instead of the default crud create function.
133
+ * @property {CrudRetrieveFn} [retrieve] - A function to be used instead of the default crud retrieve function.
134
+ * @property {CrudUpdateFn} [update] - A function to be used instead of the default crud update function.
135
+ * @property {CrudDeleteFn} [delete] - A function to be used instead of the default crud delete function.
136
+ * @property {CrudPatchFn} [patch] - A function to be used instead of the default crud patch function.
137
+ * @property {CrudObjectSubscribeFn} [subscribe] - A function to be used instead of the default crud subscribe function.
95
138
  */
96
139
 
97
140
  /**
@@ -101,53 +144,43 @@ const defaultCrud = {
101
144
  *
102
145
  */
103
146
 
147
+ const _defaultCrud = createDefaultCrud(
148
+ ["retrieve", "create", "update", "patch", "delete", "subscribe"],
149
+ new Set(["subscribe"])
150
+ );
151
+
152
+ /**
153
+ * The default object crud functions.
154
+ *
155
+ * @type {Readonly<ObjectCrudFunctions>}
156
+ */
157
+ export const defaultObjectCrud = readonly(_defaultCrud);
158
+
104
159
  /**
105
160
  * Set the object crud functions.
106
161
  *
107
162
  * @param {ObjectCrudArgs} options - The options for the object crud functions.
108
163
  * @throws {Error} - if unknown keys are passed.
109
164
  */
110
- export const setObjectCrud = ({ retrieve, create, update, patch, delete: deleteFn, subscribe, args = {}, ...rest }) => {
111
- defaultCrud.retrieve = retrieve;
112
- defaultCrud.create = create;
113
- defaultCrud.update = update;
114
- defaultCrud.patch = patch;
115
- defaultCrud.delete = deleteFn;
116
- defaultCrud.subscribe = subscribe;
117
- // defensive cloning
118
- Object.assign(defaultCrud.args, cloneDeep(args));
119
- if (Object.keys(rest).length) {
120
- throw new Error(`Unknown key(s) passed to setObjectCrud: ${Object.keys(rest).join(", ")}`);
165
+ export const setObjectCrud = ({ args = {}, ...rest }) => {
166
+ Object.assign(_defaultCrud.args, cloneDeep(args));
167
+ for (const [key, value] of Object.entries(rest)) {
168
+ if (!(key in _defaultCrud)) {
169
+ throw new Error(`Unknown key "${key}" passed to setObjectCrud`);
170
+ }
171
+ _defaultCrud[key] = value ?? _defaultCrud[key];
121
172
  }
122
173
  };
123
174
 
124
175
  /**
125
176
  * Get the previously set object crud functions.
126
177
  *
127
- * @param {import("vue").UnwrapNestedRefs<object>} reactiveCrud - The reactive object you want to add the resulting crud to.
178
+ * @param {import("vue").UnwrapNestedRefs<ObjectCrudArgsProperties>} target - The reactive object you want to add the resulting crud to.
128
179
  * @param {object} options - The options for the reactive crud object.
129
- * @param {import("vue").UnwrapNestedRefs<{
130
- * crudArgs: ObjectCrudArgsProperties|undefined,
131
- * }>} [options.props] - The props with any passed crudArgs.
180
+ * @param {import("vue").UnwrapNestedRefs<ObjectCrudArgsOption>} [options.props] - The props with any passed crudArgs.
132
181
  * @param {ObjectCrudFunctions} [options.functions] - Any functions to override the default crud functions.
133
182
  * @throws {Error} - If an invalid function is passed, or if the function is not a function.
134
183
  */
135
- export const getObjectCrud = (reactiveCrud, { props, functions } = {}) => {
136
- // don't mutate the default crud
137
- Object.assign(reactiveCrud, cloneDeep(defaultCrud));
138
- if (props?.crudArgs) {
139
- addOrUpdateReactiveObject(reactiveCrud.args, props.crudArgs);
140
- }
141
- if (functions) {
142
- for (const [key, value] of Object.entries(functions)) {
143
- if (isFunction(value) && key in reactiveCrud) {
144
- reactiveCrud[key] = isReactive(functions)
145
- ? // @ts-ignore - key is a keyof ObjectCrudFunctions...
146
- toRef(functions, key)
147
- : value;
148
- } else {
149
- throw Error(`Invalid function "${key}" for getObjectCrud: invalid key or not a function.`);
150
- }
151
- }
152
- }
184
+ export const getObjectCrud = (target, options) => {
185
+ assignCrud(target, _defaultCrud, options);
153
186
  };
package/index.js CHANGED
@@ -5,9 +5,9 @@ export * from "./use/cancellableIntent.js";
5
5
  export * from "./use/combineClasses.js";
6
6
  export * from "./use/list.js";
7
7
  export * from "./use/listCalculated.js";
8
- export * from "./use/listKeys.js";
9
8
  export * from "./use/listFilter.js";
10
9
  export * from "./use/listInstance.js";
10
+ export * from "./use/listKeys.js";
11
11
  export * from "./use/listRelated.js";
12
12
  export * from "./use/listSearch.js";
13
13
  export * from "./use/listSort.js";
@@ -23,15 +23,20 @@ export * from "./use/proxyLoadingError.js";
23
23
  export * from "./use/search.js";
24
24
  export * from "./use/watchesRunning.js";
25
25
  export * from "./utils/assignReactiveObject.js";
26
+ export * from "./utils/cancellableFetch.js";
27
+ export * from "./utils/cancellablePromise.js";
26
28
  export * from "./utils/classes.js";
27
29
  export * from "./utils/compact.js";
30
+ export * from "./utils/deepUnref.js";
28
31
  export * from "./utils/deleteKey.js";
29
32
  export * from "./utils/flattenPaths.js";
30
33
  export * from "./utils/getFakePk.js";
31
34
  export * from "./utils/keepAliveTry.js";
32
35
  export * from "./utils/keyDiff.js";
33
36
  export * from "./utils/loadingCombine.js";
37
+ export * from "./utils/refIfReactive.js";
34
38
  export * from "./utils/relatedCalculatedHelpers.js";
35
39
  export * from "./utils/set.js";
36
40
  export * from "./utils/transformWalk.js";
41
+ export * from "./utils/unwrapNested.js";
37
42
  export * from "./utils/watches.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "18.0.2",
3
+ "version": "19.0.0",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -30,7 +30,8 @@
30
30
  "prettier": "npx --no-install prettier --write .",
31
31
  "coverage": "npm test -- run --coverage=true",
32
32
  "prepare": "npx --no-install husky",
33
- "docs": "npx --no-install tsc && node make_type_doc.js"
33
+ "docs": "node make_type_doc.js",
34
+ "docs:check": "node check_type_doc.js"
34
35
  },
35
36
  "repository": {
36
37
  "type": "git",
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Assigns the default CRUD functions to the target object.
3
+ *
4
+ * @param {object} target - The reactive object to assign to.
5
+ * @param {object} defaultCrud - The default CRUD definition (usually created by `createDefaultCrud`).
6
+ * @param {object} [options] - The options object.
7
+ * @param {object} [options.props] - The props object.
8
+ * @param {object} [options.functions] - The functions to assign.
9
+ * @param {Set<string>} [options.validKeys] - The valid keys for the functions.
10
+ */
11
+ export function assignCrud(target: object, defaultCrud: object, { props, functions, validKeys }?: {
12
+ props?: object;
13
+ functions?: object;
14
+ validKeys?: Set<string>;
15
+ }): void;
16
+ export function missingMethod(name: string): (...args: any[]) => import("../utils/cancellablePromise.js").MaybeCancellablePromise<any>;
17
+ export function requiredCancelMissingMethod(name: string): ((..._args: any[]) => import("../utils/cancellablePromise.js").CancellablePromise<void>);
18
+ export function createDefaultCrud(keys: string[], cancellableKeys?: Set<string>): object;
19
+ //# sourceMappingURL=commonCrud.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commonCrud.d.ts","sourceRoot":"","sources":["../../config/commonCrud.js"],"names":[],"mappings":"AA4CA;;;;;;;;;GASG;AACH,mCAPW,MAAM,eACN,MAAM,oCAEd;IAAyB,KAAK,GAAtB,MAAM;IACW,SAAS,GAA1B,MAAM;IACgB,SAAS,GAA/B,GAAG,CAAC,MAAM,CAAC;CACrB,QAqBA;AAhEM,oCAHI,MAAM,GACJ,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,gCAAgC,EAAE,uBAAuB,CAAC,GAAG,CAAC,CAGjB;AAU9E,kDALI,MAAM,GACJ,CACZ,CAAO,GAAG,KAAK,EAAE,GAAG,EAAE,KAAK,OAAO,gCAAgC,EAAE,kBAAkB,CAAC,IAAI,CAAC,CACzF,CAOwG;AAUrG,wCAJI,MAAM,EAAE,oBACR,GAAG,CAAC,MAAM,CAAC,GACT,MAAM,CAQlB"}