refactor: share tlon channel put requests

This commit is contained in:
Peter Steinberger 2026-03-13 16:46:51 +00:00
parent e351a86290
commit acfb95e2c6
1 changed files with 36 additions and 55 deletions

View File

@ -12,6 +12,29 @@ export type UrbitChannelDeps = {
fetchImpl?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
};
async function putUrbitChannel(
deps: UrbitChannelDeps,
params: { body: unknown; auditContext: string },
) {
return await urbitFetch({
baseUrl: deps.baseUrl,
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify(params.body),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
auditContext: params.auditContext,
});
}
export async function pokeUrbitChannel(
deps: UrbitChannelDeps,
params: { app: string; mark: string; json: unknown; auditContext: string },
@ -26,21 +49,8 @@ export async function pokeUrbitChannel(
json: params.json,
};
const { response, release } = await urbitFetch({
baseUrl: deps.baseUrl,
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify([pokeData]),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
const { response, release } = await putUrbitChannel(deps, {
body: [pokeData],
auditContext: params.auditContext,
});
@ -88,23 +98,7 @@ export async function createUrbitChannel(
deps: UrbitChannelDeps,
params: { body: unknown; auditContext: string },
): Promise<void> {
const { response, release } = await urbitFetch({
baseUrl: deps.baseUrl,
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify(params.body),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
auditContext: params.auditContext,
});
const { response, release } = await putUrbitChannel(deps, params);
try {
if (!response.ok && response.status !== 204) {
@ -116,16 +110,8 @@ export async function createUrbitChannel(
}
export async function wakeUrbitChannel(deps: UrbitChannelDeps): Promise<void> {
const { response, release } = await urbitFetch({
baseUrl: deps.baseUrl,
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify([
const { response, release } = await putUrbitChannel(deps, {
body: [
{
id: Date.now(),
action: "poke",
@ -134,12 +120,7 @@ export async function wakeUrbitChannel(deps: UrbitChannelDeps): Promise<void> {
mark: "helm-hi",
json: "Opening API channel",
},
]),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
],
auditContext: "tlon-urbit-channel-wake",
});