import {z} from 'zod' export type GetApiVVersionBrokersIdArgs = {id: string, version: string}; export type GetApiVVersionBrokersIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const dtosCompanyLink = z.object({name: z.string(), id: z.string(), href: z.string()}); export const dtosProduct = z.object({quantity: z.number().int().optional(), cost: z.number().optional(), sku: z.string().optional(), type: z.string().optional(), name: z.string().optional(), invoice_type: z.string().optional(), paid_through_date: z.string().optional(), paid_by: dtosCompanyLink.optional()}); export const dtosTeamLink = z.object({id: z.string(), href: z.string()}); export const dtosProductLink = z.object({name: z.string(), id: z.string(), href: z.string()}); export const dtosBroker = z.object({id: z.string(), name: z.string(), active_employees_count: z.number().int().optional(), contract_start_date: z.string().optional(), subscription: dtosProduct.optional(), teams: z.array(dtosTeamLink).optional(), extra_seats: dtosProduct.optional(), products: z.array(dtosProductLink).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 }; /** * broker * * GET /v{version}/brokers/{id} */ export const getApiVVersionBrokersId = async (args: GetApiVVersionBrokersIdArgs, options: GetApiVVersionBrokersIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v${encodeURIComponent(String(args.version))}/brokers/${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(dtosBroker, await res.json()) };