Skip to content

setResult

hooks
Source Code | Documentation

Sets a property on each item in context.result from another property on the hook context. Supports dot-notation paths for both source and target, and can optionally operate on context.dispatch as well.

ts
  import { 
setResult
} from 'feathers-utils/hooks';

Example

ts
import { setResult } from 'feathers-utils/hooks'

app.service('posts').hooks({
  after: { all: [setResult('params.user.id', 'currentUserId')] }
})

Type declaration

Show Type Declarations
ts
export interface SetResultOptions {
  /**
   * Wether to throw if the context[from] is undefined.
   *
   * @default false
   */
  allowUndefined?: boolean
  /**
   * @default true
   */
  overwrite?: boolean | PredicateItemWithContext
  /**
   * Customize the error that is thrown if the context[from] is not available.
   * If not provided, throws a `Forbidden` error with a message indicating the missing field.
   */
  error?: (context: HookContext, from: PropertyPath) => FeathersError
  dispatch?: DispatchOption
}
/**
 * Sets a property on each item in `context.result` from another property on the hook context.
 * Supports dot-notation paths for both source and target, and can optionally
 * operate on `context.dispatch` as well.
 *
 * @example
 * ```ts
 *
 *
 * app.service('posts').hooks({
 *   after: { all: [setResult('params.user.id', 'currentUserId')] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/set-result.html
 */
export declare function setResult<H extends HookContext = HookContext>(
  /**
   * The property path of the context to set the value from. 'dot.notation' is supported.
   *
   * If the property does not exist, the hook will throw an error unless `allowUndefined` is set to true.
   * If the property exists, it will be set to the value of the `to` property path of the data item.
   *
   * @example 'params.user.id'
   */
  from: PropertyPath,
  /**
   * The property path of the data item to set the value to. 'dot.notation' is supported.
   *
   * If the property does not exist, it will be created.
   * If the property exists, it will be overwritten unless `overwrite` is set to false.
   *
   * @example 'userId'
   */
  to: PropertyPath,
  options?: SetResultOptions,
): (context: H, next?: NextFunction) => H
ArgumentTypeDescription
fromPropertyPath
toPropertyPath
optionsSetResultOptions
typemethodsmulti
after, aroundfind, get, create, update, patch, removeyes

Released under the MIT License.