import {z} from 'zod' export type GetApiContactsArgs = {name?: string | undefined, email?: string | undefined, contact?: string | undefined, reference_id?: string | undefined, from?: number | undefined, to?: number | undefined, count?: number | undefined, skip?: number | undefined}; export type GetApiContactsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const xContact = z.object({id: z.string().optional(), entity: z.literal("contact").optional(), name: z.string().min(3).max(50).optional(), email: z.string().optional(), contact: z.string().optional(), type: z.enum(["vendor", "customer", "employee", "self"]).optional(), reference_id: z.string().max(40).optional(), batch_id: z.string().optional(), active: z.boolean().optional(), notes: z.object({}).optional(), created_at: z.number().int().optional()}); export const getApiContactsResponse = z.object({entity: z.string().optional(), count: z.number().int().optional(), items: z.array(xContact).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 }; /** * List contacts * * GET /contacts * * Also accepts auth: oauth2 */ export const getApiContacts = async (args: GetApiContactsArgs, options?: GetApiContactsOptions) => { const {baseUrl = "https://api.razorpay.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.email != null) query.append("email", String(args.email)) if (args.contact != null) query.append("contact", String(args.contact)) if (args.reference_id != null) query.append("reference_id", String(args.reference_id)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.count != null) query.append("count", String(args.count)) if (args.skip != null) query.append("skip", String(args.skip)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/contacts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiContactsResponse, await res.json()) };