import {z} from 'zod' export type MerchantRegistrationInput = {brand_name: string, uri: string, category_code: string, country?: string | undefined, clientEvidence: {checkout_page_version?: string | undefined, timezone?: string | undefined, fingerprint?: string | undefined, consented_agreements: Array<{kind: string, agreement_id: string}>, checkbox_language: Array<{checkbox_id: string, rendered_text: string, covers_kinds: Array}>}}; export type CreateApiApiV1MerchantsRegisterArgs = {body: MerchantRegistrationInput}; export type CreateApiApiV1MerchantsRegisterOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const merchantRegistrationResponse = z.object({success: z.boolean(), data: z.object({merchant: z.object({id: z.string(), brand_name: z.string(), uri: z.string(), category_code: z.string(), activation_status: z.union([z.literal("active"), z.literal("inactive")]), mode: z.union([z.literal("sandbox"), z.literal("live")]), country: z.string().min(2).max(2).optional(), settlement_currency: z.string().min(3).max(3).nullable().optional(), multi_account_setting: z.boolean(), refund_retention_rate: z.string().nullable(), client_id: z.string(), has_client_secret: z.boolean(), account_owners: z.array(z.object({id: z.string(), full_name: z.string().max(255), email: z.string().max(255), phone_number: z.string().optional(), role: z.string().max(100).optional(), is_primary: z.boolean(), created_at: z.string(), updated_at: z.string()})).optional()}), access_token: z.string(), refresh_token: z.string(), token_type: z.literal("Bearer"), expires_in: z.number()})}); 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 }; /** * Self-service merchant registration for users with global:user role * * POST /api/v1/merchants/register */ export const createApiApiV1MerchantsRegister = async (args: CreateApiApiV1MerchantsRegisterArgs, options?: CreateApiApiV1MerchantsRegisterOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/merchants/register${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(merchantRegistrationResponse, await res.json()) };