import {z} from 'zod' export type GetApiApiKeysArgs = {'Accept-Language'?: ('es' | 'en') | undefined, 'X-Child-Company-Id'?: string | undefined, limit?: number | undefined, next?: string | undefined, previous?: string | undefined, search?: string | undefined}; export type GetApiApiKeysOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiKeyResponse = z.object({active: z.boolean().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), deactivated_at: z.number().int().nullable().optional(), last_used_at: z.number().int().nullable().optional(), description: z.string().optional(), id: z.string().optional(), livemode: z.boolean().optional(), object: z.string().optional(), prefix: z.string().optional(), role: z.string().optional()}); export const getApiKeysResponse = z.object({next_page_url: z.string().nullable().optional(), previous_page_url: z.string().nullable().optional(), has_more: z.boolean(), object: z.string(), data: z.array(apiKeyResponse).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 }; /** * Get list of Api Keys * * GET /api_keys */ export const getApiApiKeys = async (args: GetApiApiKeysArgs, options?: GetApiApiKeysOptions) => { const {baseUrl = "https://api.conekta.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.conekta-v2.3.0+json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['X-Child-Company-Id'] != null) headers.set("X-Child-Company-Id", String(args['X-Child-Company-Id'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.next != null) query.append("next", String(args.next)) if (args.previous != null) query.append("previous", String(args.previous)) if (args.search != null) query.append("search", String(args.search)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api_keys${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(getApiKeysResponse, await res.json()) };