Pre hooks¶
A pre hook is a TypeScript function of yours which runs before an API. It sees the request, can change it, can answer instead of the API, and can refuse it. Attach it to a whole instance, a database, a table, one API, a custom API, a system API or a third party API, and change it any time without a restart.
Where hooks live¶
| Level | Runs for | Where |
|---|---|---|
| Instance | every API of every table of the instance | Instances page › the instance › hooks |
| Database | every API of every table of the database | the database › hooks |
| Table (collection) | every API of the table | the table › hooks |
| API | one API of one table, SCHEMA_GET_ALL for example |
the API › hooks |
| Custom, system, third party API | that API | its hooks tab |
| Group | the hooks above, only for API users of the groups named on the hook | groupNames on the hook |
- Pre hooks run from the widest level to the narrowest : instance, database, table, API, each level in the order of its list. Post hooks run the other way round. See Post hooks.
- A hook with
groupNamesruns only when the API user of the call belongs to one of those groups. That is how one table gets a row scoping hook for the application users and none for the back office.
The code¶
What the return value does :
| The hook | Effect |
|---|---|
| returns nothing | The next hook, then the API, run. |
| returns a value | That value is the answer of the request : the remaining hooks and the API do not run. The response is 200 with the value in data. |
sets g.res.output and returns nothing |
The API runs ; its output replaces g.res.output unless a post hook changes it again. |
| throws | The request stops with the error : throw new Error('…') gives 500 and the message, or throw an IResponseError[] with a code for another status. |
- Every hook runs in the sandbox (or on the native process with
runOnNativeProcess), within the sandbox timeout of the call. - When the answer of an API comes from the cache, neither pre nor post hooks run.
- Calls from your own code (
g.sys.db.getAllin a custom API) skip the hooks by default. PassskipHookRunning: falseto run them.
Narrow a request to the rows of the caller¶
The most useful pre hook : a filter added to every read and a stamp on every write, so a person only reaches their own rows.
The APIs Security Report writes this hook for you, for the tables which need it, and Handle role based permissions explains the pattern step by step.
Answer without the API¶
| A pre hook which answers from the cache of the process | |
|---|---|
Validate and change¶
Utility classes in a hook¶
- Press Ctrl+Space in the editor for the list of your utility classes.
Testing a hook¶
- The API testing page runs an API with its hooks. While you edit a hook there, the Test button of the hook runs only that hook, so you see its effect alone.
- Every hook has versions : keep the old one active until the new one is ready.
- The log profile records each hook run with the request ; the log explorer shows them next to the API call.
Good to know¶
- Streams : pre hooks run before the stream starts ; post hooks can not change what was streamed.
- A hook which calls the same API it is attached to creates a cycle : API Maker detects it and stops the request with an error.
- Hooks are files in Git, under the folder of their instance, database, table or API, and deploy with a Git pull.