# Plugins

Plugins are used to encapsulate and abstract away adding functionality to your Outsmartly edge. They'll often include things like middleware, interceptors, and overrides.

Making your own plugins is possible, but more commonly you'll use one of the existing ones maintained by Outsmartly, or ones from the community:

## Common Plugins

* [@outsmartly/plugin-image-optimizations](https://www.npmjs.com/package/@outsmartly/plugin-image-optimizations)
* [@outsmartly/plugin-shopify](https://www.npmjs.com/package/@outsmartly/plugin-shopify)

## Usage

```typescript
import { somePlugin } from '@outsmartly/plugin-something';
export default {
  host: 'example.outsmartly.app',
  environments: [
    {
      name: 'production',
      origin: 'https://my-example-website.vercel.app',
    },
  ],
  plugins: [
    // Most plugins provide a factory function for passing in options:
    somePlugin({
      something: true,
    }),
  ],
};
```

## Making Your Own Plugins

Plugins are objects with a `name` and lifecycle methods.

#### [Type Definition](https://github.com/outsmartly/outsmartly/tree/ab1b088fb548cbba07e10c9433fe62c167a3fe75/packages/core/src/public/types.ts#:~:text=interface%20Plugin)

### Example

```typescript
export function myCustomPlugin(options) {
  return {
    name: '@yourcompany/outsmartly-plugin-does-something',
    setup({ config, messageBus }) {
      // Listening for messages, globally
      messageBus.on('Something.HAPPENED', async (event) => {
        const { message } = event;
        // do something with it, such as call fetch()
      });

      // Adding custom middleware
      config.middleware.unshift(async (event, next) => {
        const response = await next();
        return response;
      });

      config.routes.push({
        path: '/something',
        async intercept(event) {
          return new Response('hello, world!');
        },
      });
    },
  };
}
```


---

# 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://outsmartly.gitbook.io/outsmartly/plugins.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.
