import {z} from 'zod' export type DelayNode = {name?: string | undefined, description?: (string | null) | undefined, node_type: 'delay', ref?: string | undefined, properties: {delay_type: 'fixed' | 'dynamic' | 'relative_to', value?: string | undefined, relative_to?: ({pivot_expr?: string | undefined, offset_type?: ('before' | 'after') | undefined, offset_value_type?: ('fixed' | 'dynamic') | undefined, offset_value?: string | undefined}) | undefined}}; export type BatchNode = {name?: string | undefined, description?: (string | null) | undefined, node_type: 'batch', ref?: string | undefined, properties: {mode?: ('accumulate_all' | 'flush_leading_item') | undefined, window_type: 'fixed' | 'dynamic' | 'relative_to', fixed_window?: string | undefined, dynamic_window_expr?: string | undefined, relative_to?: ({pivot_expr?: string | undefined, offset_type?: ('before' | 'after') | undefined, offset_value_type?: ('fixed' | 'dynamic') | undefined, offset_value?: string | undefined}) | undefined, retain_count?: number | undefined, retain_order?: ('first' | 'last') | undefined, batch_key?: string | undefined}}; export type Schedule = {frequency?: ('minutely' | 'hourly' | 'daily' | 'weekly_mo2fr' | 'weekly' | 'monthly') | undefined, interval?: number | undefined, weekdays?: (Array<'su' | 'mo' | 'tu' | 'we' | 'th' | 'fr' | 'sa'>) | undefined, monthdays?: (Array<{pos?: number | undefined, day?: string | undefined}>) | undefined, time?: string | undefined, dtstart?: string | undefined, tz_selection?: ('fixed' | 'recipient') | undefined, tz_fixed?: string | undefined}; export type DigestNode = {name?: string | undefined, node_type: 'digest', description?: (string | null) | undefined, ref?: string | undefined, properties: {schedule_type: 'static' | 'dynamic' | 'category_digest_schedule', dynamic_schedule_expr?: string | undefined, schedule?: Schedule | undefined, retain_count?: number | undefined, retain_order?: ('first' | 'last') | undefined, trigger_min_count?: number | undefined}}; export type HttpApiWebhookNode = {name?: string | undefined, node_type: 'httpapi_webhook', description?: (string | null) | undefined, ref?: string | undefined, properties: {http_method: 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE', url: string, headers?: (Array<{key?: string | undefined, value?: string | undefined}>) | undefined, body_content_type?: 'application/json' | undefined, body?: string | undefined, query_params?: (Array<{key?: string | undefined, value?: string | undefined}>) | undefined, output_key?: string | undefined}}; export type HttpApiFetchNode = {name?: string | undefined, node_type: 'httpapi_fetch', description?: (string | null) | undefined, ref?: string | undefined, properties: {http_method?: 'GET' | undefined, url: string, headers?: (Array<{key?: string | undefined, value?: string | undefined}>) | undefined, query_params?: (Array<{key?: string | undefined, value?: string | undefined}>) | undefined, output_key?: string | undefined}}; export type BranchWaitUntilNode = {name?: string | undefined, node_type: 'branch_waituntil', description?: (string | null) | undefined, ref?: string | undefined, branches: Array<{name?: string | undefined, description?: string | undefined, is_default?: boolean | undefined, conditions?: (Array<{type: 'future_event' | 'delay', event_name?: string | undefined, event_conditions?: (Array<{type?: 'expression_v1' | undefined, expression_v1?: ({op?: 'AND' | undefined, args?: (Array<{variable_ns?: ('' | '$future_event' | '$actor' | '$recipient' | '$brand' | '$tenant' | '$organization' | '$category') | undefined, variable?: string | undefined, op?: ('==' | '!=' | '>' | '>=' | '<' | '<=' | 'EMPTY' | 'NON_EMPTY' | 'CONTAINS' | 'NOT_CONTAINS' | 'ARRAY_INTERSECTS' | 'NOT_ARRAY_INTERSECTS' | 'DATETIME_EQUALS' | 'DATETIME_LT' | 'DATETIME_GT') | undefined, value?: string | undefined}>) | undefined}) | undefined}>) | undefined, delay_properties?: ({delay_type?: ('fixed' | 'dynamic' | 'relative_to') | undefined, value?: string | undefined, relative_to?: ({pivot_expr?: string | undefined, offset_type?: ('before' | 'after') | undefined, offset_value_type?: ('fixed' | 'dynamic') | undefined, offset_value?: string | undefined}) | undefined}) | undefined}>) | undefined, nodes?: Array | undefined}>}; export type BranchNode = {name?: string | undefined, node_type: 'branch', description?: (string | null) | undefined, ref?: string | undefined, branches: Array<{name?: string | undefined, description?: string | undefined, is_default?: boolean | undefined, conditions?: (Array<{type?: 'expression_v1' | undefined, expression_v1?: ({op?: 'AND' | undefined, args?: (Array<{variable_ns?: ('' | '$tenant' | '$brand' | '$actor' | '$recipient' | '$organization' | '$category' | '$future_event' | '$ref') | undefined, ref?: string | undefined, variable?: string | undefined, op?: ('==' | '!=' | '>' | '>=' | '<' | '<=' | 'EMPTY' | 'NON_EMPTY' | 'CONTAINS' | 'NOT_CONTAINS' | 'ARRAY_INTERSECTS' | 'NOT_ARRAY_INTERSECTS' | 'DATETIME_EQUALS' | 'DATETIME_LT' | 'DATETIME_GT' | 'IS' | 'IS_NOT') | undefined, value?: string | undefined}>) | undefined}) | undefined}>) | undefined, nodes?: Array | undefined}>}; export type TransformNode = {name?: string | undefined, node_type: 'transform', description?: (string | null) | undefined, ref?: string | undefined, properties: {variables: Array<{key: string, value_lang: 'jsonnet' | 'handlebars', value: string}>}}; export type TimeWindowNode = {name?: string | undefined, node_type: 'timewindow', description?: (string | null) | undefined, ref?: string | undefined, properties: {windows: Array<{frequency?: ('daily' | 'weekly_mo2fr' | 'weekly' | 'monthly') | undefined, weekdays?: (Array<'su' | 'mo' | 'tu' | 'we' | 'th' | 'fr' | 'sa'>) | undefined, monthdays?: (Array<{pos?: number | undefined, day?: string | undefined}>) | undefined, stime?: string | undefined, etime?: string | undefined}>, tz_selection?: ('fixed' | 'recipient') | undefined, tz_fixed?: string | undefined}}; export type SubscriberListOperationAddUserNode = {name?: string | undefined, node_type: 'subscriberlistoperation_adduser', description?: (string | null) | undefined, ref?: string | undefined, properties: {is_list_id_dynamic?: boolean | undefined, list_id: string, create_list_if_missing?: boolean | undefined, list_name?: string | undefined, user_selection: Array<'actor' | 'recipient'>}}; export type SubscriberListOperationRemoveUserNode = {name?: string | undefined, node_type: 'subscriberlistoperation_removeuser', description?: (string | null) | undefined, ref?: string | undefined, properties: {is_list_id_dynamic?: boolean | undefined, list_id: string, user_selection: Array<'actor' | 'recipient'>}}; export type ObjectOperationAddSubscriptionNode = {name?: string | undefined, node_type: 'objectoperation_addsubscription', description?: (string | null) | undefined, ref?: string | undefined, properties: {is_object_id_dynamic?: boolean | undefined, object_type: string, object_id: string, create_object_if_missing?: boolean | undefined, user_selection: Array<'actor' | 'recipient'>, subscription_properties?: string | undefined}}; export type ObjectOperationRemoveSubscriptionNode = {name?: string | undefined, node_type: 'objectoperation_removesubscription', description?: (string | null) | undefined, ref?: string | undefined, properties: {is_object_id_dynamic?: boolean | undefined, object_type: string, object_id: string, user_selection: Array<'actor' | 'recipient'>}}; export type UserUpdateNode = {name?: string | undefined, node_type: 'userupdate', description?: (string | null) | undefined, ref?: string | undefined, properties: {user_selection: 'actor' | 'recipient', properties: string}}; export type InvokeWorkflowNode = {name?: string | undefined, node_type: 'invokeworkflow', description?: (string | null) | undefined, ref?: string | undefined, properties: {workflow: string, actor_selection?: string | undefined, actor_expression?: string | undefined, recipient_selection: 'recipient' | 'actor' | 'expression', recipient_expression?: string | undefined, data?: string | undefined, append_current_run_data?: boolean | undefined, cancellation_key?: string | undefined}}; export type SendEmailNode = {name?: string | undefined, node_type: 'send_email', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendSmsNode = {name?: string | undefined, node_type: 'send_sms', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendWhatsappNode = {name?: string | undefined, node_type: 'send_whatsapp', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendMobilePushNode = {name?: string | undefined, node_type: 'send_mobile_push', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendInboxNode = {name?: string | undefined, node_type: 'send_inbox', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendWebpushNode = {name?: string | undefined, node_type: 'send_webpush', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendSlackNode = {name?: string | undefined, node_type: 'send_slack', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendMsTeamsNode = {name?: string | undefined, node_type: 'send_ms_teams', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, success?: string | undefined}}; export type SendMultiChannelNode = {name?: string | undefined, node_type: 'send_multi_channel', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, channels?: (Array<'sms' | 'email' | 'androidpush' | 'iospush' | 'webpush' | 'inbox' | 'whatsapp' | 'slack' | 'ms_teams'>) | undefined, channels_expr?: string | undefined, success?: string | undefined}}; export type SendSmartChannelRoutingNode = {name?: string | undefined, node_type: 'send_smart_channel_routing', description?: (string | null) | undefined, ref?: string | undefined, properties: {template: string, channels_expr?: string | undefined, success: string, success_is_event?: boolean | undefined, ttl_value?: string | undefined, mandatory_channels?: (Array<'sms' | 'email' | 'androidpush' | 'iospush' | 'webpush' | 'inbox' | 'whatsapp' | 'slack' | 'ms_teams'>) | undefined, routing_basis?: 'cost_low_to_high' | undefined}}; export type WorkflowCreateUpdate = {name: string, description?: (string | null) | undefined, is_enabled?: boolean | undefined, category: string, tags?: Array | undefined, trigger_type: 'event' | 'api', payload_schema?: ({schema?: string | undefined, version_no?: number | undefined} | null) | undefined, trigger_events?: Array | undefined, ratelimit?: ({count?: number | undefined, sliding_window?: string | undefined} | null) | undefined, conditions?: (Array<{type?: 'expression_v1' | undefined, expression_v1?: ({op?: 'AND' | undefined, args?: (Array<{op?: ('==' | '!=' | '>' | '<' | '>=' | '<=' | 'EMPTY' | 'NON_EMPTY' | 'CONTAINS' | 'NOT_CONTAINS' | 'ARRAY_INTERSECTS' | 'NOT_ARRAY_INTERSECTS' | 'DATETIME_EQUALS' | 'DATETIME_LT' | 'DATETIME_GT') | undefined, value?: string | undefined, variable?: string | undefined, variable_ns?: ('' | '$recipient' | '$actor' | '$tenant' | '$brand' | '$organization' | '$category') | undefined}>) | undefined}) | undefined}> | null) | undefined, override_recipients_type?: ('user' | 'single_object_fields') | undefined, override_recipients_user_expr?: (string | null) | undefined, override_recipients_single_object_fields_expr?: ({id?: string | undefined, object_type?: string | undefined, '$object_subscriptions_query.depth'?: string | undefined} | null) | undefined, override_actor_user_expr?: (string | null) | undefined, override_tenant_expr?: (string | null) | undefined, tree: {nodes?: (Array) | undefined}}; export type CreateApiV1WorkspaceWorkflowSlugArgs = {workspace: string, slug: string, commit?: boolean | undefined, commit_message?: string | undefined, body: WorkflowCreateUpdate}; export type CreateApiV1WorkspaceWorkflowSlugOptions = {baseUrl?: string | undefined, ServiceTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1WorkspaceWorkflowSlugResponse = z.object({validation_result: z.object({is_valid: z.boolean().optional(), errors: z.array(z.string()).optional()}).optional(), commit_result: z.object({is_committed: z.boolean().optional(), errors: z.array(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 }; /** * Create or Update Workflow * * POST /v1/{workspace}/workflow/{slug}/ */ export const createApiV1WorkspaceWorkflowSlug = async (args: CreateApiV1WorkspaceWorkflowSlugArgs, options?: CreateApiV1WorkspaceWorkflowSlugOptions) => { const {baseUrl = "https://management-api.suprsend.com", fetch: fetchFn = fetch, ServiceTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.commit != null) query.append("commit", String(args.commit)) if (args.commit_message != null) query.append("commit_message", String(args.commit_message)) if (ServiceTokenAuth !== undefined) headers.set("Authorization", ServiceTokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.workspace))}/workflow/${encodeURIComponent(String(args.slug))}/${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(createApiV1WorkspaceWorkflowSlugResponse, await res.json()) };