Skip to content

Commit ad506ac

Browse files
committed
Use generic types instead
1 parent 75bb6e3 commit ad506ac

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

packages/remix/src/cloudflare/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ServerBuild } from '@remix-run/server-runtime';
21
import {
32
instrumentBuild as instrumentRemixBuild,
43
makeWrappedCreateRequestHandler,
@@ -15,14 +14,11 @@ export { makeWrappedCreateRequestHandler, sentryHandleError };
1514
* Instruments a Remix build to capture errors and performance data.
1615
* @param build The Remix build to instrument.
1716
* @returns The instrumented Remix build.
18-
*
19-
* Note: CreateRequestHandlerFunction from @shopify/remix-oxygen accepts a ServerBuild, not a function unlike the rest of the Remix ecosystem
20-
* That's why we accept and return a ServerBuild type here.
2117
*/
22-
export const instrumentBuild = (build: ServerBuild): ServerBuild => {
18+
export const instrumentBuild: typeof instrumentRemixBuild = build => {
2319
return instrumentRemixBuild(build, {
2420
instrumentTracing: true,
25-
}) as ServerBuild;
21+
});
2622
};
2723

2824
export type {

packages/remix/src/server/instrumentServer.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,9 @@ function makeWrappedRootLoader() {
246246
};
247247
}
248248

249-
function wrapRequestHandler(
249+
function wrapRequestHandler<T extends ServerBuild | (() => ServerBuild | Promise<ServerBuild>)>(
250250
origRequestHandler: RequestHandler,
251-
build:
252-
| ServerBuild
253-
| { build: ServerBuild }
254-
| (() => ServerBuild | { build: ServerBuild } | Promise<ServerBuild | { build: ServerBuild }>),
251+
build: T,
255252
options?: {
256253
instrumentTracing?: boolean;
257254
},
@@ -278,7 +275,7 @@ function wrapRequestHandler(
278275

279276
// check if the build is nested under `build` key
280277
if ('build' in resolvedBuild) {
281-
resolvedRoutes = createRoutes(resolvedBuild.build.routes);
278+
resolvedRoutes = createRoutes((resolvedBuild.build as ServerBuild).routes);
282279
} else {
283280
resolvedRoutes = createRoutes(resolvedBuild.routes);
284281
}
@@ -407,12 +404,12 @@ function instrumentBuildCallback(
407404
/**
408405
* Instruments `remix` ServerBuild for performance tracing and error tracking.
409406
*/
410-
export function instrumentBuild(
411-
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
407+
export function instrumentBuild<T extends ServerBuild | (() => ServerBuild | Promise<ServerBuild>)>(
408+
build: T,
412409
options?: {
413410
instrumentTracing?: boolean;
414411
},
415-
): ServerBuild | (() => ServerBuild | Promise<ServerBuild>) {
412+
): T {
416413
if (typeof build === 'function') {
417414
return function () {
418415
const resolvedBuild = build();
@@ -424,9 +421,9 @@ export function instrumentBuild(
424421
} else {
425422
return instrumentBuildCallback(resolvedBuild, options);
426423
}
427-
};
424+
} as T;
428425
} else {
429-
return instrumentBuildCallback(build, options);
426+
return instrumentBuildCallback(build, options) as T;
430427
}
431428
}
432429

0 commit comments

Comments
 (0)