Skip to content

Update routes option within adapter builders to allow multiple service functions #1464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 29 additions & 3 deletions packages/connect-express/src/express-connect-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import type { JsonValue } from "@bufbuild/protobuf";
import { createConnectRouter, Code, ConnectError } from "@connectrpc/connect";
import type {
ConnectRouter,
ConnectRouterOptions,
ContextValues,
RouteFn,
} from "@connectrpc/connect";
import type { UniversalHandler } from "@connectrpc/connect/protocol";
import {
Expand All @@ -43,8 +43,23 @@ interface ExpressConnectMiddlewareOptions extends ConnectRouterOptions {
* ```
*
* Then pass this function here.
*
* For more complex setups with multiple services, you may pass them as an array, like so:
*
* ```ts
* import ElizaRouter from "./eliza.ts";
* import HelloWorldRouter from "./hello-world.ts";
*
* const adapter = expressConnectMiddleware({
* routes: [
* ElizaRouter,
* HelloWorldRouter
* ]
* });
*
* ```
*/
routes: (router: ConnectRouter) => void;
routes: RouteFn | RouteFn[];

/**
* Serve all handlers under this prefix. For example, the prefix "/something"
Expand All @@ -68,8 +83,19 @@ export function expressConnectMiddleware(
if (options.acceptCompression === undefined) {
options.acceptCompression = [compressionGzip, compressionBrotli];
}

const router = createConnectRouter(options);
options.routes(router);

if (Array.isArray(options.routes)) {
// options.routes is an array of functions
for (const routeFn of options.routes) {
routeFn(router);
}
} else {
// options.routes is function
options.routes(router);
}

const prefix = options.requestPathPrefix ?? "";
const paths = new Map<string, UniversalHandler>();
for (const uHandler of router.handlers) {
Expand Down
31 changes: 28 additions & 3 deletions packages/connect-fastify/src/fastify-connect-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import type { JsonValue } from "@bufbuild/protobuf";
import { Code, ConnectError, createConnectRouter } from "@connectrpc/connect";
import { createLinkedAbortController } from "@connectrpc/connect/protocol";
import type {
ConnectRouter,
ConnectRouterOptions,
ContextValues,
RouteFn,
} from "@connectrpc/connect";
import * as protoConnect from "@connectrpc/connect/protocol-connect";
import * as protoGrpcWeb from "@connectrpc/connect/protocol-grpc-web";
Expand Down Expand Up @@ -46,8 +46,23 @@ interface FastifyConnectPluginOptions extends ConnectRouterOptions {
* ```
*
* Then pass this function here.
*
* For more complex setups with multiple services, you may pass them as an array, like so:
*
* ```ts
* import ElizaRouter from "./eliza.ts";
* import HelloWorldRouter from "./hello-world.ts";
*
* const adapter = fastifyConnectPlugin({
* routes: [
* ElizaRouter,
* HelloWorldRouter
* ]
* });
*
* ```
*/
routes?: (router: ConnectRouter) => void;
routes?: RouteFn | RouteFn[];

/**
* If set, once `fastify.close` is called, waits for the requests to be finished for the specified duration
Expand Down Expand Up @@ -93,8 +108,18 @@ export function fastifyConnectPlugin(
done();
});
}

const router = createConnectRouter(opts);
opts.routes(router);

if (Array.isArray(opts.routes)) {
// options.routes is an array of functions
for (const routeFn of opts.routes) {
routeFn(router);
}
} else {
// options.routes is function
opts.routes(router);
}

const uHandlers = router.handlers;
if (uHandlers.length == 0) {
Expand Down
32 changes: 29 additions & 3 deletions packages/connect-next/src/connect-nextjs-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import { createConnectRouter } from "@connectrpc/connect";
import type {
ConnectRouter,
ConnectRouterOptions,
ContextValues,
RouteFn,
} from "@connectrpc/connect";
import type { UniversalHandler } from "@connectrpc/connect/protocol";
import {
Expand Down Expand Up @@ -49,8 +49,23 @@ interface NextJsApiRouterOptions extends ConnectRouterOptions {
* ```
*
* Then pass this function here.
*
* For more complex setups with multiple services, you may pass them as an array, like so:
*
* ```ts
* import ElizaRouter from "./eliza.ts";
* import HelloWorldRouter from "./hello-world.ts";
*
* const adapter = nextJsApiRouter({
* routes: [
* ElizaRouter,
* HelloWorldRouter
* ]
* });
*
* ```
*/
routes: (router: ConnectRouter) => void;
routes: RouteFn | RouteFn[];

/**
* Serve all handlers under this prefix. For example, the prefix "/something"
Expand All @@ -73,8 +88,19 @@ export function nextJsApiRouter(options: NextJsApiRouterOptions): ApiRoute {
if (options.acceptCompression === undefined) {
options.acceptCompression = [compressionGzip, compressionBrotli];
}

const router = createConnectRouter(options);
options.routes(router);

if (Array.isArray(options.routes)) {
// options.routes is an array of functions
for (const routeFn of options.routes) {
routeFn(router);
}
} else {
// options.routes is function
options.routes(router);
}

const prefix = options.prefix ?? "/api";
const paths = new Map<string, UniversalHandler>();
for (const uHandler of router.handlers) {
Expand Down
32 changes: 29 additions & 3 deletions packages/connect-node/src/connect-node-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import { Code, ConnectError, createConnectRouter } from "@connectrpc/connect";
import type {
ConnectRouter,
ConnectRouterOptions,
ContextValues,
RouteFn,
} from "@connectrpc/connect";
import type { UniversalHandler } from "@connectrpc/connect/protocol";
import { uResponseNotFound } from "@connectrpc/connect/protocol";
Expand Down Expand Up @@ -46,8 +46,23 @@ export interface ConnectNodeAdapterOptions extends ConnectRouterOptions {
* ```
*
* Then pass this function here.
*
* For more complex setups with multiple services, you may pass them as an array, like so:
*
* ```ts
* import ElizaRouter from "./eliza.ts";
* import HelloWorldRouter from "./hello-world.ts";
*
* const adapter = createNodeAdapter({
* routes: [
* ElizaRouter,
* HelloWorldRouter
* ]
* });
*
* ```
*/
routes: (router: ConnectRouter) => void;
routes: RouteFn | RouteFn[];
/**
* If none of the handler request paths match, a 404 is served. This option
* can provide a custom fallback for this case.
Expand Down Expand Up @@ -77,8 +92,19 @@ export function connectNodeAdapter(
if (options.acceptCompression === undefined) {
options.acceptCompression = [compressionGzip, compressionBrotli];
}

const router = createConnectRouter(options);
options.routes(router);

if (Array.isArray(options.routes)) {
// options.routes is an array of functions
for (const routeFn of options.routes) {
routeFn(router);
}
} else {
// options.routes is function
options.routes(router);
}

const prefix = options.requestPathPrefix ?? "";
const paths = new Map<string, UniversalHandler>();
for (const uHandler of router.handlers) {
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type {
HandlerContext,
} from "./implementation.js";
export { createConnectRouter } from "./router.js";
export type { ConnectRouter, ConnectRouterOptions } from "./router.js";
export type { ConnectRouter, ConnectRouterOptions, RouteFn } from "./router.js";
export { createHandlerContext } from "./implementation.js";
export { cors } from "./cors.js";
export { createContextKey, createContextValues } from "./context-values.js";
Expand Down
5 changes: 5 additions & 0 deletions packages/connect/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export interface ConnectRouter {
) => this;
}

/**
* To be used primarily in the context of Adapters, provides a common type.
*/
export type RouteFn = (router: ConnectRouter) => void;

/**
* Options for a ConnectRouter. By default, all three protocols gRPC, gRPC-web,
* and Connect are enabled.
Expand Down