import {z} from 'zod' export type GetApiDiscountsIdArgs = {id: string}; export type GetApiDiscountsIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyAmount = z.object({formatted: z.string().optional(), amount: z.number().optional(), currency_code: z.string().optional(), currency_symbol: z.string().optional()}); export const currencyAmountNullable = z.object({formatted: z.string().optional(), amount: z.number().optional(), currency_code: z.string().optional(), currency_symbol: z.string().optional()}).nullable(); export const discount = z.object({id: z.number().int().optional(), organization_id: z.string().optional(), type: z.enum(["allowance", "startup", "enterprise"]).optional(), type_label: z.string().optional(), status: z.enum(["inactive", "active", "expired", "deactivated"]).optional(), commitment: z.object({months: z.number().int().optional(), amount: z.object({monthly: currencyAmount.optional(), commitment_period: currencyAmount.optional(), contract_total: currencyAmount.optional()}).optional(), net: z.object({monthly: currencyAmount.optional(), commitment_period: currencyAmount.optional(), contract_total: currencyAmount.optional()}).optional()}).nullable().optional(), total_months: z.number().int().nullable().optional(), discount: z.object({monthly: currencyAmount.optional(), commitment_period: currencyAmountNullable.optional(), contract_total: currencyAmountNullable.optional()}).optional(), config: z.object({}).optional(), start_at: z.string().optional(), end_at: z.string().nullable().optional()}); 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 an organization discount * * GET /discounts/{id} */ export const getApiDiscountsId = async (args: GetApiDiscountsIdArgs, options?: GetApiDiscountsIdOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/discounts/${encodeURIComponent(String(args.id))}${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(discount, await res.json()) };