import {z} from 'zod' export type Tags = {[key: string]: unknown} | null; export type CreateExternalLinkUpload = {duration?: number | undefined, tags?: Tags | undefined, type: 'DOWNLOAD' | 'UPLOAD' | 'VIEW'}; export type CreateExternalLinkDownload = {duration?: number | undefined, tags?: Tags | undefined, type: 'DOWNLOAD' | 'UPLOAD' | 'VIEW'}; export type CreateExternalLinkView = {duration?: number | undefined, tags?: Tags | undefined, type: 'DOWNLOAD' | 'UPLOAD' | 'VIEW'}; export type CreateApiFilesFileIdExternalLinksArgs = {'Finix-Version'?: string | undefined, 'Content-Type'?: string | undefined, body: CreateExternalLinkUpload | CreateExternalLinkDownload | CreateExternalLinkView}; export type CreateApiFilesFileIdExternalLinksOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createdAt = z.string(); export const tags = z.record(z.string(), z.unknown()).nullable(); export const externalLink = z.object({id: z.string().optional(), created_at: createdAt.optional(), duration: z.number().int().nullable().optional(), expired: z.boolean().optional(), expires_at: z.string().optional(), file_id: z.string().optional(), tags: tags.optional(), type: z.enum(["UPLOAD", "DOWNLOAD", "VIEW"]).optional(), url: z.string().optional(), user_id: z.string().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 an External Link * * POST /files/{file_id}/external_links */ export const createApiFilesFileIdExternalLinks = async (args: CreateApiFilesFileIdExternalLinksArgs, options?: CreateApiFilesFileIdExternalLinksOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/files/${encodeURIComponent(String(args.file_id))}/external_links${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(externalLink, await res.json()) };