import {z} from 'zod' export type GetApiApiV1MarketsMarketAddressArgs = {market_address: string, locale?: string | undefined}; export type GetApiApiV1MarketsMarketAddressOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const internalHandlerAddressInfoResponse = z.object({address: z.string().optional(), image: z.string().optional(), name: z.string().optional()}); export const internalHandlerOutcomeTranslation = z.object({name: z.string().optional()}); export const internalHandlerOutcomeResponse = z.object({holdersAtFinalisation: z.number().int().optional(), image: z.string().optional(), index: z.number().int().optional(), marketCap: z.number().optional(), mintedQuantity: z.number().optional(), name: z.string().optional(), payout: z.number().optional(), price: z.number().optional(), symbol: z.string().optional(), tokenId: z.string().optional(), translation: internalHandlerOutcomeTranslation.optional(), volume: z.number().optional()}); export const internalHandlerProposerResponse = z.object({image: z.string().optional(), name: z.string().optional()}); export const internalHandlerMarketTranslation = z.object({ancillary: z.array(z.string()).optional(), title: z.string().optional()}); export const internalHandlerMarketResponse = z.object({address: z.string().optional(), ancillaryData: z.array(z.string()).optional(), availableLocales: z.array(z.string()).optional(), backgroundImage: z.string().optional(), categories: z.array(z.string()).optional(), collateralAddress: z.string().optional(), collateralDecimals: z.number().int().optional(), collateralSymbol: z.string().optional(), contractVersion: z.number().int().optional(), createdAt: z.string().optional(), creator: internalHandlerAddressInfoResponse.optional(), curve: z.string().optional(), description: z.string().optional(), elapsedPct: z.number().optional(), endDate: z.string().optional(), feeRate: z.number().optional(), finalisedAt: z.string().optional(), image: z.string().optional(), isFlagged: z.boolean().optional(), oracle: internalHandlerAddressInfoResponse.optional(), outcomes: z.array(internalHandlerOutcomeResponse).optional(), proposer: internalHandlerProposerResponse.optional(), question: z.string().optional(), questionId: z.string().optional(), resolutionTime: z.string().optional(), resolvedAnswer: z.string().optional(), slug: z.string().optional(), sourceLocale: z.string().optional(), startDate: z.string().optional(), status: z.string().optional(), subcategories: z.array(z.string()).optional(), tags: z.array(z.string()).optional(), topics: z.array(z.string()).optional(), totalMarketCap: z.number().optional(), traders: z.number().int().optional(), translation: internalHandlerMarketTranslation.optional(), updatedAt: z.string().optional(), volume: z.number().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 }; /** * Get market by address * * GET /api/v1/markets/{market_address} */ export const getApiApiV1MarketsMarketAddress = async (args: GetApiApiV1MarketsMarketAddressArgs, options?: GetApiApiV1MarketsMarketAddressOptions) => { const {baseUrl = "https://rest.ft.42.space", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.locale != null) query.append("locale", String(args.locale)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/markets/${encodeURIComponent(String(args.market_address))}${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(internalHandlerMarketResponse, await res.json()) };