Skip to content

fix(remix): Use generic types for ServerBuild argument and return #16336

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

Merged
merged 2 commits into from
May 20, 2025
Merged
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
25 changes: 9 additions & 16 deletions packages/remix/src/server/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,9 @@ function makeWrappedRootLoader() {
};
}

function wrapRequestHandler(
function wrapRequestHandler<T extends ServerBuild | (() => ServerBuild | Promise<ServerBuild>)>(
origRequestHandler: RequestHandler,
build:
| ServerBuild
| { build: ServerBuild }
| (() => ServerBuild | { build: ServerBuild } | Promise<ServerBuild | { build: ServerBuild }>),
build: T,
options?: {
instrumentTracing?: boolean;
},
Expand All @@ -278,7 +275,7 @@ function wrapRequestHandler(

// check if the build is nested under `build` key
if ('build' in resolvedBuild) {
resolvedRoutes = createRoutes(resolvedBuild.build.routes);
resolvedRoutes = createRoutes((resolvedBuild.build as ServerBuild).routes);
} else {
resolvedRoutes = createRoutes(resolvedBuild.routes);
}
Expand Down Expand Up @@ -407,12 +404,12 @@ function instrumentBuildCallback(
/**
* Instruments `remix` ServerBuild for performance tracing and error tracking.
*/
export function instrumentBuild(
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
export function instrumentBuild<T extends ServerBuild | (() => ServerBuild | Promise<ServerBuild>)>(
build: T,
options?: {
instrumentTracing?: boolean;
},
): ServerBuild | (() => ServerBuild | Promise<ServerBuild>) {
): T {
if (typeof build === 'function') {
return function () {
const resolvedBuild = build();
Expand All @@ -424,19 +421,15 @@ export function instrumentBuild(
} else {
return instrumentBuildCallback(resolvedBuild, options);
}
};
} as T;
} else {
return instrumentBuildCallback(build, options);
return instrumentBuildCallback(build, options) as T;
}
}

export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?: boolean }) =>
function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction {
return function (
this: unknown,
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
...args: unknown[]
): RequestHandler {
return function (this: unknown, build, ...args: unknown[]): RequestHandler {
const newBuild = instrumentBuild(build, options);
const requestHandler = origCreateRequestHandler.call(this, newBuild, ...args);

Expand Down
Loading