import {z} from 'zod' export type DID = unknown; export type CreateApiEntitiesEntityDIDFlowPayoutsArgs = {entityDID: DID, body: {ref: string, currency: string, amount: string, supportedAssets?: Array | undefined, merchant: {'@id'?: string | undefined, name?: string | undefined, email?: string | undefined}, customer: {'@id'?: string | undefined, name?: string | undefined, email?: string | undefined}, memo?: string | undefined, invoice?: ({id?: string | undefined, dueDate?: string | undefined, lineItems?: (Array<{description?: string | undefined, quantity?: number | undefined, unitPrice?: number | undefined, lineTotal?: number | undefined}>) | undefined, paymentTerms?: string | undefined}) | undefined}}; export type CreateApiEntitiesEntityDIDFlowPayoutsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transferStatus = z.enum(["OUTGOING", "INCOMING", "AWAITING-YOURS", "AWAITING-COUNTERPARTY", "REJECTED", "AUTHORIZED", "FLAGGED", "SETTLED", "FLAGGED-SETTLEMENT", "REVERTED", "REVERT-AUTHORIZED", "REVERT-REJECTED", "REVERT-FLAGGED", "REVERT-REQUESTED", "FROZEN", "CLEARED"]); export const createApiEntitiesEntityDIDFlowPayoutsResponse = z.object({transfer: z.object({"@id": z.string().optional(), "@type": z.literal("Payout").optional(), ref: z.string().optional(), amount: z.string().optional(), status: transferStatus.optional(), flowState: z.string().optional(), paymentLink: z.string().optional(), customerDid: z.string().optional(), merchantDid: z.string().optional(), createdAt: z.string().optional()}).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 new payout within Flow workflows (entity-level) * * POST /entities/{entityDID}/flow/payouts */ export const createApiEntitiesEntityDIDFlowPayouts = async (args: CreateApiEntitiesEntityDIDFlowPayoutsArgs, options?: CreateApiEntitiesEntityDIDFlowPayoutsOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/${encodeURIComponent(String(args.entityDID))}/flow/payouts${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(createApiEntitiesEntityDIDFlowPayoutsResponse, await res.json()) };