import {z} from 'zod' export type GetApiV6SubscriptionsArgs = {having_any_tags?: (string | null) | undefined, having_all_tags?: (string | null) | undefined}; export type GetApiV6SubscriptionsOptions = {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 subscriptionUserResponse = z.object({email: z.string().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), user_id: z.string().optional()}); export const subscriptionResponse = z.object({channels: z.array(subscriptionChannelResponse), dedupe_duration_ms: z.number().int(), filtering_level: z.enum(["ALL", "NONE", "NOTEWORTHY", "SOME"]), having_all_tags: z.array(z.string()), subscription_id: z.string(), user: subscriptionUserResponse}); export const getApiV6SubscriptionsResponse = z.array(subscriptionResponse); 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 }; /** * List subscriptions * * GET /v6/subscriptions * * Also accepts auth: QUERY, COOKIE */ export const getApiV6Subscriptions = async (args: GetApiV6SubscriptionsArgs, options?: GetApiV6SubscriptionsOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.having_any_tags != null) query.append("having_any_tags", String(args.having_any_tags)) if (args.having_all_tags != null) query.append("having_all_tags", String(args.having_all_tags)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/subscriptions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV6SubscriptionsResponse, await res.json()) };