import {z} from 'zod' export type GetApiV1DomainsDomainIdAccountEntityIdSponsorableDomainsArgs = {domainId: string, entityId: string, status?: ('sponsored' | 'not_sponsored' | 'sponsored_by_other') | undefined, search?: string | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiV1DomainsDomainIdAccountEntityIdSponsorableDomainsOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const gasStationLedgerDto = z.object({id: z.string(), alias: z.string()}); export const gasStationSponsorshipDetailsDto = z.object({sponsorAccountId: z.string(), sponsorAccountName: z.string(), sponsorType: z.enum(["account", "domain"]), sponsoredDomainId: z.string().optional(), sponsoredDomainName: z.string().optional()}); export const gasStationSponsorableDomainDto: z.ZodType = z.object({domainId: z.string(), name: z.string(), ledgers: z.array(gasStationLedgerDto), sponsorshipStatus: z.enum(["sponsored", "not_sponsored", "sponsored_by_other"]), sponsorshipDetails: gasStationSponsorshipDetailsDto.optional(), accountIds: z.array(z.string()), parentId: z.object({}).optional(), depth: z.number(), hasAccountsSponsoredByOther: z.boolean(), children: z.array(z.lazy(() => gasStationSponsorableDomainDto))}); export const gasStationSponsorableDomainsResponseDto = z.object({items: z.array(gasStationSponsorableDomainDto), total: z.number(), page: z.number(), pageSize: z.number(), availableLedgers: z.array(gasStationLedgerDto)}); 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 sponsorable domains * * GET /v1/domains/{domainId}/account/{entityId}/sponsorable-domains */ export const getApiV1DomainsDomainIdAccountEntityIdSponsorableDomains = async (args: GetApiV1DomainsDomainIdAccountEntityIdSponsorableDomainsArgs, options: GetApiV1DomainsDomainIdAccountEntityIdSponsorableDomainsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.search != null) query.append("search", String(args.search)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/${encodeURIComponent(String(args.domainId))}/account/${encodeURIComponent(String(args.entityId))}/sponsorable-domains${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(gasStationSponsorableDomainsResponseDto, await res.json()) };