---
displayed_sidebar: servicesSidebar
---

import ParameterTypes from "@site/src/components/ParameterTypes"

# MiddlewareService

Orchestrates dynamic middleware registered through the Medusa Middleware API

## Constructors

### constructor

**new MiddlewareService**()

## Properties

<ParameterTypes parameters={[
  {
    "name": "postAuthentication_",
    "type": "[`middlewareType`](../types/middlewareType.mdx)[]",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "preAuthentication_",
    "type": "[`middlewareType`](../types/middlewareType.mdx)[]",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "preCartCreation_",
    "type": "`RequestHandler`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, Record<`string`, `any`\\>\\>[]",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "routers",
    "type": "Record<`string`, `Router`[]\\>",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

## Methods

### addPostAuthentication

**addPostAuthentication**(`middleware`, `options`): `void`

Adds a middleware function to be called after authentication is completed.

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "middleware",
    "type": "[`middlewareHandlerType`](../types/middlewareHandlerType.mdx)",
    "description": "the middleware function. Should return a middleware function.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "options",
    "type": "Record<`string`, `unknown`\\>",
    "description": "the arguments that will be passed to the middleware",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "void",
    "expandable": false,
    "children": []
  }
]} />

___

### addPreAuthentication

**addPreAuthentication**(`middleware`, `options`): `void`

Adds a middleware function to be called before authentication is completed.

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "middleware",
    "type": "[`middlewareHandlerType`](../types/middlewareHandlerType.mdx)",
    "description": "the middleware function. Should return a middleware function.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "options",
    "type": "Record<`string`, `unknown`\\>",
    "description": "the arguments that will be passed to the middleware",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "void",
    "expandable": false,
    "children": []
  }
]} />

___

### addPreCartCreation

**addPreCartCreation**(`middleware`): `void`

Adds a middleware function to be called before cart creation

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "middleware",
    "type": "`RequestHandler`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, Record<`string`, `any`\\>\\>",
    "description": "the middleware function. Should return a middleware function.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "",
    "expandable": false,
    "children": []
  }
]} />

___

### addRouter

**addRouter**(`path`, `router`): `void`

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "path",
    "type": "`string`",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "router",
    "type": "`Router`",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "",
    "expandable": false,
    "children": []
  }
]} />

___

### getRouters

**getRouters**(`path`): `Router`[]

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "path",
    "type": "`string`",
    "description": "",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`Router`[]

<ParameterTypes parameters={[
  {
    "name": "Router[]",
    "type": "`Router`[]",
    "optional": false,
    "defaultValue": "",
    "description": "",
    "expandable": false,
    "children": []
  }
]} />

___

### usePostAuthentication

**usePostAuthentication**(`app`): `void`

Adds post authentication middleware to an express app.

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "app",
    "type": "`Router`",
    "description": "the express app to add the middleware to",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "",
    "expandable": false,
    "children": []
  }
]} />

___

### usePreAuthentication

**usePreAuthentication**(`app`): `void`

Adds pre authentication middleware to an express app.

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "app",
    "type": "`Router`",
    "description": "the express app to add the middleware to",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "",
    "expandable": false,
    "children": []
  }
]} />

___

### usePreCartCreation

**usePreCartCreation**(): `RequestHandler`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, Record<`string`, `any`\>\>[]

#### Returns

`RequestHandler`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, Record<`string`, `any`\>\>[]

<ParameterTypes parameters={[
  {
    "name": "RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any\\>\\>[]",
    "type": "`RequestHandler`<`ParamsDictionary`, `any`, `any`, `ParsedQs`, Record<`string`, `any`\\>\\>[]",
    "optional": false,
    "defaultValue": "",
    "description": "",
    "expandable": false,
    "children": []
  }
]} />

___

### validateMiddleware\_

**validateMiddleware_**(`fn`): `void`

Validates a middleware function, throws if fn is not of type function.

#### Parameters

<ParameterTypes parameters={[
  {
    "name": "fn",
    "type": "`unknown`",
    "description": "the middleware function to validate.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

#### Returns

`void`

<ParameterTypes parameters={[
  {
    "name": "void",
    "type": "`void`",
    "optional": true,
    "defaultValue": "",
    "description": "nothing if the middleware is a function",
    "expandable": false,
    "children": []
  }
]} />
