import {z} from 'zod' export type GetApiApiKeysArgs = {user_id: number, status?: string | undefined, created_greater_or_equal_than?: string | undefined, created_less_or_equal_than?: string | undefined}; export type GetApiApiKeysOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const domainApiKey = z.object({client_id: z.string().optional(), created_at: z.string().optional(), id: z.number().int().optional(), status: z.string().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional()}); export const domainApiKeyResponseFind = z.object({limit: z.number().int().optional(), page: z.number().int().optional(), rows: z.array(domainApiKey).optional(), sort: z.string().optional(), total_pages: z.number().int().optional(), total_rows: z.number().int().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 }; /** * Find * * GET /apiKeys */ export const getApiApiKeys = async (args: GetApiApiKeysArgs, options?: GetApiApiKeysOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.status != null) query.append("status", String(args.status)) if (args.created_greater_or_equal_than != null) query.append("created_greater_or_equal_than", String(args.created_greater_or_equal_than)) if (args.created_less_or_equal_than != null) query.append("created_less_or_equal_than", String(args.created_less_or_equal_than)) if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/apiKeys${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(domainApiKeyResponseFind, await res.json()) };