import {z} from 'zod' export type AppModelsCoreTierBaseTierType = 'free' | 'entry' | 'entry+' | 'enhanced' | 'premium' | 'professional' | 'business' | 'ultra' | 'enterprise' | 'starter' | 'enterprise+' | 'advanced' | 'pro'; export type GetApiTiersDowngradeFeaturesLostArgs = {current_tier: AppModelsCoreTierBaseTierType, new_tier: AppModelsCoreTierBaseTierType}; export type GetApiTiersDowngradeFeaturesLostOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const tierLostFeaturesResponse = z.object({features: z.array(z.string()), nr_of_team_members_affected: z.number().int(), total_members: z.number().int()}); 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 }; /** * Get Lost Features On Downgrade * * GET /tiers/downgrade/features-lost */ export const getApiTiersDowngradeFeaturesLost = async (args: GetApiTiersDowngradeFeaturesLostArgs, options: GetApiTiersDowngradeFeaturesLostOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.current_tier != null) query.append("current_tier", String(args.current_tier)) if (args.new_tier != null) query.append("new_tier", String(args.new_tier)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/tiers/downgrade/features-lost${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(tierLostFeaturesResponse, await res.json()) };