import {z} from 'zod' export type GetApiApiV3TeamsTeamIdInvoicesArgs = {cursor?: (string | null) | undefined, page_size?: number | undefined, team_id: string}; export type GetApiApiV3TeamsTeamIdInvoicesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceStatus = z.enum(["draft", "open", "paid", "uncollectible", "void"]); export const teamInvoicesSchemasInvoiceSchema = z.object({provider_id: z.string(), status: invoiceStatus.nullable(), total: z.number().int(), created_at: z.string(), billing_reason: z.string().nullable(), url: z.string().nullable()}); export const teamInvoicesSchemasInvoiceSchemaCursorPagination = z.object({data: z.array(teamInvoicesSchemasInvoiceSchema), page_size: z.number().int(), cursor: z.string().nullable()}); 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 team invoices * * GET /api/v3/teams/{team_id}/invoices */ export const getApiApiV3TeamsTeamIdInvoices = async (args: GetApiApiV3TeamsTeamIdInvoicesArgs, options?: GetApiApiV3TeamsTeamIdInvoicesOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/teams/${encodeURIComponent(String(args.team_id))}/invoices${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(teamInvoicesSchemasInvoiceSchemaCursorPagination, await res.json()) };