import {z} from 'zod' export type GetApiOpenApiV10ThankYouContributionsArgs = {page?: number | undefined, size?: number | undefined, state?: (Array<'WAITING' | 'SKIPPED' | 'SENT' | 'CANCELLED'>) | undefined, search?: string | undefined, minValue?: number | undefined, containsMsg?: boolean | undefined}; export type GetApiOpenApiV10ThankYouContributionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const comFourthwallOpenapiModelKudosSupporterV1 = z.object({email: z.string(), fullName: z.string().optional()}); export const comFourthwallOpenapiModelKudosProductV1 = z.object({title: z.string(), thumbnailUrl: z.string().optional()}); export const comFourthwallOpenapiModelKudosMoneyV1 = z.object({amount: z.number(), currency: z.string()}); export const comFourthwallOpenapiModelKudosOrderV1 = z.object({id: z.string(), orderId: z.string(), supporter: comFourthwallOpenapiModelKudosSupporterV1, products: z.array(comFourthwallOpenapiModelKudosProductV1), message: z.string().optional(), total: comFourthwallOpenapiModelKudosMoneyV1, state: z.enum(["WAITING", "SKIPPED", "SENT", "CANCELLED"]), createdAt: z.string()}); export const comFourthwallOpenapiModelOpenApiPageResponseComFourthwallOpenapiModelKudosOrderV1 = z.object({results: z.array(comFourthwallOpenapiModelKudosOrderV1), total: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional(), totalPages: z.number().int().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 }; /** * Get contributions awaiting thank you * * GET /open-api/v1.0/thank-you-contributions * * Also accepts auth: basicAuth */ export const getApiOpenApiV10ThankYouContributions = async (args: GetApiOpenApiV10ThankYouContributionsArgs, options?: GetApiOpenApiV10ThankYouContributionsOptions) => { const {baseUrl = "https://api.fourthwall.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) for (const item of args.state ?? []) query.append("state", String(item)) if (args.search != null) query.append("search", String(args.search)) if (args.minValue != null) query.append("minValue", String(args.minValue)) if (args.containsMsg != null) query.append("containsMsg", String(args.containsMsg)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/open-api/v1.0/thank-you-contributions${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(comFourthwallOpenapiModelOpenApiPageResponseComFourthwallOpenapiModelKudosOrderV1, await res.json()) };