Application Development53% of the exam

Hook extension points with hooks.json and HookMgr

How to register OCAPI and custom hooks with package.json and hooks.json, pick a before, after or modifyResponse hook for a requirement, control the outcome with Status objects and return values, and call custom extension points with HookMgr.

Official exam objective
Given a use case, extend functionality or capture an event using hook extension points.

~8 min read · 15 practice questions ·

What the exam expects

  • Register a hook: package.json with a hooks property, a hooks.json mapping, an exported function and the cartridge on the right site's path.
  • Choose the before, after or modifyResponse hook of an OCAPI Shop API resource from a requirement (validate, change data inside the transaction, reshape the response).
  • Predict what the server does with a returned Status, a returned value, nothing returned and an uncaught exception.
  • Pass data between the hooks of one request and add properties to a response correctly.
  • Define custom extension points, call them with HookMgr, and predict which registered modules run, in which order, and what callHook returns.

Key terms: extension point (a named point in the business logic where scripts can be registered); hook (a script registered for an extension point); Status (the dw.system.Status object a hook may return, OK or ERROR); dw.order.calculate (the Calculate hook for basket totals); custom hook (an extension point you define yourself and call with HookMgr).

Key facts

package.json hooks path
Detail
Relative to the directory containing package.json
hooks.json script paths
Detail
Relative to the hooks.json file
Export name
Detail
Hook name without package qualification, such as beforePUT
Organization-level resources
Detail
Register the cartridge to the Business Manager site
GET requests
Detail
before and modifyResponse hooks only
Status ERROR
Detail
Rollback and HTTP 400 fault
Uncaught exception
Detail
Rollback and HTTP 500 fault
Data between hooks
Detail
request.custom
order.beforePOST
Detail
Called before an order is created for the basket
callHook with nothing registered
Detail
Returns undefined
Several modules for one extension point
Detail
All run; only the last returns a value
Sources for this section (4)
  • Why do GET resources have no after hook?
  • When does creating an order through POST /orders recalculate the basket by default?
  • Which two arguments does a basket modifyGETResponse function receive?
  • What does an ERROR fault returned to the caller contain?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the B2C Commerce Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 28 lessons of the B2C Commerce Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Practice exams purchase adds inline bank questions and topic practice (15 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Choose guide-only, practice-only, or save $4 with both

Practice for the B2C Commerce Developer exam