import {z} from 'zod' export type UUID = string; export type GetApiV0InvestorDocumentsArgs = {investorId: UUID, documentTypes?: (Array<'proof-of-bank-account-ownership' | 'certificate-of-incorporation' | 'articles-of-association' | 'certificate-of-good-standing' | 'official-id' | 'proof-of-address' | 'proof-of-funds' | 'extract-of-ubo-register' | 'company-register-extract' | 'screening-report' | 'power-of-attorney' | 'other-document' | 'unknown' | 'imprime-fiscal-unique' | 'cash&carry-suitability-assessment' | 'cash&carry-appropriateness-assessment' | 'regulatory-information'>) | undefined}; export type GetApiV0InvestorDocumentsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const getApiV0InvestorDocumentsResponse = z.array(z.object({id: uUID, investorId: uUID, documentType: z.enum(["proof-of-bank-account-ownership", "certificate-of-incorporation", "articles-of-association", "certificate-of-good-standing", "official-id", "proof-of-address", "proof-of-funds", "extract-of-ubo-register", "company-register-extract", "screening-report", "power-of-attorney", "other-document", "unknown", "imprime-fiscal-unique", "cash&carry-suitability-assessment", "cash&carry-appropriateness-assessment", "regulatory-information"]), contentType: z.string(), s3FilePath: z.string(), originalFileName: z.string(), createdAt: z.string(), createdBy: z.union([z.object({userId: uUID, type: z.literal("investor")}), z.object({userId: uUID, type: z.literal("user")}), z.object({userId: uUID, type: z.literal("admin")}), z.object({userId: uUID, type: z.literal("distributor-client")}), z.object({userId: uUID, type: z.literal("distributor-user")}), z.object({type: z.literal("system")})]), rejectionReason: z.string().optional(), status: z.enum(["pending", "pending_review", "active", "rejected", "archived"])})); 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 }; /** * Get investor documents * * GET /v0/investor-documents */ export const getApiV0InvestorDocuments = async (args: GetApiV0InvestorDocumentsArgs, options?: GetApiV0InvestorDocumentsOptions) => { const {baseUrl = "https://distributor-api.spiko.io", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.investorId != null) query.append("investorId", String(args.investorId)) for (const item of args.documentTypes ?? []) query.append("documentTypes", String(item)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v0/investor-documents${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(getApiV0InvestorDocumentsResponse, await res.json()) };