fix(custom-widgets): address review findings
This commit is contained in:
1 parent
349f889464
commit
6cca2bafe6
12 files changed
+158
-21
No files matched your search
@@ -96,7 +96,7 @@ Templates read \`data.requestId\`, \`status.requestId\`, \`options.name\`, and t
|
||||
<TextInput bind="search" label="Search" />
|
||||
<Pagination bind="page" resetKey={inputs.search} defaultValue={1} total={5} />
|
||||
<SubFetch requestId="search" trigger="manual" params={{ query: inputs.search }}>
|
||||
{(items) => <Stack>{(items ?? []).map(item => <Text key={item.id}>{item.name}</Text>)}</Stack>}
|
||||
{(result) => <Stack>{(result.results ?? []).map(item => <Text key={item.id}>{item.name}</Text>)}</Stack>}
|
||||
</SubFetch>
|
||||
\`\`\`
|
||||
|
||||
|
||||
@@ -12,6 +12,22 @@ const methods = new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
|
||||
const kinds = new Set(["query", "action"]);
|
||||
const permissions = new Set(["view", "modify", "full"]);
|
||||
|
||||
const parseConfirmation = (value: unknown): CustomJsxRequestCapability["confirmation"] => {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
||||
const record = value as Record<string, unknown>;
|
||||
if (typeof record.title !== "string" || typeof record.message !== "string") return undefined;
|
||||
if (record.confirmLabel !== undefined && typeof record.confirmLabel !== "string") return undefined;
|
||||
if (record.destructive !== undefined && typeof record.destructive !== "boolean") return undefined;
|
||||
|
||||
const confirmation: NonNullable<CustomJsxRequestCapability["confirmation"]> = {
|
||||
title: record.title,
|
||||
message: record.message,
|
||||
};
|
||||
if (typeof record.confirmLabel === "string") confirmation.confirmLabel = record.confirmLabel;
|
||||
if (typeof record.destructive === "boolean") confirmation.destructive = record.destructive;
|
||||
return confirmation;
|
||||
};
|
||||
|
||||
export function parseRequestCapabilities(value: unknown): CustomJsxRequestCapability[] {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return value.flatMap((candidate) => {
|
||||
@@ -34,10 +50,7 @@ export function parseRequestCapabilities(value: unknown): CustomJsxRequestCapabi
|
||||
method: record.method,
|
||||
trigger: record.trigger === "load" ? "load" : "manual",
|
||||
minimumBoardPermission: record.minimumBoardPermission,
|
||||
confirmation:
|
||||
record.confirmation && typeof record.confirmation === "object"
|
||||
? (record.confirmation as CustomJsxRequestCapability["confirmation"])
|
||||
: undefined,
|
||||
confirmation: parseConfirmation(record.confirmation),
|
||||
invalidates: Array.isArray(record.invalidates)
|
||||
? record.invalidates.filter((entry): entry is string => typeof entry === "string")
|
||||
: [],
|
||||
|
||||
@@ -75,6 +75,8 @@ export interface PreviewSessionStore {
|
||||
deleteSession(id: string): Promise<void>;
|
||||
appendJournal(id: string, value: unknown, maxEntries: number, ttlMs: number): Promise<void>;
|
||||
getJournal(id: string, maxEntries: number): Promise<unknown[]>;
|
||||
saveEvidence(id: string, key: string, value: unknown, ttlMs: number): Promise<void>;
|
||||
getEvidence(id: string): Promise<unknown[]>;
|
||||
}
|
||||
|
||||
export interface PreviewSessionServiceOptions {
|
||||
@@ -88,6 +90,7 @@ export interface PreviewSessionServiceOptions {
|
||||
export class CustomWidgetPreviewSessionService {
|
||||
private readonly sessions = new Map<string, CustomWidgetPreviewSession>();
|
||||
private readonly journals = new Map<string, CustomWidgetPreviewJournalEntry[]>();
|
||||
private readonly evidence = new Map<string, Map<string, CustomWidgetPreviewJournalEntry>>();
|
||||
private readonly now: () => number;
|
||||
|
||||
public constructor(private readonly options: PreviewSessionServiceOptions) {
|
||||
@@ -128,7 +131,11 @@ export class CustomWidgetPreviewSessionService {
|
||||
const parsed = sessionSchema.safeParse(parseStoredValue(candidate));
|
||||
if (!parsed.success || parsed.data.expiresAt <= this.now()) {
|
||||
if (this.options.store) await this.options.store.deleteSession(id);
|
||||
else this.sessions.delete(id);
|
||||
else {
|
||||
this.sessions.delete(id);
|
||||
this.journals.delete(id);
|
||||
this.evidence.delete(id);
|
||||
}
|
||||
throw new CustomWidgetDomainError({ code: "NOT_FOUND", message: "Preview session expired or was not found" });
|
||||
}
|
||||
if (parsed.data.userId !== userId) {
|
||||
@@ -164,8 +171,23 @@ export class CustomWidgetPreviewSessionService {
|
||||
): Promise<void> {
|
||||
const value = journalEntrySchema.parse({ ...input, id: this.options.createId(), timestamp: this.now() });
|
||||
const ttlMs = Math.max(1, session.expiresAt - this.now());
|
||||
if (this.options.store) await this.options.store.appendJournal(session.id, value, MAX_JOURNAL_ENTRIES, ttlMs);
|
||||
else this.journals.set(session.id, [value, ...(this.journals.get(session.id) ?? [])].slice(0, MAX_JOURNAL_ENTRIES));
|
||||
const isVerified =
|
||||
(value.kind === "query" && value.status !== null && value.status >= 200 && value.status < 300) ||
|
||||
(value.kind === "action" &&
|
||||
(value.simulated || (value.status !== null && value.status >= 200 && value.status < 300)));
|
||||
const evidenceKey = `${value.sessionRevision}:${value.kind}:${value.requestId}`;
|
||||
if (this.options.store) {
|
||||
const writes = [this.options.store.appendJournal(session.id, value, MAX_JOURNAL_ENTRIES, ttlMs)];
|
||||
if (isVerified) writes.push(this.options.store.saveEvidence(session.id, evidenceKey, value, ttlMs));
|
||||
await Promise.all(writes);
|
||||
return;
|
||||
}
|
||||
|
||||
this.journals.set(session.id, [value, ...(this.journals.get(session.id) ?? [])].slice(0, MAX_JOURNAL_ENTRIES));
|
||||
if (!isVerified) return;
|
||||
const evidence = this.evidence.get(session.id) ?? new Map<string, CustomWidgetPreviewJournalEntry>();
|
||||
evidence.set(evidenceKey, value);
|
||||
this.evidence.set(session.id, evidence);
|
||||
}
|
||||
|
||||
public async getJournal(id: string, userId: string): Promise<CustomWidgetPreviewJournalEntry[]> {
|
||||
@@ -179,6 +201,17 @@ export class CustomWidgetPreviewSessionService {
|
||||
});
|
||||
}
|
||||
|
||||
public async getEvidence(id: string, userId: string): Promise<CustomWidgetPreviewJournalEntry[]> {
|
||||
await this.get(id, userId);
|
||||
const entries = this.options.store
|
||||
? await this.options.store.getEvidence(id)
|
||||
: [...(this.evidence.get(id)?.values() ?? [])];
|
||||
return entries.flatMap((entry) => {
|
||||
const parsed = journalEntrySchema.safeParse(parseStoredValue(entry));
|
||||
return parsed.success ? [parsed.data] : [];
|
||||
});
|
||||
}
|
||||
|
||||
public getSecrets(session: CustomWidgetPreviewSession, sourceId: string) {
|
||||
return session.secrets
|
||||
.filter((secret) => secret.sourceId === sourceId)
|
||||
|
||||
@@ -134,6 +134,8 @@ describe("preview sessions", () => {
|
||||
},
|
||||
appendJournal: async () => undefined,
|
||||
getJournal: async () => [],
|
||||
saveEvidence: async () => undefined,
|
||||
getEvidence: async () => [],
|
||||
};
|
||||
const service = new CustomWidgetPreviewSessionService({
|
||||
createId: () => "session",
|
||||
|
||||
@@ -64,6 +64,13 @@ it("normalizes request capabilities and drops malformed entries", () => {
|
||||
confirmation: { title: "Approve", message: "Approve this request?" },
|
||||
invalidates: ["queue", 3],
|
||||
},
|
||||
{
|
||||
id: "malformed-confirmation",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
minimumBoardPermission: "modify",
|
||||
confirmation: { title: 3, message: "Approve this request?" },
|
||||
},
|
||||
{ id: "unsafe", kind: "action", method: "TRACE", minimumBoardPermission: "full" },
|
||||
]),
|
||||
).toEqual([
|
||||
@@ -76,6 +83,15 @@ it("normalizes request capabilities and drops malformed entries", () => {
|
||||
confirmation: { title: "Approve", message: "Approve this request?" },
|
||||
invalidates: ["queue"],
|
||||
},
|
||||
{
|
||||
id: "malformed-confirmation",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
trigger: "manual",
|
||||
minimumBoardPermission: "modify",
|
||||
confirmation: undefined,
|
||||
invalidates: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user