import {z} from 'zod' export type Gateway = {gateway?: ({gateway_type: string, description?: string | undefined, merchant_profile_key?: string | undefined, sub_merchant_key?: string | undefined, sandbox?: boolean | undefined, mode?: string | undefined} | {[key: string]: string}) | undefined}; export type CreateApiGatewaysFormatArgs = {body: Gateway}; export type CreateApiGatewaysFormatOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const gatewaySpecificFields = z.array(z.string()); export const gatewayProperties = z.object({token: z.string().optional(), name: z.string().optional(), gateway_type: z.string().optional(), description: z.string().optional(), merchant_profile_key: z.string().optional(), sub_merchant_key: z.string().optional(), state: z.string().optional(), sandbox: z.boolean().optional(), redacted: z.boolean().optional(), credentials: z.array(z.unknown()).optional(), gateway_settings: z.object({}).optional(), characteristics: z.array(z.unknown()).optional(), payment_methods: z.array(z.unknown()).optional(), gateway_specific_fields: gatewaySpecificFields.optional(), mode: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const gatewayResponse = z.object({gateway: gatewayProperties.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 }; /** * Create a gateway * * POST /gateways.{format} */ export const createApiGatewaysFormat = async (args: CreateApiGatewaysFormatArgs, options?: CreateApiGatewaysFormatOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateways.${encodeURIComponent(String(args.format))}${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(gatewayResponse, await res.json()) };