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.
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.jsonwith ahooksproperty, 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 whatcallHookreturns.
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
script paths- Detail
- Relative to the hooks.json file
- Detail
- Hook name without package qualification, such as
beforePUT
- Detail
- Register the cartridge to the Business Manager site
- Detail
- before and modifyResponse hooks only
- Detail
- Rollback and HTTP 400 fault
- Detail
- Rollback and HTTP 500 fault
- Detail
request.custom
order.beforePOST- Detail
- Called before an order is created for the basket
callHook with nothing registered- Detail
- Returns undefined
- Detail
- All run; only the last returns a value
- Why do GET resources have no after hook?
- When does creating an order through
POST /ordersrecalculate the basket by default? - Which two arguments does a basket
modifyGETResponsefunction 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