import {z} from 'zod' export type DisputeKey = string; export type OriginalFilenameInput = string; export type SchemasFreeTextField = string; export type DocumentCreate = {originalFilename: OriginalFilenameInput, description?: SchemasFreeTextField | undefined}; export type CreateApiDisputesDisputeKeyDocumentsArgs = {disputeKey: DisputeKey, body: DocumentCreate}; export type CreateApiDisputesDisputeKeyDocumentsOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const documentKey = z.string().min(1).max(120); export const componentsSchemasAgentKey = z.string().min(1).max(120); export const componentsSchemasMerchantKey = z.string().min(1).max(120); export const disputeKey = z.string().min(1).max(120); export const disputeStage = z.enum(["rapidDisputeResolution", "collaboration", "requestForInformation", "requestForInformationResponse", "collaborationResponse", "chargeback", "chargebackResponse", "preArbitration", "preArbitrationResponse", "arbitration"]); export const originalFilename = z.string(); export const owner = z.enum(["issuer", "acquirer", "merchant", "card-network"]); export const schemasFreeTextField = z.string().min(1).max(160); export const openapiComponentsSchemasCreated3 = z.string(); export const openapiComponentsSchemasVersion2 = z.number().int().gte(1).lte(1000); export const disputeDocumentWithoutFile = z.object({key: documentKey, agentKey: componentsSchemasAgentKey, merchantKey: componentsSchemasMerchantKey.optional(), disputeKey: disputeKey, disputeStage: disputeStage, originalFilename: originalFilename, owner: owner, description: schemasFreeTextField.optional(), status: z.literal("pending"), created: openapiComponentsSchemasCreated3, version: openapiComponentsSchemasVersion2}); 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 }; /** * Add Dispute Document * * POST /disputes/{disputeKey}/documents * * Also accepts auth: BearerToken */ export const createApiDisputesDisputeKeyDocuments = async (args: CreateApiDisputesDisputeKeyDocumentsArgs, options?: CreateApiDisputesDisputeKeyDocumentsOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/disputes/${encodeURIComponent(String(args.disputeKey))}/documents${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(disputeDocumentWithoutFile, await res.json()) };