import {z} from 'zod' export type GetApiTeamsArgs = {after?: string | undefined, per_page?: number | undefined, sort_order?: ('asc' | 'desc') | undefined, sort_property?: ('id' | 'updated_at') | undefined}; export type GetApiTeamsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const billingSubscriptionIncludedPriceAttributes = z.object({id: z.number().int().optional(), subscription_id: z.number().int().optional(), price_id: z.string().optional(), quantity: z.number().int().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const billingSubscriptionAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), team_id: z.number().int().optional(), status: z.string().nullable().optional(), cycle_ends_at: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), included_prices: z.array(billingSubscriptionIncludedPriceAttributes).optional()}); export const userAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), email: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), time_zone: z.string().nullable().optional(), locale: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const membershipAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), team_id: z.number().int().nullable().optional(), user_id: z.number().int().nullable().optional(), user_first_name: z.string().nullable().optional(), user_last_name: z.string().nullable().optional(), user_profile_photo_id: z.string().nullable().optional(), user_email: z.string().nullable().optional(), added_by_id: z.number().int().nullable().optional(), platform_agent_of_id: z.number().int().nullable().optional(), role_ids: z.array(z.string()).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), user: userAttributes.optional()}); export const teamAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), name: z.string().optional(), time_zone: z.string().nullable().optional(), locale: z.enum(["en", "ja"]).optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), billing_subscriptions: z.array(billingSubscriptionAttributes).optional(), memberships: z.array(membershipAttributes).optional()}); export const getApiTeamsResponse = z.array(teamAttributes); 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 Teams * * GET /teams * * Also accepts auth: OAuth2 */ export const getApiTeams = async (args: GetApiTeamsArgs, options?: GetApiTeamsOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.sort_property != null) query.append("sort_property", String(args.sort_property)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/teams${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(getApiTeamsResponse, await res.json()) };