import {z} from 'zod' export type SubMerchantQueryRequest = {application_key: string, external_id: string, language?: string | undefined}; export type CreateApiApiV1SubmerchantQueryArgs = {body: SubMerchantQueryRequest}; export type CreateApiApiV1SubmerchantQueryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const subMerchantTypeEnum = z.enum(["Individual", "PersonalCompany", "Company"]); export const subMerchant = z.object({submerchant_id: z.string().optional(), external_id: z.string().optional(), merchant_name: z.string().optional(), name: z.string().optional(), type: subMerchantTypeEnum.optional(), currency: z.string().optional(), tax_number: z.string().optional(), tax_office: z.string().optional(), title: z.string().optional(), email: z.string().optional(), gsm_number: z.string().optional(), contact_name: z.string().optional(), contact_surname: z.string().optional(), iban: z.string().optional(), address: z.string().optional(), city: z.string().optional(), payment_due_day: z.number().int().optional()}); export const submerchantExternalStatusEnum = z.enum(["Active", "Waiting", "Cancelled"]); export const subMerchantExternalInfo = z.object({payment_org: z.string().optional(), external_status: submerchantExternalStatusEnum.optional()}); export const subMerchantResponse = z.object({response_code: z.string().optional(), message: z.string().optional(), submerchant_id: z.string().optional(), detail: subMerchant.optional(), externals: z.array(subMerchantExternalInfo).optional(), service_error_code: z.string().optional(), service_error_message: 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 }; /** * Query Submerchant * * POST /api/v1/submerchant/query */ export const createApiApiV1SubmerchantQuery = async (args: CreateApiApiV1SubmerchantQueryArgs, options?: CreateApiApiV1SubmerchantQueryOptions) => { const {baseUrl = "https://api.paylox.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/submerchant/query${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(subMerchantResponse, await res.json()) };