# Middleware API reference

> Exports of @anymux/middleware.

Source: https://anymux-docs.n.yatsyk.com/docs/middleware/jsdoc

## Classes

### MemoryTokenStore

```ts
class MemoryTokenStore
```

- `get(key: string): Promise<TokenData | undefined>`
- `set(key: string, token: TokenData): Promise<void>`
- `delete(key: string): Promise<boolean>`
- `clear(): Promise<void>`

### DatabaseTokenStore

```ts
class DatabaseTokenStore
```

- `constructor(db: KeyValueDB, prefix?: string): DatabaseTokenStore`
- `get(key: string): Promise<TokenData | undefined>`
- `set(key: string, token: TokenData): Promise<void>`
- `delete(key: string): Promise<boolean>`
- `clear(): Promise<void>`

## Functions

### createOAuthMiddleware

```ts
createOAuthMiddleware(config: OAuthConfig, store: TokenStore): Hono<import("/home/andrew/gits/github/AnyFileSystem/AnyFsMnoorepo-sf-phase3-npmf/node_modules/.pnpm/hono@4.12.5/node_modules/hono/dist/types/types").BlankEnv, import("/home/andrew/gits/github/AnyFileSystem/AnyFsMnoorepo-sf-phase3-npmf/node_modules/.pnpm/hono@4.12.5/node_modules/hono/dist/types/types").BlankSchema, "/">
```

### createCorsProxy

```ts
createCorsProxy(config: CorsConfig): MiddlewareHandler
```

### createTokenRefresh

```ts
createTokenRefresh(config: TokenRefreshConfig): MiddlewareHandler
```

### toExpressMiddleware

```ts
toExpressMiddleware(honoApp: Hono): (req: IncomingMessage, res: ServerResponse, next: () => void) => Promise<void>
```

### toNextHandler

```ts
toNextHandler(honoApp: Hono): { GET: (req: Request) => Promise<Response>; POST: (req: Request) => Promise<Response>; PUT: (req: Request) => Promise<Response>; DELETE: (req: Request) => Promise<Response>; PATCH: (req: Request) => Promise<Response>; OPTIONS: (req: Request) => Promise<Response>; HEAD: (req: Request) => Promise<Response>; }
```

## Types

### OAuthConfig

```ts
type OAuthConfig = { clientId?: string; clientSecret?: string; authorizationUrl?: string; tokenUrl?: string; callbackUrl?: string; scopes?: string[]; state?: string; }
```

### CorsConfig

```ts
type CorsConfig = { allowedOrigins?: string[]; allowedMethods?: string[]; allowedHeaders?: string[]; credentials?: boolean; maxAge?: number; }
```

## Interfaces

### TokenData

```ts
interface TokenData
```

- `accessToken: string`
- `refreshToken: string`
- `expiresAt: number`
- `tokenType: string`
- `scope: string`

### TokenStore

```ts
interface TokenStore
```

- `get(key: string): Promise<TokenData | undefined>`
- `set(key: string, token: TokenData): Promise<void>`
- `delete(key: string): Promise<boolean>`
- `clear(): Promise<void>`

### KeyValueDB

```ts
interface KeyValueDB
```

- `get(key: string): Promise<string | undefined>`
- `set(key: string, value: string): Promise<void>`
- `delete(key: string): Promise<boolean>`
- `keys(): Promise<string[]>`

### TokenRefreshConfig

```ts
interface TokenRefreshConfig
```

- `tokenUrl: string`
- `clientId: string`
- `clientSecret: string`
- `store: TokenStore`

## Constants

### OAuthConfigSchema

```ts
const OAuthConfigSchema: z.ZodObject<{ clientId: z.ZodString; clientSecret: z.ZodString; authorizationUrl: z.ZodString; tokenUrl: z.ZodString; callbackUrl: z.ZodString; scopes: z.ZodArray<z.ZodString, "many">; state: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { clientId?: string; clientSecret?: string; authorizationUrl?: string; tokenUrl?: string; callbackUrl?: string; scopes?: string[]; state?: string; }, { clientId?: string; clientSecret?: string; authorizationUrl?: string; tokenUrl?: string; callbackUrl?: string; scopes?: string[]; state?: string; }>
```

### CorsConfigSchema

```ts
const CorsConfigSchema: z.ZodObject<{ allowedOrigins: z.ZodArray<z.ZodString, "many">; allowedMethods: z.ZodDefault<z.ZodArray<z.ZodString, "many">>; allowedHeaders: z.ZodDefault<z.ZodArray<z.ZodString, "many">>; credentials: z.ZodDefault<z.ZodBoolean>; maxAge: z.ZodDefault<z.ZodNumber>; }, "strip", z.ZodTypeAny, { allowedOrigins?: string[]; allowedMethods?: string[]; allowedHeaders?: string[]; credentials?: boolean; maxAge?: number; }, { allowedOrigins?: string[]; allowedMethods?: string[]; allowedHeaders?: string[]; credentials?: boolean; maxAge?: number; }>
```
