import {z} from 'zod' export type CurrencyCode = unknown; export type Money = {currency_code: CurrencyCode, value: string}; export type CountryCode = unknown; export type AddressDetails = {street_number?: string | undefined, street_name?: string | undefined, street_type?: string | undefined, delivery_service?: string | undefined, building_name?: string | undefined, sub_building?: string | undefined}; export type AddressPortable = {address_line_1?: string | undefined, address_line_2?: string | undefined, address_line_3?: string | undefined, admin_area_4?: string | undefined, admin_area_3?: string | undefined, admin_area_2?: string | undefined, admin_area_1?: string | undefined, postal_code?: string | undefined, country_code: CountryCode, address_details?: AddressDetails | undefined}; export type OfferType = 'REFUND' | 'REFUND_WITH_RETURN' | 'REFUND_WITH_REPLACEMENT' | 'REPLACEMENT_WITHOUT_REFUND'; export type MakeOffer = {note: string, offer_amount?: Money | undefined, return_shipping_address?: AddressPortable | undefined, invoice_id?: string | undefined, offer_type: OfferType}; export type CreateApiV1CustomerDisputesIdMakeOfferArgs = {id: string, body: MakeOffer}; export type CreateApiV1CustomerDisputesIdMakeOfferOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const linkDescription = z.object({href: z.string(), rel: z.string(), method: z.enum(["GET", "POST", "PUT", "DELETE", "HEAD", "CONNECT", "OPTIONS", "PATCH"]).optional()}); export const definitionsLinkDescriptionList = z.array(linkDescription); export const subsequentAction = z.object({links: definitionsLinkDescriptionList.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 }; /** * Make offer to resolve dispute * * POST /v1/customer/disputes/{id}/make-offer * * Also accepts auth: Oauth2 */ export const createApiV1CustomerDisputesIdMakeOffer = async (args: CreateApiV1CustomerDisputesIdMakeOfferArgs, options?: CreateApiV1CustomerDisputesIdMakeOfferOptions) => { const {baseUrl = "https://api-m.paypal.com", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customer/disputes/${encodeURIComponent(String(args.id))}/make-offer${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(subsequentAction, await res.json()) };