feathers-utils 2.0.0-9 → 2.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 (150) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -1
  3. package/dist/index.cjs +918 -0
  4. package/dist/index.d.ts +255 -5
  5. package/dist/index.mjs +892 -0
  6. package/package.json +34 -38
  7. package/src/filters/object.ts +1 -1
  8. package/src/hooks/forEach.ts +47 -0
  9. package/src/hooks/index.ts +3 -0
  10. package/src/hooks/makeSequelizeQuery.ts_ +4 -4
  11. package/src/hooks/parseFields.ts +27 -0
  12. package/src/index.ts +1 -0
  13. package/src/typesInternal.ts +7 -0
  14. package/src/utility-types/index.ts +116 -0
  15. package/src/utils/getItemsIsArray.ts +27 -11
  16. package/dist/esm/filters/array.d.ts +0 -2
  17. package/dist/esm/filters/array.js +0 -17
  18. package/dist/esm/filters/index.d.ts +0 -2
  19. package/dist/esm/filters/index.js +0 -2
  20. package/dist/esm/filters/object.d.ts +0 -2
  21. package/dist/esm/filters/object.js +0 -15
  22. package/dist/esm/hooks/checkMulti.d.ts +0 -5
  23. package/dist/esm/hooks/checkMulti.js +0 -20
  24. package/dist/esm/hooks/createRelated.d.ts +0 -12
  25. package/dist/esm/hooks/createRelated.js +0 -31
  26. package/dist/esm/hooks/index.d.ts +0 -6
  27. package/dist/esm/hooks/index.js +0 -6
  28. package/dist/esm/hooks/onDelete.d.ts +0 -12
  29. package/dist/esm/hooks/onDelete.js +0 -47
  30. package/dist/esm/hooks/removeRelated.d.ts +0 -11
  31. package/dist/esm/hooks/removeRelated.js +0 -37
  32. package/dist/esm/hooks/runPerItem.d.ts +0 -10
  33. package/dist/esm/hooks/runPerItem.js +0 -29
  34. package/dist/esm/hooks/setData.d.ts +0 -11
  35. package/dist/esm/hooks/setData.js +0 -46
  36. package/dist/esm/index.d.ts +0 -5
  37. package/dist/esm/index.js +0 -5
  38. package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +0 -18
  39. package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +0 -46
  40. package/dist/esm/mixins/debounce-mixin/debounceMixin.d.ts +0 -3
  41. package/dist/esm/mixins/debounce-mixin/debounceMixin.js +0 -19
  42. package/dist/esm/mixins/debounce-mixin/index.d.ts +0 -3
  43. package/dist/esm/mixins/debounce-mixin/index.js +0 -3
  44. package/dist/esm/mixins/debounce-mixin/types.d.ts +0 -13
  45. package/dist/esm/mixins/debounce-mixin/types.js +0 -1
  46. package/dist/esm/mixins/index.d.ts +0 -1
  47. package/dist/esm/mixins/index.js +0 -1
  48. package/dist/esm/types.d.ts +0 -3
  49. package/dist/esm/types.js +0 -1
  50. package/dist/esm/typesInternal.d.ts +0 -3
  51. package/dist/esm/typesInternal.js +0 -3
  52. package/dist/esm/utils/filterQuery.d.ts +0 -8
  53. package/dist/esm/utils/filterQuery.js +0 -30
  54. package/dist/esm/utils/getItemsIsArray.d.ts +0 -10
  55. package/dist/esm/utils/getItemsIsArray.js +0 -16
  56. package/dist/esm/utils/getPaginate.d.ts +0 -9
  57. package/dist/esm/utils/getPaginate.js +0 -20
  58. package/dist/esm/utils/index.d.ts +0 -11
  59. package/dist/esm/utils/index.js +0 -11
  60. package/dist/esm/utils/isMulti.d.ts +0 -11
  61. package/dist/esm/utils/isMulti.js +0 -26
  62. package/dist/esm/utils/isPaginated.d.ts +0 -5
  63. package/dist/esm/utils/isPaginated.js +0 -11
  64. package/dist/esm/utils/markHookForSkip.d.ts +0 -7
  65. package/dist/esm/utils/markHookForSkip.js +0 -18
  66. package/dist/esm/utils/mergeQuery/index.d.ts +0 -3
  67. package/dist/esm/utils/mergeQuery/index.js +0 -3
  68. package/dist/esm/utils/mergeQuery/mergeArrays.d.ts +0 -3
  69. package/dist/esm/utils/mergeQuery/mergeArrays.js +0 -37
  70. package/dist/esm/utils/mergeQuery/mergeQuery.d.ts +0 -3
  71. package/dist/esm/utils/mergeQuery/mergeQuery.js +0 -70
  72. package/dist/esm/utils/mergeQuery/types.d.ts +0 -13
  73. package/dist/esm/utils/mergeQuery/types.js +0 -1
  74. package/dist/esm/utils/mergeQuery/utils.d.ts +0 -11
  75. package/dist/esm/utils/mergeQuery/utils.js +0 -272
  76. package/dist/esm/utils/pushSet.d.ts +0 -8
  77. package/dist/esm/utils/pushSet.js +0 -22
  78. package/dist/esm/utils/setResultEmpty.d.ts +0 -5
  79. package/dist/esm/utils/setResultEmpty.js +0 -28
  80. package/dist/esm/utils/shouldSkip.d.ts +0 -8
  81. package/dist/esm/utils/shouldSkip.js +0 -29
  82. package/dist/esm/utils/validateQueryProperty.d.ts +0 -5
  83. package/dist/esm/utils/validateQueryProperty.js +0 -23
  84. package/dist/filters/array.d.ts +0 -2
  85. package/dist/filters/array.js +0 -21
  86. package/dist/filters/index.d.ts +0 -2
  87. package/dist/filters/index.js +0 -18
  88. package/dist/filters/object.d.ts +0 -2
  89. package/dist/filters/object.js +0 -22
  90. package/dist/hooks/checkMulti.d.ts +0 -5
  91. package/dist/hooks/checkMulti.js +0 -24
  92. package/dist/hooks/createRelated.d.ts +0 -12
  93. package/dist/hooks/createRelated.js +0 -44
  94. package/dist/hooks/index.d.ts +0 -6
  95. package/dist/hooks/index.js +0 -22
  96. package/dist/hooks/onDelete.d.ts +0 -12
  97. package/dist/hooks/onDelete.js +0 -60
  98. package/dist/hooks/removeRelated.d.ts +0 -11
  99. package/dist/hooks/removeRelated.js +0 -50
  100. package/dist/hooks/runPerItem.d.ts +0 -10
  101. package/dist/hooks/runPerItem.js +0 -42
  102. package/dist/hooks/setData.d.ts +0 -11
  103. package/dist/hooks/setData.js +0 -54
  104. package/dist/index.js +0 -21
  105. package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +0 -18
  106. package/dist/mixins/debounce-mixin/DebouncedStore.js +0 -65
  107. package/dist/mixins/debounce-mixin/debounceMixin.d.ts +0 -3
  108. package/dist/mixins/debounce-mixin/debounceMixin.js +0 -23
  109. package/dist/mixins/debounce-mixin/index.d.ts +0 -3
  110. package/dist/mixins/debounce-mixin/index.js +0 -19
  111. package/dist/mixins/debounce-mixin/types.d.ts +0 -13
  112. package/dist/mixins/debounce-mixin/types.js +0 -2
  113. package/dist/mixins/index.d.ts +0 -1
  114. package/dist/mixins/index.js +0 -17
  115. package/dist/types.d.ts +0 -3
  116. package/dist/types.js +0 -2
  117. package/dist/typesInternal.d.ts +0 -3
  118. package/dist/typesInternal.js +0 -4
  119. package/dist/utils/filterQuery.d.ts +0 -8
  120. package/dist/utils/filterQuery.js +0 -46
  121. package/dist/utils/getItemsIsArray.d.ts +0 -10
  122. package/dist/utils/getItemsIsArray.js +0 -20
  123. package/dist/utils/getPaginate.d.ts +0 -9
  124. package/dist/utils/getPaginate.js +0 -22
  125. package/dist/utils/index.d.ts +0 -11
  126. package/dist/utils/index.js +0 -27
  127. package/dist/utils/isMulti.d.ts +0 -11
  128. package/dist/utils/isMulti.js +0 -30
  129. package/dist/utils/isPaginated.d.ts +0 -5
  130. package/dist/utils/isPaginated.js +0 -15
  131. package/dist/utils/markHookForSkip.d.ts +0 -7
  132. package/dist/utils/markHookForSkip.js +0 -22
  133. package/dist/utils/mergeQuery/index.d.ts +0 -3
  134. package/dist/utils/mergeQuery/index.js +0 -19
  135. package/dist/utils/mergeQuery/mergeArrays.d.ts +0 -3
  136. package/dist/utils/mergeQuery/mergeArrays.js +0 -41
  137. package/dist/utils/mergeQuery/mergeQuery.d.ts +0 -3
  138. package/dist/utils/mergeQuery/mergeQuery.js +0 -77
  139. package/dist/utils/mergeQuery/types.d.ts +0 -13
  140. package/dist/utils/mergeQuery/types.js +0 -2
  141. package/dist/utils/mergeQuery/utils.d.ts +0 -11
  142. package/dist/utils/mergeQuery/utils.js +0 -287
  143. package/dist/utils/pushSet.d.ts +0 -8
  144. package/dist/utils/pushSet.js +0 -29
  145. package/dist/utils/setResultEmpty.d.ts +0 -5
  146. package/dist/utils/setResultEmpty.js +0 -32
  147. package/dist/utils/shouldSkip.d.ts +0 -8
  148. package/dist/utils/shouldSkip.js +0 -33
  149. package/dist/utils/validateQueryProperty.d.ts +0 -5
  150. package/dist/utils/validateQueryProperty.js +0 -25
@@ -1,10 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- import type { Promisable } from "../typesInternal";
3
- export interface HookRunPerItemOptions {
4
- wait?: boolean;
5
- }
6
- /**
7
- * hook to run a hook for each item in the context
8
- * uses `context.result` if it is existent. otherwise uses context.data
9
- */
10
- export declare const runPerItem: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(actionPerItem: (item: any, context: H) => Promisable<any>, _options?: HookRunPerItemOptions) => (context: H) => Promise<H>;
@@ -1,29 +0,0 @@
1
- import { shouldSkip, getItemsIsArray } from "../utils";
2
- const makeOptions = (options) => {
3
- options = options || {};
4
- return Object.assign({
5
- wait: true,
6
- }, options);
7
- };
8
- /**
9
- * hook to run a hook for each item in the context
10
- * uses `context.result` if it is existent. otherwise uses context.data
11
- */
12
- export const runPerItem = (
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- actionPerItem, _options) => {
15
- const options = makeOptions(_options);
16
- return async (context) => {
17
- if (shouldSkip("runForItems", context)) {
18
- return context;
19
- }
20
- const { items } = getItemsIsArray(context);
21
- const promises = items.map(async (item) => {
22
- await actionPerItem(item, context);
23
- });
24
- if (options.wait) {
25
- await Promise.all(promises);
26
- }
27
- return context;
28
- };
29
- };
@@ -1,11 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- import type { PropertyPath } from "lodash";
3
- import type { PredicateWithContext } from "../types";
4
- export interface HookSetDataOptions {
5
- allowUndefined?: boolean;
6
- overwrite?: boolean | PredicateWithContext;
7
- }
8
- /**
9
- * hook to set properties on `context.result` (if existent) or `context.data` (otherwise)
10
- */
11
- export declare function setData<H extends HookContext = HookContext>(from: PropertyPath, to: PropertyPath, _options?: HookSetDataOptions): (context: H) => H;
@@ -1,46 +0,0 @@
1
- import _get from "lodash/get.js";
2
- import _set from "lodash/set.js";
3
- import _has from "lodash/has.js";
4
- import { Forbidden } from "@feathersjs/errors";
5
- import { getItemsIsArray, shouldSkip } from "../utils";
6
- const defaultOptions = {
7
- allowUndefined: false,
8
- overwrite: true,
9
- };
10
- /**
11
- * hook to set properties on `context.result` (if existent) or `context.data` (otherwise)
12
- */
13
- export function setData(from, to, _options) {
14
- const options = Object.assign({}, defaultOptions, _options);
15
- return (context) => {
16
- if (shouldSkip("setData", context)) {
17
- return context;
18
- }
19
- const { items } = getItemsIsArray(context);
20
- if (!_has(context, from)) {
21
- if (!context.params?.provider || options.allowUndefined === true) {
22
- return context;
23
- }
24
- if (!options.overwrite &&
25
- items.every((item) => _has(item, to))) {
26
- return context;
27
- }
28
- throw new Forbidden(`Expected field ${from.toString()} not available`);
29
- }
30
- const val = _get(context, from);
31
- items.forEach((item) => {
32
- let overwrite;
33
- if (typeof options.overwrite === "function") {
34
- overwrite = options.overwrite(item, context);
35
- }
36
- else {
37
- overwrite = options.overwrite;
38
- }
39
- if (!overwrite && _has(item, to)) {
40
- return;
41
- }
42
- _set(item, to, val);
43
- });
44
- return context;
45
- };
46
- }
@@ -1,5 +0,0 @@
1
- export * from "./hooks";
2
- export * from "./mixins";
3
- export * from "./utils";
4
- export * from "./filters";
5
- export * from "./types";
package/dist/esm/index.js DELETED
@@ -1,5 +0,0 @@
1
- export * from "./hooks";
2
- export * from "./mixins";
3
- export * from "./utils";
4
- export * from "./filters";
5
- export * from "./types";
@@ -1,18 +0,0 @@
1
- import type { DebouncedFunc } from "lodash";
2
- import type { Application, Id } from "@feathersjs/feathers";
3
- import type { DebouncedFunctionApp, DebouncedStoreOptions } from "./types";
4
- export declare const makeDefaultOptions: () => DebouncedStoreOptions;
5
- export type DebouncedService<T = any> = T & {
6
- debouncedStore: DebouncedStore;
7
- };
8
- export declare class DebouncedStore {
9
- private _app;
10
- private _options;
11
- private _isRunningById;
12
- _queueById: Record<string, DebouncedFunc<(id: Id, action: DebouncedFunctionApp) => void | Promise<void>>>;
13
- add: (id: Id, action: (app?: Application) => void | Promise<void>) => void | Promise<void> | undefined;
14
- constructor(app: Application, options?: Partial<DebouncedStoreOptions>);
15
- private unbounced;
16
- private debounceById;
17
- cancel(id: Id): void;
18
- }
@@ -1,46 +0,0 @@
1
- import _debounce from "lodash/debounce.js";
2
- export const makeDefaultOptions = () => {
3
- return {
4
- leading: false,
5
- maxWait: undefined,
6
- trailing: true,
7
- wait: 100,
8
- };
9
- };
10
- export class DebouncedStore {
11
- constructor(app, options) {
12
- this._app = app;
13
- this._options = Object.assign(makeDefaultOptions(), options);
14
- this._queueById = {};
15
- this._isRunningById = {};
16
- //this._waitingById = {};
17
- this.add = this.debounceById(this.unbounced, this._options.wait, {
18
- leading: this._options.leading,
19
- maxWait: this._options.maxWait,
20
- trailing: this._options.trailing,
21
- });
22
- }
23
- async unbounced(id, action) {
24
- if (this._queueById[id] === undefined) {
25
- return;
26
- }
27
- delete this._queueById[id];
28
- this._isRunningById[id] = true;
29
- await action(this._app);
30
- delete this._isRunningById[id];
31
- }
32
- debounceById(func, wait, options) {
33
- return (id, action) => {
34
- if (typeof this._queueById[id] === "function") {
35
- return this._queueById[id](id, action);
36
- }
37
- this._queueById[id] = _debounce((id, action) => {
38
- this.unbounced(id, action);
39
- }, wait, { ...options, leading: false }); // leading required for return promise
40
- return this._queueById[id](id, action);
41
- };
42
- }
43
- cancel(id) {
44
- delete this._queueById[id];
45
- }
46
- }
@@ -1,3 +0,0 @@
1
- import type { Application } from "@feathersjs/feathers/lib";
2
- import type { InitDebounceMixinOptions } from "./types";
3
- export declare function debounceMixin(options?: Partial<InitDebounceMixinOptions>): (app: Application) => void;
@@ -1,19 +0,0 @@
1
- import { DebouncedStore, makeDefaultOptions } from "./DebouncedStore";
2
- export function debounceMixin(options) {
3
- return (app) => {
4
- options = options || {};
5
- const defaultOptions = Object.assign(makeDefaultOptions(), options?.default);
6
- app.mixins.push((service, path) => {
7
- // if path is on blacklist, don't add debouncedStore to service
8
- if (options?.blacklist && options.blacklist.includes(path))
9
- return;
10
- // if service already has registered something on `debouncedStore`
11
- if (service.debouncedStore) {
12
- console.warn(`[feathers-utils] service: '${path}' already has a property 'debouncedStore'. Mixin will skip creating a new debouncedStore`);
13
- return;
14
- }
15
- const serviceOptions = Object.assign({}, defaultOptions, options?.[path]);
16
- service.debouncedStore = new DebouncedStore(app, serviceOptions);
17
- });
18
- };
19
- }
@@ -1,3 +0,0 @@
1
- export * from "./types";
2
- export * from "./DebouncedStore";
3
- export * from "./debounceMixin";
@@ -1,3 +0,0 @@
1
- export * from "./types";
2
- export * from "./DebouncedStore";
3
- export * from "./debounceMixin";
@@ -1,13 +0,0 @@
1
- import type { Application } from "@feathersjs/feathers";
2
- export interface InitDebounceMixinOptions {
3
- default: Partial<DebouncedStoreOptions>;
4
- blacklist: string[];
5
- [key: string]: unknown;
6
- }
7
- export type DebouncedFunctionApp = (app?: Application) => void | Promise<void>;
8
- export interface DebouncedStoreOptions {
9
- leading: boolean;
10
- maxWait: number | undefined;
11
- trailing: boolean;
12
- wait: number;
13
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export * from "./debounce-mixin";
@@ -1 +0,0 @@
1
- export * from "./debounce-mixin";
@@ -1,3 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- export type Predicate<T = any> = (item: T) => boolean;
3
- export type PredicateWithContext<T = any> = (item: T, context: HookContext) => boolean;
package/dist/esm/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- export type MaybeArray<T> = T | T[];
2
- export type Promisable<T> = T | Promise<T>;
3
- export type Path = Array<string | number>;
@@ -1,3 +0,0 @@
1
- // here are types that are not meant to be exported!
2
- // just for internal use of this package
3
- export {};
@@ -1,8 +0,0 @@
1
- import type { FilterQueryOptions as PlainFilterQueryOptions, AdapterBase } from "@feathersjs/adapter-commons";
2
- import type { Query } from "@feathersjs/feathers";
3
- export interface FilterQueryOptions<T> {
4
- service?: AdapterBase<T>;
5
- operators?: PlainFilterQueryOptions["operators"];
6
- filters?: PlainFilterQueryOptions["filters"];
7
- }
8
- export declare function filterQuery<T>(query: Query, _options?: FilterQueryOptions<T>): any;
@@ -1,30 +0,0 @@
1
- import { filterQuery as plainFilterQuery } from "@feathersjs/adapter-commons";
2
- export function filterQuery(query, _options) {
3
- query = query || {};
4
- _options = _options || {};
5
- const { service, ...options } = _options;
6
- if (service) {
7
- const operators = options.operators
8
- ? options.operators
9
- : service.options?.operators;
10
- const filters = options.filters
11
- ? options.filters
12
- : service.options?.filters;
13
- const optionsForFilterQuery = {};
14
- if (operators) {
15
- optionsForFilterQuery.operators = operators;
16
- }
17
- if (filters) {
18
- optionsForFilterQuery.filters = filters;
19
- }
20
- if (service &&
21
- "filterQuery" in service &&
22
- typeof service.filterQuery === "function") {
23
- return service.filterQuery({ query }, optionsForFilterQuery);
24
- }
25
- else {
26
- return plainFilterQuery(query, optionsForFilterQuery);
27
- }
28
- }
29
- return plainFilterQuery(query, options);
30
- }
@@ -1,10 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- export interface GetItemsIsArrayResult<T = any> {
3
- items: T[];
4
- isArray: boolean;
5
- }
6
- /**
7
- * util to get items from context, return it as an array, no matter if it is an array or not
8
- * uses `context.result` if existent. uses `context.data` otherwise
9
- */
10
- export declare const getItemsIsArray: <T = any, H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(context: H) => GetItemsIsArrayResult<T>;
@@ -1,16 +0,0 @@
1
- /**
2
- * util to get items from context, return it as an array, no matter if it is an array or not
3
- * uses `context.result` if existent. uses `context.data` otherwise
4
- */
5
- export const getItemsIsArray = (context) => {
6
- let itemOrItems = context.result !== undefined ? context.result : context.data;
7
- itemOrItems =
8
- itemOrItems && context.method === "find"
9
- ? itemOrItems.data || itemOrItems
10
- : itemOrItems;
11
- const isArray = Array.isArray(itemOrItems);
12
- return {
13
- items: isArray ? itemOrItems : itemOrItems != null ? [itemOrItems] : [],
14
- isArray,
15
- };
16
- };
@@ -1,9 +0,0 @@
1
- import type { PaginationOptions } from "@feathersjs/adapter-commons";
2
- import type { HookContext } from "@feathersjs/feathers";
3
- /**
4
- * util to get paginate options from context
5
- * 1. it uses `context.params.paginate` if it exists
6
- * 2. it uses `service.options.paginate` if it exists
7
- * 3. it uses `context.params.adapter` if it exists
8
- */
9
- export declare const getPaginate: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(context: H) => PaginationOptions | undefined;
@@ -1,20 +0,0 @@
1
- /**
2
- * util to get paginate options from context
3
- * 1. it uses `context.params.paginate` if it exists
4
- * 2. it uses `service.options.paginate` if it exists
5
- * 3. it uses `context.params.adapter` if it exists
6
- */
7
- export const getPaginate = (context) => {
8
- if (Object.prototype.hasOwnProperty.call(context.params, "paginate")) {
9
- return context.params.paginate || undefined;
10
- }
11
- if (context.params.paginate === false) {
12
- return undefined;
13
- }
14
- let options = context.service?.options || {};
15
- options = {
16
- ...options,
17
- ...context.params.adapter,
18
- };
19
- return options.paginate || undefined;
20
- };
@@ -1,11 +0,0 @@
1
- export * from "./mergeQuery";
2
- export * from "./filterQuery";
3
- export * from "./getItemsIsArray";
4
- export * from "./getPaginate";
5
- export * from "./isMulti";
6
- export * from "./isPaginated";
7
- export * from "./markHookForSkip";
8
- export * from "./pushSet";
9
- export * from "./setResultEmpty";
10
- export * from "./shouldSkip";
11
- export * from "./validateQueryProperty";
@@ -1,11 +0,0 @@
1
- export * from "./mergeQuery";
2
- export * from "./filterQuery";
3
- export * from "./getItemsIsArray";
4
- export * from "./getPaginate";
5
- export * from "./isMulti";
6
- export * from "./isPaginated";
7
- export * from "./markHookForSkip";
8
- export * from "./pushSet";
9
- export * from "./setResultEmpty";
10
- export * from "./shouldSkip";
11
- export * from "./validateQueryProperty";
@@ -1,11 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- /**
3
- * util to check if a hook is a multi hook:
4
- * - find: true
5
- * - get: false
6
- * - create: `context.data` is an array
7
- * - update: false
8
- * - patch: `context.id == null`
9
- * - remove: `context.id == null`
10
- */
11
- export declare const isMulti: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(context: H) => boolean;
@@ -1,26 +0,0 @@
1
- /**
2
- * util to check if a hook is a multi hook:
3
- * - find: true
4
- * - get: false
5
- * - create: `context.data` is an array
6
- * - update: false
7
- * - patch: `context.id == null`
8
- * - remove: `context.id == null`
9
- */
10
- export const isMulti = (context) => {
11
- const { method } = context;
12
- if (method === "find") {
13
- return true;
14
- }
15
- else if (["patch", "remove"].includes(method)) {
16
- return context.id == null;
17
- }
18
- else if (method === "create") {
19
- const items = context.type === "before" ? context.data : context.result;
20
- return Array.isArray(items);
21
- }
22
- else if (["get", "update"].includes(method)) {
23
- return false;
24
- }
25
- return false;
26
- };
@@ -1,5 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- /**
3
- * util to check if a hook is a paginated hook using `getPaginate`
4
- */
5
- export declare const isPaginated: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any> = HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(context: H) => boolean;
@@ -1,11 +0,0 @@
1
- import { getPaginate } from "./getPaginate";
2
- /**
3
- * util to check if a hook is a paginated hook using `getPaginate`
4
- */
5
- export const isPaginated = (context) => {
6
- if (context.params.paginate === false || context.method !== "find") {
7
- return false;
8
- }
9
- const paginate = getPaginate(context);
10
- return !!paginate;
11
- };
@@ -1,7 +0,0 @@
1
- import type { HookContext } from "@feathersjs/feathers";
2
- import type { HookType } from "feathers-hooks-common";
3
- import type { MaybeArray } from "../typesInternal";
4
- /**
5
- * util to mark a hook for skip, has to be used with `shouldSkip`
6
- */
7
- export declare function markHookForSkip<H extends HookContext = HookContext>(hookName: string, type: "all" | MaybeArray<HookType>, context?: H): H | undefined;
@@ -1,18 +0,0 @@
1
- import { pushSet } from "./pushSet";
2
- /**
3
- * util to mark a hook for skip, has to be used with `shouldSkip`
4
- */
5
- export function markHookForSkip(hookName, type, context) {
6
- // @ts-expect-error context is not of type 'H'
7
- context = context || {};
8
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
9
- const params = context.params || {};
10
- const types = Array.isArray(type) ? type : [type];
11
- types.forEach((t) => {
12
- const combinedName = t === "all" ? hookName : `${type}:${hookName}`;
13
- pushSet(params, ["skipHooks"], combinedName, { unique: true });
14
- });
15
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16
- context.params = params;
17
- return context;
18
- }
@@ -1,3 +0,0 @@
1
- export * from "./mergeArrays";
2
- export * from "./mergeQuery";
3
- export * from "./types";
@@ -1,3 +0,0 @@
1
- export * from "./mergeArrays";
2
- export * from "./mergeQuery";
3
- export * from "./types";
@@ -1,3 +0,0 @@
1
- import type { Path } from "../../typesInternal";
2
- import type { Handle, ActionOnEmptyIntersect } from "./types";
3
- export declare function mergeArrays<T>(targetArr: T[], sourceArr: T[], handle: Handle, prependKey?: Path, actionOnEmptyIntersect?: ActionOnEmptyIntersect): T[] | undefined;
@@ -1,37 +0,0 @@
1
- export function mergeArrays(targetArr, sourceArr, handle, prependKey, actionOnEmptyIntersect) {
2
- if (!sourceArr && !targetArr) {
3
- return;
4
- }
5
- if (handle === "target") {
6
- return targetArr;
7
- }
8
- else if (handle === "source") {
9
- return sourceArr;
10
- }
11
- else if (handle === "combine") {
12
- if (!sourceArr || !Array.isArray(sourceArr)) {
13
- return targetArr;
14
- }
15
- if (!targetArr || !Array.isArray(targetArr)) {
16
- return sourceArr;
17
- }
18
- const arr = targetArr.concat(sourceArr);
19
- return [...new Set(arr)];
20
- }
21
- else if (handle === "intersect" || handle === "intersectOrFull") {
22
- const targetIsArray = !targetArr || !Array.isArray(targetArr);
23
- const sourceIsArray = !sourceArr || !Array.isArray(sourceArr);
24
- if ((targetIsArray || sourceIsArray) && handle === "intersect") {
25
- if (actionOnEmptyIntersect) {
26
- actionOnEmptyIntersect(targetArr, sourceArr, prependKey || []);
27
- }
28
- return;
29
- }
30
- if (handle === "intersectOrFull") {
31
- const val = !targetIsArray ? targetArr : sourceArr;
32
- return val;
33
- }
34
- return targetArr.filter((val) => sourceArr.includes(val));
35
- }
36
- return undefined;
37
- }
@@ -1,3 +0,0 @@
1
- import type { Query } from "@feathersjs/feathers";
2
- import type { MergeQueryOptions } from "./types";
3
- export declare function mergeQuery<T>(target: Query, source: Query, options?: Partial<MergeQueryOptions<T>>): Query;
@@ -1,70 +0,0 @@
1
- import _merge from "lodash/merge.js";
2
- import _isEmpty from "lodash/isEmpty.js";
3
- import { handleArray, handleCircular, makeDefaultOptions, moveProperty, } from "./utils";
4
- import { filterQuery } from "../filterQuery";
5
- export function mergeQuery(target, source, options) {
6
- const fullOptions = makeDefaultOptions(options);
7
- const { filters: targetFilters, query: targetQuery } = filterQuery(target, {
8
- operators: fullOptions.operators,
9
- filters: fullOptions.filters,
10
- service: fullOptions.service,
11
- });
12
- moveProperty(targetFilters, targetQuery, "$or");
13
- moveProperty(targetFilters, targetQuery, "$and");
14
- if (target.$limit) {
15
- targetFilters.$limit = target.$limit;
16
- }
17
- let {
18
- // eslint-disable-next-line prefer-const
19
- filters: sourceFilters, query: sourceQuery, } = filterQuery(source, {
20
- operators: fullOptions.operators,
21
- filters: fullOptions.filters,
22
- service: fullOptions.service,
23
- });
24
- moveProperty(sourceFilters, sourceQuery, "$or");
25
- moveProperty(sourceFilters, sourceQuery, "$and");
26
- if (source.$limit) {
27
- sourceFilters.$limit = source.$limit;
28
- }
29
- //#region filters
30
- if (target &&
31
- !Object.prototype.hasOwnProperty.call(target, "$limit") &&
32
- Object.prototype.hasOwnProperty.call(targetFilters, "$limit")) {
33
- delete targetFilters.$limit;
34
- }
35
- if (source &&
36
- !Object.prototype.hasOwnProperty.call(source, "$limit") &&
37
- Object.prototype.hasOwnProperty.call(sourceFilters, "$limit")) {
38
- delete sourceFilters.$limit;
39
- }
40
- handleArray(targetFilters, sourceFilters, ["$select"], fullOptions);
41
- // remaining filters
42
- delete sourceFilters["$select"];
43
- _merge(targetFilters, sourceFilters);
44
- //#endregion
45
- //#region '$or' / '$and'
46
- if (options?.useLogicalConjunction &&
47
- (options.defaultHandle === "combine" ||
48
- options.defaultHandle === "intersect") &&
49
- !_isEmpty(targetQuery)) {
50
- const logicalOp = options.defaultHandle === "combine" ? "$or" : "$and";
51
- if (Object.prototype.hasOwnProperty.call(sourceQuery, logicalOp)) {
52
- // omit '$or'/'$and' and put all other props into '$or'/'$and'
53
- const andOr = sourceQuery[logicalOp];
54
- delete sourceQuery[logicalOp];
55
- andOr.push(sourceQuery);
56
- sourceQuery = { [logicalOp]: andOr };
57
- }
58
- else {
59
- sourceQuery = { [logicalOp]: [sourceQuery] };
60
- }
61
- }
62
- //#endregion
63
- const keys = Object.keys(sourceQuery);
64
- for (let i = 0, n = keys.length; i < n; i++) {
65
- const key = keys[i];
66
- handleCircular(targetQuery, sourceQuery, [key], fullOptions);
67
- }
68
- const result = Object.assign({}, targetFilters, targetQuery);
69
- return result;
70
- }
@@ -1,13 +0,0 @@
1
- import type { Path } from "../../typesInternal";
2
- import type { FilterQueryOptions } from "../filterQuery";
3
- export type Handle = "target" | "source" | "combine" | "intersect" | "intersectOrFull";
4
- export type FirstLast = "first" | "last";
5
- export type ActionOnEmptyIntersect = (target: unknown, source: unknown, prependKey: Path) => void;
6
- export interface MergeQueryOptions<T> extends FilterQueryOptions<T> {
7
- defaultHandle: Handle;
8
- actionOnEmptyIntersect: ActionOnEmptyIntersect;
9
- useLogicalConjunction: boolean;
10
- handle?: {
11
- [key: string]: Handle;
12
- };
13
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,11 +0,0 @@
1
- import type { Path } from "../../typesInternal";
2
- import type { MergeQueryOptions } from "./types";
3
- export declare const hasOwnProperty: (obj: Record<string, unknown>, key: string) => boolean;
4
- export declare function handleArray<T>(target: Record<string, unknown>, source: Record<string, unknown>, key: Path, options: MergeQueryOptions<T>): void;
5
- export declare function handleCircular<T>(target: Record<string, unknown>, source: Record<string, unknown>, prependKey: Path, options: MergeQueryOptions<T>): void;
6
- export declare function makeDefaultOptions<T>(options?: Partial<MergeQueryOptions<T>>): MergeQueryOptions<T>;
7
- export declare function moveProperty(source: Record<string, any>, target: Record<string, any>, key: string): void;
8
- export declare function getParentProp(target: Record<string, unknown>, path: Path): any;
9
- export declare function cleanOr(target: Record<string, unknown>[]): Record<string, unknown>[] | undefined;
10
- export declare function cleanAnd(target: Record<string, unknown>[]): Record<string, unknown>[] | undefined;
11
- export declare function arrayWithoutDuplicates<T>(target: T[]): T[];