import {z} from 'zod' export type GetApiV1AccountsArgs = {country_codes?: string | undefined, sort: 'price_asc' | 'price_desc' | 'name_asc' | 'name_desc' | 'popularity_asc' | 'popularity_desc', page: number, page_size: number}; export type GetApiV1AccountsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1AccountsResponse = z.object({items: z.array(z.object({country_code: z.string(), emoji: z.string(), display_name: z.object({ru: z.string().min(1).max(100), en: z.string().min(1).max(100)}), price: z.object({amount: z.string(), currency_code: z.string()}), base_price: z.object({amount: z.string(), currency_code: z.string()}), available: z.boolean(), tags: z.array(z.enum(["HIGH_QUALITY", "HIGH_DEMAND"])), popularity_index: z.number(), available_count: z.number().nullable().optional()})), pagination: z.object({current_page: z.number().int().gte(1).lte(9007199254740991), page_size: z.number().int().gte(1).lte(250), total_items: z.number().int().gte(0).lte(9007199254740991), total_pages: z.number().int().gte(0).lte(9007199254740991), has_next: z.boolean(), has_previous: z.boolean()})}); 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 }; /** * Get countries list (personal pricing) * * GET /v1/accounts/ * * Also accepts auth: BearerAuth */ export const getApiV1Accounts = async (args: GetApiV1AccountsArgs, options?: GetApiV1AccountsOptions) => { const {baseUrl = "https://api.getmytg.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.country_codes != null) query.append("country_codes", String(args.country_codes)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(getApiV1AccountsResponse, await res.json()) };