From c7bea9e025ec42e7131888d269a003cd3ab94acf Mon Sep 17 00:00:00 2001 From: vi Date: Thu, 26 Mar 2026 04:52:05 +0000 Subject: [PATCH] feat(backend/examples): update routes; add example with pds proxying --- pkgs/server/examples/lib.ts | 73 +++++++++++++++++++----- pkgs/server/examples/scheduleDeletion.ts | 20 +++---- pkgs/server/examples/schedulePost.ts | 21 +++---- 3 files changed, 80 insertions(+), 34 deletions(-) diff --git a/pkgs/server/examples/lib.ts b/pkgs/server/examples/lib.ts index 7a84c13..77961e7 100644 --- a/pkgs/server/examples/lib.ts +++ b/pkgs/server/examples/lib.ts @@ -38,6 +38,38 @@ const handleResolver = new CompositeHandleResolver({ }, }); +type HeaderInjector = (headers: Headers) => void | Promise; +function withLogging( + inner: FetchHandler, + options?: { + augmentHeaders?: HeaderInjector; + }, +): FetchHandler { + return async (pathname: string, init: RequestInit): Promise => { + const headers = new Headers(init.headers); + + if (options?.augmentHeaders) { + await options.augmentHeaders(headers); + } + + console.log(); + console.log(`${init.method?.toUpperCase()} ${pathname}`); + + if (init.body && typeof init.body === "string") { + try { + console.log("body", JSON.parse(init.body)); + } catch { + console.log("body", init.body); + } + } + + return inner(pathname, { + ...init, + headers, + }); + }; +} + export async function getClient(): Promise<[Client, CredentialManager]> { if (!IDENTIFIER || !APP_PASSWORD) throw new Error("IDENTIFIER and APP_PASSWORD must be set"); @@ -91,23 +123,38 @@ export async function getServiceAuth( const serviceHandler = simpleFetchHandler({ service: "http://127.0.0.1:3000", }); - const serviceClient = new Client({ - handler: async (pathname: string, init: RequestInit): Promise => { - const headers = new Headers(init.headers); + + const loggingHandler = withLogging(serviceHandler, { + augmentHeaders: (headers) => { headers.set("Authorization", `Bearer ${res.token}`); + }, + }); - console.log(); - console.log(`${init.method?.toUpperCase()} ${pathname}`); - if (init.body && typeof init.body === "string") { - console.log("body", JSON.parse(init.body)); - } + const serviceClient = new Client({ + handler: loggingHandler, + }); + + return [serviceClient, serviceHandler]; +} - return serviceHandler(pathname, { - ...init, - headers, - }); +export async function getProxyClient( + client: Client, +): Promise<[Client, FetchHandler]> { + const proxyClient = new Client({ + handler: withLogging(client.handler), + proxy: { + did: "did:web:api.bbell.vt3e.cat", + serviceId: "#bbell_backend", }, }); - return [serviceClient, serviceHandler]; + return [proxyClient, proxyClient.handler]; +} + +export async function getServiceClient( + client: Client, + method: "serviceAuth" | "proxy", +): Promise<[Client, FetchHandler]> { + if (method === "serviceAuth") return getServiceAuth(client); + else return getProxyClient(client); } diff --git a/pkgs/server/examples/scheduleDeletion.ts b/pkgs/server/examples/scheduleDeletion.ts index 2c455af..3e1e711 100644 --- a/pkgs/server/examples/scheduleDeletion.ts +++ b/pkgs/server/examples/scheduleDeletion.ts @@ -1,16 +1,15 @@ import type { AppBskyFeedPost } from "@atcute/bluesky"; import { ok } from "@atcute/client"; import { - CatVt3eBbellDefs, - CatVt3eBbellScheduledDeletionCancel, - CatVt3eBbellScheduledDeletionCreate, + CatVt3eBbellSchedulingDefs, + CatVt3eBbellSchedulingScheduleDeletion, } from "@vt3e.cat/bbell-lexicons"; -import { getClient, getServiceAuth, WAIT_TIME } from "./lib"; +import { getClient, getServiceClient, WAIT_TIME } from "./lib"; // --- get clients!! --------------------------------------------- const [client, credentialManager] = await getClient(); -const [serviceClient] = await getServiceAuth(client); +const [serviceClient] = await getServiceClient(client, "proxy"); if (!credentialManager.session) throw new Error("no session"); // --- !! before proceeding !! ----------------------------------- @@ -59,7 +58,7 @@ await new Promise((resolve) => setTimeout(resolve, WAIT_TIME)); const { uri } = createRes; const scheduledDeletionList = ok( - await serviceClient.get("cat.vt3e.bbell.scheduled.deletion.list", { + await serviceClient.get("cat.vt3e.bbell.scheduling.listDeletions", { params: { // this endpoint supports pagination! it returns a `cursor` if there // are more posts to fetch. @@ -70,7 +69,7 @@ const scheduledDeletionList = ok( ); // out: -CatVt3eBbellDefs.selfDeletingPostSchema; +CatVt3eBbellSchedulingDefs.selfDeletingPostSchema; console.log("res", scheduledDeletionList.posts); const uris = scheduledDeletionList.posts.map((post) => post.uri); @@ -102,7 +101,7 @@ const newPost = ok( // and let's schedule it for deletion!! const scheduleRes = ok( - await serviceClient.post("cat.vt3e.bbell.scheduled.deletion.create", { + await serviceClient.post("cat.vt3e.bbell.scheduling.scheduleDeletion", { as: "json", input: { uri: newPost.uri, @@ -112,7 +111,7 @@ const scheduleRes = ok( ); // out: -CatVt3eBbellScheduledDeletionCreate.mainSchema.output; +CatVt3eBbellSchedulingScheduleDeletion.mainSchema.output; console.log("res", scheduleRes); // --- cancelling deletions -------------------------------------- @@ -122,7 +121,7 @@ await new Promise((resolve) => setTimeout(resolve, WAIT_TIME)); const { id } = scheduleRes; const cancelRes = ok( - await serviceClient.post("cat.vt3e.bbell.scheduled.deletion.cancel", { + await serviceClient.post("cat.vt3e.bbell.scheduling.cancelDeletion", { as: "json", input: { id, @@ -131,5 +130,4 @@ const cancelRes = ok( ); // out: -CatVt3eBbellScheduledDeletionCancel.mainSchema.output; console.log("res", cancelRes); diff --git a/pkgs/server/examples/schedulePost.ts b/pkgs/server/examples/schedulePost.ts index 3e0020f..0134917 100644 --- a/pkgs/server/examples/schedulePost.ts +++ b/pkgs/server/examples/schedulePost.ts @@ -1,15 +1,16 @@ import type { AppBskyFeedPost } from "@atcute/bluesky"; import { ok } from "@atcute/client"; import { - CatVt3eBbellDefs, - CatVt3eBbellScheduledDeletionCreate, + CatVt3eBbellSchedulingCancelPost, + CatVt3eBbellSchedulingDefs, + CatVt3eBbellSchedulingSchedulePost, } from "@vt3e.cat/bbell-lexicons"; -import { getClient, getServiceAuth, WAIT_TIME } from "./lib"; +import { getClient, getServiceClient, WAIT_TIME } from "./lib"; // --- get clients!! --------------------------------------------- const [client] = await getClient(); -const [serviceClient] = await getServiceAuth(client); +const [serviceClient] = await getServiceClient(client, "proxy"); // --- !! before proceeding !! ----------------------------------- // make sure that you've gone through the oauth flow! @@ -27,7 +28,7 @@ const post: AppBskyFeedPost.Main = { }; const scheduledRes = ok( - await serviceClient.post("cat.vt3e.bbell.scheduled.post.create", { + await serviceClient.post("cat.vt3e.bbell.scheduling.schedulePost", { input: { scheduledAt: scheduledAt.toISOString(), post: post, @@ -36,13 +37,13 @@ const scheduledRes = ok( ); // out: -CatVt3eBbellScheduledDeletionCreate.mainSchema.output.schema; +CatVt3eBbellSchedulingSchedulePost.mainSchema.output.schema; console.log("res", scheduledRes); // --- lists scheduled posts ------------------------------------- await new Promise((resolve) => setTimeout(resolve, WAIT_TIME)); const scheduledPostList = ok( - await serviceClient.get("cat.vt3e.bbell.scheduled.post.list", { + await serviceClient.get("cat.vt3e.bbell.scheduling.listPosts", { params: { // this endpoint supports pagination! it returns a `cursor` if there // are more posts to fetch. @@ -53,18 +54,18 @@ const scheduledPostList = ok( ); // out: -CatVt3eBbellDefs.scheduledPostSchema; +CatVt3eBbellSchedulingDefs.scheduledPostSchema; console.log("res", scheduledPostList.posts); // --- cancel scheduled post ------------------------------------- await new Promise((resolve) => setTimeout(resolve, WAIT_TIME)); const cancelRes = ok( - await serviceClient.post("cat.vt3e.bbell.scheduled.post.cancel", { + await serviceClient.post("cat.vt3e.bbell.scheduling.cancelPost", { as: "json", input: { id: scheduledRes.id }, }), ); // out: -CatVt3eBbellScheduledDeletionCreate.mainSchema.output.schema; +CatVt3eBbellSchedulingCancelPost.mainSchema; console.log("res", cancelRes); -- 2.51.2