import {z} from 'zod' export type EmailSubscriptionChannelRequest = {type: 'EMAIL'}; export type ExtoleClientSlackSubscriptionChannelRequest = {type: 'EXTOLE_CLIENT_SLACK'}; export type SlackSubscriptionChannelRequest = {type: 'SLACK', webhook_url: string}; export type ThirdPartyEmailSubscriptionChannelRequest = {type: 'THIRD_PARTY_EMAIL', recipient: string}; export type WebhookSubscriptionChannelRequest = {type: 'WEBHOOK', webhook_id: string}; export type SubscriptionChannelRequest = EmailSubscriptionChannelRequest | ExtoleClientSlackSubscriptionChannelRequest | SlackSubscriptionChannelRequest | ThirdPartyEmailSubscriptionChannelRequest | WebhookSubscriptionChannelRequest; export type FilterExpressionInUserSubscriptionUpdateRequest = boolean | string | string; export type UserSubscriptionUpdateRequest = {channels?: Array | undefined, dedupe_duration_ms?: number | undefined, filter_expression?: FilterExpressionInUserSubscriptionUpdateRequest | undefined, filtering_level?: ('ALL' | 'NONE' | 'NOTEWORTHY' | 'SOME') | undefined, having_all_tags?: Array | undefined}; export type UpdateApiV6UsersUserIdSubscriptionsSubscriptionIdArgs = {user_id: string, subscription_id: string, body: UserSubscriptionUpdateRequest}; export type UpdateApiV6UsersUserIdSubscriptionsSubscriptionIdOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const emailSubscriptionChannelResponse = z.object({id: z.string(), type: z.literal("EMAIL")}); export const extoleClientSlackSubscriptionChannelResponse = z.object({id: z.string(), type: z.literal("EXTOLE_CLIENT_SLACK")}); export const slackSubscriptionChannelResponse = z.object({id: z.string(), type: z.literal("SLACK"), webhook_url: z.string()}); export const thirdPartyEmailSubscriptionChannelResponse = z.object({id: z.string(), type: z.literal("THIRD_PARTY_EMAIL"), recipient: z.string()}); export const webhookSubscriptionChannelResponse = z.object({id: z.string(), type: z.literal("WEBHOOK"), webhook_id: z.string()}); export const subscriptionChannelResponse = z.discriminatedUnion("type", [emailSubscriptionChannelResponse, extoleClientSlackSubscriptionChannelResponse, slackSubscriptionChannelResponse, thirdPartyEmailSubscriptionChannelResponse, webhookSubscriptionChannelResponse]); export const filterExpressionInUserSubscriptionResponse = z.union([z.boolean(), z.string(), z.string()]); export const userSubscriptionResponse = z.object({channels: z.array(subscriptionChannelResponse), dedupe_duration_ms: z.number().int(), filter_expression: filterExpressionInUserSubscriptionResponse, filtering_level: z.enum(["ALL", "NONE", "NOTEWORTHY", "SOME"]), having_all_tags: z.array(z.string()), subscription_id: z.string()}); 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 a user subscription * * PUT /v6/users/{user_id}/subscriptions/{subscription_id} * * Also accepts auth: QUERY, COOKIE */ export const updateApiV6UsersUserIdSubscriptionsSubscriptionId = async (args: UpdateApiV6UsersUserIdSubscriptionsSubscriptionIdArgs, options?: UpdateApiV6UsersUserIdSubscriptionsSubscriptionIdOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/users/${encodeURIComponent(String(args.user_id))}/subscriptions/${encodeURIComponent(String(args.subscription_id))}${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(userSubscriptionResponse, await res.json()) };