open-index/open-npm huggingface dataset:
npm package: @leverage/plugin-http
Description: An HTTP plugin for Leverage
Author: Jake Hamilton
Latest version: 4.1.1

# @leverage/plugin-http

> A HTTP plugin for Leverage.

## Install

```shell
# Using NPM
npm install @leverage/plugin-http

# Using Yarn
yarn add @leverage/plugin-http
```

## Documentation

### Plugin Install

To install the HTTP Plugin, add `http` to Leverage.

```js
import { add } from "@leverage/core";
import { http } from "@leverage/plugin-http";

add(http);
```

### Events

The HTTP Plugin can be configured using events. The server can also be told to start listening.

```js
import { add, emit } from "@leverage/core";
import { http } from "@leverage/plugin-http";

add(http);

// Configure the Fastify instance
await emit("http:configure", {
    /*
     * All configuration options are passed directly to the Fastify server.
     * https://www.fastify.io/docs/latest/Server/
     */
    ignoreTrailingSlash: true,
});

// Tell the server to start listening on a port
await signal("http:listen", {
    port: 8080,
});
```

### Components

A HTTP component holds configuration for a route.

```js
import { useHTTP } from "@leverage/plugin-http";

export const init = () => {
    useHTTP({
        /*
         * All configuration options are passed directly to `fastify.route()`.
         * https://www.fastify.io/docs/latest/Routes/
         */
        path: "/hello-world",
        method: "GET",
    });
};

// Any Fastify handlers/callbacks are also passed to `fastify.route()`.
export const handler = (request, reply) => {
    /* ... */
};
export const preHandler = (request, reply) => {
    /* ... */
};
export const onSend = (request, reply) => {
    /* ... */
};
```

Additionally, connect-style middleware can be used by exporting a `middleware` function.

```js
import { useHTTP } from "@leverage/plugin-http";
import cors from "cors";

export const init = () => {
    useHTTP();
};

export const middleware = cors();
```

### Hooks

#### `useHTTP`

The `useHTTP` hook can be used to configure an HTTP component.

```js
import { useHTTP } from "@leverage/plugin-http";

export const init = () => {
    useHTTP({
        /*
         * All configuration options are passed directly to `fastify.route()`.
         * https://www.fastify.io/docs/latest/Routes/
         */
        path: "/hello-world",
        method: "GET",
    });
};
```

```js
import { useHTTP } from "@leverage/plugin-http";

export const init = () => {
    /*
     * When called without a config, the component is still configured for use
     *  with the HTTP plugin. This is useful when used for middleware with the
     *  `useMiddleware` hook.
     */
    useHTTP();
};
```
npm package: @leverage/plugin-websocket
Description: A Websocket plugin for Leverage
Author: Jake Hamilton
Latest version: 2.1.1

# @leverage/plugin-websocket

> A Websocket plugin for Leverage

## Install

```shell
# Using NPM
npm install @leverage/plugin-websocket

# Using Yarn
yarn add @leverage/plugin-websocket
```

## Documentation

### Plugin Install

To install the WebSocket Plugin, add `websocket` to Leverage.

```js
import { add } from "@leverage/core";
import { websocket } from "@leverage/plugin-websocket";

add(websocket);
```

### Events

The WebSocket Plugin can be configured using events. The server can also be told to attach to a HTTP plugin.

```js
import { add, emit } from "@leverage/core";
import { websocket } from "@leverage/plugin-websocket";

add(http);

// Configure the Fastify instance.
await emit("websocket:configure", {
    /*
     * All configuration options are passed directly to socket.io.
     * https://socket.io/docs/v4/server-api/
     */
});

// Tell the server to attach to the HTTP plugin.
await signal("websocket:attach");
```

### Components

A WebSocket component holds configuration for an event.

```js
import { useWebSocket } from "@leverage/plugin-websocket";

export const init = () => {
    useWebSocket({
        event: "my-event",
    });
};

/*
 * A handler must be provided to handle the event. The handler
 *  receives the socket that triggered the event and any data
 *  from the event.
 */
export const handler = (socket, data) => {};
```

### Hooks

#### `useWebSocket`

The `useWebSocket` hook can be used to configure a WebSocket component.

```js
import { useWebSocket } from "@leverage/plugin-websocket";

export const init = () => {
    useWebSocket({
        event: "my-event",
    });
};
```