import {z} from 'zod' export type CreateApiApiBankAccountsArgs = {body: {data?: ({attributes?: ({account_type?: string | undefined, card_number?: string | undefined, consent_until: unknown, description?: string | undefined, entity_id?: number | undefined, entity_type?: string | undefined, iban?: string | undefined, initial_balance: unknown, is_connected?: boolean | undefined, name?: string | undefined, nib?: string | undefined, only_one_payment: unknown, sub_type?: string | undefined, swift?: string | undefined}) | undefined, type?: string | undefined}) | undefined}}; export type CreateApiApiBankAccountsOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiBankAccountsResponse = z.object({data: z.object({attributes: z.object({account_type: z.string().optional(), card_number: z.string().optional(), consent_until: z.unknown(), description: z.string().optional(), entity_type: z.string().optional(), iban: z.string().optional(), initial_balance: z.unknown(), is_connected: z.boolean().optional(), name: z.string().optional(), nib: z.string().optional(), only_one_payment: z.unknown(), sub_type: z.string().optional(), swift: z.string().optional()}).optional(), id: z.string().optional(), relationships: z.object({supplier: z.object({data: z.unknown()}).optional(), user: z.object({data: z.unknown()}).optional()}).optional(), type: z.string().optional()}).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 }; /** * Criar Contas Bancárias de Utilizador * * POST /api/bank_accounts */ export const createApiApiBankAccounts = async (args: CreateApiApiBankAccountsArgs, options: CreateApiApiBankAccountsOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/bank_accounts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiBankAccountsResponse, await res.json()) };