import {z} from 'zod' export type GetApiTransactionsTransactionIdFragmentsFragmentIndexArgs = {transactionId: string, fragmentIndex: number}; export type GetApiTransactionsTransactionIdFragmentsFragmentIndexOptions = {baseUrl?: string | undefined, httpApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const direction = z.enum(["INBOUND", "OUTBOUND", "UNKNOWN"]); export const mode = z.enum(["test", "production", "other"]); export const transactionFragmentsSummary = z.object({keyName: z.string(), fragmentCount: z.number(), batchSize: z.number()}); export const artifactType = z.enum(["text/csv", "application/edifact", "application/filepart", "application/json", "application/pdf", "text/psv", "text/tsv", "application/edi-x12", "application/xml", "application/zip", "image/jpeg", "image/png", "image/tiff", "text/plain", "unknown"]); export const artifactUsage = z.enum(["attachment", "input", "metadata", "output"]); export const artifactModel = z.enum(["execution", "fragment", "transaction", "fault"]); export const artifact = z.object({artifactType: artifactType, usage: artifactUsage, sizeBytes: z.number(), url: z.string(), model: artifactModel}); export const partnershipType = z.enum(["x12", "edifact"]); export const partnershipSender = z.object({profileId: z.string().min(1).max(40)}); export const partnershipReceiver = z.object({profileId: z.string().min(1).max(40)}); export const partnership = z.object({partnershipId: z.string().min(1).max(81), partnershipType: partnershipType, sender: partnershipSender, receiver: partnershipReceiver}); export const x12TransactionMetadataInterchange = z.object({acknowledgmentRequestedCode: z.string(), controlNumber: z.number().gte(0).lte(999999999)}); export const x12TransactionMetadataFunctionalGroup = z.object({controlNumber: z.number().gte(0).lte(999999999), date: z.string(), functionalIdentifierCode: z.string(), time: z.string(), release: z.string()}); export const x12TransactionMetadataTransaction = z.object({controlNumber: z.string(), transactionSetIdentifier: z.string()}); export const x12TransactionMetadataProfileISA = z.object({qualifier: z.string(), id: z.string()}); export const x12TransactionMetadataProfile = z.object({applicationCode: z.string(), isa: x12TransactionMetadataProfileISA}); export const x12TransactionMetadata = z.object({interchange: x12TransactionMetadataInterchange, functionalGroup: x12TransactionMetadataFunctionalGroup, transaction: x12TransactionMetadataTransaction, sender: x12TransactionMetadataProfile, receiver: x12TransactionMetadataProfile}); export const x12TransactionSetting = z.object({guideId: z.string().optional(), transactionSettingId: z.string().optional()}); export const x12TransactionSummary = z.object({metadata: x12TransactionMetadata, transactionSetting: x12TransactionSetting.optional()}); export const getTransactionFragmentResponseContent = z.object({transactionId: z.string(), fileExecutionId: z.string(), direction: direction, mode: mode, processedAt: z.string(), fragmentIndex: z.number(), fragments: transactionFragmentsSummary, transactionUrl: z.string(), artifacts: z.array(artifact), partnership: partnership, x12: x12TransactionSummary.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 }; /** * Retrieve metadata for a fragment. * * GET /transactions/{transactionId}/fragments/{fragmentIndex} */ export const getApiTransactionsTransactionIdFragmentsFragmentIndex = async (args: GetApiTransactionsTransactionIdFragmentsFragmentIndexArgs, options?: GetApiTransactionsTransactionIdFragmentsFragmentIndexOptions) => { const {baseUrl = "https://core.us.stedi.com/2023-08-01", fetch: fetchFn = fetch, httpApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (httpApiKeyAuth !== undefined) headers.set("Authorization", httpApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/${encodeURIComponent(String(args.transactionId))}/fragments/${encodeURIComponent(String(args.fragmentIndex))}${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(getTransactionFragmentResponseContent, await res.json()) };