import {z} from 'zod' export type GetApiPartnerPartnerIDUsageArgs = {partnerID: string, from: unknown, to: unknown}; export type GetApiPartnerPartnerIDUsageOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const apiDailyUsage = z.object({allocated_capacity: z.number().int().optional(), date: z.string().optional(), usage: z.number().int().optional()}); export const apiClientUsage = z.object({allocated_capacity: z.number().int().optional(), client_id: z.string().optional(), daily_usage: z.array(apiDailyUsage).optional(), name: z.string().optional()}); export const getApiPartnerPartnerIDUsageResponse = z.array(apiClientUsage); 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 }; /** * Retrieves the usage of all the storage accounts of a given distributor partner * * GET /partner/{partnerID}/usage */ export const getApiPartnerPartnerIDUsage = async (args: GetApiPartnerPartnerIDUsageArgs, options?: GetApiPartnerPartnerIDUsageOptions) => { const {baseUrl = "https://api.partner.eu-central-2.impossiblecloud.com/v1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/partner/${encodeURIComponent(String(args.partnerID))}/usage${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(getApiPartnerPartnerIDUsageResponse, await res.json()) };