import {z} from 'zod' export type CreateApiUsersAuthorizationArgs = {APIKey?: string | undefined, country?: string | undefined, business_type?: string | undefined, token?: string | undefined, account_id?: string | undefined, account_name?: string | undefined, account_mask?: string | undefined, 'Bui-Accept'?: string | undefined}; export type CreateApiUsersAuthorizationOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const completeAuthorizationResponse = z.object({account_mask: z.string().optional(), account_name: z.string().optional(), authorization_id: z.string().optional(), bui: z.object({}).optional(), status: z.string().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 }; /** * Return fund authorization details for country * * POST /users/authorization * * Also accepts auth: API-Key */ export const createApiUsersAuthorization = async (args: CreateApiUsersAuthorizationArgs, options: CreateApiUsersAuthorizationOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.APIKey != null) query.append("APIKey", String(args.APIKey)) if (args.country != null) query.append("country", String(args.country)) if (args.business_type != null) query.append("business_type", String(args.business_type)) if (args.token != null) query.append("token", String(args.token)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.account_name != null) query.append("account_name", String(args.account_name)) if (args.account_mask != null) query.append("account_mask", String(args.account_mask)) if (args['Bui-Accept'] != null) query.append("Bui-Accept", String(args['Bui-Accept'])) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/users/authorization${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(completeAuthorizationResponse, await res.json()) };