import {z} from 'zod' export type GetApiChannelsIdInvitesArgs = {token: string, 'Channel-Id'?: string | undefined, id: string, count?: number | undefined, after?: string | undefined, before?: string | undefined, query?: string | undefined}; export type GetApiChannelsIdInvitesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const templateDto = z.object({id: z.string().optional(), name: z.string().optional()}); export const emailDto = z.object({name: z.string().optional(), email: z.string().optional()}); export const inviteResponseDto = z.object({id: z.string().optional(), _object: z.string().optional(), mode: z.enum(["AUTOMATIC", "MANUAL"]).optional(), reviewType: z.enum(["SERVICE_REVIEW", "PRODUCT_REVIEW", "SERVICE_AND_PRODUCT_REVIEW"]).optional(), accountRef: z.string().optional(), channelRef: z.string().optional(), locale: z.string().optional(), platform: z.string().optional(), customer: z.object({id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional(), phoneNumber: z.string().optional(), preferredLocale: z.string().optional(), consents: z.array(z.object({purpose: z.string().optional(), given: z.boolean().optional()})).optional()}).optional(), inviteSource: z.string().optional(), transaction: z.object({id: z.string().optional(), reference: z.string().optional(), date: z.string().optional()}).optional(), scheduledAt: z.string().optional(), estimatedDeliveryDate: z.string().optional(), template: templateDto.optional(), sender: emailDto.optional(), replyTo: emailDto.optional(), event: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), sentAt: z.string().optional(), status: z.object({name: z.enum(["SCHEDULED", "CANCELED", "SENT", "DELIVERED", "NOT_DELIVERED", "REJECTED", "OPENED", "CLICKED", "RENDERED", "REVIEW_WRITTEN", "READY_FOR_RENDER"]), reason: z.enum(["INVALID_TEMPLATE", "RENDERING_ERROR", "INVALID_RECIPIENT_EMAIL", "TEMPORARY_RECIPIENT_ERROR", "REPORTED_AS_SPAM_SENDER", "REPORTED_AS_SPAM_RECIPIENT", "REPORTED_AS_SPAM_CONTENT", "REPORTED_AS_SPAM_FREQUENCY", "PROVIDER_ERROR", "DELAYED", "RECIPIENT_UNSUBSCRIBED", "RECIPIENT_BLACKLISTED_PROVIDER", "RECIPIENT_BLACKLISTED_ACCOUNT", "RECIPIENT_BLACKLISTED_CHANNEL", "RECIPIENT_BLACKLISTED_EMAIL", "BLACKLISTED_DOMAIN", "DUPLICATE", "INVITE_SENDING_SUSPENDED_BY_EMPLOYEE", "INVITE_SENDING_SUSPENDED_BY_CUSTOMER", "INTERNAL_ERROR", "BLOCKED_BY_CONSUMER"]).optional()}).optional()}); export const inviteResponseListDto = z.object({totalElements: z.number().int().optional(), paging: z.object({count: z.number().int().optional(), cursor: z.object({after: z.string().optional(), before: z.string().optional()}).optional(), links: z.object({previous: z.string().optional(), next: z.string().optional()}).optional()}).optional(), items: z.array(inviteResponseDto).optional()}); 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 a list of invites * * GET /channels/{id}/invites */ export const getApiChannelsIdInvites = async (args: GetApiChannelsIdInvitesArgs, options: GetApiChannelsIdInvitesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.token != null) headers.set("token", String(args.token)) if (args['Channel-Id'] != null) headers.set("Channel-Id", String(args['Channel-Id'])) if (args.count != null) query.append("count", String(args.count)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.query != null) query.append("query", String(args.query)) const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.id))}/invites${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(inviteResponseListDto, await res.json()) };