import {z} from 'zod' export type GetApiSubscriptionTopicsIdSubscribersArgs = {id: string, page?: number | undefined, per_page?: number | undefined, subscribed?: ('true' | 'false') | undefined}; export type GetApiSubscriptionTopicsIdSubscribersOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationMeta = z.object({page: z.number().int(), perPage: z.number().int(), total: z.number().int(), totalPages: z.number().int()}); export const getApiSubscriptionTopicsIdSubscribersResponse = z.object({data: z.array(z.object({contactId: z.number().int().optional(), contactPublicId: z.string().optional(), email: z.string().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), subscribedAt: z.string().optional()})), meta: paginationMeta}); 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 a topic’s subscribers * * GET /subscription-topics/{id}/subscribers * * Also accepts auth: bearerOAuth */ export const getApiSubscriptionTopicsIdSubscribers = async (args: GetApiSubscriptionTopicsIdSubscribersArgs, options?: GetApiSubscriptionTopicsIdSubscribersOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = 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.per_page != null) query.append("per_page", String(args.per_page)) if (args.subscribed != null) query.append("subscribed", String(args.subscribed)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/subscription-topics/${encodeURIComponent(String(args.id))}/subscribers${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(getApiSubscriptionTopicsIdSubscribersResponse, await res.json()) };