import {z} from 'zod' export type GetApiV2PostsArgs = {after?: string | undefined, limit?: number | undefined, channel_id?: string | undefined, sort?: ('last_activity_at' | 'published_at') | undefined, direction?: ('asc' | 'desc') | undefined}; export type GetApiV2PostsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v2Project = z.object({id: z.string(), name: z.string()}); export const avatarUrls = z.object({xs: z.string(), sm: z.string(), base: z.string(), lg: z.string(), xl: z.string(), xxl: z.string()}); export const v2Author = z.object({avatar_urls: avatarUrls, name: z.string(), id: z.string(), type: z.enum(["app", "member"])}); export const v2Comment = z.object({id: z.string(), content: z.string(), created_at: z.string(), replies_count: z.number(), parent_id: z.string().nullable(), subject_id: z.string(), subject_type: z.string(), author: v2Author}); export const v2PostResolution = z.object({resolved_at: z.string(), resolved_html: z.string().nullable(), resolved_by: v2Author, resolved_comment: v2Comment.nullable()}); export const v2Post = z.object({id: z.string(), title: z.string(), created_at: z.string(), last_activity_at: z.string(), url: z.string(), content: z.string(), comments_count: z.number(), channel: v2Project, author: v2Author, resolution: v2PostResolution.nullable()}); export const v2PostPage = z.object({next_cursor: z.string().nullable().optional(), prev_cursor: z.string().nullable().optional(), data: z.array(v2Post), total_count: z.number()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List posts * * GET /v2/posts */ export const getApiV2Posts = async (args: GetApiV2PostsArgs, options?: GetApiV2PostsOptions) => { const {baseUrl = "https://api.campsite.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.channel_id != null) query.append("channel_id", String(args.channel_id)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/posts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(v2PostPage, await res.json()) };