Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"./lint-rules/no-manual-transactions.grit",
"./lint-rules/no-inline-touch-cache.grit",
"./lint-rules/no-stderr-write-in-commands.grit",
"./lint-rules/no-args-join-in-release.grit"
"./lint-rules/no-args-join-in-release.grit",
"./lint-rules/no-generic-is-record.grit"
],
"files": {
// custom-ca.ts excluded: Biome's type analysis hits the 200k type limit
Expand Down
22 changes: 22 additions & 0 deletions lint-rules/no-generic-is-record.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
file($name, $body) where {
$name <: r".*src/.*",
$body <: contains or {
`function isRecord($args): $return_type { $function_body }` as $match,
`function isRecord($args) { $function_body }` as $match,
`async function isRecord($args): $return_type { $function_body }` as $match,
`async function isRecord($args) { $function_body }` as $match,
`const isRecord = ($args): $return_type => $implementation` as $match,
`const isRecord = ($args) => $implementation` as $match,
`const isRecord = $arg => $implementation` as $match,
`const isRecord: $type = ($args) => $implementation` as $match,
`const isRecord = function ($args) { $function_body }` as $match,
`const isRecord: $type = function ($args) { $function_body }` as $match,
`let isRecord = ($args): $return_type => $implementation` as $match,
`let isRecord = ($args) => $implementation` as $match,
`let isRecord = $arg => $implementation` as $match,
`let isRecord: $type = ($args) => $implementation` as $match,
`let isRecord = function ($args) { $function_body }` as $match,
`let isRecord: $type = function ($args) { $function_body }` as $match
},
register_diagnostic(span=$match, message="Use a local, shape-specific guard (for example, hasFileMap()) instead of the generic isRecord() name.")
}
4 changes: 2 additions & 2 deletions src/commands/alert/mutation-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ function parseJsonValue(raw: string, field: string): unknown {
}
}

function isRecord(value: unknown): value is Record<string, unknown> {
function isJsonObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}

function toObject(value: unknown, field: string): Record<string, unknown> {
if (!isRecord(value)) {
if (!isJsonObject(value)) {
throw new ValidationError(`${field} entries must be JSON objects.`, field);
}
return value;
Expand Down
78 changes: 53 additions & 25 deletions src/lib/api/replays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* Functions for listing and retrieving Session Replays.
*/

import {
type ListProjectReplayRecordingSegmentsResponse,
listProjectReplayRecordingSegments,
} from "@sentry/api";
import { zListProjectReplayRecordingSegmentsResponse } from "@sentry/api/zod";
import type { z } from "zod";
import {
REPLAY_LIST_FIELDS,
Expand All @@ -14,19 +19,20 @@ import {
type ReplayListItem,
type ReplayListResponse,
ReplayListResponseSchema,
type ReplayRecordingSegments,
ReplayRecordingSegmentsSchema,
} from "../../types/index.js";

import { ApiError } from "../errors.js";
import { resolveOrgRegion } from "../region.js";

import {
API_MAX_PER_PAGE,
apiRequestToRegion,
autoPaginate,
getOrgSdkConfig,
MAX_PAGINATION_PAGES,
type PaginatedResponse,
parseLinkHeader,
unwrapPaginatedResult,
} from "./infrastructure.js";

/** Replay sort field names supported by the backend replay index endpoint. */
Expand Down Expand Up @@ -106,13 +112,31 @@ type FetchReplayPageOptions = {
};

type FetchReplayRecordingSegmentsPageOptions = {
regionUrl: string;
orgSlug: string;
projectSlugOrId: string;
replayId: string;
cursor?: string;
};

/**
* Validate the generated replay recording response before formatter code trusts
* its object boundary. The SDK invokes response validators outside its normal
* error-result path, so convert Zod failures to the CLI's API error type here.
*/
async function validateReplayRecordingSegmentsResponse(
data: unknown
): Promise<void> {
const result =
await zListProjectReplayRecordingSegmentsResponse.safeParseAsync(data);
if (!result.success) {
throw new ApiError(
"Unexpected replay recording segments response",
0,
result.error.message
);
}
}

/** Options for {@link getReplayRecordingSegments}. */
export type GetReplayRecordingSegmentsOptions = {
/**
Expand Down Expand Up @@ -236,8 +260,7 @@ export async function getReplay(
* Fetch replay recording segments for a single replay.
*
* Uses the project-scoped replay endpoint because recording segments are
* partitioned by project. `download=true` matches the frontend contract and
* returns the parsed segment payload directly.
* partitioned by project.
*
* Uses a manual pagination loop rather than {@link autoPaginate} because
* `autoPaginate` trims results to `limit`, but `expectedSegments` is a soft
Expand All @@ -248,15 +271,13 @@ export async function getReplayRecordingSegments(
projectSlugOrId: string,
replayId: string,
options: GetReplayRecordingSegmentsOptions = {}
): Promise<ReplayRecordingSegments> {
const regionUrl = await resolveOrgRegion(orgSlug);
): Promise<ListProjectReplayRecordingSegmentsResponse> {
const expectedSegments = options.expectedSegments ?? Number.POSITIVE_INFINITY;
const segments: ReplayRecordingSegments = [];
const segments: ListProjectReplayRecordingSegmentsResponse = [];
let cursor: string | undefined;

for (let page = 0; page < MAX_PAGINATION_PAGES; page += 1) {
const { data, nextCursor } = await fetchReplayRecordingSegmentsPage({
regionUrl,
orgSlug,
projectSlugOrId,
replayId,
Expand All @@ -275,25 +296,32 @@ export async function getReplayRecordingSegments(
return segments;
}

/**
* Fetch one SDK-backed recording page while preserving its cursor metadata.
*/
async function fetchReplayRecordingSegmentsPage(
options: FetchReplayRecordingSegmentsPageOptions
): Promise<PaginatedResponse<ReplayRecordingSegments>> {
const { cursor, orgSlug, projectSlugOrId, regionUrl, replayId } = options;
const { data, headers } = await apiRequestToRegion<ReplayRecordingSegments>(
regionUrl,
`/projects/${orgSlug}/${projectSlugOrId}/replays/${replayId}/recording-segments/`,
{
params: {
cursor,
download: true,
per_page: API_MAX_PER_PAGE,
},
schema: ReplayRecordingSegmentsSchema,
}
);
): Promise<PaginatedResponse<ListProjectReplayRecordingSegmentsResponse>> {
const { cursor, orgSlug, projectSlugOrId, replayId } = options;
const config = await getOrgSdkConfig(orgSlug);
const result = await listProjectReplayRecordingSegments({
...config,
path: {
organization_id_or_slug: orgSlug,
project_id_or_slug: projectSlugOrId,
replay_id: replayId,
},
query: {
cursor,
per_page: API_MAX_PER_PAGE,
},
responseValidator: validateReplayRecordingSegmentsResponse,
});
Comment thread
betegon marked this conversation as resolved.

const { nextCursor } = parseLinkHeader(headers.get("link") ?? null);
return { data, nextCursor };
return unwrapPaginatedResult<ListProjectReplayRecordingSegmentsResponse>(
result,
"Failed to fetch replay recording segments"
);
}

/**
Expand Down
Loading
Loading