Skip to content

Commit 4a88835

Browse files
authored
fix(remix): Use generic types for ServerBuild argument and return (#16336)
Closes: #15615 Ref: #15615 (comment) Looks like `createRequestHandler` exported from `@shopify/remix-oxygen` does not accept a function that returns/resolves a `ServerBuild`. [It only accepts a plain `ServerBuild`](https://github.com/Shopify/hydrogen/blob/a7e33c1dd45e3c7c27ab2e1125851468051cee0b/packages/remix-oxygen/src/server.ts#L20).
1 parent 4de9878 commit 4a88835

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

packages/remix/src/server/instrumentServer.ts

Lines changed: 9 additions & 16 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,19 +421,15 @@ 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

433430
export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?: boolean }) =>
434431
function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction {
435-
return function (
436-
this: unknown,
437-
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
438-
...args: unknown[]
439-
): RequestHandler {
432+
return function (this: unknown, build, ...args: unknown[]): RequestHandler {
440433
const newBuild = instrumentBuild(build, options);
441434
const requestHandler = origCreateRequestHandler.call(this, newBuild, ...args);
442435

0 commit comments

Comments
 (0)