feathers-utils 10.0.0-rc.8 → 10.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.
- package/dist/guards.mjs +1 -1
- package/dist/guards.mjs.map +1 -1
- package/dist/hook-context-aHkq1h0N.d.mts +51 -0
- package/dist/{traverse.hook-BLSSwn5l.mjs → hooks-DPFxxjBu.mjs} +190 -84
- package/dist/hooks-DPFxxjBu.mjs.map +1 -0
- package/dist/hooks.d.mts +116 -23
- package/dist/hooks.mjs +5 -8
- package/dist/{zip-data-result.util-DiwjF6qd.d.mts → index-CNrzxmWo.d.mts} +95 -10
- package/dist/index.d.mts +19 -9
- package/dist/index.mjs +8 -11
- package/dist/index.mjs.map +1 -1
- package/dist/{hook-context-Dfdh0e8q.d.mts → infer-service-methods-D7zRVQeJ.d.mts} +4 -33
- package/dist/{internal.utils-EIZPRZzm.mjs → internal.utils-BWQ25nOd.mjs} +2 -5
- package/dist/internal.utils-BWQ25nOd.mjs.map +1 -0
- package/dist/{mutate-result.util-Dpxp56Bo.d.mts → mutate-result.util-BIeYlqqT.d.mts} +6 -14
- package/dist/{mutate-result.util-BxXb39DP.mjs → mutate-result.util-CCBWix-G.mjs} +16 -15
- package/dist/mutate-result.util-CCBWix-G.mjs.map +1 -0
- package/dist/{throw-if.hook-DRNssJBV.mjs → predicates-CT08opkD.mjs} +69 -78
- package/dist/predicates-CT08opkD.mjs.map +1 -0
- package/dist/predicates.d.mts +18 -42
- package/dist/predicates.mjs +2 -4
- package/dist/resolve-CA7-EaTw.d.mts +118 -0
- package/dist/resolve-Cx9osy8O.mjs +215 -0
- package/dist/resolve-Cx9osy8O.mjs.map +1 -0
- package/dist/resolvers.d.mts +71 -50
- package/dist/resolvers.mjs +95 -112
- package/dist/resolvers.mjs.map +1 -1
- package/dist/{transform-result.hook-BFqIEHJJ.d.mts → transform-result.hook-BUwLePT2.d.mts} +8 -6
- package/dist/{transform-result.hook-DM0S5rkk.mjs → transform-result.hook-CevWK5TA.mjs} +5 -8
- package/dist/transform-result.hook-CevWK5TA.mjs.map +1 -0
- package/dist/transformers.d.mts +41 -53
- package/dist/transformers.mjs +80 -105
- package/dist/transformers.mjs.map +1 -1
- package/dist/{unless.hook-CWm7A-7q.d.mts → unless.hook-BYWO9hzO.d.mts} +6 -6
- package/dist/{zip-data-result.util-BSKwCFF9.mjs → utils-1I_iPZkV.mjs} +74 -42
- package/dist/utils-1I_iPZkV.mjs.map +1 -0
- package/dist/utils.d.mts +3 -5
- package/dist/utils.mjs +4 -6
- package/package.json +45 -31
- package/src/hooks/cache/cache-utils.ts +7 -0
- package/src/hooks/cache/cache.hook.ts +50 -2
- package/src/hooks/check-multi/check-multi.hook.ts +10 -12
- package/src/hooks/check-required/check-required.hook.ts +5 -6
- package/src/hooks/create-related/create-related.hook.ts +5 -1
- package/src/hooks/disable-pagination/disable-pagination.hook.ts +5 -1
- package/src/hooks/disallow/disallow.hook.ts +1 -1
- package/src/hooks/iff/iff.hook.ts +5 -5
- package/src/hooks/iff-else/iff-else.hook.ts +20 -9
- package/src/hooks/index.ts +7 -1
- package/src/hooks/on-delete/on-delete.hook.ts +5 -1
- package/src/hooks/params-from-client/params-from-client.hook.ts +1 -0
- package/src/hooks/prevent-changes/prevent-changes.hook.ts +3 -3
- package/src/hooks/rate-limit/rate-limit.hook.ts +58 -0
- package/src/hooks/set-data/set-data.hook.ts +2 -0
- package/src/hooks/set-field/set-field.hook.ts +2 -1
- package/src/hooks/set-result/set-result.hook.ts +1 -1
- package/src/hooks/skippable/skippable.hook.ts +1 -0
- package/src/hooks/soft-delete/soft-delete.hook.ts +1 -1
- package/src/hooks/stashable/stashable.hook.ts +73 -0
- package/src/hooks/transform-data/transform-data.hook.ts +10 -7
- package/src/hooks/transform-result/transform-result.hook.ts +16 -9
- package/src/internal.utils.ts +5 -0
- package/src/predicates/and/and.predicate.ts +57 -0
- package/src/predicates/index.ts +2 -2
- package/src/predicates/is-context/is-context.predicate.ts +20 -27
- package/src/predicates/or/or.predicate.ts +60 -0
- package/src/resolvers/from-predicate/from-predicate.ts +30 -0
- package/src/resolvers/helpers/defaults/defaults.ts +35 -0
- package/src/resolvers/helpers/lowercase/lowercase.ts +29 -0
- package/src/resolvers/helpers/omit/omit.ts +29 -0
- package/src/resolvers/helpers/set-now/set-now.ts +27 -0
- package/src/resolvers/helpers/trim/trim.ts +29 -0
- package/src/resolvers/index.ts +14 -1
- package/src/resolvers/{hooks → resolve}/resolve.ts +33 -13
- package/src/resolvers/resolve-data/resolve-data.ts +56 -0
- package/src/resolvers/resolve-query/resolve-query.ts +57 -0
- package/src/resolvers/resolve-result/resolve-result.ts +57 -0
- package/src/resolvers/resolver-condition.ts +6 -0
- package/src/resolvers/resolvers.internal.ts +130 -0
- package/src/transformers/defaults/defaults.transformer.ts +32 -0
- package/src/transformers/index.ts +1 -0
- package/src/transformers/lowercase/lowercase.transformer.ts +17 -20
- package/src/transformers/omit/omit.transformer.ts +8 -13
- package/src/transformers/parse-date/parse-date.transformer.ts +12 -17
- package/src/transformers/pick/pick.transformer.ts +8 -13
- package/src/transformers/set-now/set-now.transformer.ts +11 -17
- package/src/transformers/trim/trim.transformer.ts +17 -20
- package/src/types.ts +23 -0
- package/src/utils/check-context/check-context.util.ts +89 -8
- package/src/utils/chunk-find/chunk-find.util.ts +3 -1
- package/src/utils/index.ts +1 -0
- package/src/utils/iterate-find/iterate-find.util.ts +3 -1
- package/src/utils/mutate-data/mutate-data.util.ts +5 -6
- package/src/utils/mutate-result/mutate-result.util.ts +25 -18
- package/src/utils/sort-query-properties/sort-query-properties.util.ts +59 -0
- package/dist/internal.utils-BNM-vPgB.d.mts +0 -12
- package/dist/internal.utils-EIZPRZzm.mjs.map +0 -1
- package/dist/mutate-result.util-BxXb39DP.mjs.map +0 -1
- package/dist/throw-if.hook-DRNssJBV.mjs.map +0 -1
- package/dist/transform-result.hook-DM0S5rkk.mjs.map +0 -1
- package/dist/traverse.hook-BLSSwn5l.mjs.map +0 -1
- package/dist/zip-data-result.util-BSKwCFF9.mjs.map +0 -1
- package/src/.DS_Store +0 -0
- package/src/guards/has-query/has-query.guard.md +0 -5
- package/src/hooks/.DS_Store +0 -0
- package/src/hooks/cache/cache.hook.md +0 -8
- package/src/hooks/cache/cache.hook.test.ts +0 -304
- package/src/hooks/check-multi/check-multi.hook.md +0 -9
- package/src/hooks/check-multi/check-multi.hook.test.ts +0 -173
- package/src/hooks/check-required/check-required.hook.md +0 -8
- package/src/hooks/check-required/check-required.hook.test.ts +0 -41
- package/src/hooks/create-related/create-related.hook.md +0 -8
- package/src/hooks/create-related/create-related.hook.test.ts +0 -405
- package/src/hooks/debug/debug.hook.md +0 -8
- package/src/hooks/debug/debug.hook.test.ts +0 -25
- package/src/hooks/disable-pagination/disable-pagination.hook.md +0 -8
- package/src/hooks/disable-pagination/disable-pagination.hook.test.ts +0 -61
- package/src/hooks/disallow/disallow.hook.md +0 -9
- package/src/hooks/disallow/disallow.hook.test.ts +0 -159
- package/src/hooks/iff/iff.hook.md +0 -16
- package/src/hooks/iff/iff.hook.test.ts +0 -469
- package/src/hooks/iff-else/iff-else.hook.md +0 -16
- package/src/hooks/iff-else/iff-else.hook.test.ts +0 -219
- package/src/hooks/on-delete/on-delete.hook.md +0 -32
- package/src/hooks/on-delete/on-delete.hook.test.ts +0 -917
- package/src/hooks/params-for-server/params-for-server.hook.md +0 -9
- package/src/hooks/params-for-server/params-for-server.hook.test.ts +0 -68
- package/src/hooks/params-from-client/params-from-client.hook.md +0 -9
- package/src/hooks/params-from-client/params-from-client.hook.test.ts +0 -54
- package/src/hooks/prevent-changes/prevent-changes.hook.md +0 -8
- package/src/hooks/prevent-changes/prevent-changes.hook.test.ts +0 -206
- package/src/hooks/set-data/set-data.hook.md +0 -8
- package/src/hooks/set-data/set-data.hook.test.ts +0 -390
- package/src/hooks/set-field/set-field.hook.md +0 -8
- package/src/hooks/set-field/set-field.hook.test.ts +0 -94
- package/src/hooks/set-result/set-result.hook.md +0 -8
- package/src/hooks/set-result/set-result.hook.test.ts +0 -417
- package/src/hooks/set-slug/set-slug.hook.md +0 -8
- package/src/hooks/set-slug/set-slug.hook.test.ts +0 -57
- package/src/hooks/skippable/skippable.hook.md +0 -18
- package/src/hooks/skippable/skippable.hook.test.ts +0 -124
- package/src/hooks/soft-delete/soft-delete.hook.md +0 -8
- package/src/hooks/soft-delete/soft-delete.hook.test.ts +0 -213
- package/src/hooks/stash-before/stash-before.hook.md +0 -8
- package/src/hooks/stash-before/stash-before.hook.test.ts +0 -124
- package/src/hooks/stash-before/stash-before.hook.ts +0 -65
- package/src/hooks/throw-if/throw-if.hook.md +0 -9
- package/src/hooks/throw-if/throw-if.hook.test.ts +0 -27
- package/src/hooks/throw-if-is-multi/throw-if-is-multi.hook.md +0 -9
- package/src/hooks/throw-if-is-multi/throw-if-is-multi.hook.test.ts +0 -148
- package/src/hooks/throw-if-is-provider/throw-if-is-provider.hook.md +0 -9
- package/src/hooks/throw-if-is-provider/throw-if-is-provider.hook.test.ts +0 -39
- package/src/hooks/transform-data/transform-data.hook.md +0 -47
- package/src/hooks/transform-data/transform-data.hook.test.ts +0 -117
- package/src/hooks/transform-query/transform-query.hook.md +0 -18
- package/src/hooks/transform-query/transform-query.hook.test.ts +0 -30
- package/src/hooks/transform-result/transform-result.hook.md +0 -39
- package/src/hooks/transform-result/transform-result.hook.test.ts +0 -254
- package/src/hooks/traverse/traverse.hook.md +0 -8
- package/src/hooks/traverse/traverse.hook.test.ts +0 -128
- package/src/hooks/unless/unless.hook.md +0 -10
- package/src/hooks/unless/unless.hook.test.ts +0 -450
- package/src/predicates/.DS_Store +0 -0
- package/src/predicates/every/every.predicate.md +0 -5
- package/src/predicates/every/every.predicate.test.ts +0 -186
- package/src/predicates/every/every.predicate.ts +0 -66
- package/src/predicates/is-context/is-context.predicate.md +0 -4
- package/src/predicates/is-context/is-context.predicate.test.ts +0 -64
- package/src/predicates/is-multi/is-multi.predicate.md +0 -4
- package/src/predicates/is-multi/is-multi.predicate.test.ts +0 -57
- package/src/predicates/is-paginated/is-paginated.predicate.md +0 -5
- package/src/predicates/is-paginated/is-paginated.predicate.test.ts +0 -72
- package/src/predicates/is-provider/is-provider.predicate.md +0 -4
- package/src/predicates/is-provider/is-provider.predicate.test.ts +0 -138
- package/src/predicates/not/not.predicate.md +0 -5
- package/src/predicates/not/not.predicate.test.ts +0 -124
- package/src/predicates/should-skip/should-skip.predicate.md +0 -5
- package/src/predicates/should-skip/should-skip.predicate.test.ts +0 -121
- package/src/predicates/some/some.predicate.md +0 -5
- package/src/predicates/some/some.predicate.test.ts +0 -66
- package/src/predicates/some/some.predicate.ts +0 -69
- package/src/resolvers/hooks/index.ts +0 -4
- package/src/resolvers/hooks/resolve-data.test.ts +0 -129
- package/src/resolvers/hooks/resolve-data.ts +0 -34
- package/src/resolvers/hooks/resolve-query.ts +0 -34
- package/src/resolvers/hooks/resolve-result.test.ts +0 -206
- package/src/resolvers/hooks/resolve-result.ts +0 -36
- package/src/resolvers/hooks/resolvers.internal.ts +0 -118
- package/src/transformers/lowercase/lowercase.transformer.md +0 -4
- package/src/transformers/lowercase/lowercase.transformer.test.ts +0 -42
- package/src/transformers/omit/omit.transformer.md +0 -4
- package/src/transformers/omit/omit.transformer.test.ts +0 -41
- package/src/transformers/parse-date/parse-date.transformer.md +0 -4
- package/src/transformers/parse-date/parse-date.transformer.test.ts +0 -43
- package/src/transformers/pick/pick.transformer.md +0 -4
- package/src/transformers/pick/pick.transformer.test.ts +0 -29
- package/src/transformers/set-now/set-now.transformer.md +0 -4
- package/src/transformers/set-now/set-now.transformer.test.ts +0 -30
- package/src/transformers/trim/trim.transformer.md +0 -4
- package/src/transformers/trim/trim.transformer.test.ts +0 -42
- package/src/utils/.DS_Store +0 -0
- package/src/utils/add-skip/add-skip.util.md +0 -5
- package/src/utils/add-skip/add-skip.util.test.ts +0 -29
- package/src/utils/add-to-query/add-to-query.util.md +0 -4
- package/src/utils/add-to-query/add-to-query.util.test.ts +0 -72
- package/src/utils/check-context/check-context.util.md +0 -4
- package/src/utils/check-context/check-context.util.test.ts +0 -107
- package/src/utils/chunk-find/chunk-find.util.md +0 -5
- package/src/utils/chunk-find/chunk-find.util.test.ts +0 -118
- package/src/utils/combine/combine.util.md +0 -8
- package/src/utils/combine/combine.util.test.ts +0 -158
- package/src/utils/context-to-json/context-to-json.util.md +0 -4
- package/src/utils/define-hooks/define-hooks.util.md +0 -27
- package/src/utils/define-hooks/define-hooks.util.test.ts +0 -27
- package/src/utils/get-data-is-array/get-data-is-array.util.md +0 -4
- package/src/utils/get-data-is-array/get-data-is-array.util.test.ts +0 -63
- package/src/utils/get-exposed-methods/get-exposed-methods.util.md +0 -4
- package/src/utils/get-exposed-methods/get-exposed-methods.util.test.ts +0 -47
- package/src/utils/get-paginate/get-paginate.util.md +0 -4
- package/src/utils/get-paginate/get-paginate.util.test.ts +0 -73
- package/src/utils/get-result-is-array/get-result-is-array.util.md +0 -4
- package/src/utils/get-result-is-array/get-result-is-array.util.test.ts +0 -187
- package/src/utils/iterate-find/iterate-find.util.md +0 -5
- package/src/utils/iterate-find/iterate-find.util.test.ts +0 -122
- package/src/utils/mutate-data/mutate-data.util.md +0 -6
- package/src/utils/mutate-result/mutate-result.util.md +0 -6
- package/src/utils/mutate-result/mutate-result.util.test.ts +0 -175
- package/src/utils/patch-batch/patch-batch.util.md +0 -4
- package/src/utils/patch-batch/patch-batch.util.test.ts +0 -41
- package/src/utils/skip-result/skip-result.util.md +0 -4
- package/src/utils/skip-result/skip-result.util.test.ts +0 -262
- package/src/utils/to-paginated/to-paginated.util.md +0 -4
- package/src/utils/to-paginated/to-paginated.util.test.ts +0 -27
- package/src/utils/transform-params/transform-params.util.md +0 -4
- package/src/utils/walk-query/walk-query.util.md +0 -4
- package/src/utils/walk-query/walk-query.util.test.ts +0 -141
- package/src/utils/zip-data-result/zip-data-result.util.md +0 -4
- package/src/utils/zip-data-result/zip-data-result.util.test.ts +0 -180
package/dist/guards.mjs
CHANGED
package/dist/guards.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.mjs","names":[],"sources":["../src/guards/has-query/has-query.guard.ts"],"sourcesContent":["import type { Params } from '@feathersjs/feathers'\nimport type { RequiredQuery } from '../../utility-types/required-query.js'\n\n/**\n * Type guard to check if the `query` property of `Params` is present and non-nullable.\n *\n * @param params - The `Params` object to check.\n * @returns `true` if `params.query` is present and non-nullable, otherwise `false`.\n *\n * @see https://utils.feathersjs.com/guards/has-query.html\n *\n * @example\n * ```ts\n * import { hasQuery } from 'feathers-utils/guards'\n *\n * function example(params: Params) {\n * // `params.query` is optional and can be undefined at this point\n * if (!hasQuery(params)) {\n * return;\n * }\n *\n * // TypeScript now knows that params.query is present and non-nullable\n * // You can safely access params.query here without additional checks\n * }\n * ```\n */\nexport function hasQuery<P extends Params>(\n params: P,\n): params is RequiredQuery<P> {\n return !!params.query\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,SACd,QAC4B;
|
|
1
|
+
{"version":3,"file":"guards.mjs","names":[],"sources":["../src/guards/has-query/has-query.guard.ts"],"sourcesContent":["import type { Params } from '@feathersjs/feathers'\nimport type { RequiredQuery } from '../../utility-types/required-query.js'\n\n/**\n * Type guard to check if the `query` property of `Params` is present and non-nullable.\n *\n * @param params - The `Params` object to check.\n * @returns `true` if `params.query` is present and non-nullable, otherwise `false`.\n *\n * @see https://utils.feathersjs.com/guards/has-query.html\n *\n * @example\n * ```ts\n * import { hasQuery } from 'feathers-utils/guards'\n *\n * function example(params: Params) {\n * // `params.query` is optional and can be undefined at this point\n * if (!hasQuery(params)) {\n * return;\n * }\n *\n * // TypeScript now knows that params.query is present and non-nullable\n * // You can safely access params.query here without additional checks\n * }\n * ```\n */\nexport function hasQuery<P extends Params>(\n params: P,\n): params is RequiredQuery<P> {\n return !!params.query\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,SACd,QAC4B;CAC5B,OAAO,CAAC,CAAC,OAAO"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { HookContext, NextFunction, Paginated, Params } from "@feathersjs/feathers";
|
|
2
|
+
|
|
3
|
+
//#region src/internal.utils.d.ts
|
|
4
|
+
type MaybeArray<T> = T | readonly T[];
|
|
5
|
+
type UnpackMaybeArray<T> = T extends readonly (infer E)[] ? E : T;
|
|
6
|
+
type Promisable<T> = T | Promise<T>;
|
|
7
|
+
type KeyOf<T> = Extract<keyof T, string>;
|
|
8
|
+
type UnwrapArray<T> = T extends Array<infer U> ? U : T;
|
|
9
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
10
|
+
type AnyFallback<T, Fallback> = IsAny<T> extends true ? Fallback : T;
|
|
11
|
+
type NeverFallback<Never, Fallback> = [Never] extends [never] ? Fallback : Never;
|
|
12
|
+
type KeyOfOrDotNotation<D> = KeyOf<D> | `${KeyOf<D>}.${string}`;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/types.d.ts
|
|
15
|
+
declare const hookTypes: readonly ["around", "before", "after", "error"];
|
|
16
|
+
type HookType$1 = (typeof hookTypes)[number];
|
|
17
|
+
declare const methodNames: readonly ["find", "get", "create", "update", "patch", "remove"];
|
|
18
|
+
type MethodName = (typeof methodNames)[number] | ({} & string);
|
|
19
|
+
type TransportName = 'socketio' | 'rest' | 'external' | 'server';
|
|
20
|
+
type ContextFunctionSync<T, H extends HookContext = HookContext> = (context: H) => T;
|
|
21
|
+
type ContextFunctionAsync<T, H extends HookContext = HookContext> = (context: H) => Promise<T>;
|
|
22
|
+
type ContextFunction<T, H extends HookContext = HookContext> = (context: H) => T | Promise<T>;
|
|
23
|
+
type PredicateContextSync<H extends HookContext = HookContext> = (context: H) => boolean;
|
|
24
|
+
type PredicateContextAsync<H extends HookContext = HookContext> = (context: H) => Promise<boolean>;
|
|
25
|
+
type PredicateFn<H extends HookContext = HookContext> = (context: H) => boolean | Promise<boolean>;
|
|
26
|
+
type PredicateItemWithContext<T = any> = (item: T, context: HookContext) => boolean;
|
|
27
|
+
type TransformerFn<T = Record<string, any>, H extends HookContext = HookContext> = (item: T, options: {
|
|
28
|
+
context: H;
|
|
29
|
+
i: number;
|
|
30
|
+
}) => Promisable<T | undefined | void>;
|
|
31
|
+
type TransformerInputFn<T = Record<string, any>, H extends HookContext = HookContext> = (item: T, options: {
|
|
32
|
+
context: H;
|
|
33
|
+
i: number;
|
|
34
|
+
}) => Promisable<any>;
|
|
35
|
+
type FieldKey<T> = (keyof T & string) | `${Extract<keyof T, string>}.${string}`;
|
|
36
|
+
type StringFieldKey<T> = { [K in keyof T & string]: T[K] extends string | null | undefined ? K : never }[keyof T & string] | `${Extract<keyof T, string>}.${string}`;
|
|
37
|
+
type DefaultsInput<T extends Record<string, any>> = { [K in keyof T & string]?: T[K] | (() => T[K]) } & { [K in `${Extract<keyof T, string>}.${string}`]?: unknown };
|
|
38
|
+
declare type HookFunction<H extends HookContext = HookContext> = (context: H, next?: NextFunction) => Promise<H | void> | H | void;
|
|
39
|
+
type TransformParamsFn<P extends Params = Params> = (params: P) => P | void;
|
|
40
|
+
type DispatchOption = boolean | 'both';
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/utility-types/unwrap-paginated.d.ts
|
|
43
|
+
type UnwrapPaginated<R> = R extends Paginated<infer D> ? D : R;
|
|
44
|
+
type UnwrapPaginatedOrArray<T> = T extends Paginated<infer D> ? D : T extends any[] ? T[number] : T;
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/utility-types/hook-context.d.ts
|
|
47
|
+
type ResultSingleHookContext<H extends HookContext> = UnwrapPaginatedOrArray<NonNullable<H['result']>>;
|
|
48
|
+
type DataSingleHookContext<H extends HookContext> = UnwrapArray<NonNullable<H['data']>>;
|
|
49
|
+
//#endregion
|
|
50
|
+
export { Promisable as A, hookTypes as C, KeyOfOrDotNotation as D, KeyOf as E, UnwrapArray as M, MaybeArray as O, TransportName as S, AnyFallback as T, PredicateItemWithContext as _, ContextFunction as a, TransformerFn as b, DefaultsInput as c, HookFunction as d, HookType$1 as f, PredicateFn as g, PredicateContextSync as h, UnwrapPaginatedOrArray as i, UnpackMaybeArray as j, NeverFallback as k, DispatchOption as l, PredicateContextAsync as m, ResultSingleHookContext as n, ContextFunctionAsync as o, MethodName as p, UnwrapPaginated as r, ContextFunctionSync as s, DataSingleHookContext as t, FieldKey as u, StringFieldKey as v, methodNames as w, TransformerInputFn as x, TransformParamsFn as y };
|
|
51
|
+
//# sourceMappingURL=hook-context-aHkq1h0N.d.mts.map
|
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { i as getDataIsArray, r as getResultIsArray } from "./mutate-result.util-
|
|
5
|
-
import { r as transformData } from "./transform-result.hook-
|
|
1
|
+
import { g as every, p as isMulti, t as throwIf, u as isProvider } from "./predicates-CT08opkD.mjs";
|
|
2
|
+
import { d as checkContext, f as addToQuery, h as sortQueryProperties, r as transformParams, u as contextToJson } from "./utils-1I_iPZkV.mjs";
|
|
3
|
+
import { a as traverse$1, i as isPromise, r as toArray, t as early } from "./internal.utils-BWQ25nOd.mjs";
|
|
4
|
+
import { i as getDataIsArray, r as getResultIsArray } from "./mutate-result.util-CCBWix-G.mjs";
|
|
5
|
+
import { r as transformData } from "./transform-result.hook-CevWK5TA.mjs";
|
|
6
|
+
import "./resolve-Cx9osy8O.mjs";
|
|
6
7
|
import isObject from "lodash/isObject.js";
|
|
7
8
|
import { copy } from "fast-copy";
|
|
8
|
-
import { BadRequest, Forbidden, MethodNotAllowed } from "@feathersjs/errors";
|
|
9
|
+
import { BadRequest, Forbidden, MethodNotAllowed, TooManyRequests } from "@feathersjs/errors";
|
|
9
10
|
import _get from "lodash/get.js";
|
|
10
11
|
import _has from "lodash/has.js";
|
|
11
12
|
import _omit from "lodash/omit.js";
|
|
12
13
|
import _set from "lodash/set.js";
|
|
13
14
|
import _setWith from "lodash/setWith.js";
|
|
14
15
|
import _clone from "lodash/clone.js";
|
|
15
|
-
|
|
16
16
|
//#region src/hooks/cache/cache-utils.ts
|
|
17
17
|
const stableStringify = (obj) => {
|
|
18
|
+
if (obj.query) obj = {
|
|
19
|
+
...obj,
|
|
20
|
+
query: sortQueryProperties(obj.query)
|
|
21
|
+
};
|
|
18
22
|
return JSON.stringify(obj, (key, value) => {
|
|
19
23
|
if (typeof value === "function") throw new Error("Cannot stringify non JSON value");
|
|
20
24
|
if (isObject(value)) return Object.keys(value).sort().reduce((result, key) => {
|
|
@@ -24,7 +28,6 @@ const stableStringify = (obj) => {
|
|
|
24
28
|
return value;
|
|
25
29
|
});
|
|
26
30
|
};
|
|
27
|
-
|
|
28
31
|
//#endregion
|
|
29
32
|
//#region src/hooks/cache/cache.hook.ts
|
|
30
33
|
/**
|
|
@@ -76,13 +79,17 @@ var ContextCacheMap = class {
|
|
|
76
79
|
map;
|
|
77
80
|
delimiter = ":";
|
|
78
81
|
options;
|
|
82
|
+
log;
|
|
83
|
+
serialize;
|
|
79
84
|
constructor(options) {
|
|
80
85
|
this.map = options.map;
|
|
81
86
|
this.options = options;
|
|
87
|
+
this.log = options.logger;
|
|
88
|
+
this.serialize = options.serialize ?? stableStringify;
|
|
82
89
|
}
|
|
83
90
|
stringifyCacheKey(context) {
|
|
84
91
|
if (context.method !== "get" && context.method !== "find") throw new Error(`Cache can only be used with 'get' or 'find' methods, not '${context.method}'`);
|
|
85
|
-
const stringifiedParams =
|
|
92
|
+
const stringifiedParams = this.serialize(this.options.transformParams(context.params ?? {}));
|
|
86
93
|
return `${context.id ?? "null"}${this.delimiter}${stringifiedParams}`;
|
|
87
94
|
}
|
|
88
95
|
getCachedId(key) {
|
|
@@ -102,7 +109,19 @@ var ContextCacheMap = class {
|
|
|
102
109
|
async get(context) {
|
|
103
110
|
const key = this.stringifyCacheKey(context);
|
|
104
111
|
const result = this.map.get(key);
|
|
105
|
-
if (result)
|
|
112
|
+
if (result) {
|
|
113
|
+
this.log?.({
|
|
114
|
+
type: "hit",
|
|
115
|
+
method: context.method,
|
|
116
|
+
key
|
|
117
|
+
});
|
|
118
|
+
return copy(result);
|
|
119
|
+
}
|
|
120
|
+
this.log?.({
|
|
121
|
+
type: "miss",
|
|
122
|
+
method: context.method,
|
|
123
|
+
key
|
|
124
|
+
});
|
|
106
125
|
}
|
|
107
126
|
/**
|
|
108
127
|
* Called after get() and find()
|
|
@@ -111,6 +130,11 @@ var ContextCacheMap = class {
|
|
|
111
130
|
*/
|
|
112
131
|
async set(context) {
|
|
113
132
|
const key = this.stringifyCacheKey(context);
|
|
133
|
+
this.log?.({
|
|
134
|
+
type: "set",
|
|
135
|
+
method: context.method,
|
|
136
|
+
key
|
|
137
|
+
});
|
|
114
138
|
return this.map.set(key, copy(context.result));
|
|
115
139
|
}
|
|
116
140
|
async clear(context) {
|
|
@@ -118,23 +142,38 @@ var ContextCacheMap = class {
|
|
|
118
142
|
const promises = [];
|
|
119
143
|
const itemIds = results.map((item) => this.getId(item, context)).filter(Boolean);
|
|
120
144
|
if (!itemIds.length) {
|
|
145
|
+
this.log?.({
|
|
146
|
+
type: "clear",
|
|
147
|
+
method: context.method
|
|
148
|
+
});
|
|
121
149
|
await this.map.clear();
|
|
122
150
|
return context;
|
|
123
151
|
}
|
|
124
152
|
for (const key of this.map.keys()) {
|
|
125
153
|
const cachedId = this.getCachedId(key);
|
|
126
154
|
if (cachedId === "null") {
|
|
155
|
+
this.log?.({
|
|
156
|
+
type: "invalidate",
|
|
157
|
+
method: context.method,
|
|
158
|
+
key
|
|
159
|
+
});
|
|
127
160
|
promises.push(this.map.delete(key));
|
|
128
161
|
continue;
|
|
129
162
|
}
|
|
130
163
|
if (context.method === "create") continue;
|
|
131
|
-
for (const itemId of itemIds) if (cachedId === itemId)
|
|
164
|
+
for (const itemId of itemIds) if (cachedId === itemId) {
|
|
165
|
+
this.log?.({
|
|
166
|
+
type: "invalidate",
|
|
167
|
+
method: context.method,
|
|
168
|
+
key
|
|
169
|
+
});
|
|
170
|
+
promises.push(this.map.delete(key));
|
|
171
|
+
}
|
|
132
172
|
}
|
|
133
173
|
await Promise.all(promises);
|
|
134
174
|
return context;
|
|
135
175
|
}
|
|
136
176
|
};
|
|
137
|
-
|
|
138
177
|
//#endregion
|
|
139
178
|
//#region src/hooks/check-multi/check-multi.hook.ts
|
|
140
179
|
/**
|
|
@@ -156,13 +195,13 @@ var ContextCacheMap = class {
|
|
|
156
195
|
function checkMulti(options) {
|
|
157
196
|
return (context, next) => {
|
|
158
197
|
const { service, method } = context;
|
|
159
|
-
if (!service.allowsMulti || !isMulti(context) || method === "find")
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
198
|
+
if (!service.allowsMulti || !isMulti(context) || method === "find" || service.allowsMulti(method)) {
|
|
199
|
+
if (next) return next();
|
|
200
|
+
return context;
|
|
201
|
+
}
|
|
202
|
+
throw options?.error ? options.error(context) : new MethodNotAllowed(`Can not ${method} multiple entries`);
|
|
163
203
|
};
|
|
164
204
|
}
|
|
165
|
-
|
|
166
205
|
//#endregion
|
|
167
206
|
//#region src/hooks/check-required/check-required.hook.ts
|
|
168
207
|
/**
|
|
@@ -184,11 +223,15 @@ function checkMulti(options) {
|
|
|
184
223
|
function checkRequired(fieldNames) {
|
|
185
224
|
const fieldNamesArray = toArray(fieldNames);
|
|
186
225
|
return (context, next) => {
|
|
187
|
-
checkContext(context,
|
|
188
|
-
"
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
226
|
+
checkContext(context, {
|
|
227
|
+
type: ["before", "around"],
|
|
228
|
+
method: [
|
|
229
|
+
"create",
|
|
230
|
+
"update",
|
|
231
|
+
"patch"
|
|
232
|
+
],
|
|
233
|
+
label: "checkRequired"
|
|
234
|
+
});
|
|
192
235
|
const { data } = getDataIsArray(context);
|
|
193
236
|
for (let i = 0; i < data.length; i++) {
|
|
194
237
|
const item = data[i];
|
|
@@ -202,7 +245,6 @@ function checkRequired(fieldNames) {
|
|
|
202
245
|
if (next) return next();
|
|
203
246
|
};
|
|
204
247
|
}
|
|
205
|
-
|
|
206
248
|
//#endregion
|
|
207
249
|
//#region src/hooks/create-related/create-related.hook.ts
|
|
208
250
|
/**
|
|
@@ -225,7 +267,11 @@ function checkRequired(fieldNames) {
|
|
|
225
267
|
*/
|
|
226
268
|
function createRelated(options) {
|
|
227
269
|
return async (context, next) => {
|
|
228
|
-
checkContext(context,
|
|
270
|
+
checkContext(context, {
|
|
271
|
+
type: ["after", "around"],
|
|
272
|
+
method: ["create"],
|
|
273
|
+
label: "createRelated"
|
|
274
|
+
});
|
|
229
275
|
if (next) await next();
|
|
230
276
|
const { result } = getResultIsArray(context);
|
|
231
277
|
const entries = Array.isArray(options) ? options : [options];
|
|
@@ -238,7 +284,6 @@ function createRelated(options) {
|
|
|
238
284
|
}));
|
|
239
285
|
};
|
|
240
286
|
}
|
|
241
|
-
|
|
242
287
|
//#endregion
|
|
243
288
|
//#region src/hooks/debug/debug.hook.ts
|
|
244
289
|
/**
|
|
@@ -274,7 +319,6 @@ const debug = (msg, ...fieldNames) => async (context, next) => {
|
|
|
274
319
|
});
|
|
275
320
|
if (context.error) console.log("error", context.error);
|
|
276
321
|
};
|
|
277
|
-
|
|
278
322
|
//#endregion
|
|
279
323
|
//#region src/hooks/disable-pagination/disable-pagination.hook.ts
|
|
280
324
|
/**
|
|
@@ -295,7 +339,11 @@ const debug = (msg, ...fieldNames) => async (context, next) => {
|
|
|
295
339
|
* @see https://utils.feathersjs.com/hooks/disable-pagination.html
|
|
296
340
|
*/
|
|
297
341
|
const disablePagination = () => (context, next) => {
|
|
298
|
-
checkContext(context,
|
|
342
|
+
checkContext(context, {
|
|
343
|
+
type: ["before", "around"],
|
|
344
|
+
method: ["find"],
|
|
345
|
+
label: "disablePagination"
|
|
346
|
+
});
|
|
299
347
|
const $limit = context.params?.query?.$limit;
|
|
300
348
|
if ($limit === "-1" || $limit === -1) {
|
|
301
349
|
context.params.paginate = false;
|
|
@@ -304,7 +352,6 @@ const disablePagination = () => (context, next) => {
|
|
|
304
352
|
if (next) return next();
|
|
305
353
|
return context;
|
|
306
354
|
};
|
|
307
|
-
|
|
308
355
|
//#endregion
|
|
309
356
|
//#region src/hooks/disallow/disallow.hook.ts
|
|
310
357
|
/**
|
|
@@ -330,17 +377,20 @@ const disallow = (transports) => {
|
|
|
330
377
|
const transportsArr = toArray(transports);
|
|
331
378
|
return (context, next) => {
|
|
332
379
|
if (!transports) throw new MethodNotAllowed("Method not allowed");
|
|
333
|
-
if (isProvider(...transportsArr)(context)) throw new MethodNotAllowed(`Provider '${context.params.provider}' can not call '${context.method}'. (disallow)`);
|
|
380
|
+
if (isProvider(...transportsArr)(context)) throw new MethodNotAllowed(`Provider '${context.params.provider}' can not call '${context.method}' on '${context.path}'. (disallow)`);
|
|
334
381
|
if (next) return next().then(() => context);
|
|
335
382
|
};
|
|
336
383
|
};
|
|
337
|
-
|
|
338
384
|
//#endregion
|
|
339
385
|
//#region src/hooks/on-delete/on-delete.hook.ts
|
|
340
386
|
const onDelete = (options) => {
|
|
341
387
|
const optionsMulti = Array.isArray(options) ? options : [options];
|
|
342
388
|
return async (context, next) => {
|
|
343
|
-
checkContext(context,
|
|
389
|
+
checkContext(context, {
|
|
390
|
+
type: ["after", "around"],
|
|
391
|
+
method: "remove",
|
|
392
|
+
label: "onDelete"
|
|
393
|
+
});
|
|
344
394
|
if (next) await next();
|
|
345
395
|
const { result } = getResultIsArray(context);
|
|
346
396
|
if (!result.length) return;
|
|
@@ -367,11 +417,9 @@ const onDelete = (options) => {
|
|
|
367
417
|
if (promises.length) await Promise.all(promises);
|
|
368
418
|
};
|
|
369
419
|
};
|
|
370
|
-
|
|
371
420
|
//#endregion
|
|
372
421
|
//#region src/hooks/params-for-server/params-for-from-shared.ts
|
|
373
422
|
const FROM_CLIENT_FOR_SERVER_DEFAULT_KEY = "_$client";
|
|
374
|
-
|
|
375
423
|
//#endregion
|
|
376
424
|
//#region src/hooks/params-for-server/params-for-server.hook.ts
|
|
377
425
|
/**
|
|
@@ -413,7 +461,6 @@ const paramsForServer = (whitelist, options) => {
|
|
|
413
461
|
return context;
|
|
414
462
|
};
|
|
415
463
|
};
|
|
416
|
-
|
|
417
464
|
//#endregion
|
|
418
465
|
//#region src/hooks/params-from-client/params-from-client.hook.ts
|
|
419
466
|
/**
|
|
@@ -437,7 +484,10 @@ const paramsFromClient = (whitelist, options) => {
|
|
|
437
484
|
const whitelistArr = toArray(whitelist);
|
|
438
485
|
const { keyToHide = FROM_CLIENT_FOR_SERVER_DEFAULT_KEY } = options || {};
|
|
439
486
|
return (context, next) => {
|
|
440
|
-
if (!context.params?.query?.[keyToHide] || typeof context.params.query[keyToHide] !== "object")
|
|
487
|
+
if (!context.params?.query?.[keyToHide] || typeof context.params.query[keyToHide] !== "object") {
|
|
488
|
+
if (next) return next();
|
|
489
|
+
return context;
|
|
490
|
+
}
|
|
441
491
|
const params = {
|
|
442
492
|
...context.params,
|
|
443
493
|
query: {
|
|
@@ -458,7 +508,6 @@ const paramsFromClient = (whitelist, options) => {
|
|
|
458
508
|
return context;
|
|
459
509
|
};
|
|
460
510
|
};
|
|
461
|
-
|
|
462
511
|
//#endregion
|
|
463
512
|
//#region src/hooks/prevent-changes/prevent-changes.hook.ts
|
|
464
513
|
/**
|
|
@@ -486,11 +535,51 @@ const preventChanges = (fieldNames, options) => {
|
|
|
486
535
|
const name = fieldNamesArr[i];
|
|
487
536
|
if (_has(item, name)) throw typeof options.error === "function" ? options.error(item, name) : new BadRequest(`Field ${String(name)} may not be patched. (preventChanges)`);
|
|
488
537
|
}
|
|
489
|
-
else
|
|
538
|
+
else return _omit(item, fieldNamesArr);
|
|
490
539
|
return item;
|
|
491
540
|
});
|
|
492
541
|
};
|
|
493
|
-
|
|
542
|
+
//#endregion
|
|
543
|
+
//#region src/hooks/rate-limit/rate-limit.hook.ts
|
|
544
|
+
/**
|
|
545
|
+
* Rate limits service method calls using `rate-limiter-flexible`.
|
|
546
|
+
* You provide a pre-configured `RateLimiterAbstract` instance
|
|
547
|
+
* (Memory, Redis, Mongo, etc.) and the hook consumes points per request.
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* ```ts
|
|
551
|
+
* import { rateLimit } from 'feathers-utils/hooks'
|
|
552
|
+
* import { RateLimiterMemory } from 'rate-limiter-flexible'
|
|
553
|
+
*
|
|
554
|
+
* const rateLimiter = new RateLimiterMemory({ points: 10, duration: 1 })
|
|
555
|
+
*
|
|
556
|
+
* app.service('users').hooks({
|
|
557
|
+
* before: { find: [rateLimit(rateLimiter)] }
|
|
558
|
+
* })
|
|
559
|
+
* ```
|
|
560
|
+
*
|
|
561
|
+
* @see https://utils.feathersjs.com/hooks/rate-limit.html
|
|
562
|
+
*/
|
|
563
|
+
const rateLimit = (rateLimiter, options) => {
|
|
564
|
+
const key = options?.key ?? ((context) => context.path);
|
|
565
|
+
const points = options?.points ?? (() => 1);
|
|
566
|
+
return async (context, next) => {
|
|
567
|
+
checkContext(context, {
|
|
568
|
+
type: ["before", "around"],
|
|
569
|
+
label: "rateLimit"
|
|
570
|
+
});
|
|
571
|
+
const resolvedKey = await key(context);
|
|
572
|
+
const resolvedPoints = await points(context);
|
|
573
|
+
try {
|
|
574
|
+
const res = await rateLimiter.consume(resolvedKey, resolvedPoints);
|
|
575
|
+
context.params.rateLimit = res;
|
|
576
|
+
} catch (res) {
|
|
577
|
+
context.params.rateLimit = res;
|
|
578
|
+
throw new TooManyRequests("Too many requests", { rateLimitRes: res });
|
|
579
|
+
}
|
|
580
|
+
if (next) return await next();
|
|
581
|
+
};
|
|
582
|
+
};
|
|
494
583
|
//#endregion
|
|
495
584
|
//#region src/hooks/set-data/set-data.hook.ts
|
|
496
585
|
/**
|
|
@@ -515,8 +604,14 @@ function setData(from, to, options) {
|
|
|
515
604
|
const { data } = getDataIsArray(context);
|
|
516
605
|
const contextJson = contextToJson(context);
|
|
517
606
|
if (!_has(contextJson, from)) {
|
|
518
|
-
if (!context.params?.provider || allowUndefined === true)
|
|
519
|
-
|
|
607
|
+
if (!context.params?.provider || allowUndefined === true) {
|
|
608
|
+
if (next) return next();
|
|
609
|
+
return context;
|
|
610
|
+
}
|
|
611
|
+
if (!overwrite && data.every((item) => _has(item, to))) {
|
|
612
|
+
if (next) return next();
|
|
613
|
+
return context;
|
|
614
|
+
}
|
|
520
615
|
throw options?.error ? options.error(context, from) : new Forbidden(`Expected field ${from.toString()} not available`);
|
|
521
616
|
}
|
|
522
617
|
const val = _get(contextJson, from);
|
|
@@ -529,7 +624,6 @@ function setData(from, to, options) {
|
|
|
529
624
|
return context;
|
|
530
625
|
};
|
|
531
626
|
}
|
|
532
|
-
|
|
533
627
|
//#endregion
|
|
534
628
|
//#region src/hooks/set-field/set-field.hook.ts
|
|
535
629
|
/**
|
|
@@ -550,17 +644,22 @@ function setData(from, to, options) {
|
|
|
550
644
|
*/
|
|
551
645
|
const setField = ({ as, from, allowUndefined = false, error }) => (context, next) => {
|
|
552
646
|
const { params } = context;
|
|
553
|
-
checkContext(context,
|
|
647
|
+
checkContext(context, {
|
|
648
|
+
type: ["before", "around"],
|
|
649
|
+
label: "setField"
|
|
650
|
+
});
|
|
554
651
|
const value = _get(context, from);
|
|
555
652
|
if (value === void 0) {
|
|
556
|
-
if (!params.provider || allowUndefined)
|
|
653
|
+
if (!params.provider || allowUndefined) {
|
|
654
|
+
if (next) return next();
|
|
655
|
+
return context;
|
|
656
|
+
}
|
|
557
657
|
throw error ? error(context, from) : new Forbidden(`Expected field ${as} not available`);
|
|
558
658
|
}
|
|
559
659
|
context = _setWith(context, as, value, _clone);
|
|
560
660
|
if (next) return next();
|
|
561
661
|
return context;
|
|
562
662
|
};
|
|
563
|
-
|
|
564
663
|
//#endregion
|
|
565
664
|
//#region src/hooks/set-result/set-result.hook.ts
|
|
566
665
|
/**
|
|
@@ -605,11 +704,10 @@ function setResult(from, to, options) {
|
|
|
605
704
|
return forResultOrDispatch(context, !!options?.dispatch);
|
|
606
705
|
};
|
|
607
706
|
return (context, next) => {
|
|
608
|
-
if (next) next().then(() => fn(context));
|
|
707
|
+
if (next) return next().then(() => fn(context));
|
|
609
708
|
return fn(context);
|
|
610
709
|
};
|
|
611
710
|
}
|
|
612
|
-
|
|
613
711
|
//#endregion
|
|
614
712
|
//#region src/hooks/set-slug/set-slug.hook.ts
|
|
615
713
|
/**
|
|
@@ -639,7 +737,6 @@ const setSlug = (slug, fieldName) => {
|
|
|
639
737
|
return context;
|
|
640
738
|
};
|
|
641
739
|
};
|
|
642
|
-
|
|
643
740
|
//#endregion
|
|
644
741
|
//#region src/hooks/soft-delete/soft-delete.hook.ts
|
|
645
742
|
/**
|
|
@@ -663,7 +760,10 @@ const setSlug = (slug, fieldName) => {
|
|
|
663
760
|
const softDelete = (options) => {
|
|
664
761
|
if (!options?.deletedQuery || !options?.removeData) throw new Error("You must provide `deletedQuery` and `removeData` options to the softDelete hook.");
|
|
665
762
|
return async (context, next) => {
|
|
666
|
-
checkContext(context,
|
|
763
|
+
checkContext(context, {
|
|
764
|
+
type: ["before", "around"],
|
|
765
|
+
label: "softDelete"
|
|
766
|
+
});
|
|
667
767
|
const { disableSoftDeleteKey = "disableSoftDelete" } = options;
|
|
668
768
|
if (context.params[disableSoftDeleteKey]) return early(context, next);
|
|
669
769
|
const { deletedQuery, removeData } = options;
|
|
@@ -689,52 +789,60 @@ const getValue = (value, ...args) => {
|
|
|
689
789
|
if (typeof value === "function") return value(...args);
|
|
690
790
|
return value;
|
|
691
791
|
};
|
|
692
|
-
|
|
693
792
|
//#endregion
|
|
694
|
-
//#region src/hooks/
|
|
793
|
+
//#region src/hooks/stashable/stashable.hook.ts
|
|
794
|
+
const defaultStashFunc = (context) => {
|
|
795
|
+
const isMulti = context.id == null;
|
|
796
|
+
const params = {
|
|
797
|
+
...context.params,
|
|
798
|
+
_stashable: true,
|
|
799
|
+
...isMulti ? { paginate: false } : {}
|
|
800
|
+
};
|
|
801
|
+
return isMulti ? context.service.find(params) : context.service.get(context.id, params);
|
|
802
|
+
};
|
|
695
803
|
/**
|
|
696
|
-
* Stashes the
|
|
697
|
-
*
|
|
698
|
-
*
|
|
804
|
+
* Stashes the pre-mutation state of a record into `context.params`.
|
|
805
|
+
* Eagerly starts the fetch but exposes a memoized function — calling it
|
|
806
|
+
* multiple times only hits the database once.
|
|
807
|
+
* Use in `before` hooks on `update`, `patch`, or `remove` methods.
|
|
699
808
|
*
|
|
700
809
|
* @example
|
|
701
810
|
* ```ts
|
|
702
|
-
* import {
|
|
811
|
+
* import { stashable } from 'feathers-utils/hooks'
|
|
703
812
|
*
|
|
704
813
|
* app.service('users').hooks({
|
|
705
|
-
* before: { patch: [
|
|
814
|
+
* before: { patch: [stashable()] }
|
|
706
815
|
* })
|
|
707
|
-
*
|
|
816
|
+
*
|
|
817
|
+
* // In a later hook (before or after):
|
|
818
|
+
* const before = await context.params.stashed()
|
|
708
819
|
* ```
|
|
709
820
|
*
|
|
710
|
-
* @see https://utils.feathersjs.com/hooks/
|
|
821
|
+
* @see https://utils.feathersjs.com/hooks/stashable.html
|
|
711
822
|
*/
|
|
712
|
-
function
|
|
713
|
-
const
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
"patch",
|
|
719
|
-
"remove"
|
|
720
|
-
], "stashBefore");
|
|
721
|
-
const isMulti = context.id == null;
|
|
722
|
-
const params = {
|
|
723
|
-
...context.params,
|
|
724
|
-
disableStashBefore: true,
|
|
725
|
-
...isMulti ? { paginate: false } : {}
|
|
726
|
-
};
|
|
727
|
-
await (!isMulti ? context.service.get(context.id, params) : context.service.find(params)).then((result) => {
|
|
728
|
-
context.params[beforeField] = result;
|
|
729
|
-
return context;
|
|
730
|
-
}).catch(() => {
|
|
823
|
+
function stashable(options) {
|
|
824
|
+
const propName = options?.propName ?? "stashed";
|
|
825
|
+
const stashFunc = options?.stashFunc ?? defaultStashFunc;
|
|
826
|
+
return ((context, next) => {
|
|
827
|
+
if (context.params._stashable) {
|
|
828
|
+
if (next) return next();
|
|
731
829
|
return context;
|
|
830
|
+
}
|
|
831
|
+
checkContext(context, {
|
|
832
|
+
type: ["before", "around"],
|
|
833
|
+
method: [
|
|
834
|
+
"update",
|
|
835
|
+
"patch",
|
|
836
|
+
"remove"
|
|
837
|
+
],
|
|
838
|
+
label: "stashable"
|
|
732
839
|
});
|
|
733
|
-
|
|
840
|
+
const promise = stashFunc(context).catch(() => void 0);
|
|
841
|
+
context.params[propName] = () => promise;
|
|
842
|
+
if (next) return next();
|
|
734
843
|
return context;
|
|
735
|
-
};
|
|
844
|
+
});
|
|
736
845
|
}
|
|
737
|
-
|
|
738
846
|
//#endregion
|
|
739
847
|
//#region src/hooks/throw-if-is-multi/throw-if-is-multi.hook.ts
|
|
740
848
|
const defaultError$1 = (context) => new BadRequest(`Cannot perform multi operation on method '${context.method}'`);
|
|
@@ -755,7 +863,6 @@ const defaultError$1 = (context) => new BadRequest(`Cannot perform multi operati
|
|
|
755
863
|
* @see https://utils.feathersjs.com/hooks/throw-if-is-multi.html
|
|
756
864
|
*/
|
|
757
865
|
const throwIfIsMulti = (options) => throwIf(every(every(isMulti, (context) => context.method !== "find"), options?.filter), { error: options?.error ?? defaultError$1 });
|
|
758
|
-
|
|
759
866
|
//#endregion
|
|
760
867
|
//#region src/hooks/throw-if-is-provider/throw-if-is-provider.hook.ts
|
|
761
868
|
const defaultError = (context) => new MethodNotAllowed(`Provider '${context.params.provider}' can not call '${context.method}'.`);
|
|
@@ -778,7 +885,6 @@ const defaultError = (context) => new MethodNotAllowed(`Provider '${context.para
|
|
|
778
885
|
const throwIfIsProvider = (transports, options) => {
|
|
779
886
|
return throwIf(every(isProvider(...toArray(transports)), options?.filter), { error: options?.error ?? defaultError });
|
|
780
887
|
};
|
|
781
|
-
|
|
782
888
|
//#endregion
|
|
783
889
|
//#region src/hooks/traverse/traverse.hook.ts
|
|
784
890
|
/**
|
|
@@ -804,7 +910,7 @@ const traverse = ({ transformer, getObject }) => (context, next) => {
|
|
|
804
910
|
if (next) return next();
|
|
805
911
|
return context;
|
|
806
912
|
};
|
|
807
|
-
|
|
808
913
|
//#endregion
|
|
809
|
-
export {
|
|
810
|
-
|
|
914
|
+
export { debug as _, softDelete as a, checkMulti as b, setField as c, preventChanges as d, paramsFromClient as f, disablePagination as g, disallow as h, stashable as i, setData as l, onDelete as m, throwIfIsProvider as n, setSlug as o, paramsForServer as p, throwIfIsMulti as r, setResult as s, traverse as t, rateLimit as u, createRelated as v, cache as x, checkRequired as y };
|
|
915
|
+
|
|
916
|
+
//# sourceMappingURL=hooks-DPFxxjBu.mjs.map
|