import {z} from 'zod' export type GetApiAnalyticsGatewaysTopArgs = {gateway_domain: string, start_date: string, end_date: string, by: 'cid' | 'country' | 'region' | 'user_agent' | 'referrer' | 'file_name', sort_by: 'requests' | 'bandwidth', cid?: string | undefined, file_name?: string | undefined, user_agent?: string | undefined, country?: string | undefined, region?: string | undefined, referer?: string | undefined, http_status_code?: ('200' | '206' | '301' | '302' | '304' | '307' | '308' | '404' | '499') | undefined, limit?: number | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiAnalyticsGatewaysTopOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAnalyticsGatewaysTopResponse = z.object({}); 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 }; /** * Top Gateway Analytics * * GET /analytics/gateways/top */ export const getApiAnalyticsGatewaysTop = async (args: GetApiAnalyticsGatewaysTopArgs, options?: GetApiAnalyticsGatewaysTopOptions) => { const {baseUrl = "https://api.pinata.cloud/v3", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.gateway_domain != null) query.append("gateway_domain", String(args.gateway_domain)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.by != null) query.append("by", String(args.by)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.cid != null) query.append("cid", String(args.cid)) if (args.file_name != null) query.append("file_name", String(args.file_name)) if (args.user_agent != null) query.append("user_agent", String(args.user_agent)) if (args.country != null) query.append("country", String(args.country)) if (args.region != null) query.append("region", String(args.region)) if (args.referer != null) query.append("referer", String(args.referer)) if (args.http_status_code != null) query.append("http_status_code", String(args.http_status_code)) if (args.limit != null) query.append("limit", String(args.limit)) 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(/\/+$/, '')}/analytics/gateways/top${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(getApiAnalyticsGatewaysTopResponse, await res.json()) };