# hooks

`hooks` is an array of objects. Single object contains `event` and `handler` properties. For example:

```javascript
{
    event: 'preGetItem',
    handler: ({ cacheInstance, key }) => {
        // do something with key for example
        const prefixedKey = `prefix${key}`;

        return { cacheInstance, key: prefixedKey };
    }
}
```

Above example is a hook for `preGetItem` event, and it will trigger `handler` for this event, which will alter (add prefix) passed key. From now on, such prefixed key will be used to store / get / remove items.

## `event`

`event` is a name of the event for which given handler will be fired. For stash-it, events are: `preMethodName` and `postMethodName`

{% hint style="info" %}
event’s name is constructed from prefix + method name, for instance `preSetItem` or `postRemoveItem` (mind the camel case name structure)
{% endhint %}

### **Why `pre` and `post`?**

It’s about data control (before and after) of each method.

## `handler`

`handler` is a function that takes certain arguments (in an object), does something with them and returns an object. Most of the time it should return something. But not always. If, for example, some plugin should throw when certain criteria are met, then this handler will throw, not return a value.

### **What arguments are passed to each handler?**

Depending on method for which you create the handler for, you will get different arguments. All of them are described in detail in [cacheInstance's API](https://stash-it.gitbook.io/stash-it/api/cacheinstance).

### **What about 3rd party plugins and arguments for their methods (and events).**

It solely depends on those plugins. You need to read their docs in order to find out.<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stash-it.gitbook.io/stash-it/plugins/hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
