Features
Docs
CLI
Benchmarks
Examples
GitHub

© 2024 MoroJs

@morojs/moro


@morojs/moro / builtInMiddleware

Variable: builtInMiddleware

const builtInMiddleware: object

Defined in: src/core/middleware/built-in/index.ts:67

Type Declaration

auth()

auth: (options) => MiddlewareInterface

Auth hook for global usage Registers with the hooks system for application-wide Auth.js authentication

Parameters

options

AuthOptions

Returns

MiddlewareInterface

Example

import { auth, providers } from '@/middleware/built-in/auth';

app.use(auth({
  secret: process.env.AUTH_SECRET,
  providers: [
    providers.google({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET
    }),
    providers.github({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET
    })
  ],
  session: {
    strategy: 'jwt',
    maxAge: 30 * 24 * 60 * 60
  }
}));

cache()

cache: (options) => MiddlewareInterface

Advanced cache hook with pluggable storage adapters Registers with the hooks system for global usage

Parameters

options

CacheOptions = {}

Returns

MiddlewareInterface

Example

import { cache } from '@/middleware/built-in/cache';

app.use(cache({
  adapter: 'redis',
  defaultTtl: 3600,
  strategies: {
    '/api/.*': { ttl: 60 }
  }
}));

cdn()

cdn: (options) => MiddlewareInterface

CDN hook for global usage Registers with the hooks system for application-wide CDN support

Parameters

options

CDNOptions = {}

Returns

MiddlewareInterface

Example

import { cdn } from '@/middleware/built-in/cdn';

app.use(cdn({
  adapter: 'cloudflare',
  adapterOptions: {
    zoneId: 'your-zone-id',
    apiToken: 'your-api-token'
  },
  autoInvalidate: true,
  invalidationPatterns: ['/api/.*']
}));

cookie()

cookie: (config) => MiddlewareInterface

Cookie hook for global usage Registers with the hooks system for application-wide cookie handling

Parameters

config

CookieConfig = {}

Returns

MiddlewareInterface

Example

import { cookie } from '@/middleware/built-in/cookie';

app.use(cookie({
  secret: 'my-secret-key',
  signed: true
}));

cors()

cors: (options) => MiddlewareInterface

CORS hook for global usage Registers with the hooks system for application-wide CORS

Parameters

options

CORSOptions = {}

Returns

MiddlewareInterface

Example

import { cors } from '@/middleware/built-in/cors';

app.use(cors({
  origin: 'https://example.com',
  credentials: true
}));

csp()

csp: (options) => MiddlewareInterface

CSP hook for global usage Registers with the hooks system for application-wide Content Security Policy

Parameters

options

CSPOptions = {}

Returns

MiddlewareInterface

Example

import { csp } from '@/middleware/built-in/csp';

app.use(csp({
  directives: {
    defaultSrc: ["'self'"],
    scriptSrc: ["'self'", 'https://cdn.example.com'],
    styleSrc: ["'self'", "'unsafe-inline'"]
  },
  nonce: true,
  reportUri: '/csp-report'
}));

csrf()

csrf: (options) => MiddlewareInterface

CSRF hook for global usage Registers with the hooks system for application-wide CSRF protection

Parameters

options

CSRFOptions = {}

Returns

MiddlewareInterface

Example

import { csrf } from '@/middleware/built-in/csrf';

app.use(csrf({
  cookieName: '_csrf',
  ignoreMethods: ['GET', 'HEAD', 'OPTIONS']
}));

rateLimit()

rateLimit: (options) => MiddlewareInterface

Rate limit hook for global usage Registers with the hooks system for application-wide rate limiting

Parameters

options
max?

number

message?

string

windowMs?

number

Returns

MiddlewareInterface

Example

import { rateLimit } from '@/middleware/built-in/rate-limit';

app.use(rateLimit({
  windowMs: 60000,  // 1 minute
  max: 100          // 100 requests per window
}));

session()

session: (options) => MiddlewareInterface

Session hook for global usage Registers with the hooks system for application-wide session management

Parameters

options

SessionOptions = {}

Returns

MiddlewareInterface

Example

import { session } from '@/middleware/built-in/session';

app.use(session({
  store: 'redis',
  cookie: { maxAge: 86400000 }
}));

sse()

sse: (options) => MiddlewareInterface

SSE hook for global usage Registers with the hooks system for application-wide Server-Sent Events support

Parameters

options

SSEOptions = {}

Returns

MiddlewareInterface

Example

import { sse } from '@/middleware/built-in/sse';

app.use(sse({
  heartbeat: 30000,
  retry: 3000,
  cors: true
}));

validation()

validation: () => MiddlewareInterface

Basic validation hook for global usage Registers with the hooks system for content-type checking

Returns

MiddlewareInterface

Example

import { validation } from '@/middleware/built-in/validation';

app.use(validation());