import {z} from 'zod' export type GetApiV3RelationshipsArgs = {'orderful-api-key': string, autoSend?: ('ENABLED' | 'DISABLED') | undefined, limit?: number | undefined, prevCursor?: string | undefined, nextCursor?: string | undefined}; export type GetApiV3RelationshipsOptions = {baseUrl?: string | undefined, APIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationLinkV3 = z.object({next: z.string().nullable(), prev: z.string().nullable()}); export const paginationV3 = z.object({links: paginationLinkV3}); export const listRelationshipsResponseV3MetadataDto = z.object({pagination: paginationV3}); export const relationshipIdentityV3Dto = z.object({ediAccountId: z.number(), liveIsaId: z.string(), testIsaId: z.string().nullable(), organizationId: z.number(), organizationName: z.string(), ediAccountName: z.string()}); export const relationshipTransactionTypeV3Dto = z.object({name: z.string()}); export const relationshipResponseV3Dto = z.object({id: z.number(), createdAt: z.string(), updatedAt: z.string(), sender: relationshipIdentityV3Dto, receiver: relationshipIdentityV3Dto, transactionType: relationshipTransactionTypeV3Dto, status: z.enum(["BLOCKED", "PENDING", "SETUP", "TEST", "READY", "LIVE"]), autoSend: z.enum(["ENABLED", "DISABLED"]).optional()}); export const listRelationshipsResponseV3Dto = z.object({metadata: listRelationshipsResponseV3MetadataDto, data: z.array(relationshipResponseV3Dto)}); 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 }; /** * List Relationships * * GET /v3/relationships */ export const getApiV3Relationships = async (args: GetApiV3RelationshipsArgs, options?: GetApiV3RelationshipsOptions) => { const {baseUrl = "https://api.orderful.com", fetch: fetchFn = fetch, APIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['orderful-api-key'] != null) headers.set("orderful-api-key", String(args['orderful-api-key'])) if (args.autoSend != null) query.append("autoSend", String(args.autoSend)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.prevCursor != null) query.append("prevCursor", String(args.prevCursor)) if (args.nextCursor != null) query.append("nextCursor", String(args.nextCursor)) if (APIKEY !== undefined) headers.set("orderful-api-key", APIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/v3/relationships${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(listRelationshipsResponseV3Dto, await res.json()) };