import {z} from 'zod' export type RiskTypeDTO = {id: number, name?: string | undefined}; export type RiskLimitDTO = {absoluteRiskLimit?: number | undefined, accountId?: string | undefined, active?: boolean | undefined, closeAllOpenPositions?: boolean | undefined, copierId?: string | undefined, fallbackAbsoluteRiskLimit?: number | undefined, fallbackRelativeRiskLimit?: number | undefined, fallbackRiskLimit?: number | undefined, fulfillSeconds?: number | undefined, id?: string | undefined, relativeRiskLimit?: number | undefined, resetTime?: string | undefined, riskLimit: number, riskType: RiskTypeDTO, signalFollowerId?: string | undefined}; export type CreateApiRestApiV1AccountsAccountIdRiskLimitsArgs = {accountId: string, body: RiskLimitDTO}; export type CreateApiRestApiV1AccountsAccountIdRiskLimitsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const riskTypeDTO = z.object({id: z.number().int(), name: z.string().optional()}); export const riskLimitDTO = z.object({absoluteRiskLimit: z.number().gte(0).optional(), accountId: z.string().optional(), active: z.boolean().optional(), closeAllOpenPositions: z.boolean().optional(), copierId: z.string().optional(), fallbackAbsoluteRiskLimit: z.number().gte(0).optional(), fallbackRelativeRiskLimit: z.number().gte(0).optional(), fallbackRiskLimit: z.number().gte(0).lte(1).optional(), fulfillSeconds: z.number().int().optional(), id: z.string().optional(), relativeRiskLimit: z.number().gte(0).optional(), resetTime: z.string().optional(), riskLimit: z.number().gte(0).lte(1), riskType: riskTypeDTO, signalFollowerId: 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 account risk limit * * POST /rest/api/v1/accounts/{accountId}/riskLimits */ export const createApiRestApiV1AccountsAccountIdRiskLimits = async (args: CreateApiRestApiV1AccountsAccountIdRiskLimitsArgs, options?: CreateApiRestApiV1AccountsAccountIdRiskLimitsOptions) => { const {baseUrl = "https://api.metacopier.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rest/api/v1/accounts/${encodeURIComponent(String(args.accountId))}/riskLimits${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(riskLimitDTO, await res.json()) };