traverse
Recursively walks and transforms fields in record(s) using neotraverse. The getObject function extracts the target from the context, and transformer is called for every node during traversal --- ideal for deep, structural transformations.
ts
import { } from 'feathers-utils/hooks';Example
ts
import { traverse } from 'feathers-utils/hooks'
app.service('users').hooks({
after: {
all: [traverse({ getObject: (ctx) => ctx.result, transformer: function () { if (this.key === 'password') this.remove() } })]
}
})Type declaration
Show Type Declarations
ts
export type TraverseOptions = {
transformer: (transformContext: any) => any
getObject: (
context: HookContext,
) => Record<string, any> | Record<string, any>[]
/**
* For `around` hooks only: run the traversal *after* `next()` instead of before.
* Required when `getObject` targets `context.result`, which is only populated
* once the service method has run. Defaults to `false` (run before `next()`),
* which is correct for `context.data`/`context.params.query` targets.
*
* @default false
*/
runAfter?: boolean
}
/**
* Recursively walks and transforms fields in record(s) using `neotraverse`.
* The `getObject` function extracts the target from the context, and `transformer`
* is called for every node during traversal --- ideal for deep, structural transformations.
*
* @example
* ```ts
*
*
* app.service('users').hooks({
* after: {
* all: [traverse({ getObject: (ctx) => ctx.result, transformer: function () { if (this.key === 'password') this.remove() } })]
* }
* })
* ```
*
* @see https://utils.feathersjs.com/hooks/traverse.html
*/
export declare const traverse: <H extends HookContext = HookContext>({
transformer,
getObject,
runAfter,
}: TraverseOptions) => {
(context: H): void
(context: H, next: NextFunction): Promise<void>
}| Argument | Type | Description |
|---|---|---|
| { transformer, getObject, runAfter = false, } | TraverseOptions |
| type | methods | multi |
|---|---|---|
| before, after, around | find, get, create, update, patch, remove | yes |
