Skip to content

lspeasy / core/src / createScopedMiddleware

Function: createScopedMiddleware()

ts
function createScopedMiddleware(filter, middleware): ScopedMiddleware;

Defined in: packages/core/src/middleware/scoped.ts:31

Wraps a middleware with a filter so it only runs for matching LSP messages.

Parameters

ParameterTypeDescription
filterMethodFilterA MethodFilter describing which messages to intercept.
middlewareMiddlewareThe middleware function to run when the filter matches.

Returns

ScopedMiddleware

A ScopedMiddleware object ready to pass to ServerOptions.middleware or ClientOptions.middleware.

Remarks

Prefer this over putting an if (context.method === '...') guard inside your middleware — scoped middleware is skipped before the function is even called, which keeps the pipeline overhead low.

Example

ts
import { createScopedMiddleware } from '@lspeasy/core';

const hoverLogger = createScopedMiddleware(
  { methods: ['textDocument/hover'], direction: 'clientToServer' },
  async (ctx, next) => {
    console.log('hover params:', ctx.message);
    return next();
  }
);

Released under the MIT License.