import {z} from 'zod' export type UUID = string; export type GetApiApiV1ProjectsProjectUuidAnnouncementsArgs = {projectUuid: UUID, page?: number | undefined, pageSize?: number | undefined, includeUnpublished?: boolean | undefined}; export type GetApiApiV1ProjectsProjectUuidAnnouncementsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const announcementCategory = z.enum(["launch", "update", "heads_up"]); export const projectAnnouncement = z.object({updatedAt: z.string(), createdAt: z.string(), authorName: z.string().nullable(), createdByUserUuid: z.string().nullable(), pendingSlackChannelId: z.string().nullable(), published: z.boolean(), pinned: z.boolean(), category: announcementCategory.nullable(), body: z.string().nullable(), title: z.string(), projectUuid: z.string(), announcementUuid: z.string()}); export const announcementsPage = z.object({totalCount: z.number(), items: z.array(projectAnnouncement)}); export const apiAnnouncementsResponse = z.object({results: announcementsPage, status: z.literal("ok")}); 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 }; /** * GET /api/v1/projects/{projectUuid}/announcements */ export const getApiApiV1ProjectsProjectUuidAnnouncements = async (args: GetApiApiV1ProjectsProjectUuidAnnouncementsArgs, options: GetApiApiV1ProjectsProjectUuidAnnouncementsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.includeUnpublished != null) query.append("includeUnpublished", String(args.includeUnpublished)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/announcements${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(apiAnnouncementsResponse, await res.json()) };