import {z} from 'zod' export type SubAccountsListSubAccountsRequestEnabled = 'true' | 'false'; export type SubAccountsListSubAccountsRequestSortBy = 'created_at'; export type SubAccountsListSubAccountsRequestSortOrder = 'asc' | 'desc'; export type GetApiV1SubAccountsArgs = {q?: string | undefined, enabled?: SubAccountsListSubAccountsRequestEnabled | undefined, page?: number | undefined, per_page?: number | undefined, sort_by?: SubAccountsListSubAccountsRequestSortBy | undefined, sort_order?: SubAccountsListSubAccountsRequestSortOrder | undefined}; export type GetApiV1SubAccountsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subAccountObject = z.literal("sub_account"); export const subAccount = z.object({object: subAccountObject, id: z.string(), name: z.string(), external_id: z.string().nullable(), enabled: z.boolean(), products: z.array(z.string()), created_at: z.string()}); export const subAccountsListSubAccountsResponse = z.object({data: z.array(subAccount), object: z.string(), has_more: z.boolean(), total_count: z.number().int().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 }; /** * List sub-accounts * * GET /v1/sub_accounts */ export const getApiV1SubAccounts = async (args: GetApiV1SubAccountsArgs, options?: GetApiV1SubAccountsOptions) => { const {baseUrl = "https://api.middesk.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.enabled != null) query.append("enabled", String(args.enabled)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sub_accounts${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(subAccountsListSubAccountsResponse, await res.json()) };