import {z} from 'zod' export type ListNonprofitsRequest = {accountHolderIds: Array}; export type CreateApiCompaniesCompanyIdNonprofitsArgs = {companyId: string, searchTerm?: string | undefined, pageNumber?: number | undefined, pageSize?: number | undefined, goal?: Array | undefined, body: ListNonprofitsRequest}; export type CreateApiCompaniesCompanyIdNonprofitsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const linksElement = z.object({href: z.string().optional()}); export const paginationLinks = z.object({first: linksElement, last: linksElement, next: linksElement.optional(), prev: linksElement.optional(), self: linksElement}); export const nonprofitCause = z.object({bannerUrl: z.string(), description: z.string(), id: z.string().optional(), locales: z.array(z.string()), name: z.string()}); export const nonprofit = z.object({causes: z.array(nonprofitCause), description: z.string(), goals: z.array(z.string()), id: z.string().optional(), locales: z.array(z.string()), logoUrl: z.string(), name: z.string(), regions: z.array(z.string()), termsAndConditionsUrl: z.string(), website: z.string()}); export const listNonprofitsResponse = z.object({_links: paginationLinks.optional(), itemsTotal: z.number().int(), nonprofits: z.array(nonprofit).optional(), pagesTotal: z.number().int()}); 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 }; /** * Get a list of nonprofits * * POST /companies/{companyId}/nonprofits * * Also accepts auth: ApiKeyAuth */ export const createApiCompaniesCompanyIdNonprofits = async (args: CreateApiCompaniesCompanyIdNonprofitsArgs, options?: CreateApiCompaniesCompanyIdNonprofitsOptions) => { const {baseUrl = "https://management-test.adyen.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 (args.searchTerm != null) query.append("searchTerm", String(args.searchTerm)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) for (const item of args.goal ?? []) query.append("goal", String(item)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companyId))}/nonprofits${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(listNonprofitsResponse, await res.json()) };