forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity-feed.cursor.ts
More file actions
33 lines (29 loc) · 909 Bytes
/
Copy pathactivity-feed.cursor.ts
File metadata and controls
33 lines (29 loc) · 909 Bytes
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
import { BadRequestException } from "@nestjs/common";
export interface ActivityCursor {
createdAt: Date;
id: string;
}
interface CursorPayload {
createdAt: string;
id: string;
}
export function encodeActivityCursor(createdAt: Date, id: string): string {
const payload: CursorPayload = {
createdAt: createdAt.toISOString(),
id,
};
return Buffer.from(JSON.stringify(payload)).toString("base64url");
}
export function decodeActivityCursor(cursor: string): ActivityCursor {
try {
const raw = Buffer.from(cursor, "base64url").toString("utf8");
const parsed = JSON.parse(raw) as Partial<CursorPayload>;
const createdAt = new Date(parsed.createdAt ?? "");
if (!parsed.id || Number.isNaN(createdAt.getTime())) {
throw new Error("Malformed cursor");
}
return { createdAt, id: parsed.id };
} catch {
throw new BadRequestException("Invalid cursor");
}
}