import {z} from 'zod' export type SortParameter = {asc?: (string | null) | undefined, desc?: (string | null) | undefined}; export type OffsetParameter = {el?: (number | null) | undefined, pg?: (number | null) | undefined, cr?: (number | null) | undefined}; export type MichelineFormat = 'Json' | 'JsonString' | 'Raw' | 'RawString'; export type GetApiV1ContractsAddressBigmapsNameKeysKeyUpdatesArgs = {address: string, name: string, key: string, sort?: (SortParameter | null) | undefined, offset?: (OffsetParameter | null) | undefined, limit?: number | undefined, micheline?: MichelineFormat | undefined}; export type GetApiV1ContractsAddressBigmapsNameKeysKeyUpdatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bigMapKeyUpdate = z.object({id: z.number().int().optional(), level: z.number().int().optional(), timestamp: z.string().optional(), action: z.string(), value: z.unknown()}); export const getApiV1ContractsAddressBigmapsNameKeysKeyUpdatesResponse = z.array(bigMapKeyUpdate); 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 bigmap key updates * * GET /v1/contracts/{address}/bigmaps/{name}/keys/{key}/updates */ export const getApiV1ContractsAddressBigmapsNameKeysKeyUpdates = async (args: GetApiV1ContractsAddressBigmapsNameKeysKeyUpdatesArgs, options?: GetApiV1ContractsAddressBigmapsNameKeysKeyUpdatesOptions) => { const {baseUrl = "https://api.tzkt.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort != null) query.append("sort", JSON.stringify(args.sort)) if (args.offset != null) query.append("offset", JSON.stringify(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.micheline != null) query.append("micheline", String(args.micheline)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contracts/${encodeURIComponent(String(args.address))}/bigmaps/${encodeURIComponent(String(args.name))}/keys/${encodeURIComponent(String(args.key))}/updates${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(getApiV1ContractsAddressBigmapsNameKeysKeyUpdatesResponse, await res.json()) };