Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/api-mux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# `@lowerdeck/api-mux`

Route multiplexer for API endpoints. Dispatches requests to different services based on domain, path, and HTTP method with support for fallback handlers.

## Installation

```bash
npm install @lowerdeck/api-mux
yarn add @lowerdeck/api-mux
bun add @lowerdeck/api-mux
pnpm add @lowerdeck/api-mux
```

## Usage

```typescript
import { apiMux } from '@lowerdeck/api-mux';

// Define your services
const mux = apiMux([
{
domains: ['api.example.com'],
methods: ['GET', 'POST'],
endpoint: {
path: '/users',
fetch: async (req) => {
return new Response('Users API');
}
}
},
{
methods: ['POST'],
endpoint: {
path: '/webhooks',
exact: true, // Only match exact path
fetch: async (req) => {
return new Response('Webhook handler');
}
}
}
], async (req, server) => {
// Fallback handler for unmatched routes
return new Response('Not found', { status: 404 });
});

// Use with your HTTP server
const server = Bun.serve({
fetch: mux
});
```

## License

This project is licensed under the Apache License 2.0.

<div align="center">
<sub>Built with ❤️ by <a href="https://metorial.com">Metorial</a></sub>
</div>
35 changes: 35 additions & 0 deletions packages/api-mux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@lowerdeck/api-mux",
"version": "1.0.0",
"publishConfig": {
"access": "public"
},
"author": "Tobias Herber",
"license": "Apache 2",
"type": "module",
"source": "src/index.ts",
"exports": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.module.js",
"default": "./dist/index.module.js"
},
"main": "./dist/index.cjs",
"module": "./dist/index.module.js",
"types": "dist/index.d.ts",
"unpkg": "./dist/index.umd.js",
"scripts": {
"test": "vitest run --passWithNoTests",
"lint": "prettier src/**/*.ts --check",
"build": "microbundle"
},
"devDependencies": {
"microbundle": "^0.15.1",
"@lowerdeck/tsconfig": "^1.0.0",
"typescript": "^5.9.3",
"vitest": "^3.2.4"
},
"dependencies": {
"@lowerdeck/error": "^1.0.5"
}
}
1 change: 1 addition & 0 deletions packages/api-mux/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mux';
54 changes: 54 additions & 0 deletions packages/api-mux/src/mux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { notFoundError } from '@lowerdeck/error';

export let apiMux = (
services: {
domains?: string[];
methods?: string[];
endpoint: { path: string | string[]; fetch: (req: any) => Promise<any>; exact?: boolean };
}[],
fallback?: (req: any, server: any) => Promise<any>
) => {
let servicesWithRegex = services.flatMap(({ domains, endpoint, methods }) =>
(Array.isArray(endpoint.path) ? endpoint.path : [endpoint.path]).map(path => {
return {
path,
domains,
endpoint,
exact: endpoint.exact,
methods: methods?.map(m => m.toUpperCase())
};
})
);

return (req: Request, server: any) => {
let url = new URL(req.url);
let host = (req.headers.get('x-host') ?? req.headers.get('host') ?? url.hostname).split(
':'
)[0];
url.host = host;

if (url.pathname == '/ping') {
return new Response('OK') as any;
}

for (let { domains, endpoint, path, methods, exact } of servicesWithRegex) {
if (domains && !domains.includes(host)) continue;

if (
(url.pathname == path || (!exact && url.pathname.startsWith(`${path}/`))) &&
(!methods || methods.includes(req.method))
) {
return endpoint.fetch(req);
}
}

if (fallback) return fallback(req, server);

return new Response(JSON.stringify(notFoundError('route').toResponse()), {
status: 404,
headers: {
'Content-Type': 'application/json'
}
}) as any;
};
};
13 changes: 13 additions & 0 deletions packages/api-mux/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@lowerdeck/tsconfig/base.json",
"exclude": [
"dist"
],
"include": [
"src"
],
"compilerOptions": {
"outDir": "dist"
}
}
11 changes: 11 additions & 0 deletions packages/api-mux/tsup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
splitting: false,
sourcemap: true,
clean: true,
bundle: true,
dts: true
});
Loading