import {z} from 'zod' export type LinkSignerDtoData = {category?: ('TRADE_SESSION' | 'API') | undefined, name?: string | undefined, nonce: string, sender: unknown, signedAt: number, signer: unknown, subaccount: unknown, subaccountId: string}; export type LinkSignerDto = {data: LinkSignerDtoData, signature: unknown, signerSignature: unknown}; export type CreateApiV1LinkedSignerLinkArgs = {body: LinkSignerDto}; export type CreateApiV1LinkedSignerLinkOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const signerDto = z.object({blockNumber: z.string().optional(), category: z.enum(["TRADE_SESSION", "API"]).optional(), createdAt: z.number().int(), expiresAt: z.number().int(), id: z.string(), linkedAt: z.number().int().optional(), name: z.string().optional(), revokedAt: z.number().int().optional(), revokedBlockNumber: z.string().optional(), signer: z.string(), status: z.enum(["PENDING", "ACTIVE", "PENDING_REVOKE", "REVOKED", "REJECTED"])}); 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 }; /** * Links a signer address with the sender address for order delegation * * POST /v1/linked-signer/link */ export const createApiV1LinkedSignerLink = async (args: CreateApiV1LinkedSignerLinkArgs, options?: CreateApiV1LinkedSignerLinkOptions) => { const {baseUrl = "https://api.ethereal.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/linked-signer/link${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(signerDto, await res.json()) };