-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.ts
More file actions
53 lines (47 loc) · 2.01 KB
/
utils.ts
File metadata and controls
53 lines (47 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { ActionFunctionArgs, LoaderFunctionArgs, ServerBuild } from '@remix-run/node';
import type { AgnosticRouteObject } from '@remix-run/router';
import type { Span, TransactionSource } from '@sentry/core';
import { logger } from '@sentry/core';
import { DEBUG_BUILD } from './debug-build';
import { getRequestMatch, matchServerRoutes } from './vendor/response';
type ServerRouteManifest = ServerBuild['routes'];
/**
*
*/
export async function storeFormDataKeys(args: LoaderFunctionArgs | ActionFunctionArgs, span: Span): Promise<void> {
try {
// We clone the request for Remix be able to read the FormData later.
const clonedRequest = args.request.clone();
// This only will return the last name of multiple file uploads in a single FormData entry.
// We can switch to `unstable_parseMultipartFormData` when it's stable.
// https://remix.run/docs/en/main/utils/parse-multipart-form-data#unstable_parsemultipartformdata
const formData = await clonedRequest.formData();
formData.forEach((value, key) => {
span.setAttribute(`remix.action_form_data.${key}`, typeof value === 'string' ? value : '[non-string value]');
});
} catch (e) {
DEBUG_BUILD && logger.warn('Failed to read FormData from request', e);
}
}
/**
* Get transaction name from routes and url
*/
export function getTransactionName(routes: AgnosticRouteObject[], url: URL): [string, TransactionSource] {
const matches = matchServerRoutes(routes, url.pathname);
const match = matches && getRequestMatch(url, matches);
return match === null ? [url.pathname, 'url'] : [match.route.id || 'no-route-id', 'route'];
}
/**
* Creates routes from the server route manifest
*
* @param manifest
* @param parentId
*/
export function createRoutes(manifest: ServerRouteManifest, parentId?: string): AgnosticRouteObject[] {
return Object.entries(manifest)
.filter(([, route]) => route.parentId === parentId)
.map(([id, route]) => ({
...route,
children: createRoutes(manifest, id),
})) as AgnosticRouteObject[];
}