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 FilterExpressionInUserSubscriptionRequest = boolean | string | string; export type UserSubscriptionRequest = {channels?: Array | undefined, dedupe_duration_ms?: number | undefined, filter_expression?: FilterExpressionInUserSubscriptionRequest | undefined, filtering_level: 'ALL' | 'NONE' | 'NOTEWORTHY' | 'SOME', having_all_tags: Array}; export type CreateApiV6UsersUserIdSubscriptionsArgs = {user_id: string, body: UserSubscriptionRequest}; export type CreateApiV6UsersUserIdSubscriptionsOptions = {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 }; /** * Create a user subscription * * POST /v6/users/{user_id}/subscriptions * * Also accepts auth: QUERY, COOKIE */ export const createApiV6UsersUserIdSubscriptions = async (args: CreateApiV6UsersUserIdSubscriptionsArgs, options?: CreateApiV6UsersUserIdSubscriptionsOptions) => { 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${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(userSubscriptionResponse, await res.json()) };