import {z} from 'zod' export type UpdateSubscriptionRequest = {eventFilters: Array, expiresIn?: number | undefined}; export type UpdateApiRestapiV10SubscriptionSubscriptionIdArgs = {subscriptionId: string, body: UpdateSubscriptionRequest}; export type UpdateApiRestapiV10SubscriptionSubscriptionIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const disabledFilterInfo = z.object({filter: z.string(), reason: z.string(), message: z.string().optional()}); export const webhookDeliveryMode = z.object({transportType: z.literal("WebHook"), address: z.string(), encryption: z.literal(false)}); export const mobileDeliveryMode = z.object({transportType: z.enum(["RC/APNS", "RC/GCM"]), certificateName: z.string(), registrationId: z.string(), encryption: z.literal(false)}); export const pubNubDeliveryMode = z.object({transportType: z.literal("PubNub"), encryption: z.boolean(), address: z.string(), subscriberKey: z.string(), secretKey: z.string(), encryptionAlgorithm: z.literal("AES").optional(), encryptionKey: z.string().optional()}); export const webSocketDeliveryMode = z.object({transportType: z.literal("WebSocket")}); export const notificationDeliveryMode = z.discriminatedUnion("transportType", [webhookDeliveryMode, mobileDeliveryMode, pubNubDeliveryMode, webSocketDeliveryMode]); export const subscriptionInfo = z.object({uri: z.string(), id: z.string(), eventFilters: z.array(z.string().min(1)), disabledFilters: z.array(disabledFilterInfo).optional(), expirationTime: z.string(), expiresIn: z.number().int().gte(1).optional(), status: z.enum(["Active", "Blacklisted"]), creationTime: z.string(), deliveryMode: notificationDeliveryMode, blacklistedData: z.object({blacklistedAt: z.string().optional(), reason: z.string().optional()}).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 }; /** * Update Subscription * * PUT /restapi/v1.0/subscription/{subscriptionId} */ export const updateApiRestapiV10SubscriptionSubscriptionId = async (args: UpdateApiRestapiV10SubscriptionSubscriptionIdArgs, options?: UpdateApiRestapiV10SubscriptionSubscriptionIdOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v1.0/subscription/${encodeURIComponent(String(args.subscriptionId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(subscriptionInfo, await res.json()) };