import { AtUriString } from '@atproto/lex' import { InvalidRequestError, Server } from '@atproto/xrpc-server' import { AppContext } from '../../../../context.js' import { HydrateCtx, Hydrator } from '../../../../hydration/hydrator.js' import { app } from '../../../../lexicons/index.js' import { HydrationFnInput, PresentationFnInput, SkeletonFnInput, createPipeline, noRules, } from '../../../../pipeline.js' import { Views } from '../../../../views/index.js' import { resHeaders } from '../../../util.js' export default function (server: Server, ctx: AppContext) { const getStarterPack = createPipeline( skeleton, hydration, noRules, presentation, ) server.add(app.bsky.graph.getStarterPack, { auth: ctx.authVerifier.standardOptional, handler: async ({ params, auth, req }) => { const { viewer, includeTakedowns, skipViewerBlocks } = ctx.authVerifier.parseCreds(auth) const labelers = ctx.reqLabelers(req) const hydrateCtx = await ctx.hydrator.createContext({ labelers, viewer, includeTakedowns, skipViewerBlocks, }) const result = await getStarterPack({ ...params, hydrateCtx }, ctx) return { encoding: 'application/json', body: result, headers: resHeaders({ labelers: hydrateCtx.labelers }), } }, }) } const skeleton = async ( input: SkeletonFnInput, ): Promise => { const { ctx, params } = input const uri = await ctx.hydrator.resolveUri(params.starterPack) return { uri } } const hydration = async ( input: HydrationFnInput, ) => { const { ctx, params, skeleton } = input return ctx.hydrator.hydrateStarterPacks([skeleton.uri], params.hydrateCtx) } const presentation = ( input: PresentationFnInput, ) => { const { ctx, skeleton, hydration } = input const starterPack = ctx.views.starterPack(skeleton.uri, hydration) if (!starterPack) { throw new InvalidRequestError('Starter pack not found') } return { starterPack } } type Context = { hydrator: Hydrator views: Views } type Params = app.bsky.graph.getStarterPack.$Params & { hydrateCtx: HydrateCtx } type SkeletonState = { uri: AtUriString }