import {z} from 'zod' export type AccountId = string; export type GetApiAccountsAccountIdLocateOrdersArgs = {account_id: AccountId}; export type GetApiAccountsAccountIdLocateOrdersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const locateOrderId = z.string().min(1).max(256); export const timestamp = z.number().int().gte(1710613560668).lte(2900000000000); export const mpId = z.string().min(1).max(12); export const accountId = z.string().min(1).max(256); export const accountNumber = z.string().min(1).max(256); export const symbol = z.string().min(1).max(32); export const locateOrderStatus = z.enum(["pending", "offered", "filled", "rejected", "declined", "expired", "cancelled"]); export const quantity = z.string().min(1).max(16); export const locateId = z.string().min(1).max(256); export const rate = z.string().min(1).max(16); export const price = z.string().min(1).max(16); export const referenceId = z.string().min(1).max(256); export const locateOrder = z.object({locate_order_id: locateOrderId, requested_at: timestamp, updated_at: timestamp, mpid: mpId, account_id: accountId, account_number: accountNumber, symbol: symbol, status: locateOrderStatus, requested_quantity: quantity, locate_id: locateId.optional(), located_at: timestamp.optional(), expires_at: timestamp.optional(), borrow_rate: rate.optional(), located_quantity: quantity.optional(), total_cost: price.optional(), desk_comment: z.string().optional(), trader_comment: z.string().optional(), reference_id: referenceId.optional()}); export const getApiAccountsAccountIdLocateOrdersResponse = z.object({data: z.array(locateOrder)}); 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 locate orders * * GET /accounts/{account_id}/locate-orders */ export const getApiAccountsAccountIdLocateOrders = async (args: GetApiAccountsAccountIdLocateOrdersArgs, options?: GetApiAccountsAccountIdLocateOrdersOptions) => { const {baseUrl = "https://api.clearstreet.io/studio/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/locate-orders${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(getApiAccountsAccountIdLocateOrdersResponse, await res.json()) };