import {z} from 'zod' export type OnboardingUpdateStatusInput = {status: 'enrolling' | 'onboarded'}; export type UpdateApiApiV1MerchantsMerchantIdOnboardingProviderStatusArgs = {merchantId: string, provider: string, body: OnboardingUpdateStatusInput}; export type UpdateApiApiV1MerchantsMerchantIdOnboardingProviderStatusOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const onboardingProviderReadinessResponse = z.object({success: z.boolean(), data: z.object({provider: z.string(), mode: z.union([z.literal("sandbox"), z.literal("live")]), status: z.union([z.literal("not_applicable"), z.literal("not_ready"), z.literal("ready"), z.literal("enrolling"), z.literal("onboarded")]), requirements: z.array(z.object({code: z.string(), label: z.string(), met: z.boolean(), blocker: z.string().optional(), problems: z.array(z.object({field: z.string(), message: z.string(), value: z.unknown()})).optional()})), depends_on: z.array(z.object({provider: z.string(), satisfied: z.boolean()})), can_enroll: z.boolean()})}); 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 }; /** * Update onboarding status for a merchant with a specific provider. Use 'enrolling' to start enrollment or 'onboarded' to complete it. * * PUT /api/v1/merchants/{merchantId}/onboarding/{provider}/status */ export const updateApiApiV1MerchantsMerchantIdOnboardingProviderStatus = async (args: UpdateApiApiV1MerchantsMerchantIdOnboardingProviderStatusArgs, options?: UpdateApiApiV1MerchantsMerchantIdOnboardingProviderStatusOptions) => { 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/${encodeURIComponent(String(args.merchantId))}/onboarding/${encodeURIComponent(String(args.provider))}/status${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(onboardingProviderReadinessResponse, await res.json()) };