refactor: share tlon channel put requests

This commit is contained in:
Peter Steinberger 2026-03-13 21:58:56 +00:00
parent 7ca8804a33
commit 5af8322ff5
1 changed files with 26 additions and 42 deletions

View File

@ -115,20 +115,7 @@ export class UrbitSSEClient {
app: string; app: string;
path: string; path: string;
}) { }) {
const { response, release } = await urbitFetch({ const { response, release } = await this.putChannelPayload([subscription], {
baseUrl: this.url,
path: `/~/channel/${this.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: this.cookie,
},
body: JSON.stringify([subscription]),
},
ssrfPolicy: this.ssrfPolicy,
lookupFn: this.lookupFn,
fetchImpl: this.fetchImpl,
timeoutMs: 30_000, timeoutMs: 30_000,
auditContext: "tlon-urbit-subscribe", auditContext: "tlon-urbit-subscribe",
}); });
@ -359,20 +346,7 @@ export class UrbitSSEClient {
"event-id": eventId, "event-id": eventId,
}; };
const { response, release } = await urbitFetch({ const { response, release } = await this.putChannelPayload([ackData], {
baseUrl: this.url,
path: `/~/channel/${this.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: this.cookie,
},
body: JSON.stringify([ackData]),
},
ssrfPolicy: this.ssrfPolicy,
lookupFn: this.lookupFn,
fetchImpl: this.fetchImpl,
timeoutMs: 10_000, timeoutMs: 10_000,
auditContext: "tlon-urbit-ack", auditContext: "tlon-urbit-ack",
}); });
@ -445,20 +419,7 @@ export class UrbitSSEClient {
})); }));
{ {
const { response, release } = await urbitFetch({ const { response, release } = await this.putChannelPayload(unsubscribes, {
baseUrl: this.url,
path: `/~/channel/${this.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: this.cookie,
},
body: JSON.stringify(unsubscribes),
},
ssrfPolicy: this.ssrfPolicy,
lookupFn: this.lookupFn,
fetchImpl: this.fetchImpl,
timeoutMs: 30_000, timeoutMs: 30_000,
auditContext: "tlon-urbit-unsubscribe", auditContext: "tlon-urbit-unsubscribe",
}); });
@ -501,4 +462,27 @@ export class UrbitSSEClient {
await release(); await release();
} }
} }
private async putChannelPayload(
payload: unknown,
params: { timeoutMs: number; auditContext: string },
) {
return await urbitFetch({
baseUrl: this.url,
path: `/~/channel/${this.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: this.cookie,
},
body: JSON.stringify(payload),
},
ssrfPolicy: this.ssrfPolicy,
lookupFn: this.lookupFn,
fetchImpl: this.fetchImpl,
timeoutMs: params.timeoutMs,
auditContext: params.auditContext,
});
}
} }