import {z} from 'zod' export type Ordering = 'asc' | 'desc'; export type EventType = 'account.closed' | 'account.created' | 'account.opened' | 'account.rejected' | 'batch_order.accepted' | 'batch_order.canceled' | 'batch_order.canceling' | 'batch_order.confirmed' | 'batch_order.created' | 'batch_order.executed' | 'batch_order.rejected' | 'cash_account.created' | 'check.action_required' | 'check.processing' | 'check.pending' | 'check.succeeded' | 'check.failed' | 'corporate_action.created' | 'corporate_action.processed' | 'deposit.created' | 'deposit.rejected' | 'deposit.received' | 'dividend_distribution.created' | 'document.created' | 'identification.created' | 'identification.failed' | 'identification.started' | 'identification.succeeded' | 'income_distribution.created' | 'order.accepted' | 'order.canceled' | 'order.canceling' | 'order.confirmed' | 'order.created' | 'order.executed' | 'order.rejected' | 'payment_instrument_mandate.canceled' | 'payment_instrument_mandate.confirmed' | 'payment_instrument_mandate.created' | 'payment_instrument_mandate.rejected' | 'securities_account.created' | 'securities_account.opened' | 'securities_account.rejected' | 'securities_account.closed' | 'securities_transfer.created' | 'securities_transfer.completed' | 'settlement.created' | 'sweep.canceled' | 'sweep.created' | 'sweep.processed' | 'sweep.rejected' | 'sweep.started' | 'tax.canceled' | 'tax.created' | 'tax.processed' | 'tax_exemption_order.accepted' | 'tax_exemption_order.canceled' | 'tax_exemption_order.canceling' | 'tax_exemption_order.created' | 'tax_exemption_order.rejected' | 'trade.cash_settled' | 'trade.created' | 'trade.taxes_processed' | 'treasury_mandate.accepted' | 'treasury_mandate.canceled' | 'treasury_mandate.confirmed' | 'treasury_mandate.created' | 'treasury_mandate.rejected' | 'treasury_transfer.created' | 'treasury_transfer.pending' | 'treasury_transfer.processed' | 'treasury_transfer.canceled' | 'treasury_transfer.canceling' | 'treasury_transfer.settled' | 'treasury_transfer.rejected' | 'update.created' | 'update.succeeded' | 'update.failed' | 'withdrawal.accepted' | 'withdrawal.canceled' | 'withdrawal.confirmed' | 'withdrawal.created' | 'withdrawal.processed' | 'withdrawal.rejected' | 'workflow.created' | 'workflow.confirmed' | 'workflow.canceled'; export type GetApiEventsArgs = {'context.account'?: string | undefined, 'context.batch_order'?: string | undefined, 'context.identification'?: string | undefined, 'context.order'?: string | undefined, 'context.person'?: string | undefined, 'context.securities_account'?: string | undefined, 'context.securities_transfer'?: string | undefined, 'context.sweep'?: string | undefined, 'context.tax_exemption_order'?: string | undefined, 'context.trade'?: string | undefined, 'context.transaction'?: string | undefined, 'context.treasury_mandate'?: string | undefined, 'context.treasury_transfer'?: string | undefined, 'context.update'?: string | undefined, 'context.workflow'?: string | undefined, cursor?: string | undefined, limit?: number | undefined, since?: string | undefined, sort?: Ordering | undefined, type?: EventType | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiEventsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventType = z.enum(["account.closed", "account.created", "account.opened", "account.rejected", "batch_order.accepted", "batch_order.canceled", "batch_order.canceling", "batch_order.confirmed", "batch_order.created", "batch_order.executed", "batch_order.rejected", "cash_account.created", "check.action_required", "check.processing", "check.pending", "check.succeeded", "check.failed", "corporate_action.created", "corporate_action.processed", "deposit.created", "deposit.rejected", "deposit.received", "dividend_distribution.created", "document.created", "identification.created", "identification.failed", "identification.started", "identification.succeeded", "income_distribution.created", "order.accepted", "order.canceled", "order.canceling", "order.confirmed", "order.created", "order.executed", "order.rejected", "payment_instrument_mandate.canceled", "payment_instrument_mandate.confirmed", "payment_instrument_mandate.created", "payment_instrument_mandate.rejected", "securities_account.created", "securities_account.opened", "securities_account.rejected", "securities_account.closed", "securities_transfer.created", "securities_transfer.completed", "settlement.created", "sweep.canceled", "sweep.created", "sweep.processed", "sweep.rejected", "sweep.started", "tax.canceled", "tax.created", "tax.processed", "tax_exemption_order.accepted", "tax_exemption_order.canceled", "tax_exemption_order.canceling", "tax_exemption_order.created", "tax_exemption_order.rejected", "trade.cash_settled", "trade.created", "trade.taxes_processed", "treasury_mandate.accepted", "treasury_mandate.canceled", "treasury_mandate.confirmed", "treasury_mandate.created", "treasury_mandate.rejected", "treasury_transfer.created", "treasury_transfer.pending", "treasury_transfer.processed", "treasury_transfer.canceled", "treasury_transfer.canceling", "treasury_transfer.settled", "treasury_transfer.rejected", "update.created", "update.succeeded", "update.failed", "withdrawal.accepted", "withdrawal.canceled", "withdrawal.confirmed", "withdrawal.created", "withdrawal.processed", "withdrawal.rejected", "workflow.created", "workflow.confirmed", "workflow.canceled"]); export const accountEventContext = z.object({account: z.string()}); export const batchOrderEventContext = z.object({account: z.string(), batch_order: z.string()}); export const customerAccountCheckEventContext = z.object({account: z.string(), check: z.string()}); export const customerWorkflowEventContext = z.object({account: z.string(), workflow: z.string()}); export const customerWorkflowDocumentEventContext = z.object({account: z.string(), document: z.string(), workflow: z.string()}); export const customerWorkflowOrderTradeDocumentEventContext = z.object({account: z.string(), batch_order: z.string(), document: z.string(), order: z.string().optional(), trade: z.string(), transaction: z.string(), workflow: z.string()}); export const depositEventContext = z.object({account: z.string(), deposit: z.string()}); export const depositTransactionEventContext = z.object({account: z.string(), deposit: z.string(), transaction: z.string()}); export const dividendDistributionEventContext = z.object({account: z.string(), dividend_distribution: z.string(), securities_account: z.string(), transaction: z.string()}); export const documentEventContext = z.object({account: z.string(), document: z.string()}); export const identificationEventContext = z.object({person: z.string(), identification: z.string()}); export const incomeDistributionConfirmationDocumentEventContext = z.object({account: z.string(), document: z.string(), income_distribution: z.string()}); export const incomeDistributionEventContext = z.object({account: z.string(), income_distribution: z.string(), securities_account: z.string()}); export const instrumentUpdateEventContext = z.object({instrument: z.string()}); export const orderDocumentEventContext = z.object({account: z.string(), document: z.string(), batch_order: z.string(), order: z.string().nullable().optional()}); export const orderEventContext = z.object({account: z.string(), order: z.string()}); export const tradeEventContext = z.object({account: z.string(), securities_account: z.string(), trade: z.string(), order: z.string()}); export const tradeSettledEventContext = z.object({account: z.string(), order: z.string(), trade: z.string(), transaction: z.string()}); export const orderTradeDocumentEventContext = z.object({account: z.string(), document: z.string(), batch_order: z.string(), order: z.string().nullable().optional(), trade: z.string(), transaction: z.string().nullable().optional()}); export const orderWorkflowEventContext = z.object({account: z.string(), order: z.string(), workflow: z.string()}); export const paymentInstrumentMandateEventContext = z.object({account: z.string(), payment_instrument_mandate: z.string()}); export const personDataUpdateEventContext = z.object({person: z.string(), update: z.string()}); export const securitiesAccountEventContext = z.object({account: z.string(), securities_account: z.string(), product: z.string().optional()}); export const securitiesTransferEventContext = z.object({account: z.string(), transaction: z.string(), securities_transfer: z.string()}); export const settlementEventContext = z.object({settlement: z.string().optional(), settlement_account: z.string().optional()}); export const taxExemptionOrderEventContext = z.object({account: z.string(), tax_exemption_order: z.string()}); export const taxGeneralEventContext = z.object({account: z.string(), tax: z.string(), transaction: z.string()}); export const taxInstrumentTransactionEventContext = z.object({account: z.string(), instrument: z.string(), tax: z.string(), transaction: z.string()}); export const treasuryMandateEventContext = z.object({account: z.string(), treasury_mandate: z.string()}); export const treasuryTransferEventContext = z.object({account: z.string(), order: z.string(), treasury_transfer: z.string()}); export const treasuryTransferSettlementEventContext = z.object({account: z.string(), order: z.string(), trade: z.string(), treasury_transfer: z.string()}); export const withdrawalEventContext = z.object({account: z.string(), withdrawal: z.string()}); export const withdrawalTransactionEventContext = z.object({account: z.string(), transaction: z.string(), withdrawal: z.string()}); export const eventExternal = z.object({id: z.string(), type: eventType, created_at: z.string(), context: z.union([accountEventContext, batchOrderEventContext, customerAccountCheckEventContext, customerWorkflowEventContext, customerWorkflowDocumentEventContext, customerWorkflowOrderTradeDocumentEventContext, batchOrderEventContext, depositEventContext, depositTransactionEventContext, dividendDistributionEventContext, documentEventContext, identificationEventContext, incomeDistributionConfirmationDocumentEventContext, incomeDistributionEventContext, instrumentUpdateEventContext, orderDocumentEventContext, orderEventContext, tradeEventContext, tradeSettledEventContext, orderTradeDocumentEventContext, orderWorkflowEventContext, paymentInstrumentMandateEventContext, personDataUpdateEventContext, securitiesAccountEventContext, securitiesTransferEventContext, settlementEventContext, taxExemptionOrderEventContext, taxGeneralEventContext, taxInstrumentTransactionEventContext, treasuryMandateEventContext, treasuryTransferEventContext, treasuryTransferSettlementEventContext, withdrawalEventContext, withdrawalTransactionEventContext])}); export const pagination = z.object({next_cursor: z.string().optional()}); export const pageEventExternal = z.object({data: z.array(eventExternal), pagination: pagination}); 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 Events * * GET /events */ export const getApiEvents = async (args: GetApiEventsArgs, options?: GetApiEventsOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['context.account'] != null) query.append("context.account", String(args['context.account'])) if (args['context.batch_order'] != null) query.append("context.batch_order", String(args['context.batch_order'])) if (args['context.identification'] != null) query.append("context.identification", String(args['context.identification'])) if (args['context.order'] != null) query.append("context.order", String(args['context.order'])) if (args['context.person'] != null) query.append("context.person", String(args['context.person'])) if (args['context.securities_account'] != null) query.append("context.securities_account", String(args['context.securities_account'])) if (args['context.securities_transfer'] != null) query.append("context.securities_transfer", String(args['context.securities_transfer'])) if (args['context.sweep'] != null) query.append("context.sweep", String(args['context.sweep'])) if (args['context.tax_exemption_order'] != null) query.append("context.tax_exemption_order", String(args['context.tax_exemption_order'])) if (args['context.trade'] != null) query.append("context.trade", String(args['context.trade'])) if (args['context.transaction'] != null) query.append("context.transaction", String(args['context.transaction'])) if (args['context.treasury_mandate'] != null) query.append("context.treasury_mandate", String(args['context.treasury_mandate'])) if (args['context.treasury_transfer'] != null) query.append("context.treasury_transfer", String(args['context.treasury_transfer'])) if (args['context.update'] != null) query.append("context.update", String(args['context.update'])) if (args['context.workflow'] != null) query.append("context.workflow", String(args['context.workflow'])) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.since != null) query.append("since", String(args.since)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.type != null) query.append("type", String(args.type)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events${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(pageEventExternal, await res.json()) };