import {z} from 'zod' export type StringArray = Array; export type UuidArray = Array; export type SubscriptionListId = string; export type SubscriptionListArray = Array; export type WhereObject = {compare_as?: ('text' | 'number' | 'date' | 'boolean') | undefined, operator: 'greater' | 'less' | 'equals' | 'greater_eq' | 'less_eq' | 'contains' | 'present' | 'range' | 'before' | 'after', precision?: ('minutes' | 'days' | 'months' | 'years') | undefined, property: string, relative_to?: ('future' | 'past') | undefined, value: string | number | boolean}; export type AtomicSelector = {group?: string | undefined, tag: string | StringArray} | {segment: string | UuidArray} | {channel: string | UuidArray} | {named_user: string | StringArray} | {amazon_channel: string | UuidArray} | {android_channel: string | UuidArray} | {open_channel: string | UuidArray} | {msisdn: string, sender: string} | {static_list: string | StringArray} | {subscription_list: string | SubscriptionListArray} | {attribute: string, operator: 'equals' | 'contains' | 'less' | 'greater' | 'is_empty', value: string} | {attribute: string, operator: 'equals' | 'contains' | 'less' | 'greater' | 'is_empty', value: number} | {attribute: string, operator: 'is_empty' | 'before' | 'after' | 'range' | 'equals', precision?: ('years' | 'months' | 'days' | 'hours' | 'minutes' | 'day' | 'month' | 'month_day' | 'year_month' | 'year_month_day') | undefined, relative_to?: ('future' | 'past') | undefined, value?: (string | number) | undefined} | {attribute: string, where: {compare_as?: ('text' | 'number' | 'date' | 'boolean') | undefined, operator: 'equals' | 'contains' | 'not_contains' | 'less' | 'less_eq' | 'greater' | 'greater_eq' | 'range' | 'before' | 'after' | 'is_empty' | 'not_empty', precision?: ('minutes' | 'days' | 'months' | 'years') | undefined, property: string, relative_to?: ('future' | 'past') | undefined, value?: (string | number | boolean) | undefined}} | {activity: 'app_open' | 'message_received' | 'message_center_read' | 'message_center_delivered' | 'message_center_deleted' | 'in_app_message_display' | 'in_app_message_resolution' | 'in_app_message_expiration' | 'screen_view' | 'web_session' | 'web_click' | 'short_link_click' | 'first_seen' | 'sms_aborted' | 'sms_delivered' | 'sms_dispatched' | 'sms_expired' | 'sms_failed' | 'sms_rejected' | 'sms_undeliverable' | 'sms_unknown' | 'email_bounce' | 'email_click' | 'email_delay' | 'email_delivered' | 'email_injection' | 'email_open' | 'email_unsubscribe' | 'scene_displayed' | 'scene_completed' | 'scene_incomplete' | 'survey_displayed' | 'survey_submitted' | 'survey_not_submitted', after?: (string | number) | undefined, before?: (string | number) | undefined, metric?: ('count' | 'total_value' | 'last' | 'first') | undefined, on?: string | undefined, on_precision?: ('day' | 'month' | 'month_day' | 'year_month' | 'year_month_day') | undefined, operator: 'greater' | 'less' | 'equals' | 'greater_eq' | 'less_eq', precision?: ('days' | 'weeks' | 'months' | 'years') | undefined, value: number, where?: WhereObject | undefined}; export type CompoundSelector = {AND?: (Array) | undefined} | {OR?: (Array) | undefined} | {NOT?: (CompoundSelector | AtomicSelector) | undefined}; export type CreateApiApiChannelsSubscriptionListsArgs = {body: {AND?: (Array) | undefined} | {OR?: (Array) | undefined} | {NOT?: (CompoundSelector | AtomicSelector) | undefined}}; export type CreateApiApiChannelsSubscriptionListsOptions = {baseUrl?: string | undefined, basicAppAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const okResponseObject = z.object({ok: z.boolean(), operation_id: z.string().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 }; /** * Subscribe or unsubscribe channels to/from subscription lists * * POST /api/channels/subscription_lists * * Also accepts auth: basicAuth, bearerAuth, oauth2Token */ export const createApiApiChannelsSubscriptionLists = async (args: CreateApiApiChannelsSubscriptionListsArgs, options?: CreateApiApiChannelsSubscriptionListsOptions) => { const {baseUrl = "https://go.urbanairship.com", fetch: fetchFn = fetch, basicAppAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.urbanairship+json; version=3") if (basicAppAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAppAuth.username}:${basicAppAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/channels/subscription_lists${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(okResponseObject, await res.json()) };