Skip to content

Commit fea71ce

Browse files
committed
fix(remix): Lighten vendored types
1 parent 33e0686 commit fea71ce

File tree

6 files changed

+43
-269
lines changed

6 files changed

+43
-269
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type {
2+
ActionFunction,
23
ActionFunctionArgs,
34
EntryContext,
45
HandleDocumentRequestFunction,
6+
LoaderFunction,
57
LoaderFunctionArgs,
68
} from '@remix-run/node';
79
import type { RequestEventData, Span } from '@sentry/core';
@@ -18,7 +20,8 @@ import { DEBUG_BUILD } from '../utils/debug-build';
1820
import type { RemixOptions } from '../utils/remixOptions';
1921
import { storeFormDataKeys } from '../utils/utils';
2022
import { extractData, isResponse, isRouteErrorResponse } from '../utils/vendor/response';
21-
import type { DataFunction, RemixRequest } from '../utils/vendor/types';
23+
24+
type DataFunction = LoaderFunction | ActionFunction;
2225

2326
/**
2427
* Captures an exception happened in the Remix server.
@@ -87,7 +90,7 @@ export function errorHandleDocumentRequestFunction(
8790
this: unknown,
8891
origDocumentRequestFunction: HandleDocumentRequestFunction,
8992
requestContext: {
90-
request: RemixRequest;
93+
request: Request;
9194
responseStatusCode: number;
9295
responseHeaders: Headers;
9396
context: EntryContext;

packages/remix/src/server/instrumentServer.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
/* eslint-disable max-lines */
2+
import type { AgnosticRouteObject } from '@remix-run/router';
3+
import type {
4+
ActionFunction,
5+
ActionFunctionArgs,
6+
AppLoadContext,
7+
CreateRequestHandlerFunction,
8+
EntryContext,
9+
HandleDocumentRequestFunction,
10+
LoaderFunction,
11+
LoaderFunctionArgs,
12+
RequestHandler,
13+
ServerBuild,
14+
} from '@remix-run/server-runtime';
215
import type { RequestEventData, Span, TransactionSource, WrappedFunction } from '@sentry/core';
316
import {
417
continueTrace,
@@ -23,22 +36,14 @@ import {
2336
import { DEBUG_BUILD } from '../utils/debug-build';
2437
import { createRoutes, getTransactionName } from '../utils/utils';
2538
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';
4039
import { captureRemixServerException, errorHandleDataFunction, errorHandleDocumentRequestFunction } from './errors';
4140

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

264-
let resolvedRoutes: ServerRoute[] | undefined;
269+
let resolvedRoutes: AgnosticRouteObject[] | undefined;
265270

266271
if (options?.instrumentTracing) {
267272
if (typeof build === 'function') {
@@ -428,7 +433,7 @@ export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?:
428433
function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction {
429434
return function (
430435
this: unknown,
431-
build: ServerBuild | (() => Promise<ServerBuild>),
436+
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
432437
...args: unknown[]
433438
): RequestHandler {
434439
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

+8-11
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
//
77
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88

9-
import type { AgnosticRouteMatch, AgnosticRouteObject } from '@remix-run/router';
9+
import type {
10+
AgnosticRouteMatch,
11+
AgnosticRouteObject,
12+
ErrorResponse,
13+
UNSAFE_DeferredData as DeferredData,
14+
} from '@remix-run/router';
1015
import { matchRoutes } from '@remix-run/router';
11-
import type { DeferredData, ErrorResponse, ServerRoute } from './types';
12-
1316
/**
1417
* Based on Remix Implementation
1518
*
@@ -76,7 +79,7 @@ export const json: JsonFunction = (data, init = {}) => {
7679
* Changed so that `matchRoutes` function is passed in.
7780
*/
7881
export function matchServerRoutes(
79-
routes: ServerRoute[],
82+
routes: AgnosticRouteObject[],
8083
pathname: string,
8184
): AgnosticRouteMatch<string, AgnosticRouteObject>[] | null {
8285
const matches = matchRoutes(routes, pathname);
@@ -153,11 +156,5 @@ export function isDeferredData(value: any): value is DeferredData {
153156
export function isRouteErrorResponse(value: any): value is ErrorResponse {
154157
const error: ErrorResponse = value;
155158

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-
);
159+
return error != null && typeof error.status === 'number' && typeof error.statusText === 'string' && 'data' in error;
163160
}

0 commit comments

Comments
 (0)