Skip to content

Commit db2f604

Browse files
authored
fix(remix): Remove vendored types (#16218)
Removed the vendored types and started importing them from Remix v2 packages. Also added `@remix-run/server-runtime` as `devDependency` and `peerDependency`
1 parent f2afa29 commit db2f604

File tree

6 files changed

+40
-298
lines changed

6 files changed

+40
-298
lines changed

packages/remix/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@
7878
"devDependencies": {
7979
"@remix-run/node": "^2.15.2",
8080
"@remix-run/react": "^2.15.2",
81+
"@remix-run/server-runtime": "2.15.2",
8182
"@types/express": "^4.17.14",
8283
"vite": "^5.4.11"
8384
},
8485
"peerDependencies": {
8586
"@remix-run/node": "2.x",
8687
"@remix-run/react": "2.x",
88+
"@remix-run/server-runtime": "2.x",
8789
"react": "18.x"
8890
},
8991
"scripts": {

packages/remix/src/server/errors.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type {
2+
ActionFunction,
23
ActionFunctionArgs,
34
EntryContext,
45
HandleDocumentRequestFunction,
6+
LoaderFunction,
57
LoaderFunctionArgs,
68
} from '@remix-run/node';
9+
import { isRouteErrorResponse } from '@remix-run/router';
710
import type { RequestEventData, Span } from '@sentry/core';
811
import {
912
addExceptionMechanism,
@@ -17,8 +20,9 @@ import {
1720
import { DEBUG_BUILD } from '../utils/debug-build';
1821
import type { RemixOptions } from '../utils/remixOptions';
1922
import { storeFormDataKeys } from '../utils/utils';
20-
import { extractData, isResponse, isRouteErrorResponse } from '../utils/vendor/response';
21-
import type { DataFunction, RemixRequest } from '../utils/vendor/types';
23+
import { extractData, isResponse } from '../utils/vendor/response';
24+
25+
type DataFunction = LoaderFunction | ActionFunction;
2226

2327
/**
2428
* Captures an exception happened in the Remix server.
@@ -87,7 +91,7 @@ export function errorHandleDocumentRequestFunction(
8791
this: unknown,
8892
origDocumentRequestFunction: HandleDocumentRequestFunction,
8993
requestContext: {
90-
request: RemixRequest;
94+
request: Request;
9195
responseStatusCode: number;
9296
responseHeaders: Headers;
9397
context: EntryContext;

packages/remix/src/server/instrumentServer.ts

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
/* eslint-disable max-lines */
2+
import type { AgnosticRouteObject } from '@remix-run/router';
3+
import { isDeferredData, isRouteErrorResponse } from '@remix-run/router';
4+
import type {
5+
ActionFunction,
6+
ActionFunctionArgs,
7+
AppLoadContext,
8+
CreateRequestHandlerFunction,
9+
EntryContext,
10+
HandleDocumentRequestFunction,
11+
LoaderFunction,
12+
LoaderFunctionArgs,
13+
RequestHandler,
14+
ServerBuild,
15+
} from '@remix-run/server-runtime';
216
import type { RequestEventData, Span, TransactionSource, WrappedFunction } from '@sentry/core';
317
import {
418
continueTrace,
@@ -22,23 +36,15 @@ import {
2236
} from '@sentry/core';
2337
import { DEBUG_BUILD } from '../utils/debug-build';
2438
import { createRoutes, getTransactionName } from '../utils/utils';
25-
import { extractData, isDeferredData, isResponse, isRouteErrorResponse, json } from '../utils/vendor/response';
26-
import type {
27-
AppData,
28-
AppLoadContext,
29-
CreateRequestHandlerFunction,
30-
DataFunction,
31-
DataFunctionArgs,
32-
EntryContext,
33-
HandleDocumentRequestFunction,
34-
RemixRequest,
35-
RequestHandler,
36-
ServerBuild,
37-
ServerRoute,
38-
ServerRouteManifest,
39-
} from '../utils/vendor/types';
39+
import { extractData, isResponse, json } from '../utils/vendor/response';
4040
import { captureRemixServerException, errorHandleDataFunction, errorHandleDocumentRequestFunction } from './errors';
4141

42+
type AppData = unknown;
43+
type RemixRequest = Parameters<RequestHandler>[0];
44+
type ServerRouteManifest = ServerBuild['routes'];
45+
type DataFunction = LoaderFunction | ActionFunction;
46+
type DataFunctionArgs = LoaderFunctionArgs | ActionFunctionArgs;
47+
4248
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
4349
function isRedirectResponse(response: Response): boolean {
4450
return redirectStatusCodes.has(response.status);
@@ -261,7 +267,7 @@ function wrapRequestHandler(
261267
return origRequestHandler.call(this, request, loadContext);
262268
}
263269

264-
let resolvedRoutes: ServerRoute[] | undefined;
270+
let resolvedRoutes: AgnosticRouteObject[] | undefined;
265271

266272
if (options?.instrumentTracing) {
267273
if (typeof build === 'function') {
@@ -428,7 +434,7 @@ export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?:
428434
function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction {
429435
return function (
430436
this: unknown,
431-
build: ServerBuild | (() => Promise<ServerBuild>),
437+
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
432438
...args: unknown[]
433439
): RequestHandler {
434440
const newBuild = instrumentBuild(build, options);

packages/remix/src/utils/utils.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
1+
import type { ActionFunctionArgs, LoaderFunctionArgs, ServerBuild } from '@remix-run/node';
2+
import type { AgnosticRouteObject } from '@remix-run/router';
23
import type { Span, TransactionSource } from '@sentry/core';
34
import { logger } from '@sentry/core';
45
import { DEBUG_BUILD } from './debug-build';
56
import { getRequestMatch, matchServerRoutes } from './vendor/response';
6-
import type { ServerRoute, ServerRouteManifest } from './vendor/types';
7+
8+
type ServerRouteManifest = ServerBuild['routes'];
79

810
/**
911
*
@@ -29,7 +31,7 @@ export async function storeFormDataKeys(args: LoaderFunctionArgs | ActionFunctio
2931
/**
3032
* Get transaction name from routes and url
3133
*/
32-
export function getTransactionName(routes: ServerRoute[], url: URL): [string, TransactionSource] {
34+
export function getTransactionName(routes: AgnosticRouteObject[], url: URL): [string, TransactionSource] {
3335
const matches = matchServerRoutes(routes, url.pathname);
3436
const match = matches && getRequestMatch(url, matches);
3537
return match === null ? [url.pathname, 'url'] : [match.route.id || 'no-route-id', 'route'];
@@ -41,11 +43,11 @@ export function getTransactionName(routes: ServerRoute[], url: URL): [string, Tr
4143
* @param manifest
4244
* @param parentId
4345
*/
44-
export function createRoutes(manifest: ServerRouteManifest, parentId?: string): ServerRoute[] {
46+
export function createRoutes(manifest: ServerRouteManifest, parentId?: string): AgnosticRouteObject[] {
4547
return Object.entries(manifest)
4648
.filter(([, route]) => route.parentId === parentId)
4749
.map(([id, route]) => ({
4850
...route,
4951
children: createRoutes(manifest, id),
50-
}));
52+
})) as AgnosticRouteObject[];
5153
}

packages/remix/src/utils/vendor/response.ts

+1-38
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import type { AgnosticRouteMatch, AgnosticRouteObject } from '@remix-run/router';
1010
import { matchRoutes } from '@remix-run/router';
11-
import type { DeferredData, ErrorResponse, ServerRoute } from './types';
12-
1311
/**
1412
* Based on Remix Implementation
1513
*
@@ -76,7 +74,7 @@ export const json: JsonFunction = (data, init = {}) => {
7674
* Changed so that `matchRoutes` function is passed in.
7775
*/
7876
export function matchServerRoutes(
79-
routes: ServerRoute[],
77+
routes: AgnosticRouteObject[],
8078
pathname: string,
8179
): AgnosticRouteMatch<string, AgnosticRouteObject>[] | null {
8280
const matches = matchRoutes(routes, pathname);
@@ -126,38 +124,3 @@ export function getRequestMatch(
126124

127125
return match;
128126
}
129-
130-
/**
131-
* https://github.com/remix-run/remix/blob/3e589152bc717d04e2054c31bea5a1056080d4b9/packages/remix-server-runtime/responses.ts#L75-L85
132-
*/
133-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
134-
export function isDeferredData(value: any): value is DeferredData {
135-
const deferred: DeferredData = value;
136-
return (
137-
deferred &&
138-
typeof deferred === 'object' &&
139-
typeof deferred.data === 'object' &&
140-
typeof deferred.subscribe === 'function' &&
141-
typeof deferred.cancel === 'function' &&
142-
typeof deferred.resolveData === 'function'
143-
);
144-
}
145-
146-
/**
147-
* https://github.com/remix-run/react-router/blob/f9b3dbd9cbf513366c456b33d95227f42f36da63/packages/router/utils.ts#L1574
148-
*
149-
* Check if the given error is an ErrorResponse generated from a 4xx/5xx
150-
* Response thrown from an action/loader
151-
*/
152-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
153-
export function isRouteErrorResponse(value: any): value is ErrorResponse {
154-
const error: ErrorResponse = value;
155-
156-
return (
157-
error != null &&
158-
typeof error.status === 'number' &&
159-
typeof error.statusText === 'string' &&
160-
typeof error.internal === 'boolean' &&
161-
'data' in error
162-
);
163-
}

0 commit comments

Comments
 (0)