import {z} from 'zod' export type Pair = {id?: string | undefined, baseTokenSymbol?: string | undefined, baseTokenAddress?: string | undefined, baseTokenDecimals?: number | undefined, quoteTokenSymbol?: string | undefined, quoteTokenAddress?: string | undefined, quoteTokenDecimals?: number | undefined, listed?: boolean | undefined, active?: boolean | undefined, rank?: number | undefined, makeFee?: string | undefined, takeFee?: string | undefined, createdAt?: string | undefined, updatedAt?: string | undefined}; export type CreateApiPairsArgs = {body: Pair}; export type CreateApiPairsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pair = z.object({id: z.string().optional(), baseTokenSymbol: z.string().optional(), baseTokenAddress: z.string().optional(), baseTokenDecimals: z.number().int().optional(), quoteTokenSymbol: z.string().optional(), quoteTokenAddress: z.string().optional(), quoteTokenDecimals: z.number().int().optional(), listed: z.boolean().optional(), active: z.boolean().optional(), rank: z.number().int().optional(), makeFee: z.string().optional(), takeFee: z.string().optional(), createdAt: z.string().optional(), updatedAt: 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 new pair * * POST /pairs */ export const createApiPairs = async (args: CreateApiPairsArgs, options?: CreateApiPairsOptions) => { const {baseUrl = "https://dex.tomochain.com/api", 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(/\/+$/, '')}/pairs${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(pair, await res.json()) };